You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
5.8KB

  1. #include <SPI.h>
  2. #include <SdFat.h>
  3. #include <SdFatUtil.h>
  4. const uint8_t SD_CS_PIN = SS;
  5. SdFat sd;
  6. SdFile file;
  7. char name[260];
  8. //------------------------------------------------------------------------------
  9. char* testName[] = {
  10. "low.low",
  11. "low.Mix",
  12. "low.UP",
  13. "Mix.low",
  14. "Mix.Mix",
  15. "Mix.UP",
  16. "UP.low",
  17. "UP.Mix",
  18. "UP.UP",
  19. ".dot",
  20. ".dot.dot",
  21. "A b c . txt",
  22. " Leading space and no extension",
  23. "Trailing dots and space . . .",
  24. "Long extension.extension",
  25. "Space after dot. txt",
  26. "Dot.dot.test.txt",
  27. "Dot.dot.test.seq.txt",
  28. "LOW.LOW",
  29. "MIX.MIX",
  30. "Invalid character *.test"
  31. };
  32. //------------------------------------------------------------------------------
  33. bool checkName(char first, size_t len) {
  34. size_t i;
  35. if (len < 5 || len > sizeof(name)) {
  36. return false;
  37. }
  38. if ( name[0] != first) {
  39. return false;
  40. }
  41. for (i = 1; i < (len - 4); i++) {
  42. if (name[i] != ('0' + (i + 1) %10)) {
  43. return false;
  44. }
  45. }
  46. char* p = ".txt";
  47. while (*p) {
  48. if (name[i++] != *p++) {
  49. return false;
  50. }
  51. }
  52. return name[i] == 0;
  53. }
  54. //------------------------------------------------------------------------------
  55. void makeName(char first, size_t len) {
  56. size_t i;
  57. if (len > sizeof(name)) {
  58. len = 255;
  59. }
  60. if (len < 5) {
  61. len = 5;
  62. }
  63. name[0] = first;
  64. for (i = 1; i < (len - 4); i++) {
  65. name[i] = '0' + (i + 1) %10;
  66. }
  67. char* p = ".txt";
  68. while (*p) name[i++] = *p++;
  69. name[i] = 0;
  70. }
  71. //------------------------------------------------------------------------------
  72. // test open, remove, getName, and ls.
  73. void basicTest() {
  74. size_t i;
  75. size_t n = sd.vol()->fatType() == 32 ? 255 : 99;
  76. uint16_t index;
  77. uint16_t maxIndex = 0;
  78. makeName('Z', 256);
  79. if (!file.open(name, O_RDWR | O_CREAT)) {
  80. Serial.println(F("255 limit OK"));
  81. } else {
  82. sd.errorHalt(F("255 limit"));
  83. }
  84. for (i = 5; i <= n; i++) {
  85. makeName('A', i);
  86. if (!file.open(name, O_RDWR | O_CREAT)) {
  87. sd.errorHalt(F("open A"));
  88. }
  89. file.println(name);
  90. Serial.print(i);
  91. Serial.write(' ');
  92. Serial.print(file.dirIndex());
  93. Serial.write(' ');
  94. Serial.print(file.fileSize());
  95. Serial.println(F(" open A"));
  96. if (file.fileSize() != (i + 2)) {
  97. sd.errorHalt(F("file size A"));
  98. }
  99. if (file.dirIndex() >= maxIndex) {
  100. maxIndex = file.dirIndex();
  101. } else {
  102. Serial.print(maxIndex); Serial.print(',');Serial.println(file.dirIndex());
  103. sd.errorHalt(F("dirIndex"));
  104. }
  105. file.close();
  106. if (!file.open(sd.vwd(), maxIndex, O_READ)) {
  107. sd.errorHalt(F("open by index"));
  108. }
  109. memset(name, 0, sizeof(name));
  110. if (!file.getName(name, sizeof(name))) {
  111. sd.errorHalt(F("getName"));
  112. }
  113. if (!checkName('A', i)) {
  114. Serial.println(name);
  115. sd.errorHalt(F("checkName"));
  116. }
  117. file.close();
  118. }
  119. for (i = n; i >= 5; i -= 2) {
  120. makeName('A', i);
  121. Serial.print(i);
  122. Serial.println(F( " rm A"));
  123. if (!sd.remove(name)) {
  124. sd.errorHalt(F("remove A"));
  125. }
  126. }
  127. for (i = n; i >= 5; i -= 2) {
  128. makeName('B', i);
  129. if (!file.open(name, O_RDWR | O_CREAT)) {
  130. sd.errorHalt(F("open B"));
  131. }
  132. file.println(name);
  133. Serial.print(i);
  134. Serial.write(' ');
  135. Serial.print(file.dirIndex());
  136. Serial.write(' ');
  137. Serial.print(file.fileSize());
  138. Serial.println(F(" open B"));
  139. if (file.fileSize() != (i + 2)) {
  140. sd.errorHalt(F("file size B"));
  141. }
  142. if (file.dirIndex() > maxIndex) {
  143. sd.errorHalt(F("maxIndex"));
  144. }
  145. file.close();
  146. }
  147. Serial.println(F("----- ls ------"));
  148. sd.ls();
  149. for (i = 5; i <= n; i++) {
  150. char fc = i & 1 ? 'B' : 'A';
  151. makeName(fc, i);
  152. Serial.print(i);
  153. Serial.print(F(" rm "));
  154. Serial.println(fc);
  155. if (!sd.remove(name)) {
  156. sd.errorHalt(F("remove A/B"));
  157. }
  158. }
  159. if (file.openNext(sd.vwd())) {
  160. sd.errorHalt(F("remove all"));
  161. }
  162. Serial.println();
  163. Serial.println(F("basicTest done"));
  164. }
  165. //------------------------------------------------------------------------------
  166. void nameTest() {
  167. Serial.println();
  168. uint8_t n = sizeof(testName)/sizeof(char*);
  169. for (uint8_t i = 0; i < n; i++) {
  170. Serial.print(F("Name: "));
  171. Serial.write('"');
  172. Serial.print(testName[i]);
  173. Serial.println('"');
  174. if(!file.open(testName[i], O_CREAT | O_RDWR)) {
  175. Serial.println(F("Open failed"));
  176. } else {
  177. file.println(testName[i]);
  178. if (!file.getName(name, sizeof(name))) {
  179. sd.errorHalt(F("getFilemame"));
  180. }
  181. file.println(name);
  182. Serial.print(F("LFN: "));
  183. Serial.write('"');
  184. Serial.print(name);
  185. Serial.println('"');
  186. Serial.print(F("SFN: "));
  187. Serial.write('"');
  188. file.printSFN(&Serial);
  189. Serial.println('"');
  190. Serial.print(F("Index: "));
  191. if (file.dirIndex() < 10) {
  192. Serial.write(' ');
  193. }
  194. Serial.println(file.dirIndex());
  195. file.close();
  196. }
  197. Serial.println();
  198. }
  199. Serial.println(F("----- ls ------"));
  200. sd.ls();
  201. Serial.println();
  202. Serial.println(F("nameTest done"));
  203. }
  204. //------------------------------------------------------------------------------
  205. void setup() {
  206. Serial.begin(9600);
  207. while(!Serial);
  208. Serial.print(F("\r\nFreeRam: "));
  209. Serial.println(FreeRam());
  210. Serial.println(F("Type any character to start."));
  211. while (Serial.read() < 0) {}
  212. if (!sd.begin(SD_CS_PIN)) sd.initErrorHalt();
  213. if (file.openNext(sd.vwd())) {
  214. file.close();
  215. delay(100);
  216. while (Serial.read() >= 0) {}
  217. Serial.print(F("Type 'W' to wipe the card: "));
  218. int c;
  219. while ((c = Serial.read()) < 0) {}
  220. if (c != 'W') {
  221. sd.errorHalt(F("Invalid"));
  222. }
  223. Serial.println((char)c);
  224. if (!sd.wipe(&Serial) || !sd.begin(SD_CS_PIN)) {
  225. sd.errorHalt(F("wipe failed"));
  226. }
  227. }
  228. basicTest();
  229. nameTest();
  230. }
  231. //------------------------------------------------------------------------------
  232. void loop() {}