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:
- 32:f2f8ae34aadc
- Child:
- 34:9b66c5188051
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedLogger/MbedLogger.cpp Wed Dec 20 13:52:50 2017 +0000 @@ -0,0 +1,97 @@ +#include "MbedLogger.hpp" +#include "StaticDefs.hpp" + +MbedLogger::MbedLogger() { + _f = 0; + _file_number = 0; +} + +//creates a new file each time it's called +void MbedLogger::createFile() { + + 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,"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::saveArrayToFile(float *input) { + //write to the file + _fprintf(_fp, "%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",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()); +} + +void MbedLogger::printMbedDirectory() { + DirectoryList dir( "/local" ); + + if ( dir.error_check() ) + error( "directory could not be opened\r\n" ); + + for ( int i = 0; i < dir.size(); i++ ) + 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("CURRENT 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"); +} + +//ONLY CLOSE THE FILE WITH THIS +//always close the file when you're done using it +void MbedLogger::closeFile() { + fclose(_fp); +} \ No newline at end of file