ReadCsvArray.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Read a two dimensional array from a CSV file.
  2. //
  3. #include <SPI.h>
  4. #include <SdFat.h>
  5. #define CS_PIN SS
  6. // 5 X 4 array
  7. #define ROW_DIM 5
  8. #define COL_DIM 4
  9. SdFat SD;
  10. File file;
  11. /*
  12. * Read a file one field at a time.
  13. *
  14. * file - File to read.
  15. *
  16. * str - Character array for the field.
  17. *
  18. * size - Size of str array.
  19. *
  20. * delim - String containing field delimiters.
  21. *
  22. * return - length of field including terminating delimiter.
  23. *
  24. * Note, the last character of str will not be a delimiter if
  25. * a read error occurs, the field is too long, or the file
  26. * does not end with a delimiter. Consider this an error
  27. * if not at end-of-file.
  28. *
  29. */
  30. size_t readField(File* file, char* str, size_t size, const char* delim) {
  31. char ch;
  32. size_t n = 0;
  33. while ((n + 1) < size && file->read(&ch, 1) == 1) {
  34. // Delete CR.
  35. if (ch == '\r') {
  36. continue;
  37. }
  38. str[n++] = ch;
  39. if (strchr(delim, ch)) {
  40. break;
  41. }
  42. }
  43. str[n] = '\0';
  44. return n;
  45. }
  46. //------------------------------------------------------------------------------
  47. #define errorHalt(msg) {Serial.println(F(msg)); while (true) {}}
  48. //------------------------------------------------------------------------------
  49. void setup() {
  50. Serial.begin(9600);
  51. // Wait for USB Serial
  52. while (!Serial) {
  53. yield();
  54. }
  55. Serial.println("Type any character to start");
  56. while (!Serial.available()) {
  57. yield();
  58. }
  59. // Initialize the SD.
  60. if (!SD.begin(CS_PIN)) {
  61. errorHalt("begin failed");
  62. }
  63. // Create or open the file.
  64. file = SD.open("READNUM.TXT", FILE_WRITE);
  65. if (!file) {
  66. errorHalt("open failed");
  67. }
  68. // Rewind file so test data is not appended.
  69. file.rewind();
  70. // Write test data.
  71. file.print(F(
  72. "11,12,13,14\r\n"
  73. "21,22,23,24\r\n"
  74. "31,32,33,34\r\n"
  75. "41,42,43,44\r\n"
  76. "51,52,53,54" // Allow missing endl at eof.
  77. ));
  78. // Rewind the file for read.
  79. file.rewind();
  80. // Array for data.
  81. int array[ROW_DIM][COL_DIM];
  82. int i = 0; // First array index.
  83. int j = 0; // Second array index
  84. size_t n; // Length of returned field with delimiter.
  85. char str[20]; // Must hold longest field with delimiter and zero byte.
  86. char *ptr; // Test for valid field.
  87. // Read the file and store the data.
  88. for (i = 0; i < ROW_DIM; i++) {
  89. for (j = 0; j < COL_DIM; j++) {
  90. n = readField(&file, str, sizeof(str), ",\n");
  91. if (n == 0) {
  92. errorHalt("Too few lines");
  93. }
  94. array[i][j] = strtol(str, &ptr, 10);
  95. if (ptr == str) {
  96. errorHalt("bad number");
  97. }
  98. while (*ptr == ' ') {
  99. ptr++;
  100. }
  101. if (*ptr != ',' && *ptr != '\n' && *ptr != '\0') {
  102. errorHalt("extra characters in field");
  103. }
  104. if (j < (COL_DIM-1) && str[n-1] != ',') {
  105. errorHalt("line with too few fields");
  106. }
  107. }
  108. // Allow missing endl at eof.
  109. if (str[n-1] != '\n' && file.available()) {
  110. errorHalt("missing endl");
  111. }
  112. }
  113. // Print the array.
  114. for (i = 0; i < ROW_DIM; i++) {
  115. for (j = 0; j < COL_DIM; j++) {
  116. if (j) {
  117. Serial.print(' ');
  118. }
  119. Serial.print(array[i][j]);
  120. }
  121. Serial.println();
  122. }
  123. Serial.println("Done");
  124. file.close();
  125. }
  126. //------------------------------------------------------------------------------
  127. void loop() {
  128. }