Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MODSERIAL FATFileSystem
Diff: MbedLogger/MbedLogger.cpp
- Revision:
- 36:966a86937e17
- Parent:
- 35:2f66ea4863d5
- Child:
- 39:58375ca6b6ff
--- a/MbedLogger/MbedLogger.cpp Wed Dec 20 22:44:02 2017 +0000 +++ b/MbedLogger/MbedLogger.cpp Wed Dec 20 23:21:15 2017 +0000 @@ -17,63 +17,14 @@ //file pointer is a class variable, close the file after you are done writing to it } -//creates a new file each time it's called -void MbedLogger::createNewFiles() { - - int filenum = 0; - char filename[128]; - - while(true) { - //create a file named Log001.csv through Log999.csv - sprintf(filename, "/local/Log%03d.csv", filenum); - - //try to read a file - _fp = fopen(filename, "r"); - - //check if this file does not exist, create it, and write to it - if (_fp == NULL) { - //you want to do stuff here! - _fp = fopen(filename, "a"); - - //write the header - fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth(ft),pitch_cmd,pitch(deg), bce_cmd, bce(mm), batt_cmd, batt(mm)\n"); - - //DO NOT CLOSE THE FILE, keep it open - break; - } - - //if the file already exists, keep upping the counter - else { - fclose(_fp); //close the old file you tried to read - filenum++; //file name number - } - wait_us(100); - } - - //set this variable to print the current file - _file_number = filenum; +void MbedLogger::saveDataToFile(string string_state, int input_state, float *input) { + //write to the file (header and data) + fprintf(_fp, "%s,%d,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",string_state.c_str(),input_state,input[0],input[1],input[2],input[3],input[4],input[5],input[6],input[7],input[8]); } -void MbedLogger::saveArrayToFile(string string_state, int input_state, float *input) { - //open single log file - FILE *fp = fopen("/local/Log000.csv", "r"); - +void MbedLogger::saveDataToFile(int input_state, float *input) { //write to the file (header and data) - fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth(ft),pitch_cmd,pitch(deg), bce_cmd, bce(mm), batt_cmd, batt(mm)\n"); - fprintf(fp, "%s,%d,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",string_state.c_str(),input_state,input[0],input[1],input[2],input[3],input[4],input[5],input[6],input[7],input[8]); - - //close file - fclose(fp); -} - -//void MbedLogger::saveArrayToFile(string string_state, int input_state, float *input) { -// //write to the file -// fprintf(_fp, "%s,%d,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",string_state.c_str(),input_state,input[0],input[1],input[2],input[3],input[4],input[5],input[6],input[7],input[8]); -//} - -void MbedLogger::saveSequenceStringToFile(string str_input) { - //write to the file - fprintf(_fp, "%s\n",str_input.c_str()); + fprintf(_fp, "%d,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",input_state,input[0],input[1],input[2],input[3],input[4],input[5],input[6],input[7],input[8]); } void MbedLogger::printMbedDirectory() { @@ -86,33 +37,6 @@ printf( "%s\r\n", dir[ i ].c_str() ); } -void MbedLogger::printFromLogFile() { - char filename[128]; - sprintf(filename, "/local/Log%03d.csv", _file_number); - - //open the file - FILE *fp = fopen(filename, "r"); - - // http://people.cs.uchicago.edu/~dmfranklin/tutorials/fgets.txt - - //while not end of file, read through line by line??? - char buffer[500]; - - //read the file line by line (and print each line) - pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\r",_file_number); - while (!feof(fp)) { - // read in the line and make sure it was successful - if (fgets(buffer,500,fp) != NULL) { - pc().printf("%s\r",buffer); - //pc().printf("%d: %s\n\r",lineno++,buffer); - } - } - - //close the file - fclose(fp); - pc().printf("\n\rLog file closed.\n\r"); -} - void MbedLogger::printCurrentLogFile() { //open the file for reading FILE *fp = fopen("/local/Log000.csv", "r"); @@ -123,7 +47,7 @@ char buffer[500]; //read the file line by line (and print each line) - pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\r",_file_number); + pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\n\r",_file_number); while (!feof(fp)) { // read in the line and make sure it was successful if (fgets(buffer,500,fp) != NULL) { @@ -141,4 +65,5 @@ //always close the file when you're done using it void MbedLogger::closeFile() { fclose(_fp); + pc().printf("\n\rDEBUG: Log file closed.\n\r"); } \ No newline at end of file