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.

121 rinda
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. #include "sdios.h"
  8. // SD chip select pin
  9. const uint8_t chipSelect = SS;
  10. // file system object
  11. SdFat sd;
  12. // create Serial stream
  13. ArduinoOutStream cout(Serial);
  14. char fileName[] = "testfile.csv";
  15. //------------------------------------------------------------------------------
  16. // store error strings in flash to save RAM
  17. #define error(s) sd.errorHalt(F(s))
  18. //------------------------------------------------------------------------------
  19. // read and print CSV test file
  20. void readFile() {
  21. long lg = 0;
  22. float f1, f2;
  23. char text[10];
  24. char c1, c2, c3; // space for commas.
  25. // open input file
  26. ifstream sdin(fileName);
  27. // check for open error
  28. if (!sdin.is_open()) {
  29. error("open");
  30. }
  31. // read until input fails
  32. while (1) {
  33. // Get text field.
  34. sdin.get(text, sizeof(text), ',');
  35. // Assume EOF if fail.
  36. if (sdin.fail()) {
  37. break;
  38. }
  39. // Get commas and numbers.
  40. sdin >> c1 >> lg >> c2 >> f1 >> c3 >> f2;
  41. // Skip CR/LF.
  42. sdin.skipWhite();
  43. if (sdin.fail()) {
  44. error("bad input");
  45. }
  46. // error in line if not commas
  47. if (c1 != ',' || c2 != ',' || c3 != ',') {
  48. error("comma");
  49. }
  50. // print in six character wide columns
  51. cout << text << setw(6) << lg << setw(6) << f1 << setw(6) << f2 << endl;
  52. }
  53. // Error in an input line if file is not at EOF.
  54. if (!sdin.eof()) {
  55. error("readFile");
  56. }
  57. }
  58. //------------------------------------------------------------------------------
  59. // write test file
  60. void writeFile() {
  61. // create or open and truncate output file
  62. ofstream sdout(fileName);
  63. // write file from string stored in flash
  64. sdout << F(
  65. "Line 1,1,2.3,4.5\n"
  66. "Line 2,6,7.8,9.0\n"
  67. "Line 3,9,8.7,6.5\n"
  68. "Line 4,-4,-3.2,-1\n") << flush;
  69. // check for any errors
  70. if (!sdout) {
  71. error("writeFile");
  72. }
  73. sdout.close();
  74. }
  75. //------------------------------------------------------------------------------
  76. void setup() {
  77. Serial.begin(9600);
  78. // Wait for USB Serial
  79. while (!Serial) {
  80. SysCall::yield();
  81. }
  82. cout << F("Type any character to start\n");
  83. while (!Serial.available()) {
  84. SysCall::yield();
  85. }
  86. // Initialize at the highest speed supported by the board that is
  87. // not over 50 MHz. Try a lower speed if SPI errors occur.
  88. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  89. sd.initErrorHalt();
  90. }
  91. // create test file
  92. writeFile();
  93. cout << endl;
  94. // read and print test
  95. readFile();
  96. cout << "\nDone!" << endl;
  97. }
  98. void loop() {}