Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

115 rindas
2.6KB

  1. /*
  2. * This example reads a simple CSV, comma-separated values, file.
  3. * Each line of the file has a label and three values, a long and two floats.
  4. */
  5. #include <SPI.h>
  6. #include <SdFat.h>
  7. // SD chip select pin
  8. const uint8_t chipSelect = SS;
  9. // file system object
  10. SdFat sd;
  11. // create Serial stream
  12. ArduinoOutStream cout(Serial);
  13. char fileName[] = "testfile.csv";
  14. //------------------------------------------------------------------------------
  15. // store error strings in flash to save RAM
  16. #define error(s) sd.errorHalt(F(s))
  17. //------------------------------------------------------------------------------
  18. // read and print CSV test file
  19. void readFile() {
  20. long lg;
  21. float f1, f2;
  22. char text[10];
  23. char c1, c2, c3; // space for commas.
  24. // open input file
  25. ifstream sdin(fileName);
  26. // check for open error
  27. if (!sdin.is_open()) {
  28. error("open");
  29. }
  30. // read until input fails
  31. while (1) {
  32. // Get text field.
  33. sdin.get(text, sizeof(text), ',');
  34. // Assume EOF if fail.
  35. if (sdin.fail()) {
  36. break;
  37. }
  38. // Get commas and numbers.
  39. sdin >> c1 >> lg >> c2 >> f1 >> c3 >> f2;
  40. // Skip CR/LF.
  41. sdin.skipWhite();
  42. if (sdin.fail()) {
  43. error("bad input");
  44. }
  45. // error in line if not commas
  46. if (c1 != ',' || c2 != ',' || c3 != ',') {
  47. error("comma");
  48. }
  49. // print in six character wide columns
  50. cout << text << setw(6) << lg << setw(6) << f1 << setw(6) << f2 << endl;
  51. }
  52. // Error in an input line if file is not at EOF.
  53. if (!sdin.eof()) {
  54. error("readFile");
  55. }
  56. }
  57. //------------------------------------------------------------------------------
  58. // write test file
  59. void writeFile() {
  60. // create or open and truncate output file
  61. ofstream sdout(fileName);
  62. // write file from string stored in flash
  63. sdout << F(
  64. "Line 1,1,2.3,4.5\n"
  65. "Line 2,6,7.8,9.0\n"
  66. "Line 3,9,8.7,6.5\n"
  67. "Line 4,-4,-3.2,-1\n") << flush;
  68. // check for any errors
  69. if (!sdout) {
  70. error("writeFile");
  71. }
  72. sdout.close();
  73. }
  74. //------------------------------------------------------------------------------
  75. void setup() {
  76. Serial.begin(9600);
  77. while (!Serial) {} // wait for Leonardo
  78. cout << F("Type any character to start\n");
  79. while (Serial.read() <= 0) {}
  80. delay(400); // catch Due reset problem
  81. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  82. // breadboards. use SPI_FULL_SPEED for better performance
  83. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  84. sd.initErrorHalt();
  85. }
  86. // create test file
  87. writeFile();
  88. cout << endl;
  89. // read and print test
  90. readFile();
  91. cout << "\nDone!" << endl;
  92. }
  93. void loop() {}