Skip to content

Commit e4dbbce

Browse files
committed
Added 'readSDfile' method.
1 parent ba06bd5 commit e4dbbce

5 files changed

Lines changed: 32 additions & 28 deletions

File tree

CSV_Parser.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* https://github.com/michalmonday/CSV-Parser-for-Arduino */
22

33
#include "CSV_Parser.h"
4+
#include <SD.h>
45

56
//#include "mem_check.h" // COMMENT-OUT BEFORE UPLOAD
67

@@ -104,6 +105,24 @@ CSV_Parser::~CSV_Parser() {
104105
free(leftover);
105106
}
106107

108+
bool CSV_Parser::readSDfile(const char *f_name) {
109+
// open the file. note that only one file can be open at a time,
110+
// so you have to close this one before opening another.
111+
File csv_file = SD.open(f_name);
112+
if (!csv_file)
113+
return false;
114+
115+
// read file and supply it to csv parser
116+
while (csv_file.available())
117+
*this << csv_file.read();
118+
119+
csv_file.close();
120+
121+
// ensure that the last value of the file is parsed (even if the file doesn't end with '\n')
122+
parseLeftover();
123+
return true;
124+
}
125+
107126
/* It ensures that '\r\n' characters, delimiter and quote characters that are enclosed within string
108127
value itself are properly parsed. It dynamically allocates memory, creates copy of parsed string
109128
value and returns a pointer to it. Memory is supposed to be released outside of this function.
@@ -440,4 +459,4 @@ void CSV_Parser::parseLeftover() {
440459
free(leftover);
441460
leftover = 0;
442461
}
443-
}
462+
}

CSV_Parser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ class CSV_Parser {
111111
/** @brief Releases all dynamically allocated memory.
112112
Making values unusable once the CSV_Parser goes out of scope. */
113113
~CSV_Parser();
114+
115+
/** @brief Reads file from SD card.
116+
@param f_name - file name (provided file must have format that was supplied in CSV_Parser constructor)
117+
@return True if file could be read, false if not.
118+
It requires previously calling "SD.begin()". */
119+
bool readSDfile(const char *f_name);
114120

115121
int getColumnsCount();
116122

examples/reading_from_sd_card/reading_from_sd_card.ino

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,10 @@ void setup() {
3434
}
3535
Serial.println("card initialized.");
3636

37-
// open the file. note that only one file can be open at a time,
38-
// so you have to close this one before opening another.
39-
File csv_file = SD.open("file.csv");
4037

4138
CSV_Parser cp(/*format*/ "dd", /*has_header*/ true, /*delimiter*/ ',');
42-
43-
if (!csv_file) {
44-
Serial.println("error opening file.csv");
45-
46-
// don't do anything more:
47-
while (1);
48-
}
49-
50-
// read file and supply it to csv parser
51-
while (csv_file.available()) {
52-
cp << csv_file.read();
53-
}
54-
csv_file.close();
55-
56-
// ensure that the last value of the file is parsed (even if the file doesn't end with '\n')
57-
cp.parseLeftover();
58-
59-
// output parsed values (allows to check that the file was parsed correctly)
60-
cp.print(); // assumes that "Serial.begin()" was called before (otherwise it won't work)
61-
62-
39+
cp.readSDfile("file.csv"); // this wouldn't work if SD.begin wasn't called before
6340

64-
65-
// Getting all the values:
6641
int16_t *column_1 = (int16_t*)cp["column_1"];
6742
int16_t *column_2 = (int16_t*)cp["column_2"];
6843

@@ -78,6 +53,9 @@ void setup() {
7853
} else {
7954
Serial.println("At least 1 of the columns was not found, something went wrong.");
8055
}
56+
57+
// output parsed values (allows to check that the file was parsed correctly)
58+
cp.print(); // assumes that "Serial.begin()" was called before (otherwise it won't work)
8159
}
8260

8361
void loop() {

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ print KEYWORD2
1818
printKeys KEYWORD2
1919
setDebugSerial KEYWORD2
2020
parseLeftover KEYWORD2
21+
readSDfile KEYWORD2
2122

2223
######################################
2324
# Constants (LITERAL1)

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=CSV Parser
2-
version=0.1.4
2+
version=0.2.0
33
author=Michal Borowski <michalmonday17@gmail.com>
44
maintainer=Michal Borowski <michalmonday17@gmail.com>
55
sentence=CSV Parser for Arduino.

0 commit comments

Comments
 (0)