123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #include <SPI.h>
- #include "SdFat.h"
- #include "sdios.h"
- const uint8_t chipSelect = SS;
- SdFat sd;
- ArduinoOutStream cout(Serial);
- char fileName[] = "testfile.csv";
- #define error(s) sd.errorHalt(F(s))
- void readFile() {
- long lg = 0;
- float f1, f2;
- char text[10];
- char c1, c2, c3;
-
- ifstream sdin(fileName);
-
- if (!sdin.is_open()) {
- error("open");
- }
-
- while (1) {
-
- sdin.get(text, sizeof(text), ',');
-
- if (sdin.fail()) {
- break;
- }
-
- sdin >> c1 >> lg >> c2 >> f1 >> c3 >> f2;
-
- sdin.skipWhite();
- if (sdin.fail()) {
- error("bad input");
- }
-
- if (c1 != ',' || c2 != ',' || c3 != ',') {
- error("comma");
- }
-
- cout << text << setw(6) << lg << setw(6) << f1 << setw(6) << f2 << endl;
- }
-
- if (!sdin.eof()) {
- error("readFile");
- }
- }
- void writeFile() {
-
- ofstream sdout(fileName);
-
- sdout << F(
- "Line 1,1,2.3,4.5\n"
- "Line 2,6,7.8,9.0\n"
- "Line 3,9,8.7,6.5\n"
- "Line 4,-4,-3.2,-1\n") << flush;
-
- if (!sdout) {
- error("writeFile");
- }
- sdout.close();
- }
- void setup() {
- Serial.begin(9600);
-
-
- while (!Serial) {
- SysCall::yield();
- }
- cout << F("Type any character to start\n");
- while (!Serial.available()) {
- SysCall::yield();
- }
-
-
- if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
- sd.initErrorHalt();
- }
-
- writeFile();
- cout << endl;
-
- readFile();
- cout << "\nDone!" << endl;
- }
- void loop() {}
|