BufferedPrint.ino 6.1 KB

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