Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

BufferedPrint.ino 5.9KB

5 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Test and benchmark of the fast bufferedPrint class.
  2. //
  3. // Mainly for AVR but may improve print performance with other CPUs.
  4. #include "SdFat.h"
  5. #include "BufferedPrint.h"
  6. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  7. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  8. #define SD_FAT_TYPE 0
  9. /*
  10. Change the value of SD_CS_PIN if you are using SPI and
  11. your hardware does not use the default value, SS.
  12. Common values are:
  13. Arduino Ethernet shield: pin 4
  14. Sparkfun SD shield: pin 8
  15. Adafruit SD shields and modules: pin 10
  16. */
  17. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  18. #ifndef SDCARD_SS_PIN
  19. const uint8_t SD_CS_PIN = SS;
  20. #else // SDCARD_SS_PIN
  21. // Assume built-in SD is used.
  22. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  23. #endif // SDCARD_SS_PIN
  24. // Try to select the best SD card configuration.
  25. #if HAS_SDIO_CLASS
  26. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  27. #elif ENABLE_DEDICATED_SPI
  28. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  29. #else // HAS_SDIO_CLASS
  30. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  31. #endif // HAS_SDIO_CLASS
  32. #if SD_FAT_TYPE == 0
  33. SdFat sd;
  34. typedef File file_t;
  35. #elif SD_FAT_TYPE == 1
  36. SdFat32 sd;
  37. typedef File32 file_t;
  38. #elif SD_FAT_TYPE == 2
  39. SdExFat sd;
  40. typedef ExFile file_t;
  41. #elif SD_FAT_TYPE == 3
  42. SdFs sd;
  43. typedef FsFile file_t;
  44. #else // SD_FAT_TYPE
  45. #error Invalid SD_FAT_TYPE
  46. #endif // SD_FAT_TYPE
  47. // number of lines to print
  48. const uint16_t N_PRINT = 20000;
  49. //------------------------------------------------------------------------------
  50. void benchmark() {
  51. file_t file;
  52. BufferedPrint<file_t, 64> bp;
  53. // do write test
  54. Serial.println();
  55. for (int test = 0; test < 6; test++) {
  56. char fileName[13] = "bench0.txt";
  57. fileName[5] = '0' + test;
  58. // open or create file - truncate existing file.
  59. if (!file.open(fileName, O_RDWR | O_CREAT | O_TRUNC)) {
  60. sd.errorHalt(&Serial, F("open failed"));
  61. }
  62. if (test & 1) {
  63. bp.begin(&file);
  64. }
  65. uint32_t t = millis();
  66. switch(test) {
  67. case 0:
  68. Serial.println(F("Test of println(uint16_t)"));
  69. for (uint16_t i = 0; i < N_PRINT; i++) {
  70. file.println(i);
  71. }
  72. break;
  73. case 1:
  74. Serial.println(F("Test of printField(uint16_t, char)"));
  75. for (uint16_t i = 0; i < N_PRINT; i++) {
  76. bp.printField(i, '\n');
  77. }
  78. break;
  79. case 2:
  80. Serial.println(F("Test of println(uint32_t)"));
  81. for (uint16_t i = 0; i < N_PRINT; i++) {
  82. file.println(12345678UL + i);
  83. }
  84. break;
  85. case 3:
  86. Serial.println(F("Test of printField(uint32_t, char)"));
  87. for (uint16_t i = 0; i < N_PRINT; i++) {
  88. bp.printField(12345678UL + i, '\n');
  89. }
  90. break;
  91. case 4:
  92. Serial.println(F("Test of println(double)"));
  93. for (uint16_t i = 0; i < N_PRINT; i++) {
  94. file.println((double)0.01*i);
  95. }
  96. break;
  97. case 5:
  98. Serial.println(F("Test of printField(double, char)"));
  99. for (uint16_t i = 0; i < N_PRINT; i++) {
  100. bp.printField((double)0.01*i, '\n');
  101. }
  102. break;
  103. }
  104. if (test & 1) {
  105. bp.sync();
  106. }
  107. if (file.getWriteError()) {
  108. sd.errorHalt(&Serial, F("write failed"));
  109. }
  110. double s = file.fileSize();
  111. file.close();
  112. t = millis() - t;
  113. Serial.print(F("Time "));
  114. Serial.print(0.001*t, 3);
  115. Serial.println(F(" sec"));
  116. Serial.print(F("File size "));
  117. Serial.print(0.001*s);
  118. Serial.println(F(" KB"));
  119. Serial.print(F("Write "));
  120. Serial.print(s/t);
  121. Serial.println(F(" KB/sec"));
  122. Serial.println();
  123. }
  124. }
  125. //------------------------------------------------------------------------------
  126. void testMemberFunctions() {
  127. BufferedPrint<Print, 32> bp(&Serial);
  128. char c = 'c'; // char
  129. //#define BASIC_TYPES
  130. #ifdef BASIC_TYPES
  131. signed char sc = -1; // signed 8-bit
  132. unsigned char uc = 1; // unsiged 8-bit
  133. signed short ss = -2; // signed 16-bit
  134. unsigned short us = 2; // unsigned 16-bit
  135. signed long sl = -4; // signed 32-bit
  136. unsigned long ul = 4; // unsigned 32-bit
  137. #else // BASIC_TYPES
  138. int8_t sc = -1; // signed 8-bit
  139. uint8_t uc = 1; // unsiged 8-bit
  140. int16_t ss = -2; // signed 16-bit
  141. uint16_t us = 2; // unsigned 16-bit
  142. int32_t sl = -4; // signed 32-bit
  143. uint32_t ul = 4; // unsigned 32-bit
  144. #endif // BASIC_TYPES
  145. float f = -1.234;
  146. double d = -5.678;
  147. bp.println();
  148. bp.println("Test print()");
  149. bp.print(c);
  150. bp.println();
  151. bp.print("string");
  152. bp.println();
  153. bp.print(F("flash"));
  154. bp.println();
  155. bp.print(sc);
  156. bp.println();
  157. bp.print(uc);
  158. bp.println();
  159. bp.print(ss);
  160. bp.println();
  161. bp.print(us);
  162. bp.println();
  163. bp.print(sl);
  164. bp.println();
  165. bp.print(ul);
  166. bp.println();
  167. bp.print(f);
  168. bp.println();
  169. bp.print(d);
  170. bp.println();
  171. bp.println();
  172. bp.println("Test println()");
  173. bp.println(c);
  174. bp.println("string");
  175. bp.println(F("flash"));
  176. bp.println(sc);
  177. bp.println(uc);
  178. bp.println(ss);
  179. bp.println(us);
  180. bp.println(sl);
  181. bp.println(ul);
  182. bp.println(f);
  183. bp.println(d);
  184. bp.println();
  185. bp.println("Test printField()");
  186. bp.printField(c, ',');
  187. bp.printField("string", ',');
  188. bp.printField(F("flash"), ',');
  189. bp.printField(sc, ',');
  190. bp.printField(uc, ',');
  191. bp.printField(ss, ',');
  192. bp.printField(us, ',');
  193. bp.printField(sl, ',');
  194. bp.printField(ul, ',');
  195. bp.printField(f, ',');
  196. bp.printField(d, '\n');
  197. bp.sync();
  198. }
  199. //------------------------------------------------------------------------------
  200. void setup() {
  201. Serial.begin(9600);
  202. while (!Serial) {}
  203. Serial.println("Type any character to begin.");
  204. while(!Serial.available()) {}
  205. if (!sd.begin(SD_CONFIG)) {
  206. sd.initErrorHalt(&Serial);
  207. }
  208. Serial.println();
  209. Serial.println(F("Test member funcions:"));
  210. testMemberFunctions();
  211. Serial.println();
  212. Serial.println(F("Benchmark performance for uint16_t, uint32_t, and double:"));
  213. benchmark();
  214. Serial.println("Done");
  215. }
  216. //------------------------------------------------------------------------------
  217. void loop() {
  218. }