#include <SD.h>
#include <CSV_Parser.h>
File myFile;
char feedRowParser(){
return myFile.read();
}
bool rowParserFinished(){
return ((myFile.available()>0)?false:true);
}
void setup() {
// put your setup code here, to run once:
SD.begin();
Serial.begin(115200);
myFile = SD.open("output.csv", FILE_WRITE);
//myFile.println("PACKET_COUNT,MODE,STATE,HS_DEPLOYED,PC_DEPLOYED,CMD_ECHO");
myFile.println("TEAM_ID,MISSION_TIME,PACKET_COUNT,MODE,STATE,ALTITUDE,AIR_SPEED,HS_DEPLOYED,PC_DEPLOYED,TEMPERATURE,VOLTAGE,PRESSURE,GPS_TIME,GPS_ALTITUDE,GPS_LATITUDE,GPS_LONGITUDE,GPS_SATS,TILT_X,TILT_Y,ROT_Z,CMD_ECHO \n");
// - s d c s - - c c - - - - - - - - - - - s
String output;
String i_str;
//i_str = "stringtest";
for(int i = 1; i <= 100; i++ ){
i_str = String(i);
//output = "PACKET_COUNT"+i_str+",MODE"+i_str+",STATE"+i_str+",HS_DEPLOYED"+i_str+",PC_DEPLOYED"+i_str+",CMD_ECHO" + i_str;
output = "2034, missiontime, " + String(i) + ", m, state, 55,55,h,p,67,5,5,4,3,gpstime,444,i,j,k,l,cmd_echo\n";
// - s d c s - - c c - - - - - - - - - - - s
myFile.println(output);
}
myFile.close();
//COMBINE INTO ONE STRING
/////////////////////////////////
//Serial.print(SD.exists("output.txt"));
//delay(1000);
// Victoria
// parse the csv in the sd card return the last row parsed
// so last_packet is the last item in the packet column
// so last_mode is the last item in the mode column
// the wrap this is a fucntion
// this is the same parser we are using
// it is documented in the github read me
// https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master
// there is also examples in the github
// https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples
// Reopen the file for reading
myFile = SD.open("output.csv");
if (myFile) {
Serial.println("output.csv:");
// Read file line by line
while (myFile.available()) {
String dataLine = myFile.readStringUntil('\n');
Serial.println(dataLine);
}
myFile.close(); // Close the file
} else {
Serial.println("Error opening data.csv");
}
myFile = SD.open("output.txt", FILE_READ);
CSV_Parser cp("-ssdss--ss-----------s", true, ',');
if(cp.readSDfile("output.csv")){
Serial.print("opened");
}else{
Serial.print("not there");
}
char **mission_time=(char**)cp[1];
int32_t *packet_count=(int32_t*)cp[2];
char **mode=(char**)cp[3];
char **state = (char**)cp[4];
char **hs_deployed= (char**)cp[7];
char **pc_deployed=(char**)cp[8];
char **cmd_echo=(char**)cp[20];
// while(parser.parseRow()){}
// mission_time = (char**)cp[1];
// packet_count = *(int*)cp[2];
// mode = (char**)cp[3];
// state=(char**)cp[4];
// hs_deployed = (char**)cp[7];
// pc_deployed = (char**)cp[8];
// cmd_echo=(char**)cp[20];
Serial.println("Rows " + String(cp.getRowsCount()));
for(int row = 0; row < cp.getRowsCount(); row++) {
// mission_time = mission_time[row];
// packet_count = packet_count[row];
// mode = mission_time[row];
// state=mission_time[row];
// hs_deployed = mission_time[row];
// pc_deployed = mission_time[row];
// cmd_echo=mission_time[row];
Serial.println("MISSION TIME=" + String(mission_time[row]));
Serial.print("PACKETs =" + String(packet_count[row]));
Serial.print("MODE =" + String(mode[row]));
Serial.print("STATE =" + String(state[row]));
Serial.print("HS =" + String(hs_deployed[row]));
Serial.print("PC =" + String(pc_deployed[row]));
Serial.print("CMD =" + String(cmd_echo[row]));
}
delay(250);
// Serial.println( "PACKET COUNT: " + String(packet_count) +
// ", MODE: " + String(mode) + ", STATE: " + state + ", HS DEPLOYED: " + String(hs_deployed) +
// ", PC DEPLOYED: " + String(pc_deployed) + ", CMD ECHO: " + cmd_echo);
myFile.close();
}
void loop() {
// put your main code here, to run repeatedly:
}
// while(myFile.available()){
// String csvLine = myFile.readStringUntil('\n');
// Serial.println("PRINT" + csvLine);
// // if(parser.parseRow()){
// mission_time = String((char*)parser[0]);
// packet_count = atoi((char*)parser[1]);
// mode = *(char*)parser[2];
// state = String((char*)parser[3]);
// hs_deployed = *(char*)parser[4];
// pc_deployed = *(char*)parser[5];
// cmd_echo = String((char*)parser[6]);
// //}
// }