Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
tnhnrl
Date:
Wed Dec 20 22:44:02 2017 +0000
Revision:
35:2f66ea4863d5
Parent:
34:9b66c5188051
Child:
36:966a86937e17
Previous version before MBED save fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tnhnrl 32:f2f8ae34aadc 1 #include "MbedLogger.hpp"
tnhnrl 32:f2f8ae34aadc 2 #include "StaticDefs.hpp"
tnhnrl 32:f2f8ae34aadc 3
tnhnrl 32:f2f8ae34aadc 4 MbedLogger::MbedLogger() {
tnhnrl 32:f2f8ae34aadc 5 _f = 0;
tnhnrl 32:f2f8ae34aadc 6 _file_number = 0;
tnhnrl 32:f2f8ae34aadc 7 }
tnhnrl 32:f2f8ae34aadc 8
tnhnrl 35:2f66ea4863d5 9 //one log file only, works faster
tnhnrl 35:2f66ea4863d5 10 void MbedLogger::openFile() {
tnhnrl 35:2f66ea4863d5 11 //create a file for writing to it (overwrites existing file)
tnhnrl 35:2f66ea4863d5 12 _fp = fopen("/local/LOG000.csv", "w");
tnhnrl 35:2f66ea4863d5 13
tnhnrl 35:2f66ea4863d5 14 //write the header
tnhnrl 35:2f66ea4863d5 15 fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth(ft),pitch_cmd,pitch(deg), bce_cmd, bce(mm), batt_cmd, batt(mm)\n");
tnhnrl 35:2f66ea4863d5 16
tnhnrl 35:2f66ea4863d5 17 //file pointer is a class variable, close the file after you are done writing to it
tnhnrl 35:2f66ea4863d5 18 }
tnhnrl 35:2f66ea4863d5 19
tnhnrl 32:f2f8ae34aadc 20 //creates a new file each time it's called
tnhnrl 34:9b66c5188051 21 void MbedLogger::createNewFiles() {
tnhnrl 32:f2f8ae34aadc 22
tnhnrl 32:f2f8ae34aadc 23 int filenum = 0;
tnhnrl 32:f2f8ae34aadc 24 char filename[128];
tnhnrl 32:f2f8ae34aadc 25
tnhnrl 32:f2f8ae34aadc 26 while(true) {
tnhnrl 32:f2f8ae34aadc 27 //create a file named Log001.csv through Log999.csv
tnhnrl 32:f2f8ae34aadc 28 sprintf(filename, "/local/Log%03d.csv", filenum);
tnhnrl 32:f2f8ae34aadc 29
tnhnrl 32:f2f8ae34aadc 30 //try to read a file
tnhnrl 32:f2f8ae34aadc 31 _fp = fopen(filename, "r");
tnhnrl 32:f2f8ae34aadc 32
tnhnrl 32:f2f8ae34aadc 33 //check if this file does not exist, create it, and write to it
tnhnrl 32:f2f8ae34aadc 34 if (_fp == NULL) {
tnhnrl 32:f2f8ae34aadc 35 //you want to do stuff here!
tnhnrl 32:f2f8ae34aadc 36 _fp = fopen(filename, "a");
tnhnrl 32:f2f8ae34aadc 37
tnhnrl 32:f2f8ae34aadc 38 //write the header
tnhnrl 34:9b66c5188051 39 fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth(ft),pitch_cmd,pitch(deg), bce_cmd, bce(mm), batt_cmd, batt(mm)\n");
tnhnrl 32:f2f8ae34aadc 40
tnhnrl 32:f2f8ae34aadc 41 //DO NOT CLOSE THE FILE, keep it open
tnhnrl 32:f2f8ae34aadc 42 break;
tnhnrl 32:f2f8ae34aadc 43 }
tnhnrl 32:f2f8ae34aadc 44
tnhnrl 32:f2f8ae34aadc 45 //if the file already exists, keep upping the counter
tnhnrl 32:f2f8ae34aadc 46 else {
tnhnrl 32:f2f8ae34aadc 47 fclose(_fp); //close the old file you tried to read
tnhnrl 32:f2f8ae34aadc 48 filenum++; //file name number
tnhnrl 32:f2f8ae34aadc 49 }
tnhnrl 32:f2f8ae34aadc 50 wait_us(100);
tnhnrl 32:f2f8ae34aadc 51 }
tnhnrl 32:f2f8ae34aadc 52
tnhnrl 32:f2f8ae34aadc 53 //set this variable to print the current file
tnhnrl 32:f2f8ae34aadc 54 _file_number = filenum;
tnhnrl 32:f2f8ae34aadc 55 }
tnhnrl 32:f2f8ae34aadc 56
tnhnrl 34:9b66c5188051 57 void MbedLogger::saveArrayToFile(string string_state, int input_state, float *input) {
tnhnrl 34:9b66c5188051 58 //open single log file
tnhnrl 34:9b66c5188051 59 FILE *fp = fopen("/local/Log000.csv", "r");
tnhnrl 34:9b66c5188051 60
tnhnrl 34:9b66c5188051 61 //write to the file (header and data)
tnhnrl 34:9b66c5188051 62 fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth(ft),pitch_cmd,pitch(deg), bce_cmd, bce(mm), batt_cmd, batt(mm)\n");
tnhnrl 34:9b66c5188051 63 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]);
tnhnrl 34:9b66c5188051 64
tnhnrl 34:9b66c5188051 65 //close file
tnhnrl 34:9b66c5188051 66 fclose(fp);
tnhnrl 32:f2f8ae34aadc 67 }
tnhnrl 32:f2f8ae34aadc 68
tnhnrl 34:9b66c5188051 69 //void MbedLogger::saveArrayToFile(string string_state, int input_state, float *input) {
tnhnrl 34:9b66c5188051 70 // //write to the file
tnhnrl 34:9b66c5188051 71 // 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]);
tnhnrl 34:9b66c5188051 72 //}
tnhnrl 34:9b66c5188051 73
tnhnrl 32:f2f8ae34aadc 74 void MbedLogger::saveSequenceStringToFile(string str_input) {
tnhnrl 32:f2f8ae34aadc 75 //write to the file
tnhnrl 34:9b66c5188051 76 fprintf(_fp, "%s\n",str_input.c_str());
tnhnrl 32:f2f8ae34aadc 77 }
tnhnrl 32:f2f8ae34aadc 78
tnhnrl 32:f2f8ae34aadc 79 void MbedLogger::printMbedDirectory() {
tnhnrl 32:f2f8ae34aadc 80 DirectoryList dir( "/local" );
tnhnrl 32:f2f8ae34aadc 81
tnhnrl 32:f2f8ae34aadc 82 if ( dir.error_check() )
tnhnrl 32:f2f8ae34aadc 83 error( "directory could not be opened\r\n" );
tnhnrl 32:f2f8ae34aadc 84
tnhnrl 32:f2f8ae34aadc 85 for ( int i = 0; i < dir.size(); i++ )
tnhnrl 32:f2f8ae34aadc 86 printf( "%s\r\n", dir[ i ].c_str() );
tnhnrl 32:f2f8ae34aadc 87 }
tnhnrl 32:f2f8ae34aadc 88
tnhnrl 32:f2f8ae34aadc 89 void MbedLogger::printFromLogFile() {
tnhnrl 32:f2f8ae34aadc 90 char filename[128];
tnhnrl 32:f2f8ae34aadc 91 sprintf(filename, "/local/Log%03d.csv", _file_number);
tnhnrl 32:f2f8ae34aadc 92
tnhnrl 32:f2f8ae34aadc 93 //open the file
tnhnrl 32:f2f8ae34aadc 94 FILE *fp = fopen(filename, "r");
tnhnrl 32:f2f8ae34aadc 95
tnhnrl 32:f2f8ae34aadc 96 // http://people.cs.uchicago.edu/~dmfranklin/tutorials/fgets.txt
tnhnrl 32:f2f8ae34aadc 97
tnhnrl 32:f2f8ae34aadc 98 //while not end of file, read through line by line???
tnhnrl 32:f2f8ae34aadc 99 char buffer[500];
tnhnrl 32:f2f8ae34aadc 100
tnhnrl 32:f2f8ae34aadc 101 //read the file line by line (and print each line)
tnhnrl 34:9b66c5188051 102 pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\r",_file_number);
tnhnrl 32:f2f8ae34aadc 103 while (!feof(fp)) {
tnhnrl 32:f2f8ae34aadc 104 // read in the line and make sure it was successful
tnhnrl 32:f2f8ae34aadc 105 if (fgets(buffer,500,fp) != NULL) {
tnhnrl 32:f2f8ae34aadc 106 pc().printf("%s\r",buffer);
tnhnrl 32:f2f8ae34aadc 107 //pc().printf("%d: %s\n\r",lineno++,buffer);
tnhnrl 32:f2f8ae34aadc 108 }
tnhnrl 32:f2f8ae34aadc 109 }
tnhnrl 32:f2f8ae34aadc 110
tnhnrl 32:f2f8ae34aadc 111 //close the file
tnhnrl 32:f2f8ae34aadc 112 fclose(fp);
tnhnrl 32:f2f8ae34aadc 113 pc().printf("\n\rLog file closed.\n\r");
tnhnrl 32:f2f8ae34aadc 114 }
tnhnrl 32:f2f8ae34aadc 115
tnhnrl 35:2f66ea4863d5 116 void MbedLogger::printCurrentLogFile() {
tnhnrl 35:2f66ea4863d5 117 //open the file for reading
tnhnrl 35:2f66ea4863d5 118 FILE *fp = fopen("/local/Log000.csv", "r");
tnhnrl 35:2f66ea4863d5 119
tnhnrl 35:2f66ea4863d5 120 // http://people.cs.uchicago.edu/~dmfranklin/tutorials/fgets.txt
tnhnrl 35:2f66ea4863d5 121
tnhnrl 35:2f66ea4863d5 122 //while not end of file, read through line by line???
tnhnrl 35:2f66ea4863d5 123 char buffer[500];
tnhnrl 35:2f66ea4863d5 124
tnhnrl 35:2f66ea4863d5 125 //read the file line by line (and print each line)
tnhnrl 35:2f66ea4863d5 126 pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\r",_file_number);
tnhnrl 35:2f66ea4863d5 127 while (!feof(fp)) {
tnhnrl 35:2f66ea4863d5 128 // read in the line and make sure it was successful
tnhnrl 35:2f66ea4863d5 129 if (fgets(buffer,500,fp) != NULL) {
tnhnrl 35:2f66ea4863d5 130 pc().printf("%s\r",buffer);
tnhnrl 35:2f66ea4863d5 131 //pc().printf("%d: %s\n\r",lineno++,buffer);
tnhnrl 35:2f66ea4863d5 132 }
tnhnrl 35:2f66ea4863d5 133 }
tnhnrl 35:2f66ea4863d5 134
tnhnrl 35:2f66ea4863d5 135 //close the file
tnhnrl 35:2f66ea4863d5 136 fclose(fp);
tnhnrl 35:2f66ea4863d5 137 pc().printf("\n\rLog file closed.\n\r");
tnhnrl 35:2f66ea4863d5 138 }
tnhnrl 35:2f66ea4863d5 139
tnhnrl 32:f2f8ae34aadc 140 //ONLY CLOSE THE FILE WITH THIS
tnhnrl 32:f2f8ae34aadc 141 //always close the file when you're done using it
tnhnrl 32:f2f8ae34aadc 142 void MbedLogger::closeFile() {
tnhnrl 32:f2f8ae34aadc 143 fclose(_fp);
tnhnrl 32:f2f8ae34aadc 144 }