Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

219 lines
5.4KB

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