Flying Sea Glider / Mbed 2 deprecated 2019_13sep_jcw_nosd

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
CodyMarquardt
Date:
Fri Jun 28 13:59:11 2019 +0000
Revision:
99:9d0849f5fcd7
Parent:
87:6d95f853dab3
Program has bugs. Committing in order to access in MBED studio to debug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tnhnrl 62:d502889e74f1 1 #include "MbedLogger.hpp"
tnhnrl 62:d502889e74f1 2 #include "StaticDefs.hpp"
joel_ssc 82:0981b9ada820 3 #include <stdarg.h>
tnhnrl 62:d502889e74f1 4
tnhnrl 62:d502889e74f1 5 //Timer t; //used to test time to create packet //timing debug
tnhnrl 62:d502889e74f1 6
tnhnrl 62:d502889e74f1 7 MbedLogger::MbedLogger(string file_system_input_string) {
joel_ssc 82:0981b9ada820 8 _file_system_string = file_system_input_string; // how to make the LOG000.csv a variable string?
tnhnrl 62:d502889e74f1 9 _full_file_path_string = _file_system_string + "LOG000.csv"; //use multiple logs in the future? (after file size is too large)
joel_ssc 82:0981b9ada820 10 _full_diagfile_path_string = _file_system_string + "DIAG000.txt";
tnhnrl 62:d502889e74f1 11 _file_number = 0;
tnhnrl 62:d502889e74f1 12 _file_transmission = true;
tnhnrl 62:d502889e74f1 13 _confirmed_packet_number = 0; //must set this to zero
tnhnrl 62:d502889e74f1 14 _transmit_counter = 0;
tnhnrl 62:d502889e74f1 15 _file_transmission_state = -1;
tnhnrl 62:d502889e74f1 16 _total_number_of_packets = 0;
tnhnrl 62:d502889e74f1 17 _mbed_transmit_loop = false;
tnhnrl 62:d502889e74f1 18 _received_filename = "";
tnhnrl 63:6cb0405fc6e6 19
tnhnrl 63:6cb0405fc6e6 20 _log_file_line_counter = 0; //used to set timer in finite state machine based on size of log
tnhnrl 67:c86a4b464682 21
tnhnrl 74:d281aaef9766 22 //heading string is 254 bytes long, FIXED LENGTH
CodyMarquardt 99:9d0849f5fcd7 23 _heading_string = "StateStr,St#,TimeSec,DepthCmd,DepthFt,PitchCmd,PitchDeg,RudderPWM,RudderCmdDeg,HeadDeg,bceCmd,bce_mm,battCmd,batt_mm,PitchRateDegSec,depth_rate_fps,SystemAmps,SystemVolts,AltChRd,Alt_m,Int_PSI,BCE_p,i,d,BATT_p,i,d,DEPTH_p,i,d,fq,db,PITCH_p,i,d,HEAD_p,i,d,fq,db\n"; //edit by CAM
joel_ssc 82:0981b9ada820 24 _diag_heading_string = "Diagnostics file header \n";
tnhnrl 69:919ac8d7e023 25
tnhnrl 69:919ac8d7e023 26 _fsm_transmit_complete = false;
tnhnrl 73:f6f378311c8d 27
tnhnrl 73:f6f378311c8d 28 _end_transmit_packet = false;
joel_ssc 86:ba3a118b0080 29 _default_timestamp_time = 1518467832;
tnhnrl 74:d281aaef9766 30
tnhnrl 74:d281aaef9766 31 _end_sequence_transmission = false;
joel_ssc 87:6d95f853dab3 32 _time_set =0;
tnhnrl 62:d502889e74f1 33 }
tnhnrl 62:d502889e74f1 34
tnhnrl 62:d502889e74f1 35 //this function has to be called for the time to function correctly
joel_ssc 86:ba3a118b0080 36 void MbedLogger::setLogTime(long int setting_time) {
joel_ssc 86:ba3a118b0080 37
joel_ssc 86:ba3a118b0080 38 if ( setting_time == -1) { set_time(_default_timestamp_time); }
joel_ssc 86:ba3a118b0080 39 else { set_time(setting_time); _default_timestamp_time = setting_time; }
tnhnrl 74:d281aaef9766 40 xbee().printf("\n%s log time set.\n\r", _file_system_string.c_str());
joel_ssc 86:ba3a118b0080 41 // set_time(time_stamp); // Set RTC time to Mon, 12 FEB 2018 15:37
joel_ssc 87:6d95f853dab3 42 _time_set =1;
tnhnrl 62:d502889e74f1 43 }
tnhnrl 62:d502889e74f1 44
tnhnrl 62:d502889e74f1 45 //in the future create the ability to set the start time
joel_ssc 86:ba3a118b0080 46 time_t MbedLogger::getSystemTime() {
tnhnrl 62:d502889e74f1 47 time_t seconds = time(NULL); // Time as seconds since January 1, 1970
tnhnrl 62:d502889e74f1 48
tnhnrl 62:d502889e74f1 49 return seconds;
tnhnrl 62:d502889e74f1 50 }
tnhnrl 62:d502889e74f1 51
tnhnrl 62:d502889e74f1 52 void MbedLogger::recordData(int current_state) {
tnhnrl 74:d281aaef9766 53 int data_log_time = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp
joel_ssc 82:0981b9ada820 54 int start_time = 1518467832;
joel_ssc 87:6d95f853dab3 55 if(_time_set) { start_time = _default_timestamp_time;}
tnhnrl 74:d281aaef9766 56 _data_log[0] = depthLoop().getCommand(); //depth command
tnhnrl 74:d281aaef9766 57 _data_log[1] = depthLoop().getPosition(); //depth reading (filtered depth)
tnhnrl 74:d281aaef9766 58 _data_log[2] = pitchLoop().getCommand(); //pitch command
tnhnrl 74:d281aaef9766 59 _data_log[3] = pitchLoop().getPosition(); //pitch reading (filtered pitch)
tnhnrl 74:d281aaef9766 60 _data_log[4] = rudder().getSetPosition_pwm(); //rudder command PWM
tnhnrl 74:d281aaef9766 61 _data_log[5] = rudder().getSetPosition_deg(); //rudder command DEG
tnhnrl 73:f6f378311c8d 62 _data_log[6] = headingLoop().getPosition(); //heading reading (filtered heading)
tnhnrl 73:f6f378311c8d 63
tnhnrl 73:f6f378311c8d 64 _data_log[7] = bce().getSetPosition_mm(); //BCE command
tnhnrl 73:f6f378311c8d 65 _data_log[8] = bce().getPosition_mm(); //BCE reading
tnhnrl 73:f6f378311c8d 66 _data_log[9] = batt().getSetPosition_mm(); //Batt command
tnhnrl 73:f6f378311c8d 67 _data_log[10] = batt().getPosition_mm(); //Batt reading
tnhnrl 73:f6f378311c8d 68 _data_log[11] = pitchLoop().getVelocity(); // pitchRate_degs (degrees per second)
tnhnrl 73:f6f378311c8d 69 _data_log[12] = depthLoop().getVelocity(); // depthRate_fps (feet per second)
tnhnrl 62:d502889e74f1 70
tnhnrl 73:f6f378311c8d 71 _data_log[13] = sensors().getCurrentInput(); // i_in
tnhnrl 73:f6f378311c8d 72 _data_log[14] = sensors().getVoltageInput(); // v_in
tnhnrl 74:d281aaef9766 73 _data_log[15] = sensors().getAltimeterChannelReadings(); // Altimeter Channel Readings
CodyMarquardt 99:9d0849f5fcd7 74 _data_log[16] = sensors().getAltimeterReading_m(); // Altimeter Distance Reading (m)
CodyMarquardt 99:9d0849f5fcd7 75 _data_log[17] = sensors().getInternalPressurePSI(); // int_press_PSI
tnhnrl 73:f6f378311c8d 76
tnhnrl 74:d281aaef9766 77 //BCE_p,i,d,freq,deadband
CodyMarquardt 99:9d0849f5fcd7 78 _data_log[18] = bce().getControllerP();
CodyMarquardt 99:9d0849f5fcd7 79 _data_log[19] = bce().getControllerI();
CodyMarquardt 99:9d0849f5fcd7 80 _data_log[20] = bce().getControllerD();
tnhnrl 74:d281aaef9766 81
CodyMarquardt 99:9d0849f5fcd7 82 _data_log[21] = batt().getControllerP();
CodyMarquardt 99:9d0849f5fcd7 83 _data_log[22] = batt().getControllerI();
CodyMarquardt 99:9d0849f5fcd7 84 _data_log[23] = batt().getControllerD();
tnhnrl 67:c86a4b464682 85
CodyMarquardt 99:9d0849f5fcd7 86 _data_log[24] = depthLoop().getControllerP();
CodyMarquardt 99:9d0849f5fcd7 87 _data_log[25] = depthLoop().getControllerI();
CodyMarquardt 99:9d0849f5fcd7 88 _data_log[26] = depthLoop().getControllerD();
CodyMarquardt 99:9d0849f5fcd7 89 _data_log[27] = depthLoop().getFilterFrequency();
CodyMarquardt 99:9d0849f5fcd7 90 _data_log[28] = depthLoop().getDeadband();
tnhnrl 67:c86a4b464682 91
CodyMarquardt 99:9d0849f5fcd7 92 _data_log[29] = pitchLoop().getControllerP();
CodyMarquardt 99:9d0849f5fcd7 93 _data_log[30] = pitchLoop().getControllerI();
CodyMarquardt 99:9d0849f5fcd7 94 _data_log[31] = pitchLoop().getControllerD();
tnhnrl 67:c86a4b464682 95
CodyMarquardt 99:9d0849f5fcd7 96 _data_log[32] = headingLoop().getControllerP();
CodyMarquardt 99:9d0849f5fcd7 97 _data_log[33] = headingLoop().getControllerI();
CodyMarquardt 99:9d0849f5fcd7 98 _data_log[34] = headingLoop().getControllerD();
CodyMarquardt 99:9d0849f5fcd7 99 _data_log[35] = headingLoop().getFilterFrequency();
CodyMarquardt 99:9d0849f5fcd7 100 _data_log[36] = headingLoop().getDeadband();
CodyMarquardt 99:9d0849f5fcd7 101
tnhnrl 74:d281aaef9766 102
joel_ssc 87:6d95f853dab3 103 string string_state; string_state= "UNKNOWN"; //default just in case.
joel_ssc 87:6d95f853dab3 104
tnhnrl 62:d502889e74f1 105 if (current_state == SIT_IDLE)
tnhnrl 62:d502889e74f1 106 string_state = "SIT_IDLE";
tnhnrl 62:d502889e74f1 107 else if (current_state == FIND_NEUTRAL)
tnhnrl 62:d502889e74f1 108 string_state = "FIND_NEUTRAL";
tnhnrl 62:d502889e74f1 109 else if (current_state == DIVE)
tnhnrl 62:d502889e74f1 110 string_state = "DIVE";
tnhnrl 62:d502889e74f1 111 else if (current_state == RISE)
tnhnrl 62:d502889e74f1 112 string_state = "RISE";
tnhnrl 62:d502889e74f1 113 else if (current_state == FLOAT_LEVEL)
tnhnrl 62:d502889e74f1 114 string_state = "FLOAT_LEVEL";
tnhnrl 62:d502889e74f1 115 else if (current_state == FLOAT_BROADCAST)
tnhnrl 62:d502889e74f1 116 string_state = "FLOAT_BROADCAST";
tnhnrl 62:d502889e74f1 117 else if (current_state == EMERGENCY_CLIMB)
tnhnrl 62:d502889e74f1 118 string_state = "EMERGENCY_CLIMB";
tnhnrl 62:d502889e74f1 119 else if (current_state == MULTI_DIVE)
tnhnrl 62:d502889e74f1 120 string_state = "MULTI_DIVE";
tnhnrl 62:d502889e74f1 121 else if (current_state == MULTI_RISE)
tnhnrl 62:d502889e74f1 122 string_state = "MULTI_RISE";
tnhnrl 62:d502889e74f1 123 else if (current_state == KEYBOARD)
tnhnrl 62:d502889e74f1 124 string_state = "KEYBOARD";
tnhnrl 62:d502889e74f1 125 else if (current_state == CHECK_TUNING)
tnhnrl 63:6cb0405fc6e6 126 string_state = "CHECK_TUNING";
tnhnrl 63:6cb0405fc6e6 127 else if (current_state == POSITION_DIVE)
tnhnrl 63:6cb0405fc6e6 128 string_state = "POSITION_DIVE";
tnhnrl 63:6cb0405fc6e6 129 else if (current_state == POSITION_RISE)
tnhnrl 69:919ac8d7e023 130 string_state = "POSITION_RISE";
tnhnrl 71:939d179478c4 131 else if (current_state == TX_MBED_LOG)
tnhnrl 71:939d179478c4 132 string_state = "TX_MBED_LOG";
tnhnrl 74:d281aaef9766 133 else if (current_state == RX_SEQUENCE)
joel_ssc 82:0981b9ada820 134 string_state = "RECEIVE_SEQUENCE";
joel_ssc 82:0981b9ada820 135 else if (current_state == LEG_POSITION_DIVE)
joel_ssc 82:0981b9ada820 136 string_state = "LEG_POS_DIVE";
joel_ssc 82:0981b9ada820 137 else if (current_state == LEG_POSITION_RISE)
joel_ssc 82:0981b9ada820 138 string_state = "LEG_POS_RISE";
joel_ssc 82:0981b9ada820 139 else if (current_state == FB_EXIT)
joel_ssc 85:dd8176285b6e 140 string_state = "FB_EXIT";
joel_ssc 87:6d95f853dab3 141 else if (current_state == START_SWIM)
joel_ssc 87:6d95f853dab3 142 string_state = "START_SWIM";
joel_ssc 87:6d95f853dab3 143 else if (current_state == FLYING_IDLE)
joel_ssc 87:6d95f853dab3 144 string_state = "FLYING_IDLE";
joel_ssc 85:dd8176285b6e 145 else if (current_state == ENDLEG_WAIT)
joel_ssc 85:dd8176285b6e 146 string_state = "ENDLEG_WAIT";
tnhnrl 69:919ac8d7e023 147
tnhnrl 74:d281aaef9766 148 string blank_space = ""; //to get consistent spacing in the file (had a nonsense char w/o this)
tnhnrl 62:d502889e74f1 149
tnhnrl 76:c802e1da4179 150 //below this format is used for data transmission, each packet needs to be 254 characters long (not counting newline char)
tnhnrl 68:8f549749b8ce 151
tnhnrl 76:c802e1da4179 152 //verified that this generates the correct line length of 254 using SOLELY an mbed 08/16/2018
CodyMarquardt 99:9d0849f5fcd7 153 fprintf(_fp, "%17s,%.2d,%10d,%5.1f,%5.1f,%6.1f,%6.1f,%4.0f,%4.0f,%6.1f,%5.1f,%6.1f,%5.1f,%6.1f,%6.1f,%6.1f,%6.3f,%6.2f,%5.0f,%4.1f,%6.2f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%6.2f,%5.3f,%5.3f,%4.1f,%4.1f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f,%4.1f,%4.1f\n", // edit by CAM
joel_ssc 85:dd8176285b6e 154 string_state.c_str(),current_state,
joel_ssc 85:dd8176285b6e 155 data_log_time-start_time, //note offset relative to start
tnhnrl 74:d281aaef9766 156 _data_log[0],_data_log[1],_data_log[2],_data_log[3],_data_log[4],_data_log[5],_data_log[6],_data_log[7],_data_log[8],_data_log[9],_data_log[10],_data_log[11],_data_log[12],_data_log[13],_data_log[14],_data_log[15],
tnhnrl 74:d281aaef9766 157 _data_log[16],_data_log[17],_data_log[18],_data_log[19],_data_log[20],_data_log[21],_data_log[22],_data_log[23],_data_log[24],_data_log[25],_data_log[26],_data_log[27],_data_log[28],_data_log[29],_data_log[30],
CodyMarquardt 99:9d0849f5fcd7 158 _data_log[31],_data_log[32],_data_log[33],_data_log[34],_data_log[35],_data_log[36]);
tnhnrl 74:d281aaef9766 159
tnhnrl 67:c86a4b464682 160 //each line in the file is 160 characters long text-wise, check this with a file read
tnhnrl 62:d502889e74f1 161 }
tnhnrl 62:d502889e74f1 162
tnhnrl 62:d502889e74f1 163 void MbedLogger::printMbedDirectory() {
tnhnrl 62:d502889e74f1 164 DIR *dir;
tnhnrl 62:d502889e74f1 165 struct dirent *dp; //dirent.h is the format of directory entries
tnhnrl 62:d502889e74f1 166 int log_found =0,loop=0;
tnhnrl 62:d502889e74f1 167 long int temp=0;
tnhnrl 62:d502889e74f1 168
tnhnrl 62:d502889e74f1 169 //char * char_pointer;
tnhnrl 62:d502889e74f1 170 char * numstart;
tnhnrl 62:d502889e74f1 171 //char *numstop;
tnhnrl 62:d502889e74f1 172
tnhnrl 74:d281aaef9766 173 xbee().printf("\n\rPrinting out the directory of device %s\n\r", _file_system_string.c_str());
tnhnrl 62:d502889e74f1 174
tnhnrl 62:d502889e74f1 175 if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) {
tnhnrl 74:d281aaef9766 176 xbee().printf("MBED directory could not be opened\r\n");
tnhnrl 62:d502889e74f1 177 }
tnhnrl 62:d502889e74f1 178 else
tnhnrl 62:d502889e74f1 179 {
tnhnrl 62:d502889e74f1 180 while ( NULL != (dp = readdir( dir )) )
tnhnrl 62:d502889e74f1 181 {
tnhnrl 62:d502889e74f1 182 if(strncmp(dp->d_name,"LOG",3)==0)
tnhnrl 62:d502889e74f1 183 {
tnhnrl 62:d502889e74f1 184 log_found=1;
tnhnrl 62:d502889e74f1 185 /*numstart = dp->d_name; //Look for third character (do safety)
tnhnrl 62:d502889e74f1 186 numstop = strchr(dp->d_name,'.');
tnhnrl 62:d502889e74f1 187 if(numstart!=NULL&&numstop!=NULL)
tnhnrl 62:d502889e74f1 188 {
tnhnrl 62:d502889e74f1 189 temp=numstop-numstart;
tnhnrl 62:d502889e74f1 190 }
tnhnrl 62:d502889e74f1 191 else
tnhnrl 62:d502889e74f1 192 log_found=0; //Something is not right. Ignore
tnhnrl 62:d502889e74f1 193 */
tnhnrl 62:d502889e74f1 194 numstart=dp->d_name+3;
tnhnrl 62:d502889e74f1 195 temp=strtol(numstart,NULL,10); //add in check to see if this is null (start logs at one)
tnhnrl 62:d502889e74f1 196 }
tnhnrl 62:d502889e74f1 197 else
tnhnrl 62:d502889e74f1 198 log_found=0;
tnhnrl 74:d281aaef9766 199 xbee().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp);
tnhnrl 62:d502889e74f1 200
tnhnrl 62:d502889e74f1 201 loop++;
tnhnrl 62:d502889e74f1 202 }
tnhnrl 62:d502889e74f1 203 }
tnhnrl 62:d502889e74f1 204 }
tnhnrl 62:d502889e74f1 205
tnhnrl 68:8f549749b8ce 206 //prints current log file to the screen (terminal)
tnhnrl 62:d502889e74f1 207 void MbedLogger::printCurrentLogFile() {
tnhnrl 62:d502889e74f1 208 //open the file for reading
joel_ssc 84:eccd8e837134 209 // string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 210 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 211 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
tnhnrl 62:d502889e74f1 212
tnhnrl 63:6cb0405fc6e6 213 _log_file_line_counter = 0;
tnhnrl 62:d502889e74f1 214
tnhnrl 62:d502889e74f1 215 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 62:d502889e74f1 216
tnhnrl 62:d502889e74f1 217 char buffer[500];
tnhnrl 62:d502889e74f1 218
tnhnrl 62:d502889e74f1 219 //read the file line-by-line and print that to the screen
tnhnrl 74:d281aaef9766 220 xbee().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\n\r",_file_number);
tnhnrl 62:d502889e74f1 221 while (!feof(_fp)) {
tnhnrl 62:d502889e74f1 222 // read in the line and make sure it was successful
tnhnrl 62:d502889e74f1 223 if (fgets(buffer,500,_fp) != NULL) { //stops at new line
tnhnrl 74:d281aaef9766 224 xbee().printf("%s\r",buffer);
tnhnrl 63:6cb0405fc6e6 225 _log_file_line_counter++;
tnhnrl 62:d502889e74f1 226 }
tnhnrl 62:d502889e74f1 227 }
tnhnrl 62:d502889e74f1 228
tnhnrl 62:d502889e74f1 229 //fgets stops when either (n-1) characters are read, the newline character is read,
tnhnrl 62:d502889e74f1 230 // or the end-of-file is reached
tnhnrl 62:d502889e74f1 231
tnhnrl 62:d502889e74f1 232 //close the file
tnhnrl 62:d502889e74f1 233 closeLogFile();
tnhnrl 74:d281aaef9766 234 xbee().printf("\n\rLog file closed. Lines in log file: %d.\n\r", _log_file_line_counter);
tnhnrl 62:d502889e74f1 235 }
tnhnrl 62:d502889e74f1 236
tnhnrl 73:f6f378311c8d 237 void MbedLogger::blastData() {
tnhnrl 74:d281aaef9766 238 xbee().printf("blastData FUNCTION\n\r");
tnhnrl 73:f6f378311c8d 239
tnhnrl 73:f6f378311c8d 240 //OPEN FILE FOR READING
tnhnrl 73:f6f378311c8d 241
joel_ssc 84:eccd8e837134 242 //string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 243 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 244 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
tnhnrl 73:f6f378311c8d 245
tnhnrl 73:f6f378311c8d 246 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 73:f6f378311c8d 247
tnhnrl 73:f6f378311c8d 248 /******************************/
tnhnrl 73:f6f378311c8d 249 while(1) {
tnhnrl 73:f6f378311c8d 250 wait(0.05);
tnhnrl 73:f6f378311c8d 251 if (!feof(_fp)) { //check for end of file
tnhnrl 73:f6f378311c8d 252 //based on the internal packet number of the class //createDataPacket //transmit data packet
tnhnrl 73:f6f378311c8d 253 transmitPacketNumber(_packet_number);
tnhnrl 74:d281aaef9766 254 xbee().printf("\r"); // for proper spacing
tnhnrl 73:f6f378311c8d 255
tnhnrl 73:f6f378311c8d 256 _packet_number++;
tnhnrl 73:f6f378311c8d 257 }
tnhnrl 73:f6f378311c8d 258 else {
tnhnrl 73:f6f378311c8d 259 _packet_number = 0; //reset packet number
tnhnrl 73:f6f378311c8d 260 break;
tnhnrl 73:f6f378311c8d 261 }
tnhnrl 73:f6f378311c8d 262 }
tnhnrl 73:f6f378311c8d 263 /******************************/
tnhnrl 73:f6f378311c8d 264
tnhnrl 73:f6f378311c8d 265 //CLOSE THE FILE
tnhnrl 73:f6f378311c8d 266 closeLogFile();
tnhnrl 74:d281aaef9766 267 xbee().printf("\n\rblastData: Log file closed. %d.\n\r");
tnhnrl 62:d502889e74f1 268 }
tnhnrl 62:d502889e74f1 269
tnhnrl 62:d502889e74f1 270 void MbedLogger::createDataPacket() {
tnhnrl 62:d502889e74f1 271 // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2
tnhnrl 62:d502889e74f1 272
tnhnrl 62:d502889e74f1 273 //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
tnhnrl 62:d502889e74f1 274 _data_packet.clear();
tnhnrl 62:d502889e74f1 275
tnhnrl 62:d502889e74f1 276 //DATA PACKET HEADER
tnhnrl 62:d502889e74f1 277 _data_packet.push_back(117); //0x75
tnhnrl 62:d502889e74f1 278 _data_packet.push_back(101); //0x65
tnhnrl 62:d502889e74f1 279
tnhnrl 62:d502889e74f1 280 _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form
tnhnrl 62:d502889e74f1 281 _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form
tnhnrl 62:d502889e74f1 282
tnhnrl 62:d502889e74f1 283 _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form
tnhnrl 62:d502889e74f1 284 _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form
tnhnrl 62:d502889e74f1 285
tnhnrl 62:d502889e74f1 286 _data_packet.push_back(_current_line_length);
tnhnrl 62:d502889e74f1 287
tnhnrl 74:d281aaef9766 288 //xbee().printf("DEBUG: Current line buffer: %s\n\r", _line_buffer); //debug
tnhnrl 62:d502889e74f1 289
tnhnrl 62:d502889e74f1 290 //DATA FROM LINE READ (read string character by chracter)
tnhnrl 62:d502889e74f1 291 for (int i = 0; i < _current_line_length; i++) {
tnhnrl 62:d502889e74f1 292 _data_packet.push_back(_line_buffer[i]);
tnhnrl 62:d502889e74f1 293 }
tnhnrl 62:d502889e74f1 294
tnhnrl 62:d502889e74f1 295 //CRC CALCULATIONS BELOW
tnhnrl 62:d502889e74f1 296 //character version of calculation screws up on null character 0x00, scrapped and using vector of integers
tnhnrl 62:d502889e74f1 297
tnhnrl 62:d502889e74f1 298 int crc_one = calcCrcOne();
tnhnrl 62:d502889e74f1 299 int crc_two = calcCrcTwo();
tnhnrl 62:d502889e74f1 300
tnhnrl 62:d502889e74f1 301 //place the crc bytes into the data packet that is transmitted
tnhnrl 62:d502889e74f1 302 _data_packet.push_back(crc_one);
tnhnrl 62:d502889e74f1 303 _data_packet.push_back(crc_two);
tnhnrl 62:d502889e74f1 304 }
tnhnrl 62:d502889e74f1 305
tnhnrl 69:919ac8d7e023 306 //new 6/27/2018
tnhnrl 69:919ac8d7e023 307 void MbedLogger::createDataPacket(char line_buffer_sent[], int line_length_sent) {
tnhnrl 69:919ac8d7e023 308 // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2
tnhnrl 69:919ac8d7e023 309
tnhnrl 69:919ac8d7e023 310 //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
tnhnrl 69:919ac8d7e023 311 _data_packet.clear();
tnhnrl 69:919ac8d7e023 312
tnhnrl 69:919ac8d7e023 313 //DATA PACKET HEADER
tnhnrl 69:919ac8d7e023 314 _data_packet.push_back(117); //0x75
tnhnrl 69:919ac8d7e023 315 _data_packet.push_back(101); //0x65
tnhnrl 69:919ac8d7e023 316
tnhnrl 69:919ac8d7e023 317 _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form
tnhnrl 69:919ac8d7e023 318 _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form
tnhnrl 69:919ac8d7e023 319
tnhnrl 69:919ac8d7e023 320 _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form
tnhnrl 69:919ac8d7e023 321 _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form
tnhnrl 69:919ac8d7e023 322
tnhnrl 69:919ac8d7e023 323 _data_packet.push_back(line_length_sent);
tnhnrl 69:919ac8d7e023 324
tnhnrl 74:d281aaef9766 325 //xbee().printf("DEBUG: Current line buffer: %s\n\r", _line_buffer); //debug
tnhnrl 69:919ac8d7e023 326
tnhnrl 69:919ac8d7e023 327 //DATA FROM LINE READ (read string character by chracter)
tnhnrl 69:919ac8d7e023 328 for (int i = 0; i < line_length_sent; i++) {
tnhnrl 69:919ac8d7e023 329 _data_packet.push_back(line_buffer_sent[i]);
tnhnrl 69:919ac8d7e023 330 }
tnhnrl 69:919ac8d7e023 331
tnhnrl 69:919ac8d7e023 332 //CRC CALCULATIONS BELOW
tnhnrl 69:919ac8d7e023 333 //character version of calculation screws up on null character 0x00, scrapped and using vector of integers
tnhnrl 69:919ac8d7e023 334
tnhnrl 69:919ac8d7e023 335 int crc_one = calcCrcOne();
tnhnrl 69:919ac8d7e023 336 int crc_two = calcCrcTwo();
tnhnrl 69:919ac8d7e023 337
tnhnrl 69:919ac8d7e023 338 //place the crc bytes into the data packet that is transmitted
tnhnrl 69:919ac8d7e023 339 _data_packet.push_back(crc_one);
tnhnrl 69:919ac8d7e023 340 _data_packet.push_back(crc_two);
tnhnrl 69:919ac8d7e023 341
tnhnrl 74:d281aaef9766 342 //xbee().printf("debug createDataPacket(char line_buffer_sent[], int line_length_sent)\n\r");
tnhnrl 69:919ac8d7e023 343 }
tnhnrl 69:919ac8d7e023 344
tnhnrl 62:d502889e74f1 345 //should i save it as a string and transmit the string? next iteration maybe
tnhnrl 62:d502889e74f1 346
tnhnrl 62:d502889e74f1 347 void MbedLogger::transmitDataPacket() {
tnhnrl 62:d502889e74f1 348 //WRITE the data (in bytes) to the serial port
tnhnrl 62:d502889e74f1 349 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) {
tnhnrl 74:d281aaef9766 350 xbee().putc(*_it); //send integers over serial port one byte at a time
tnhnrl 62:d502889e74f1 351 }
tnhnrl 62:d502889e74f1 352 }
tnhnrl 62:d502889e74f1 353
tnhnrl 62:d502889e74f1 354 // test to see if transmitDataPacket is working slower than you think.
tnhnrl 62:d502889e74f1 355
tnhnrl 62:d502889e74f1 356 // REACH ONE CHAR AT A TIME (EACH ITERATION OF STATE MACHINE...)
tnhnrl 62:d502889e74f1 357
tnhnrl 68:8f549749b8ce 358
tnhnrl 68:8f549749b8ce 359 void MbedLogger::continuouslyTransmitDataNoTimer() {
joel_ssc 84:eccd8e837134 360 // string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 361 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 362 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
tnhnrl 68:8f549749b8ce 363
tnhnrl 68:8f549749b8ce 364 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 68:8f549749b8ce 365
tnhnrl 68:8f549749b8ce 366 static int packet_num = 0;
tnhnrl 68:8f549749b8ce 367
tnhnrl 68:8f549749b8ce 368 while(!feof(_fp)) {
tnhnrl 68:8f549749b8ce 369
tnhnrl 68:8f549749b8ce 370 _packet_number = packet_num;
tnhnrl 68:8f549749b8ce 371
tnhnrl 68:8f549749b8ce 372 readPacketInSeries(); //not using this but for testing purposes...
tnhnrl 68:8f549749b8ce 373
tnhnrl 68:8f549749b8ce 374 // // createDataPacket requires _packet_number, _total_number_of_packets, _current_line_length (data packet size)
tnhnrl 68:8f549749b8ce 375 // // uses char _line_buffer[256] variable to hold characters read from the file
tnhnrl 69:919ac8d7e023 376 // // packs this into a vector for transmission
tnhnrl 69:919ac8d7e023 377
tnhnrl 68:8f549749b8ce 378 createDataPacket();
tnhnrl 68:8f549749b8ce 379
tnhnrl 68:8f549749b8ce 380 transmitDataPacket();
tnhnrl 68:8f549749b8ce 381
tnhnrl 68:8f549749b8ce 382 packet_num++;
tnhnrl 68:8f549749b8ce 383
tnhnrl 68:8f549749b8ce 384 led3() = !led3();
tnhnrl 68:8f549749b8ce 385
tnhnrl 68:8f549749b8ce 386 //check for incoming request
tnhnrl 68:8f549749b8ce 387 }
tnhnrl 68:8f549749b8ce 388
tnhnrl 68:8f549749b8ce 389 closeLogFile(); //close log file if open
tnhnrl 68:8f549749b8ce 390 led4() = !led4();
tnhnrl 68:8f549749b8ce 391 }
tnhnrl 68:8f549749b8ce 392
tnhnrl 73:f6f378311c8d 393 void MbedLogger::fsmTransmitData() { //using the finite state machine
tnhnrl 73:f6f378311c8d 394 if ( !feof(_fp) and (!_fsm_transmit_complete) ) { //check for end of file
tnhnrl 73:f6f378311c8d 395 //based on the internal packet number of the class //createDataPacket //transmit data packet
tnhnrl 69:919ac8d7e023 396 transmitPacketNumber(_packet_number);
tnhnrl 69:919ac8d7e023 397
tnhnrl 74:d281aaef9766 398 xbee().printf("\r"); // for proper spacing
tnhnrl 73:f6f378311c8d 399
tnhnrl 69:919ac8d7e023 400 _packet_number++;
tnhnrl 68:8f549749b8ce 401
tnhnrl 73:f6f378311c8d 402 led2() = !led2();
tnhnrl 68:8f549749b8ce 403 }
tnhnrl 69:919ac8d7e023 404 else {
tnhnrl 73:f6f378311c8d 405 _packet_number = 0; //reset packet number
tnhnrl 73:f6f378311c8d 406
tnhnrl 69:919ac8d7e023 407 _fsm_transmit_complete = true;
tnhnrl 73:f6f378311c8d 408
tnhnrl 73:f6f378311c8d 409 led3() = !led3();
tnhnrl 69:919ac8d7e023 410 }
tnhnrl 68:8f549749b8ce 411 }
tnhnrl 68:8f549749b8ce 412
tnhnrl 71:939d179478c4 413 //transmitting log file with fixed length of characters to receiver program
tnhnrl 68:8f549749b8ce 414 void MbedLogger::transmitPacketNumber(int line_number) {
tnhnrl 73:f6f378311c8d 415 int line_size = 254; //length of lines in the log file, EVERY LINE MUST BE THE SAME LENGTH
tnhnrl 69:919ac8d7e023 416
tnhnrl 68:8f549749b8ce 417 fseek(_fp,(line_size+1)*line_number,SEEK_SET); //fseek must use the +1 to get the newline character
tnhnrl 69:919ac8d7e023 418
tnhnrl 69:919ac8d7e023 419 //write over the internal _line_buffer //start from the beginning and go to this position
tnhnrl 69:919ac8d7e023 420 fread(_line_buffer, 1, line_size, _fp); //read the line that is exactly 160 characters long
tnhnrl 68:8f549749b8ce 421
tnhnrl 74:d281aaef9766 422 //xbee().printf("Debug (transmitPacketNumber): line_buffer <<%s>> (line size: %d)\n\r", line_buffer,line_size);
tnhnrl 68:8f549749b8ce 423
tnhnrl 68:8f549749b8ce 424 // createDataPacket requires _packet_number, _total_number_of_packets, _current_line_length (data packet size)
tnhnrl 68:8f549749b8ce 425 // uses char _line_buffer[256] variable to hold characters read from the file
tnhnrl 68:8f549749b8ce 426 // packs this into a vector for transmission
tnhnrl 68:8f549749b8ce 427
tnhnrl 71:939d179478c4 428 //change the internal member variable for packet number, reorg this later
tnhnrl 71:939d179478c4 429 _packet_number = line_number;
tnhnrl 71:939d179478c4 430
tnhnrl 69:919ac8d7e023 431 createDataPacket(_line_buffer, line_size); //create the data packet from the _line_buffer (char array)
tnhnrl 69:919ac8d7e023 432
tnhnrl 68:8f549749b8ce 433 transmitDataPacket(); //transmit the assembled packet
tnhnrl 68:8f549749b8ce 434 }
tnhnrl 68:8f549749b8ce 435
tnhnrl 69:919ac8d7e023 436
tnhnrl 69:919ac8d7e023 437 //receive correct data packet from python, immediately send a transmit packet from the MBED
tnhnrl 73:f6f378311c8d 438 void MbedLogger::transmitOnePacket() {
tnhnrl 74:d281aaef9766 439 xbee().printf("transmitOnePacket\n");
tnhnrl 73:f6f378311c8d 440
tnhnrl 68:8f549749b8ce 441 static int transmit_state = HEADER_117; //state in switch statement
tnhnrl 73:f6f378311c8d 442 //int incoming_byte = -1; //reset each time a character is read
tnhnrl 68:8f549749b8ce 443 int requested_packet_number = -1; //reset each time a character is read
tnhnrl 68:8f549749b8ce 444 static int input_packet[4]; //changed from char in previous iteration 03/28/2018
tnhnrl 68:8f549749b8ce 445 static int transmit_crc_one = 0; //hold crc values until they're reset with calculations
tnhnrl 68:8f549749b8ce 446 static int transmit_crc_two = 0;
tnhnrl 73:f6f378311c8d 447
tnhnrl 73:f6f378311c8d 448 int incoming_byte[6];
tnhnrl 73:f6f378311c8d 449
tnhnrl 73:f6f378311c8d 450 static int bytes_received = 0;
tnhnrl 73:f6f378311c8d 451
tnhnrl 73:f6f378311c8d 452 int req_packet_number = -1;
tnhnrl 68:8f549749b8ce 453
tnhnrl 73:f6f378311c8d 454 while (1) {
tnhnrl 74:d281aaef9766 455 incoming_byte[bytes_received] = xbee().getc();
tnhnrl 74:d281aaef9766 456 xbee().printf("<%d> ", incoming_byte[bytes_received]);
tnhnrl 68:8f549749b8ce 457
tnhnrl 73:f6f378311c8d 458 bytes_received++;
tnhnrl 73:f6f378311c8d 459
tnhnrl 73:f6f378311c8d 460 if (bytes_received > 5) {
tnhnrl 68:8f549749b8ce 461
tnhnrl 73:f6f378311c8d 462 req_packet_number = incoming_byte[2] * 256 + incoming_byte[3];
tnhnrl 68:8f549749b8ce 463
tnhnrl 74:d281aaef9766 464 xbee().printf("req_packet_number = %d\n\r", req_packet_number);
tnhnrl 68:8f549749b8ce 465
tnhnrl 73:f6f378311c8d 466 bytes_received = 0;
tnhnrl 68:8f549749b8ce 467 break;
tnhnrl 73:f6f378311c8d 468 }
tnhnrl 73:f6f378311c8d 469 }
tnhnrl 73:f6f378311c8d 470
tnhnrl 73:f6f378311c8d 471 setTransmitPacketNumber(0);
tnhnrl 73:f6f378311c8d 472
tnhnrl 73:f6f378311c8d 473 //open the file
joel_ssc 84:eccd8e837134 474 // string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 475 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 476 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
tnhnrl 73:f6f378311c8d 477 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 73:f6f378311c8d 478
tnhnrl 73:f6f378311c8d 479 //receive correct checksum, immediately send this packet
tnhnrl 73:f6f378311c8d 480 transmitPacketNumber(req_packet_number);
tnhnrl 73:f6f378311c8d 481
tnhnrl 74:d281aaef9766 482 //fclose(_fp);
tnhnrl 74:d281aaef9766 483 closeLogFile();
tnhnrl 68:8f549749b8ce 484 }
tnhnrl 68:8f549749b8ce 485
tnhnrl 73:f6f378311c8d 486 void MbedLogger::transmitMultiplePackets() {
tnhnrl 74:d281aaef9766 487 xbee().printf("transmitMultiplePackets\n");
tnhnrl 73:f6f378311c8d 488
tnhnrl 74:d281aaef9766 489 //static int transmit_state = HEADER_117; //state in switch statement
tnhnrl 73:f6f378311c8d 490 //int incoming_byte = -1; //reset each time a character is read
tnhnrl 74:d281aaef9766 491 //int requested_packet_number = -1; //reset each time a character is read
tnhnrl 74:d281aaef9766 492 static int input_packet; //changed from char in previous iteration 03/28/2018
tnhnrl 62:d502889e74f1 493 static int transmit_crc_one = 0; //hold crc values until they're reset with calculations
tnhnrl 62:d502889e74f1 494 static int transmit_crc_two = 0;
tnhnrl 74:d281aaef9766 495
tnhnrl 74:d281aaef9766 496 int current_byte = -1;
tnhnrl 62:d502889e74f1 497
tnhnrl 73:f6f378311c8d 498 static int bytes_received = 0;
tnhnrl 73:f6f378311c8d 499
tnhnrl 73:f6f378311c8d 500 int req_packet_number = -1;
tnhnrl 73:f6f378311c8d 501
tnhnrl 74:d281aaef9766 502 //GET TOTAL NUMBER OF PACKETS!
tnhnrl 74:d281aaef9766 503 getNumberOfPacketsInCurrentLog();
tnhnrl 74:d281aaef9766 504 //GET TOTAL NUMBER OF PACKETS!
tnhnrl 74:d281aaef9766 505
tnhnrl 73:f6f378311c8d 506 //open the file
joel_ssc 84:eccd8e837134 507 // string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 508 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 509 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
tnhnrl 73:f6f378311c8d 510 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 74:d281aaef9766 511
tnhnrl 74:d281aaef9766 512 //DEFAULT STATE
tnhnrl 74:d281aaef9766 513 static int current_state = HEADER_117;
tnhnrl 74:d281aaef9766 514
tnhnrl 74:d281aaef9766 515 bool active_loop = true;
tnhnrl 74:d281aaef9766 516
tnhnrl 74:d281aaef9766 517 while (active_loop) {
tnhnrl 73:f6f378311c8d 518 //INCOMING BYTE
tnhnrl 74:d281aaef9766 519 current_byte = xbee().getc();
tnhnrl 74:d281aaef9766 520
tnhnrl 74:d281aaef9766 521 //provide the next byte / state
tnhnrl 73:f6f378311c8d 522
tnhnrl 74:d281aaef9766 523 switch (current_state) {
tnhnrl 74:d281aaef9766 524 case HEADER_117:
tnhnrl 74:d281aaef9766 525 //xbee().printf("HEADING 117\n\r");
tnhnrl 74:d281aaef9766 526 if (current_byte == 0x75) {
tnhnrl 74:d281aaef9766 527 current_state = HEADER_101;
tnhnrl 74:d281aaef9766 528 }
tnhnrl 74:d281aaef9766 529
tnhnrl 74:d281aaef9766 530
tnhnrl 74:d281aaef9766 531 else if (current_byte == 0x10) {
tnhnrl 74:d281aaef9766 532 current_state = END_TX_1;
tnhnrl 74:d281aaef9766 533 }
tnhnrl 74:d281aaef9766 534 break;
tnhnrl 74:d281aaef9766 535 case HEADER_101:
tnhnrl 74:d281aaef9766 536 //xbee().printf("HEADING 101\n\r");
tnhnrl 74:d281aaef9766 537 if (current_byte == 0x65) {
tnhnrl 74:d281aaef9766 538 current_state = PACKET_NO_1;
tnhnrl 73:f6f378311c8d 539 }
tnhnrl 74:d281aaef9766 540 break;
tnhnrl 74:d281aaef9766 541 case PACKET_NO_1:
tnhnrl 74:d281aaef9766 542 //xbee().printf("PACKET_NO_1\n\r");
tnhnrl 74:d281aaef9766 543 input_packet = current_byte * 256;
tnhnrl 74:d281aaef9766 544
tnhnrl 74:d281aaef9766 545 current_state = PACKET_NO_2;
tnhnrl 74:d281aaef9766 546 //xbee().printf("PACKET # 1 current byte %d\n\r", current_byte);
tnhnrl 74:d281aaef9766 547 break;
tnhnrl 73:f6f378311c8d 548
tnhnrl 74:d281aaef9766 549 case PACKET_NO_2:
tnhnrl 74:d281aaef9766 550 //xbee().printf("PACKET_NO_2\n\r");
tnhnrl 74:d281aaef9766 551 input_packet = input_packet + current_byte;
tnhnrl 74:d281aaef9766 552
tnhnrl 74:d281aaef9766 553 current_state = PACKET_CRC_ONE;
tnhnrl 74:d281aaef9766 554 //xbee().printf("PACKET # 2 current byte %d (req packet num: %d)\n\r", current_byte, input_packet);
tnhnrl 74:d281aaef9766 555 break;
tnhnrl 74:d281aaef9766 556
tnhnrl 74:d281aaef9766 557 case PACKET_CRC_ONE:
tnhnrl 74:d281aaef9766 558 //xbee().printf("PACKET_CRC_ONE\n\r");
tnhnrl 74:d281aaef9766 559 current_state = PACKET_CRC_TWO;
tnhnrl 74:d281aaef9766 560 break;
tnhnrl 73:f6f378311c8d 561
tnhnrl 74:d281aaef9766 562 case PACKET_CRC_TWO:
tnhnrl 74:d281aaef9766 563 //xbee().printf("PACKET_CRC_TWO\n\r");
tnhnrl 74:d281aaef9766 564 current_state = HEADER_117;
tnhnrl 74:d281aaef9766 565 transmitPacketNumber(input_packet);
tnhnrl 74:d281aaef9766 566 break;
tnhnrl 74:d281aaef9766 567
tnhnrl 74:d281aaef9766 568 case END_TX_1:
tnhnrl 74:d281aaef9766 569 //xbee().printf("END_TX_1\n\r");
tnhnrl 74:d281aaef9766 570 current_state = END_TX_2;
tnhnrl 74:d281aaef9766 571 break;
tnhnrl 74:d281aaef9766 572 case END_TX_2:
tnhnrl 74:d281aaef9766 573 //xbee().printf("END_TX_2\n\r");
tnhnrl 74:d281aaef9766 574 current_state = HEADER_117;
tnhnrl 74:d281aaef9766 575 active_loop = false;
tnhnrl 74:d281aaef9766 576 break;
tnhnrl 73:f6f378311c8d 577
tnhnrl 74:d281aaef9766 578 default:
tnhnrl 74:d281aaef9766 579 //reset state
tnhnrl 74:d281aaef9766 580 //xbee().printf("DEFAULT. HEADER_117\n\r");
tnhnrl 74:d281aaef9766 581 current_state = HEADER_117; //reset here
tnhnrl 74:d281aaef9766 582 break;
tnhnrl 73:f6f378311c8d 583
tnhnrl 73:f6f378311c8d 584 }
tnhnrl 73:f6f378311c8d 585 }
tnhnrl 73:f6f378311c8d 586
tnhnrl 73:f6f378311c8d 587 //CLOSE THE FILE
tnhnrl 74:d281aaef9766 588 closeLogFile();
tnhnrl 74:d281aaef9766 589 xbee().printf("08/05/2018 CLOSE THE LOG FILE\n\r");
tnhnrl 74:d281aaef9766 590
tnhnrl 74:d281aaef9766 591 //RESET THE STATE
tnhnrl 74:d281aaef9766 592 //current_state = HEADER_117;
tnhnrl 73:f6f378311c8d 593 }
tnhnrl 73:f6f378311c8d 594
tnhnrl 73:f6f378311c8d 595 void MbedLogger::checkForPythonTransmitRequest() {
tnhnrl 74:d281aaef9766 596 //xbee().printf("checkForPythonTransmitRequest\n");
tnhnrl 73:f6f378311c8d 597
tnhnrl 74:d281aaef9766 598 if ( xbee().readable() ) {
tnhnrl 73:f6f378311c8d 599
tnhnrl 73:f6f378311c8d 600 //PC READABLE DOES NOT WORK HERE?!
tnhnrl 73:f6f378311c8d 601
tnhnrl 73:f6f378311c8d 602 led2() = !led2();
tnhnrl 63:6cb0405fc6e6 603 led3() = !led3();
tnhnrl 62:d502889e74f1 604
tnhnrl 74:d281aaef9766 605 xbee().printf("########################\n");
tnhnrl 74:d281aaef9766 606 //xbee().printf("%d", xbee().getc());
tnhnrl 73:f6f378311c8d 607 }
tnhnrl 73:f6f378311c8d 608
tnhnrl 73:f6f378311c8d 609 // static int transmit_state = HEADER_117; //state in switch statement
tnhnrl 73:f6f378311c8d 610 // //int incoming_byte = -1; //reset each time a character is read
tnhnrl 73:f6f378311c8d 611 // int requested_packet_number = -1; //reset each time a character is read
tnhnrl 73:f6f378311c8d 612 // static int input_packet[4]; //changed from char in previous iteration 03/28/2018
tnhnrl 73:f6f378311c8d 613 // static int transmit_crc_one = 0; //hold crc values until they're reset with calculations
tnhnrl 73:f6f378311c8d 614 // static int transmit_crc_two = 0;
tnhnrl 73:f6f378311c8d 615 //
tnhnrl 73:f6f378311c8d 616 // static int incoming_byte[6];
tnhnrl 73:f6f378311c8d 617 //
tnhnrl 73:f6f378311c8d 618 // static int bytes_received = 0;
tnhnrl 73:f6f378311c8d 619 //
tnhnrl 73:f6f378311c8d 620 // int req_packet_number = -1;
tnhnrl 73:f6f378311c8d 621 //
tnhnrl 74:d281aaef9766 622 // if (xbee().readable()) {
tnhnrl 74:d281aaef9766 623 // incoming_byte[bytes_received] = xbee().getc();
tnhnrl 74:d281aaef9766 624 // xbee().printf("<%d> ", incoming_byte[bytes_received]);
tnhnrl 73:f6f378311c8d 625 //
tnhnrl 73:f6f378311c8d 626 // bytes_received++;
tnhnrl 73:f6f378311c8d 627 //
tnhnrl 73:f6f378311c8d 628 // if (bytes_received > 5) {
tnhnrl 73:f6f378311c8d 629 //
tnhnrl 73:f6f378311c8d 630 // req_packet_number = incoming_byte[2] * 256 + incoming_byte[3];
tnhnrl 73:f6f378311c8d 631 //
tnhnrl 74:d281aaef9766 632 // xbee().printf("req_packet_number = %d\n\r", req_packet_number);
tnhnrl 73:f6f378311c8d 633 //
tnhnrl 73:f6f378311c8d 634 // //reset
tnhnrl 73:f6f378311c8d 635 // bytes_received = 0;
tnhnrl 73:f6f378311c8d 636 // }
tnhnrl 73:f6f378311c8d 637 // }
tnhnrl 73:f6f378311c8d 638 //
tnhnrl 73:f6f378311c8d 639 // //setTransmitPacketNumber(0);
tnhnrl 73:f6f378311c8d 640 //
tnhnrl 73:f6f378311c8d 641 // /* OPEN THE FILE */
tnhnrl 73:f6f378311c8d 642 // string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 73:f6f378311c8d 643 //_fp = fopen(file_name_string.c_str(), "r");
tnhnrl 73:f6f378311c8d 644
tnhnrl 73:f6f378311c8d 645 /* RECEIVE CORRECT CHECKSUM, SEND PACKET */
tnhnrl 73:f6f378311c8d 646 //transmitPacketNumber(req_packet_number);
tnhnrl 73:f6f378311c8d 647
tnhnrl 73:f6f378311c8d 648 //fclose(_fp);
tnhnrl 73:f6f378311c8d 649
tnhnrl 73:f6f378311c8d 650 // switch(transmit_state) {
tnhnrl 73:f6f378311c8d 651 // case HEADER_117:
tnhnrl 73:f6f378311c8d 652 // led3() = !led3();
tnhnrl 73:f6f378311c8d 653 //
tnhnrl 73:f6f378311c8d 654 // //continue processing
tnhnrl 73:f6f378311c8d 655 // if (incoming_byte == 117){ //"u"
tnhnrl 73:f6f378311c8d 656 // transmit_state = HEADER_101;
tnhnrl 73:f6f378311c8d 657 // }
tnhnrl 73:f6f378311c8d 658 // //did not receive byte 1
tnhnrl 73:f6f378311c8d 659 // else {
tnhnrl 73:f6f378311c8d 660 // transmit_state = HEADER_117; //go back to checking the first packet
tnhnrl 73:f6f378311c8d 661 // }
tnhnrl 73:f6f378311c8d 662 // break;
tnhnrl 73:f6f378311c8d 663 //
tnhnrl 73:f6f378311c8d 664 // case HEADER_101:
tnhnrl 73:f6f378311c8d 665 // if(incoming_byte == 101) { //"e"
tnhnrl 73:f6f378311c8d 666 // transmit_state = TRANSMIT_PACKET_1;
tnhnrl 74:d281aaef9766 667 // //xbee().printf(" U E \n\r");
tnhnrl 73:f6f378311c8d 668 // }
tnhnrl 73:f6f378311c8d 669 // else {
tnhnrl 73:f6f378311c8d 670 // transmit_state = HEADER_117;
tnhnrl 73:f6f378311c8d 671 // }
tnhnrl 73:f6f378311c8d 672 // break;
tnhnrl 73:f6f378311c8d 673 //
tnhnrl 73:f6f378311c8d 674 // case TRANSMIT_PACKET_1:
tnhnrl 73:f6f378311c8d 675 // if (incoming_byte >= 0) {
tnhnrl 73:f6f378311c8d 676 // input_packet[0] = 117;
tnhnrl 73:f6f378311c8d 677 // input_packet[1] = 101;
tnhnrl 73:f6f378311c8d 678 // input_packet[2] = incoming_byte;
tnhnrl 73:f6f378311c8d 679 //
tnhnrl 73:f6f378311c8d 680 // transmit_state = TRANSMIT_PACKET_2;
tnhnrl 73:f6f378311c8d 681 // //_reply_byte3 = incoming_byte;
tnhnrl 73:f6f378311c8d 682 //
tnhnrl 73:f6f378311c8d 683 // led1() = !led1();
tnhnrl 74:d281aaef9766 684 // //xbee().printf(" T P \n\r");
tnhnrl 74:d281aaef9766 685 // //xbee().printf("DEBUG: Transmit Packet %d\n\r", incoming_byte);
tnhnrl 73:f6f378311c8d 686 // }
tnhnrl 73:f6f378311c8d 687 // break;
tnhnrl 73:f6f378311c8d 688 //
tnhnrl 73:f6f378311c8d 689 // case TRANSMIT_PACKET_2:
tnhnrl 73:f6f378311c8d 690 //
tnhnrl 73:f6f378311c8d 691 // if (incoming_byte >= 0) {
tnhnrl 73:f6f378311c8d 692 // input_packet[3] = incoming_byte;
tnhnrl 73:f6f378311c8d 693 // //_reply_byte4 = incoming_byte;
tnhnrl 73:f6f378311c8d 694 // }
tnhnrl 73:f6f378311c8d 695 // transmit_state = PACKET_CRC_ONE;
tnhnrl 73:f6f378311c8d 696 //
tnhnrl 73:f6f378311c8d 697 // break;
tnhnrl 73:f6f378311c8d 698 //
tnhnrl 73:f6f378311c8d 699 // case (PACKET_CRC_ONE):
tnhnrl 73:f6f378311c8d 700 // transmit_crc_one = calcCrcOneArray(input_packet, 4); //calc CRC 1 from the input packet (size 4)
tnhnrl 73:f6f378311c8d 701 //
tnhnrl 73:f6f378311c8d 702 // if (incoming_byte == transmit_crc_one) {
tnhnrl 73:f6f378311c8d 703 // transmit_state = PACKET_CRC_TWO;
tnhnrl 73:f6f378311c8d 704 // }
tnhnrl 73:f6f378311c8d 705 //
tnhnrl 73:f6f378311c8d 706 // else
tnhnrl 73:f6f378311c8d 707 // transmit_state = HEADER_117;
tnhnrl 73:f6f378311c8d 708 // //or state remains the same?
tnhnrl 73:f6f378311c8d 709 //
tnhnrl 73:f6f378311c8d 710 // break;
tnhnrl 73:f6f378311c8d 711 //
tnhnrl 73:f6f378311c8d 712 // case (PACKET_CRC_TWO):
tnhnrl 73:f6f378311c8d 713 // transmit_state = HEADER_117;
tnhnrl 73:f6f378311c8d 714 // transmit_crc_two = calcCrcTwoArray(input_packet, 4); //calc CRC 2 from the input packet (size 4)
tnhnrl 73:f6f378311c8d 715 //
tnhnrl 73:f6f378311c8d 716 // //check if CRC TWO is correct (then send full packet)
tnhnrl 73:f6f378311c8d 717 // if (incoming_byte == transmit_crc_two) {
tnhnrl 73:f6f378311c8d 718 // requested_packet_number = input_packet[2] * 256 + input_packet[3]; //compute the numbers 0 through 65535 with the two bytes
tnhnrl 73:f6f378311c8d 719 //
tnhnrl 73:f6f378311c8d 720 // //receive correct checksum, immediately send this packet
tnhnrl 73:f6f378311c8d 721 // transmitPacketNumber(requested_packet_number);
tnhnrl 73:f6f378311c8d 722 //
tnhnrl 73:f6f378311c8d 723 // //fseek(_fp, 0, SEEK_END); //reset _fp (this was causing errors with the other function)
tnhnrl 73:f6f378311c8d 724 // led3() = !led3();
tnhnrl 73:f6f378311c8d 725 //
tnhnrl 73:f6f378311c8d 726 // } //end of checksum (incoming_byte) if statement
tnhnrl 73:f6f378311c8d 727 //
tnhnrl 73:f6f378311c8d 728 // break;
tnhnrl 73:f6f378311c8d 729 // } //switch statement complete
tnhnrl 73:f6f378311c8d 730 // } //while statement complete
tnhnrl 62:d502889e74f1 731 }
tnhnrl 62:d502889e74f1 732
tnhnrl 62:d502889e74f1 733 int MbedLogger::readTransmitPacket() {
tnhnrl 62:d502889e74f1 734 _file_transmission = true;
tnhnrl 62:d502889e74f1 735 _file_transmission_state = 0;
tnhnrl 62:d502889e74f1 736
tnhnrl 62:d502889e74f1 737 //first check if you're receiving data, then read four bytes
tnhnrl 62:d502889e74f1 738
tnhnrl 62:d502889e74f1 739 int transmit_state = 0; //for state machine
tnhnrl 62:d502889e74f1 740
tnhnrl 62:d502889e74f1 741 int incoming_byte = -1;
tnhnrl 62:d502889e74f1 742
tnhnrl 62:d502889e74f1 743 int requested_packet_number = -1;
tnhnrl 62:d502889e74f1 744
tnhnrl 62:d502889e74f1 745 static int inside_while_loop = 1;
tnhnrl 62:d502889e74f1 746
tnhnrl 62:d502889e74f1 747 while (inside_while_loop) {
tnhnrl 74:d281aaef9766 748 if (xbee().readable()) { //don't rely on pc readable being open all the time
tnhnrl 62:d502889e74f1 749
tnhnrl 74:d281aaef9766 750 incoming_byte = xbee().getc();
tnhnrl 62:d502889e74f1 751
tnhnrl 62:d502889e74f1 752 switch(transmit_state) {
tnhnrl 62:d502889e74f1 753
tnhnrl 62:d502889e74f1 754 case (HEADER_117):
tnhnrl 62:d502889e74f1 755 //continue processing
tnhnrl 62:d502889e74f1 756 if (incoming_byte == 117){
tnhnrl 62:d502889e74f1 757 transmit_state = HEADER_101;
tnhnrl 62:d502889e74f1 758 }
tnhnrl 62:d502889e74f1 759 //end transmission
tnhnrl 62:d502889e74f1 760 else if (incoming_byte == 16) {
tnhnrl 62:d502889e74f1 761 transmit_state = END_TRANSMISSION;
tnhnrl 62:d502889e74f1 762 }
tnhnrl 62:d502889e74f1 763 else {
tnhnrl 62:d502889e74f1 764 transmit_state = HEADER_117;
tnhnrl 62:d502889e74f1 765 }
tnhnrl 62:d502889e74f1 766 break;
tnhnrl 62:d502889e74f1 767
tnhnrl 62:d502889e74f1 768 case (HEADER_101):
tnhnrl 62:d502889e74f1 769 if(incoming_byte == 101) {
tnhnrl 62:d502889e74f1 770 transmit_state = TRANSMIT_PACKET_1;
tnhnrl 62:d502889e74f1 771 }
tnhnrl 62:d502889e74f1 772 break;
tnhnrl 62:d502889e74f1 773
tnhnrl 62:d502889e74f1 774 case (TRANSMIT_PACKET_1):
tnhnrl 62:d502889e74f1 775 if (incoming_byte >= 0) {
tnhnrl 62:d502889e74f1 776 transmit_state = TRANSMIT_PACKET_2;
tnhnrl 62:d502889e74f1 777 _reply_byte3 = incoming_byte;
tnhnrl 62:d502889e74f1 778 }
tnhnrl 62:d502889e74f1 779 break;
tnhnrl 62:d502889e74f1 780
tnhnrl 62:d502889e74f1 781 case (TRANSMIT_PACKET_2):
tnhnrl 62:d502889e74f1 782 if (incoming_byte >= 0) {
tnhnrl 62:d502889e74f1 783 _reply_byte4 = incoming_byte;
tnhnrl 62:d502889e74f1 784 }
tnhnrl 62:d502889e74f1 785
tnhnrl 62:d502889e74f1 786 requested_packet_number = _reply_byte3 * 256 + _reply_byte4; //compute the numbers 0 through 65535 with the two bytes
tnhnrl 62:d502889e74f1 787
tnhnrl 62:d502889e74f1 788 if (requested_packet_number != _previous_reply_byte) { //CHANGE THE NAME TO SOMETHING NOT BYTE!
tnhnrl 62:d502889e74f1 789 //MUST SET THE PACKET NUMBER
tnhnrl 62:d502889e74f1 790 _packet_number = requested_packet_number;
tnhnrl 62:d502889e74f1 791
tnhnrl 62:d502889e74f1 792 readPacketInSeries();
tnhnrl 62:d502889e74f1 793 createDataPacket();
tnhnrl 62:d502889e74f1 794 _previous_reply_byte = requested_packet_number; //RECORD THIS BYTE to prevent new packets being created
tnhnrl 62:d502889e74f1 795 }
tnhnrl 62:d502889e74f1 796
tnhnrl 62:d502889e74f1 797 //continuously transmit current packet until it tells you to do the next packet (response from Python program)
tnhnrl 62:d502889e74f1 798 transmitDataPacket();
tnhnrl 62:d502889e74f1 799
tnhnrl 62:d502889e74f1 800 //TRANSMIT_PACKET_2
tnhnrl 62:d502889e74f1 801 inside_while_loop = 0; //exit the while loop with this
tnhnrl 74:d281aaef9766 802 xbee().printf("(TRANSMIT_PACKET_2)reached inside_while_loop = 0\n\r"); //DEBUG
tnhnrl 62:d502889e74f1 803 break;
tnhnrl 62:d502889e74f1 804 } //end of switch statement
tnhnrl 62:d502889e74f1 805 } //end of while loop
tnhnrl 62:d502889e74f1 806
tnhnrl 62:d502889e74f1 807 // else {
tnhnrl 74:d281aaef9766 808 // //xbee().printf("pc not readable \n\r");
tnhnrl 62:d502889e74f1 809 // }
tnhnrl 62:d502889e74f1 810 //
tnhnrl 62:d502889e74f1 811 }
tnhnrl 62:d502889e74f1 812
tnhnrl 62:d502889e74f1 813 //once you're outside of the while loop
tnhnrl 62:d502889e74f1 814 inside_while_loop = true; //for next iteration
tnhnrl 62:d502889e74f1 815
tnhnrl 74:d281aaef9766 816 xbee().printf("DEBUG: (readTransmitPacket) Outside of while loop\n\r");
tnhnrl 62:d502889e74f1 817 return false;
tnhnrl 62:d502889e74f1 818 }
tnhnrl 62:d502889e74f1 819
tnhnrl 73:f6f378311c8d 820 bool MbedLogger::endTransmitPacket() {
tnhnrl 73:f6f378311c8d 821
tnhnrl 73:f6f378311c8d 822 int incoming_byte;
tnhnrl 73:f6f378311c8d 823
tnhnrl 74:d281aaef9766 824 while (xbee().readable()) {
tnhnrl 74:d281aaef9766 825 incoming_byte = xbee().getc(); //get first byte
tnhnrl 73:f6f378311c8d 826
tnhnrl 73:f6f378311c8d 827 if (incoming_byte == 16) {
tnhnrl 74:d281aaef9766 828 incoming_byte = xbee().getc(); //get second byte
tnhnrl 73:f6f378311c8d 829 return true;
tnhnrl 73:f6f378311c8d 830 }
tnhnrl 73:f6f378311c8d 831 //quick and dirty
tnhnrl 73:f6f378311c8d 832
tnhnrl 73:f6f378311c8d 833 }
tnhnrl 73:f6f378311c8d 834
tnhnrl 73:f6f378311c8d 835 return false;
tnhnrl 73:f6f378311c8d 836 }
tnhnrl 73:f6f378311c8d 837
tnhnrl 62:d502889e74f1 838 void MbedLogger::reOpenLineReader() {
tnhnrl 62:d502889e74f1 839 //open a new one
joel_ssc 84:eccd8e837134 840 // string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 841 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 842 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
tnhnrl 62:d502889e74f1 843
tnhnrl 62:d502889e74f1 844 _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read
tnhnrl 62:d502889e74f1 845
tnhnrl 62:d502889e74f1 846 //check if this actually worked...
tnhnrl 62:d502889e74f1 847 if (!_fp) {
tnhnrl 74:d281aaef9766 848 xbee().printf("ERROR: Log file could not be opened\n\r");
tnhnrl 62:d502889e74f1 849 }
tnhnrl 62:d502889e74f1 850 else {
joel_ssc 84:eccd8e837134 851 xbee().printf("Current Log file (%s) was opened.\n\r", configFileIO().logFilesStruct.logFileName);
tnhnrl 62:d502889e74f1 852 }
tnhnrl 62:d502889e74f1 853 }
tnhnrl 62:d502889e74f1 854
tnhnrl 62:d502889e74f1 855 bool MbedLogger::openLineReader() {
joel_ssc 84:eccd8e837134 856 // string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 857 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 858 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
joel_ssc 84:eccd8e837134 859
tnhnrl 62:d502889e74f1 860
tnhnrl 62:d502889e74f1 861 _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read
tnhnrl 62:d502889e74f1 862
tnhnrl 62:d502889e74f1 863 //check if this actually worked...
tnhnrl 62:d502889e74f1 864 if (!_fp) {
tnhnrl 74:d281aaef9766 865 //xbee().printf("ERROR: Log file could not be opened\n\r");
tnhnrl 62:d502889e74f1 866 return false;
tnhnrl 62:d502889e74f1 867 }
tnhnrl 62:d502889e74f1 868 else {
tnhnrl 74:d281aaef9766 869 //xbee().printf("Current Log file (LOG000.csv) was opened.\n\r");
tnhnrl 62:d502889e74f1 870 return true;
tnhnrl 62:d502889e74f1 871 }
tnhnrl 62:d502889e74f1 872 }
tnhnrl 62:d502889e74f1 873
tnhnrl 62:d502889e74f1 874 //VERIFIED THIS IS WORKING
tnhnrl 62:d502889e74f1 875 void MbedLogger::readPacketInSeries(){
tnhnrl 62:d502889e74f1 876 memset(&_line_buffer[0], 0, sizeof(_line_buffer)); //clear buffer each time, start at the address, fill with zeroes, go to the end of the char array
tnhnrl 62:d502889e74f1 877
tnhnrl 62:d502889e74f1 878 //read the current line in the file
tnhnrl 62:d502889e74f1 879 fgets(_line_buffer, 256, _fp); //reads the line of characters until you reach a newline character
tnhnrl 62:d502889e74f1 880
tnhnrl 62:d502889e74f1 881 //RECORD THE STRING LENGTH
tnhnrl 62:d502889e74f1 882 _current_line_length = strlen(_line_buffer);
tnhnrl 62:d502889e74f1 883 }
tnhnrl 62:d502889e74f1 884
tnhnrl 69:919ac8d7e023 885 int MbedLogger::getNumberOfPacketsInCurrentLog() {
tnhnrl 73:f6f378311c8d 886 //takes less than a second to complete, verified 7/24/2018
tnhnrl 73:f6f378311c8d 887
tnhnrl 73:f6f378311c8d 888 //open the file
joel_ssc 84:eccd8e837134 889 // string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 890 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 891 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
joel_ssc 84:eccd8e837134 892
tnhnrl 73:f6f378311c8d 893 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 62:d502889e74f1 894
tnhnrl 73:f6f378311c8d 895 fseek(_fp, 0L, SEEK_END);
tnhnrl 73:f6f378311c8d 896
tnhnrl 73:f6f378311c8d 897 size_t size = ftell(_fp);
tnhnrl 62:d502889e74f1 898
tnhnrl 62:d502889e74f1 899 //move the FILE pointer back to the start
tnhnrl 62:d502889e74f1 900 fseek(_fp, 0, SEEK_SET); // SEEK_SET is the beginning of file
tnhnrl 62:d502889e74f1 901
tnhnrl 73:f6f378311c8d 902 _total_number_of_packets = size/254;
tnhnrl 69:919ac8d7e023 903
tnhnrl 74:d281aaef9766 904 //CLOSE THE FILE
tnhnrl 74:d281aaef9766 905 closeLogFile();
tnhnrl 74:d281aaef9766 906
tnhnrl 69:919ac8d7e023 907 return _total_number_of_packets;
tnhnrl 62:d502889e74f1 908 }
tnhnrl 62:d502889e74f1 909
tnhnrl 62:d502889e74f1 910 void MbedLogger::endTransmissionCloseFile() {
tnhnrl 62:d502889e74f1 911 // if the file pointer is null, the file was not opened in the first place
tnhnrl 62:d502889e74f1 912 if (!_fp) {
tnhnrl 74:d281aaef9766 913 xbee().printf("\n endTransmissionCloseFile: FILE WAS NOT OPENED!\n\r");
tnhnrl 62:d502889e74f1 914 }
tnhnrl 62:d502889e74f1 915 else {
tnhnrl 74:d281aaef9766 916 xbee().printf("\n endTransmissionCloseFile: FILE FOUND AND CLOSED!\n\r");
tnhnrl 62:d502889e74f1 917 closeLogFile();
tnhnrl 62:d502889e74f1 918 }
tnhnrl 62:d502889e74f1 919
tnhnrl 62:d502889e74f1 920 _file_transmission = false;
tnhnrl 62:d502889e74f1 921 }
tnhnrl 62:d502889e74f1 922
tnhnrl 62:d502889e74f1 923 void MbedLogger::openWriteFile() {
tnhnrl 74:d281aaef9766 924 xbee().printf("Opening file for reception.\n\r");
tnhnrl 62:d502889e74f1 925
joel_ssc 84:eccd8e837134 926 // string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 927 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 928 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
joel_ssc 84:eccd8e837134 929
tnhnrl 62:d502889e74f1 930
tnhnrl 62:d502889e74f1 931 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 62:d502889e74f1 932 }
tnhnrl 62:d502889e74f1 933
tnhnrl 62:d502889e74f1 934
tnhnrl 62:d502889e74f1 935 //weird bug noticed on 5/25/2018 where if you're not sending data the function is not completing
tnhnrl 62:d502889e74f1 936
tnhnrl 62:d502889e74f1 937
tnhnrl 62:d502889e74f1 938
tnhnrl 62:d502889e74f1 939
tnhnrl 62:d502889e74f1 940 // function checks for incoming data (receiver function) from a Python program that transmits a file
tnhnrl 62:d502889e74f1 941 // current limit is a file that has 255 lines of data
tnhnrl 68:8f549749b8ce 942 bool MbedLogger::checkForIncomingData() {
tnhnrl 62:d502889e74f1 943 int receive_packet_number;
tnhnrl 62:d502889e74f1 944 int receive_total_number_packets;
tnhnrl 62:d502889e74f1 945 int receive_packet_size;
tnhnrl 62:d502889e74f1 946
tnhnrl 62:d502889e74f1 947 bool data_transmission_complete = false;
tnhnrl 62:d502889e74f1 948
tnhnrl 62:d502889e74f1 949 int incoming_byte;
tnhnrl 62:d502889e74f1 950
tnhnrl 62:d502889e74f1 951 char char_buffer[256] = {}; //create empty buffer
tnhnrl 62:d502889e74f1 952
tnhnrl 62:d502889e74f1 953 //starting state
tnhnrl 62:d502889e74f1 954 int process_state = HEADER_117;
tnhnrl 62:d502889e74f1 955
tnhnrl 62:d502889e74f1 956 //variables for processing data below
tnhnrl 62:d502889e74f1 957 int checksum_one = -1;
tnhnrl 62:d502889e74f1 958 int checksum_two = -1;
tnhnrl 62:d502889e74f1 959
tnhnrl 62:d502889e74f1 960 int i = 5;
tnhnrl 62:d502889e74f1 961 int serial_timeout = 0;
tnhnrl 62:d502889e74f1 962
tnhnrl 74:d281aaef9766 963 while (xbee().readable() && !data_transmission_complete) {
tnhnrl 74:d281aaef9766 964 incoming_byte = xbee().getc(); //getc returns an unsigned char cast to an int
CodyMarquardt 99:9d0849f5fcd7 965 xbee().printf("DEBUG: State 0 || byte: %d\n\r",incoming_byte);
tnhnrl 62:d502889e74f1 966
tnhnrl 62:d502889e74f1 967 switch(process_state) {
tnhnrl 62:d502889e74f1 968 case HEADER_117:
tnhnrl 62:d502889e74f1 969 //continue processing
tnhnrl 62:d502889e74f1 970 if (incoming_byte == 117){
tnhnrl 62:d502889e74f1 971 process_state = HEADER_101;
CodyMarquardt 99:9d0849f5fcd7 972 xbee().printf("DEBUG: Case 117\n\r");
tnhnrl 62:d502889e74f1 973 }
tnhnrl 62:d502889e74f1 974 //end transmission
tnhnrl 62:d502889e74f1 975 else if (incoming_byte == 16) {
tnhnrl 62:d502889e74f1 976 process_state = END_TRANSMISSION;
CodyMarquardt 99:9d0849f5fcd7 977 xbee().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r");
tnhnrl 62:d502889e74f1 978 }
tnhnrl 62:d502889e74f1 979 else {
tnhnrl 62:d502889e74f1 980 process_state = HEADER_117; // ???
CodyMarquardt 99:9d0849f5fcd7 981 xbee().printf("DEBUG: State Header 117\n\r");
tnhnrl 62:d502889e74f1 982 }
tnhnrl 62:d502889e74f1 983 break;
tnhnrl 62:d502889e74f1 984
tnhnrl 62:d502889e74f1 985 case HEADER_101:
tnhnrl 62:d502889e74f1 986 if(incoming_byte == 101) {
tnhnrl 62:d502889e74f1 987 process_state = PACKET_NUM;
CodyMarquardt 99:9d0849f5fcd7 988 xbee().printf("DEBUG: Case 101\n\r");
tnhnrl 62:d502889e74f1 989 }
tnhnrl 62:d502889e74f1 990 break;
tnhnrl 62:d502889e74f1 991
tnhnrl 62:d502889e74f1 992 case PACKET_NUM:
tnhnrl 62:d502889e74f1 993 receive_packet_number = incoming_byte;
tnhnrl 62:d502889e74f1 994 process_state = TOTAL_NUM_PACKETS;
CodyMarquardt 99:9d0849f5fcd7 995 xbee().printf("DEBUG: Case PACKET_NUM\n\r");
tnhnrl 62:d502889e74f1 996 break;
tnhnrl 62:d502889e74f1 997
tnhnrl 62:d502889e74f1 998 case TOTAL_NUM_PACKETS:
tnhnrl 62:d502889e74f1 999 receive_total_number_packets = incoming_byte;
tnhnrl 62:d502889e74f1 1000 process_state = PACKET_SIZE;
CodyMarquardt 99:9d0849f5fcd7 1001 xbee().printf("DEBUG: Case TOTAL_NUM_PACKETS\n\r");
tnhnrl 62:d502889e74f1 1002 break;
tnhnrl 62:d502889e74f1 1003
tnhnrl 62:d502889e74f1 1004 case PACKET_SIZE:
tnhnrl 62:d502889e74f1 1005 receive_packet_size = incoming_byte;
CodyMarquardt 99:9d0849f5fcd7 1006 xbee().printf("DEBUG: Case PACKET_SIZE\n\r");
tnhnrl 62:d502889e74f1 1007
tnhnrl 62:d502889e74f1 1008 //write the header stuff to it
tnhnrl 62:d502889e74f1 1009 char_buffer[0] = 117;
tnhnrl 62:d502889e74f1 1010 char_buffer[1] = 101;
tnhnrl 62:d502889e74f1 1011 char_buffer[2] = receive_packet_number;
tnhnrl 62:d502889e74f1 1012 char_buffer[3] = receive_total_number_packets;
tnhnrl 62:d502889e74f1 1013 char_buffer[4] = receive_packet_size;
tnhnrl 62:d502889e74f1 1014
tnhnrl 62:d502889e74f1 1015 // tests confirmed that packet number is zero, number of packets is 12, packet size is 12
CodyMarquardt 99:9d0849f5fcd7 1016 xbee().printf("char_buffer 2/3/4: %d %d %d\n\r", receive_packet_number,receive_total_number_packets,receive_packet_size);
tnhnrl 68:8f549749b8ce 1017
tnhnrl 62:d502889e74f1 1018 //process packet data, future version will append for larger data sizes, 0xFFFF
tnhnrl 62:d502889e74f1 1019
tnhnrl 62:d502889e74f1 1020 // IF YOU GET AN INTERRUPTED DATA STREAM YOU NEED TO BREAK OUT OF THE LOOP
tnhnrl 62:d502889e74f1 1021 i = 5;
tnhnrl 62:d502889e74f1 1022 serial_timeout = 0;
tnhnrl 62:d502889e74f1 1023
tnhnrl 62:d502889e74f1 1024 while (true) {
tnhnrl 74:d281aaef9766 1025 if (xbee().readable()) {
tnhnrl 74:d281aaef9766 1026 char_buffer[i] = xbee().getc(); //read all of the data packets
tnhnrl 62:d502889e74f1 1027 i++;
tnhnrl 62:d502889e74f1 1028
tnhnrl 62:d502889e74f1 1029 serial_timeout = 0; //reset the timeout
tnhnrl 62:d502889e74f1 1030 }
tnhnrl 62:d502889e74f1 1031 else {
CodyMarquardt 99:9d0849f5fcd7 1032 serial_timeout++;
CodyMarquardt 99:9d0849f5fcd7 1033 //xbee().printf("DEBUG: timeout = %d\n\r",serial_timeout);
tnhnrl 62:d502889e74f1 1034 }
tnhnrl 62:d502889e74f1 1035
tnhnrl 62:d502889e74f1 1036 // When full data packet is received...
tnhnrl 62:d502889e74f1 1037 if (i >= receive_packet_size+5) { //cannot do this properly with a for loop
tnhnrl 62:d502889e74f1 1038 //get checksum bytes
tnhnrl 74:d281aaef9766 1039 checksum_one = xbee().getc();
tnhnrl 74:d281aaef9766 1040 checksum_two = xbee().getc();
tnhnrl 62:d502889e74f1 1041
tnhnrl 62:d502889e74f1 1042 //calculate the CRC from the header and data bytes using _data_packet (vector)
tnhnrl 62:d502889e74f1 1043 //found out calculating crc with string was dropping empty or null spaces
tnhnrl 62:d502889e74f1 1044 _data_packet.clear(); //clear it just in case
tnhnrl 62:d502889e74f1 1045
tnhnrl 62:d502889e74f1 1046 for (int a = 0; a < receive_packet_size+5; a++) {
tnhnrl 62:d502889e74f1 1047 _data_packet.push_back(char_buffer[a]); //push the character array into the buffer
tnhnrl 62:d502889e74f1 1048 }
tnhnrl 62:d502889e74f1 1049
tnhnrl 62:d502889e74f1 1050 //calculate the CRC using the vector (strings will cut off null characters)
tnhnrl 62:d502889e74f1 1051 int calc_crc_one = calcCrcOne();
tnhnrl 62:d502889e74f1 1052 int calc_crc_two = calcCrcTwo();
tnhnrl 62:d502889e74f1 1053
CodyMarquardt 99:9d0849f5fcd7 1054 xbee().printf("DEBUG: calc crc 1: %d, crc 2: %d\n\r", calc_crc_one, calc_crc_two);
tnhnrl 62:d502889e74f1 1055
tnhnrl 62:d502889e74f1 1056 // first confirm that the checksum is correct
tnhnrl 74:d281aaef9766 1057 if ((calc_crc_one == checksum_one) and (calc_crc_two == checksum_two)) {
tnhnrl 74:d281aaef9766 1058
CodyMarquardt 99:9d0849f5fcd7 1059 xbee().printf("DEBUG: checksums are good!\n\r");
tnhnrl 74:d281aaef9766 1060
CodyMarquardt 99:9d0849f5fcd7 1061 xbee().printf("receive_packet_number %d and _confirmed_packet_number %d\n\r", receive_packet_number, _confirmed_packet_number); //debug
tnhnrl 74:d281aaef9766 1062
tnhnrl 74:d281aaef9766 1063 // // CHECKSUM CORRECT & get the filename from the first packet (check that you receive first packet)
tnhnrl 74:d281aaef9766 1064 // if (receive_packet_number == 0) {
tnhnrl 74:d281aaef9766 1065 // char temp_char[receive_packet_size+1]; //temp array for memcpy
tnhnrl 74:d281aaef9766 1066 // memset(&temp_char[0], 0, sizeof(temp_char)); //clear full array (or get random characters)
tnhnrl 74:d281aaef9766 1067 // strncpy(temp_char, char_buffer + 5 /* Offset */, receive_packet_size*sizeof(char) /* Length */); //memcpy introduced random characters
tnhnrl 74:d281aaef9766 1068 //
tnhnrl 74:d281aaef9766 1069 // //xbee().printf("SEQUENCE: <<%s>>\n\r", temp_char]) // ERASE
tnhnrl 74:d281aaef9766 1070 //
tnhnrl 74:d281aaef9766 1071 // //have to terminate the string with '\0'
tnhnrl 74:d281aaef9766 1072 // temp_char[receive_packet_size] = '\0';
tnhnrl 74:d281aaef9766 1073 //
tnhnrl 74:d281aaef9766 1074 // //xbee().printf("\n\rDEBUG: ------ Filename? <<%s>>\n\r", temp_char);
tnhnrl 74:d281aaef9766 1075 // //_received_filename = temp_char;
tnhnrl 74:d281aaef9766 1076 //
tnhnrl 74:d281aaef9766 1077 // //xbee().printf("\n\rDEBUG: _received_filename <<%s>>\n\r", _received_filename);
tnhnrl 74:d281aaef9766 1078 //
tnhnrl 74:d281aaef9766 1079 // //open a file for writing
tnhnrl 74:d281aaef9766 1080 // //openReceiveFile(_received_filename);
tnhnrl 74:d281aaef9766 1081 //
tnhnrl 74:d281aaef9766 1082 // //send a reply to Python transmit program
tnhnrl 74:d281aaef9766 1083 // sendReply();
tnhnrl 74:d281aaef9766 1084 //
tnhnrl 74:d281aaef9766 1085 // led3() = 1;
tnhnrl 74:d281aaef9766 1086 //
tnhnrl 74:d281aaef9766 1087 // // even if correct CRC, counter prevents the program from writing the same packet twice
tnhnrl 74:d281aaef9766 1088 // _confirmed_packet_number++;
tnhnrl 74:d281aaef9766 1089 // }
tnhnrl 62:d502889e74f1 1090
tnhnrl 62:d502889e74f1 1091 // check if the packet that you're receiving (receive_packet_number) has been received already...
tnhnrl 62:d502889e74f1 1092
tnhnrl 62:d502889e74f1 1093 //CHECKSUM CORRECT & packet numbers that are 1 through N packets
tnhnrl 74:d281aaef9766 1094 if (receive_packet_number == _confirmed_packet_number){
tnhnrl 62:d502889e74f1 1095 //save the data (char buffer) to the file if both checksums work...
tnhnrl 62:d502889e74f1 1096
tnhnrl 62:d502889e74f1 1097 // when a packet is received (successfully) send a reply
tnhnrl 74:d281aaef9766 1098 //sendReply();
tnhnrl 62:d502889e74f1 1099
tnhnrl 62:d502889e74f1 1100 // write correct data to file
tnhnrl 62:d502889e74f1 1101 fprintf(_fp, "%s", char_buffer+5);
tnhnrl 62:d502889e74f1 1102
tnhnrl 62:d502889e74f1 1103 // even if correct CRC, counter prevents the program from writing the same packet twice
tnhnrl 62:d502889e74f1 1104 _confirmed_packet_number++;
tnhnrl 62:d502889e74f1 1105 }
tnhnrl 62:d502889e74f1 1106
tnhnrl 62:d502889e74f1 1107 //clear the variables
tnhnrl 62:d502889e74f1 1108 checksum_one = -1;
tnhnrl 62:d502889e74f1 1109 checksum_two = -1;
tnhnrl 62:d502889e74f1 1110 }
tnhnrl 62:d502889e74f1 1111
tnhnrl 62:d502889e74f1 1112 process_state = HEADER_117;
tnhnrl 62:d502889e74f1 1113
tnhnrl 62:d502889e74f1 1114 break;
tnhnrl 62:d502889e74f1 1115 }
tnhnrl 62:d502889e74f1 1116
CodyMarquardt 99:9d0849f5fcd7 1117 //counter breaks out of the loop if no data received
CodyMarquardt 99:9d0849f5fcd7 1118 if (serial_timeout >= 1000) {
CodyMarquardt 99:9d0849f5fcd7 1119 xbee().printf("break serial_timeout %d\n\r", serial_timeout);
CodyMarquardt 99:9d0849f5fcd7 1120 break;
CodyMarquardt 99:9d0849f5fcd7 1121 }
tnhnrl 62:d502889e74f1 1122 }
tnhnrl 62:d502889e74f1 1123 break;
tnhnrl 62:d502889e74f1 1124
tnhnrl 62:d502889e74f1 1125 case END_TRANSMISSION:
tnhnrl 74:d281aaef9766 1126 if (xbee().getc() == 16) {
CodyMarquardt 99:9d0849f5fcd7 1127 xbee().printf("DEBUG: END_TRANSMISSION REACHED: 1. \n\r");
tnhnrl 62:d502889e74f1 1128
tnhnrl 74:d281aaef9766 1129 if (xbee().getc() == 16) {
CodyMarquardt 99:9d0849f5fcd7 1130 xbee().printf("DEBUG: END_TRANSMISSION REACHED: 2. \n\r");
tnhnrl 62:d502889e74f1 1131
tnhnrl 74:d281aaef9766 1132 _end_sequence_transmission = true;
tnhnrl 74:d281aaef9766 1133 //endReceiveData();
tnhnrl 62:d502889e74f1 1134 }
tnhnrl 62:d502889e74f1 1135 }
tnhnrl 62:d502889e74f1 1136
CodyMarquardt 99:9d0849f5fcd7 1137 xbee().printf("DEBUG: END_TRANSMISSION REACHED: 5. \n\r");
tnhnrl 62:d502889e74f1 1138
tnhnrl 62:d502889e74f1 1139 //process_state = HEADER_117; //don't do this unless the check is wrong
CodyMarquardt 99:9d0849f5fcd7 1140 xbee().printf("END_TRANSMISSION process_state is %d\n\r", process_state); //should be 5 (debug) 02/06/2018
tnhnrl 62:d502889e74f1 1141 data_transmission_complete = true;
tnhnrl 62:d502889e74f1 1142 break;
tnhnrl 62:d502889e74f1 1143 }//END OF SWITCH
tnhnrl 62:d502889e74f1 1144
tnhnrl 62:d502889e74f1 1145 if (data_transmission_complete) {
CodyMarquardt 99:9d0849f5fcd7 1146 xbee().printf("DEBUG: checkForIncomingData data_transmission_complete \n\r");
tnhnrl 62:d502889e74f1 1147 break; //out of while loop
tnhnrl 62:d502889e74f1 1148 }
tnhnrl 62:d502889e74f1 1149 } // while loop
tnhnrl 62:d502889e74f1 1150
tnhnrl 62:d502889e74f1 1151 led3() = !led3();
tnhnrl 62:d502889e74f1 1152
tnhnrl 62:d502889e74f1 1153 if (data_transmission_complete)
tnhnrl 62:d502889e74f1 1154 return false; //tell state machine class that this is done (not transmitting, false)
tnhnrl 62:d502889e74f1 1155 else {
tnhnrl 62:d502889e74f1 1156 return true;
tnhnrl 62:d502889e74f1 1157 }
tnhnrl 62:d502889e74f1 1158 }
tnhnrl 62:d502889e74f1 1159
tnhnrl 62:d502889e74f1 1160 int MbedLogger::sendReply() {
tnhnrl 62:d502889e74f1 1161 //being explicit in what's being transmitted
tnhnrl 62:d502889e74f1 1162
tnhnrl 62:d502889e74f1 1163 //change this method to be more explicit later
tnhnrl 62:d502889e74f1 1164
tnhnrl 62:d502889e74f1 1165 //integer vector _data_packet is used here, cleared fist just in case
tnhnrl 62:d502889e74f1 1166
tnhnrl 62:d502889e74f1 1167 _data_packet.clear(); //same data packet for transmission
tnhnrl 62:d502889e74f1 1168 _data_packet.push_back(117);
tnhnrl 62:d502889e74f1 1169 _data_packet.push_back(101);
tnhnrl 62:d502889e74f1 1170
tnhnrl 62:d502889e74f1 1171 //_confirmed_packet_number comes from the packet number that is sent from the Python program
tnhnrl 62:d502889e74f1 1172 _data_packet.push_back(_confirmed_packet_number / 256); //packet number only changed when confirmed
tnhnrl 62:d502889e74f1 1173 _data_packet.push_back(_confirmed_packet_number % 256); //split into first and second byte
tnhnrl 62:d502889e74f1 1174
tnhnrl 62:d502889e74f1 1175 //compute checksums
tnhnrl 62:d502889e74f1 1176
tnhnrl 62:d502889e74f1 1177 int receiver_crc_one = calcCrcOne();
tnhnrl 62:d502889e74f1 1178 int receiver_crc_two = calcCrcTwo();
tnhnrl 62:d502889e74f1 1179
tnhnrl 62:d502889e74f1 1180 _data_packet.push_back(receiver_crc_one);
tnhnrl 62:d502889e74f1 1181 _data_packet.push_back(receiver_crc_two);
tnhnrl 62:d502889e74f1 1182
tnhnrl 62:d502889e74f1 1183 //transmit this packet
tnhnrl 62:d502889e74f1 1184 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) {
tnhnrl 74:d281aaef9766 1185 xbee().putc(*_it); //send integers over serial port one byte at a time
tnhnrl 62:d502889e74f1 1186 }
tnhnrl 62:d502889e74f1 1187
tnhnrl 62:d502889e74f1 1188 //change process methodology later...
tnhnrl 62:d502889e74f1 1189
tnhnrl 62:d502889e74f1 1190 return _confirmed_packet_number;
tnhnrl 62:d502889e74f1 1191 }
tnhnrl 62:d502889e74f1 1192
tnhnrl 62:d502889e74f1 1193 void MbedLogger::endReceiveData() { //DLE character * 4 ==> 10 10 10 10
tnhnrl 62:d502889e74f1 1194 closeLogFile(); //close the file here
tnhnrl 74:d281aaef9766 1195 xbee().printf("endReceiveData closed the file and ended transmission\n\r");
tnhnrl 62:d502889e74f1 1196 }
tnhnrl 62:d502889e74f1 1197
tnhnrl 62:d502889e74f1 1198 //calculate the crc with an integer array
tnhnrl 62:d502889e74f1 1199 int MbedLogger::calcCrcOneArray(int *input_array, int array_length) {
tnhnrl 62:d502889e74f1 1200 //can't initialize the table in the constructor in c++
tnhnrl 62:d502889e74f1 1201 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 62:d502889e74f1 1202 int crc = 0;
tnhnrl 62:d502889e74f1 1203 for (int z = 0; z < array_length; z++) {
tnhnrl 62:d502889e74f1 1204 crc = (crc_table[(input_array[z] ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 62:d502889e74f1 1205 }
tnhnrl 62:d502889e74f1 1206 return crc / 256; //second-to-last byte
tnhnrl 62:d502889e74f1 1207 }
tnhnrl 62:d502889e74f1 1208
tnhnrl 62:d502889e74f1 1209 int MbedLogger::calcCrcTwoArray(int *input_array, int array_length) {
tnhnrl 62:d502889e74f1 1210 //can't initialize the table in the constructor in c++
tnhnrl 62:d502889e74f1 1211 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 62:d502889e74f1 1212 int crc = 0;
tnhnrl 62:d502889e74f1 1213 for (int z = 0; z < array_length; z++) {
tnhnrl 62:d502889e74f1 1214 crc = (crc_table[(input_array[z] ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 62:d502889e74f1 1215 }
tnhnrl 62:d502889e74f1 1216 return crc % 256; //second-to-last byte
tnhnrl 62:d502889e74f1 1217 }
tnhnrl 62:d502889e74f1 1218
tnhnrl 62:d502889e74f1 1219 int MbedLogger::calcCrcOne() {
tnhnrl 62:d502889e74f1 1220 //can't initialize the table in the constructor in c++
tnhnrl 62:d502889e74f1 1221 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 62:d502889e74f1 1222
tnhnrl 62:d502889e74f1 1223 int crc = 0;
tnhnrl 62:d502889e74f1 1224 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++)
tnhnrl 62:d502889e74f1 1225 crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 62:d502889e74f1 1226
tnhnrl 62:d502889e74f1 1227 return crc / 256; //second-to-last byte
tnhnrl 62:d502889e74f1 1228 }
tnhnrl 62:d502889e74f1 1229
tnhnrl 62:d502889e74f1 1230 int MbedLogger::calcCrcTwo() {
tnhnrl 62:d502889e74f1 1231 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 62:d502889e74f1 1232
tnhnrl 62:d502889e74f1 1233 int crc = 0;
tnhnrl 62:d502889e74f1 1234 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++)
tnhnrl 62:d502889e74f1 1235 crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 62:d502889e74f1 1236
tnhnrl 74:d281aaef9766 1237 //xbee().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_array.length(), crc % 256);
tnhnrl 62:d502889e74f1 1238
tnhnrl 62:d502889e74f1 1239 return crc % 256; //last byte
tnhnrl 62:d502889e74f1 1240 }
tnhnrl 62:d502889e74f1 1241
tnhnrl 62:d502889e74f1 1242 void MbedLogger::resetReplyPacket() {
tnhnrl 62:d502889e74f1 1243 _confirmed_packet_number = 0;
tnhnrl 62:d502889e74f1 1244 }
tnhnrl 62:d502889e74f1 1245
tnhnrl 62:d502889e74f1 1246 void MbedLogger::openNewMissionFile() {
tnhnrl 74:d281aaef9766 1247 xbee().printf("Opening Mission file (sequence.txt) for reception.\n\r");
tnhnrl 62:d502889e74f1 1248 string filename_string = _file_system_string + "sequence.txt";
tnhnrl 62:d502889e74f1 1249
tnhnrl 74:d281aaef9766 1250 xbee().printf("openNewMissionFile: %s\n\r", filename_string.c_str());
tnhnrl 62:d502889e74f1 1251
tnhnrl 62:d502889e74f1 1252 _fp = fopen(filename_string.c_str(), "w");
tnhnrl 62:d502889e74f1 1253 }
tnhnrl 62:d502889e74f1 1254
tnhnrl 62:d502889e74f1 1255 void MbedLogger::openReceiveFile(string filename) {
tnhnrl 62:d502889e74f1 1256 string filename_string = _file_system_string + filename; //example "sequence.txt"
tnhnrl 62:d502889e74f1 1257
tnhnrl 62:d502889e74f1 1258 _fp = fopen(filename_string.c_str(), "w"); //open a file for writing
tnhnrl 62:d502889e74f1 1259 }
tnhnrl 62:d502889e74f1 1260
tnhnrl 62:d502889e74f1 1261 void MbedLogger::setDataCounter(int input_counter) {
tnhnrl 62:d502889e74f1 1262 _transmit_counter = input_counter;
tnhnrl 62:d502889e74f1 1263 }
tnhnrl 62:d502889e74f1 1264
tnhnrl 62:d502889e74f1 1265 void MbedLogger::closeIncompleteFile() {
tnhnrl 62:d502889e74f1 1266 fprintf(_fp, "TRANSMISSION INTERRUPTED!"); //write this warning to the file
tnhnrl 62:d502889e74f1 1267 closeLogFile(); //close file
tnhnrl 62:d502889e74f1 1268 }
tnhnrl 62:d502889e74f1 1269
tnhnrl 62:d502889e74f1 1270 void MbedLogger::appendLogFile(int current_state, int option) {
tnhnrl 62:d502889e74f1 1271 //option one means write to file
tnhnrl 62:d502889e74f1 1272
tnhnrl 62:d502889e74f1 1273 if (option == 1) {
tnhnrl 62:d502889e74f1 1274 if (!_fp) { //if not present
tnhnrl 62:d502889e74f1 1275 _fp = fopen(_full_file_path_string.c_str(), "a");
tnhnrl 62:d502889e74f1 1276 }
tnhnrl 62:d502889e74f1 1277
tnhnrl 62:d502889e74f1 1278 //record data using the recordData function (takes in the state integer)
tnhnrl 62:d502889e74f1 1279 recordData(current_state);
tnhnrl 62:d502889e74f1 1280 }
tnhnrl 62:d502889e74f1 1281
tnhnrl 62:d502889e74f1 1282 else {
tnhnrl 62:d502889e74f1 1283 closeLogFile();
tnhnrl 62:d502889e74f1 1284 }
tnhnrl 62:d502889e74f1 1285 }
joel_ssc 82:0981b9ada820 1286 void MbedLogger::appendDiagFile(char *printf_string, int flushclose) {
joel_ssc 82:0981b9ada820 1287 //
joel_ssc 82:0981b9ada820 1288
joel_ssc 87:6d95f853dab3 1289 int start_time = 1518467832;
joel_ssc 87:6d95f853dab3 1290 if(_time_set) { start_time = _default_timestamp_time;}
joel_ssc 82:0981b9ada820 1291 //in the future create the ability to set the start time
joel_ssc 82:0981b9ada820 1292
joel_ssc 82:0981b9ada820 1293 int time_now = mbedLogger().getSystemTime();
joel_ssc 82:0981b9ada820 1294 if (!_fp2) { //if not present
joel_ssc 82:0981b9ada820 1295 _fp2 = fopen(_full_diagfile_path_string.c_str(), "a");
joel_ssc 82:0981b9ada820 1296 }
joel_ssc 82:0981b9ada820 1297 int del_time = time_now - start_time;
joel_ssc 82:0981b9ada820 1298 //record data using the recordData function (takes in the state integer)
joel_ssc 82:0981b9ada820 1299 fprintf(_fp2, "time=%d seconds ", del_time);
joel_ssc 82:0981b9ada820 1300 fprintf(_fp2, printf_string);
joel_ssc 82:0981b9ada820 1301 if(flushclose == 1) { fflush(_fp2); }
joel_ssc 82:0981b9ada820 1302 if(flushclose == 0) {fclose(_fp2); _fp2 = NULL; }
joel_ssc 82:0981b9ada820 1303 }
tnhnrl 62:d502889e74f1 1304
tnhnrl 62:d502889e74f1 1305 // initialize and close the file
tnhnrl 62:d502889e74f1 1306 // log file freezes at 0x0000006c
tnhnrl 62:d502889e74f1 1307 void MbedLogger::initializeLogFile() {
CodyMarquardt 99:9d0849f5fcd7 1308 char buf[256];
joel_ssc 82:0981b9ada820 1309 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 82:0981b9ada820 1310 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
CodyMarquardt 99:9d0849f5fcd7 1311
joel_ssc 82:0981b9ada820 1312 _full_file_path_string = file_name_string;
tnhnrl 74:d281aaef9766 1313 xbee().printf("%s file system init\n\r", _file_system_string.c_str());
joel_ssc 82:0981b9ada820 1314 sprintf(buf, "%s mbedlogger():initializelogfile: file system init\n\r", _file_system_string.c_str());
CodyMarquardt 99:9d0849f5fcd7 1315 appendDiagFile(buf,1);
tnhnrl 62:d502889e74f1 1316 //try to open this file...
tnhnrl 62:d502889e74f1 1317 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 62:d502889e74f1 1318
tnhnrl 62:d502889e74f1 1319 //if the file is empty, create this.
tnhnrl 62:d502889e74f1 1320 if (!_fp) {
tnhnrl 62:d502889e74f1 1321 _fp = fopen(file_name_string.c_str(), "w"); //write,print,close
tnhnrl 67:c86a4b464682 1322 //fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth_ft,pitch_cmd,pitch_deg,bce_cmd,bce_mm,batt_cmd,batt_mm,pitchRate_degs,depthRate_fps\nempty log file!\n");
tnhnrl 67:c86a4b464682 1323 fprintf(_fp,_heading_string.c_str());
tnhnrl 62:d502889e74f1 1324 closeLogFile();
tnhnrl 62:d502889e74f1 1325 }
tnhnrl 62:d502889e74f1 1326 else
tnhnrl 62:d502889e74f1 1327 closeLogFile(); //close the opened read file
tnhnrl 62:d502889e74f1 1328 }
joel_ssc 82:0981b9ada820 1329 void MbedLogger::initializeDiagFile(int print_diag) {
joel_ssc 82:0981b9ada820 1330 //string file_name_string = _file_system_string + "DIAG000.txt"; // how to pass in log file name as a string?
joel_ssc 82:0981b9ada820 1331 string file_name_string = _file_system_string + configFileIO().logFilesStruct.diagFileName; // "DIAG000.txt";
joel_ssc 82:0981b9ada820 1332 _full_diagfile_path_string = file_name_string;
joel_ssc 82:0981b9ada820 1333 char buf[256];
joel_ssc 82:0981b9ada820 1334 xbee().printf("%s file system init\n\r", _file_system_string.c_str());
joel_ssc 82:0981b9ada820 1335 sprintf(buf, "mbedlogger():Initializediagfile input variable diagfilename: %s file system init\n\r", file_name_string.c_str());
joel_ssc 82:0981b9ada820 1336 if(print_diag == 1) {appendDiagFile(buf,3);} //configFileIO().logFilesStruct.diagFileName
joel_ssc 82:0981b9ada820 1337 //try to open this file...
joel_ssc 82:0981b9ada820 1338 if( _fp2 == NULL) { _fp2 = fopen(file_name_string.c_str(), "r"); }
joel_ssc 82:0981b9ada820 1339
joel_ssc 82:0981b9ada820 1340 //if the file is empty, create this.
joel_ssc 82:0981b9ada820 1341 if (!_fp2) {
joel_ssc 82:0981b9ada820 1342 _fp2 = fopen(file_name_string.c_str(), "w"); //write,print,close
joel_ssc 82:0981b9ada820 1343 //fprintf(_fp,"state_string,state_ID,timer,depth_cmd,depth_ft,pitch_cmd,pitch_deg,bce_cmd,bce_mm,batt_cmd,batt_mm,pitchRate_degs,depthRate_fps\nempty log file!\n");
joel_ssc 82:0981b9ada820 1344 fprintf(_fp2,_diag_heading_string.c_str());
joel_ssc 82:0981b9ada820 1345 closeDiagFile();
joel_ssc 82:0981b9ada820 1346 }
joel_ssc 82:0981b9ada820 1347 else
joel_ssc 82:0981b9ada820 1348 closeDiagFile(); //close the opened read file
joel_ssc 82:0981b9ada820 1349 }
tnhnrl 62:d502889e74f1 1350
tnhnrl 62:d502889e74f1 1351 int MbedLogger::fileTransmitState() {
tnhnrl 62:d502889e74f1 1352 return _file_transmission_state;
tnhnrl 62:d502889e74f1 1353 }
tnhnrl 62:d502889e74f1 1354
tnhnrl 62:d502889e74f1 1355 int MbedLogger::calcCrcOneString (string input_string) {
tnhnrl 62:d502889e74f1 1356 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 62:d502889e74f1 1357
tnhnrl 62:d502889e74f1 1358 int crc = 0;
tnhnrl 62:d502889e74f1 1359 for (unsigned int i = 0; i < input_string.length(); i++) {
tnhnrl 62:d502889e74f1 1360 //convert each character to an integer
tnhnrl 62:d502889e74f1 1361 int character_to_integer = input_string[i]; //correct
tnhnrl 62:d502889e74f1 1362
tnhnrl 62:d502889e74f1 1363 crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 62:d502889e74f1 1364 }
tnhnrl 62:d502889e74f1 1365
tnhnrl 74:d281aaef9766 1366 //xbee().printf("DEBUG: calcCrcOne string length: %d crc: %d\n\r", input_string.length(), crc/256);
tnhnrl 62:d502889e74f1 1367
tnhnrl 62:d502889e74f1 1368 return crc / 256; //second-to-last byte
tnhnrl 62:d502889e74f1 1369 }
tnhnrl 62:d502889e74f1 1370
tnhnrl 62:d502889e74f1 1371 int MbedLogger::calcCrcTwoString (string input_string) {
tnhnrl 62:d502889e74f1 1372 int crc_table [256] = {0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897, 16448};
tnhnrl 62:d502889e74f1 1373
tnhnrl 62:d502889e74f1 1374 int crc = 0;
tnhnrl 62:d502889e74f1 1375 for (unsigned int i = 0; i < input_string.length(); i++) {
tnhnrl 62:d502889e74f1 1376 //convert each character to an integer
tnhnrl 62:d502889e74f1 1377 int character_to_integer = input_string[i]; //correct
tnhnrl 62:d502889e74f1 1378
tnhnrl 62:d502889e74f1 1379 crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 62:d502889e74f1 1380 }
tnhnrl 62:d502889e74f1 1381
tnhnrl 74:d281aaef9766 1382 //xbee().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_string.length(), crc % 256);
tnhnrl 62:d502889e74f1 1383
tnhnrl 62:d502889e74f1 1384 return crc % 256; //last byte
tnhnrl 62:d502889e74f1 1385 }
tnhnrl 62:d502889e74f1 1386
tnhnrl 62:d502889e74f1 1387 void MbedLogger::createEmptyLog() {
joel_ssc 84:eccd8e837134 1388 // string file_name_string = _file_system_string + "LOG000.csv";
joel_ssc 84:eccd8e837134 1389 string file_name_string = _file_system_string + configFileIO().logFilesStruct.logFileName; // "DIAG000.txt";
joel_ssc 84:eccd8e837134 1390 //string file_name_string = _file_system_string + "LOG000.csv"; // how to pass in log file name as a string?
tnhnrl 67:c86a4b464682 1391 string empty_log = "EMPTY LOG";
tnhnrl 62:d502889e74f1 1392
tnhnrl 62:d502889e74f1 1393 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 67:c86a4b464682 1394
tnhnrl 67:c86a4b464682 1395 fprintf(_fp, "%.25s\n",empty_log.c_str()); //just write this string to the log (processing needs a file size that is not zero)
tnhnrl 62:d502889e74f1 1396 closeLogFile();
tnhnrl 62:d502889e74f1 1397 }
tnhnrl 62:d502889e74f1 1398
tnhnrl 62:d502889e74f1 1399 int MbedLogger::getFileSize(string filename) {
tnhnrl 62:d502889e74f1 1400 // fixed the const char * errror:
tnhnrl 62:d502889e74f1 1401 // https://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char
tnhnrl 62:d502889e74f1 1402 const char * char_filename = filename.c_str(); // Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
tnhnrl 62:d502889e74f1 1403 //http://www.cplusplus.com/reference/string/string/c_str/
tnhnrl 62:d502889e74f1 1404
tnhnrl 62:d502889e74f1 1405 _fp = fopen(filename.c_str(), "rb"); //open the file for reading as a binary file
tnhnrl 62:d502889e74f1 1406
tnhnrl 62:d502889e74f1 1407 fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file)
tnhnrl 62:d502889e74f1 1408 unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file.
tnhnrl 62:d502889e74f1 1409 fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary
tnhnrl 80:4e5d306d695b 1410
tnhnrl 80:4e5d306d695b 1411 xbee().printf("%s file size is %d bytes.\n\r", filename.c_str(), file_size);
tnhnrl 62:d502889e74f1 1412
tnhnrl 80:4e5d306d695b 1413 closeLogFile(); //can probably just close the file pointer and not worry about position
tnhnrl 62:d502889e74f1 1414
tnhnrl 62:d502889e74f1 1415 return file_size;
tnhnrl 62:d502889e74f1 1416 }
tnhnrl 62:d502889e74f1 1417
tnhnrl 62:d502889e74f1 1418 int MbedLogger::debugFileState() {
tnhnrl 74:d281aaef9766 1419 xbee().printf("What is _fp right now? %p\n\r", _fp); //pointer notation
tnhnrl 62:d502889e74f1 1420
tnhnrl 62:d502889e74f1 1421 if (_fp)
tnhnrl 62:d502889e74f1 1422 return 1; //file pointer does exist
tnhnrl 62:d502889e74f1 1423 else
tnhnrl 62:d502889e74f1 1424 return 0; //file pointer does not exist
tnhnrl 62:d502889e74f1 1425 }
tnhnrl 62:d502889e74f1 1426
tnhnrl 62:d502889e74f1 1427 void MbedLogger::specifyFileForTransmit(string input_string) {
tnhnrl 74:d281aaef9766 1428 xbee().printf("specifyFileForTransmit\n\r");
tnhnrl 62:d502889e74f1 1429
tnhnrl 62:d502889e74f1 1430 string file_string = _file_system_string + input_string;
tnhnrl 62:d502889e74f1 1431
tnhnrl 74:d281aaef9766 1432 xbee().printf("file_string is <%s>\n\r", file_string.c_str());
tnhnrl 62:d502889e74f1 1433
tnhnrl 62:d502889e74f1 1434 //open this file to read
tnhnrl 62:d502889e74f1 1435 _fp = fopen(file_string.c_str(), "r");
tnhnrl 62:d502889e74f1 1436
tnhnrl 62:d502889e74f1 1437 //transmit that file
tnhnrl 62:d502889e74f1 1438 //transmitDataWithTicker(); //replaced ticker
tnhnrl 62:d502889e74f1 1439
tnhnrl 62:d502889e74f1 1440 _file_transmission = true;
tnhnrl 62:d502889e74f1 1441 }
tnhnrl 62:d502889e74f1 1442
tnhnrl 62:d502889e74f1 1443 void MbedLogger::transmitFileFromDirectory( int file_number ) {
tnhnrl 62:d502889e74f1 1444 file_number = file_number + 1; //to get the correct number
tnhnrl 62:d502889e74f1 1445
tnhnrl 62:d502889e74f1 1446 DIR *dir;
tnhnrl 62:d502889e74f1 1447 struct dirent *dp; //dirent.h is the format of directory entries
tnhnrl 62:d502889e74f1 1448 int log_found =0, loop=1; //start file numbers at 1
tnhnrl 62:d502889e74f1 1449 long int temp=0;
tnhnrl 62:d502889e74f1 1450
tnhnrl 62:d502889e74f1 1451 // char * char_pointer;
tnhnrl 62:d502889e74f1 1452 // char * numstart, *numstop;
tnhnrl 62:d502889e74f1 1453
tnhnrl 62:d502889e74f1 1454 if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) {
tnhnrl 74:d281aaef9766 1455 xbee().printf("MBED directory could not be opened\r\n");
tnhnrl 62:d502889e74f1 1456 }
tnhnrl 62:d502889e74f1 1457 else
tnhnrl 62:d502889e74f1 1458 {
tnhnrl 62:d502889e74f1 1459 while ( NULL != (dp = readdir( dir )) )
tnhnrl 62:d502889e74f1 1460 {
tnhnrl 74:d281aaef9766 1461 xbee().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp);
tnhnrl 62:d502889e74f1 1462
tnhnrl 62:d502889e74f1 1463 //process current file if it matches the file number
tnhnrl 62:d502889e74f1 1464 if (file_number == loop) {
tnhnrl 62:d502889e74f1 1465 char * current_file_name = dp->d_name; //pointer to this char array
tnhnrl 62:d502889e74f1 1466
tnhnrl 62:d502889e74f1 1467 specifyFileForTransmit(current_file_name);
tnhnrl 62:d502889e74f1 1468
tnhnrl 62:d502889e74f1 1469 break;
tnhnrl 62:d502889e74f1 1470 }
tnhnrl 62:d502889e74f1 1471
tnhnrl 62:d502889e74f1 1472 loop++;
tnhnrl 62:d502889e74f1 1473 }
tnhnrl 62:d502889e74f1 1474 }
tnhnrl 62:d502889e74f1 1475 }
tnhnrl 62:d502889e74f1 1476
tnhnrl 62:d502889e74f1 1477 void MbedLogger::accessMbedDirectory() {
tnhnrl 62:d502889e74f1 1478 printMbedDirectory();
tnhnrl 62:d502889e74f1 1479
tnhnrl 74:d281aaef9766 1480 xbee().printf("Type in the number of the file you want to transmit.\n\r");
tnhnrl 62:d502889e74f1 1481
tnhnrl 62:d502889e74f1 1482 char message[42];
tnhnrl 62:d502889e74f1 1483
tnhnrl 74:d281aaef9766 1484 xbee().scanf("%41s", message);
tnhnrl 62:d502889e74f1 1485
tnhnrl 74:d281aaef9766 1486 xbee().printf("Input received!\n\r");
tnhnrl 62:d502889e74f1 1487
tnhnrl 62:d502889e74f1 1488 //check if char array is an integer
tnhnrl 62:d502889e74f1 1489 char* conversion_pointer;
tnhnrl 62:d502889e74f1 1490 long converted = strtol(message, &conversion_pointer, 10);
tnhnrl 62:d502889e74f1 1491
tnhnrl 62:d502889e74f1 1492 if (*conversion_pointer) {
tnhnrl 62:d502889e74f1 1493 //conversion failed because the input was not a number
tnhnrl 74:d281aaef9766 1494 xbee().printf("NOT A VALID FILE NUMBER!\n\r");
tnhnrl 62:d502889e74f1 1495 }
tnhnrl 62:d502889e74f1 1496 else {
tnhnrl 62:d502889e74f1 1497 //conversion worked!
tnhnrl 74:d281aaef9766 1498 xbee().printf("You chose file number: %d\n\r", converted);
tnhnrl 62:d502889e74f1 1499
tnhnrl 62:d502889e74f1 1500 // transmit the file
tnhnrl 62:d502889e74f1 1501 transmitFileFromDirectory(converted);
tnhnrl 62:d502889e74f1 1502 }
tnhnrl 62:d502889e74f1 1503 }
tnhnrl 62:d502889e74f1 1504
tnhnrl 62:d502889e74f1 1505 void MbedLogger::closeLogFile() {
tnhnrl 68:8f549749b8ce 1506 led4() = 1;
tnhnrl 68:8f549749b8ce 1507
tnhnrl 62:d502889e74f1 1508 if (_fp == NULL){
tnhnrl 74:d281aaef9766 1509 xbee().printf("MbedLogger: (%s) LOG FILE WAS ALREADY CLOSED!\n\r", _file_system_string.c_str());
tnhnrl 62:d502889e74f1 1510 }
tnhnrl 62:d502889e74f1 1511
tnhnrl 62:d502889e74f1 1512 else {
tnhnrl 74:d281aaef9766 1513 xbee().printf("MbedLogger: (%s) CLOSING LOG FILE!\n\r", _file_system_string.c_str());
tnhnrl 62:d502889e74f1 1514
tnhnrl 62:d502889e74f1 1515 //close file
tnhnrl 62:d502889e74f1 1516 fclose(_fp);
tnhnrl 62:d502889e74f1 1517
tnhnrl 62:d502889e74f1 1518 _fp = NULL; //set pointer to zero
tnhnrl 62:d502889e74f1 1519 }
tnhnrl 62:d502889e74f1 1520 }
joel_ssc 82:0981b9ada820 1521 void MbedLogger::closeDiagFile() { // closes he present diag file on _fp2
joel_ssc 82:0981b9ada820 1522 led4() = 1;
joel_ssc 82:0981b9ada820 1523
joel_ssc 82:0981b9ada820 1524 if (_fp2 == NULL){
joel_ssc 82:0981b9ada820 1525 xbee().printf("MbedLogger: (%s) DIAG FILE WAS ALREADY CLOSED!\n\r", _full_diagfile_path_string.c_str());
joel_ssc 82:0981b9ada820 1526 }
joel_ssc 82:0981b9ada820 1527
joel_ssc 82:0981b9ada820 1528 else {
joel_ssc 82:0981b9ada820 1529 xbee().printf("MbedLogger: (%s) CLOSING LOG FILE!\n\r", _full_diagfile_path_string.c_str());
joel_ssc 82:0981b9ada820 1530
joel_ssc 82:0981b9ada820 1531 //close file
joel_ssc 82:0981b9ada820 1532 fclose(_fp2);
joel_ssc 82:0981b9ada820 1533
joel_ssc 82:0981b9ada820 1534 _fp2 = NULL; //set pointer to zero
joel_ssc 82:0981b9ada820 1535 }
joel_ssc 82:0981b9ada820 1536 }
tnhnrl 62:d502889e74f1 1537
tnhnrl 62:d502889e74f1 1538 void MbedLogger::activateReceivePacket() {
tnhnrl 62:d502889e74f1 1539 _mbed_receive_loop = true;
tnhnrl 62:d502889e74f1 1540 }
tnhnrl 62:d502889e74f1 1541
tnhnrl 74:d281aaef9766 1542 void MbedLogger::receiveSequenceFile() {
tnhnrl 74:d281aaef9766 1543 //restart each time
tnhnrl 74:d281aaef9766 1544 _end_sequence_transmission = false;
tnhnrl 74:d281aaef9766 1545 openNewMissionFile();
tnhnrl 74:d281aaef9766 1546 //zero will be reserved for the file name, future
tnhnrl 74:d281aaef9766 1547 _confirmed_packet_number = 1; //in sendReply() function that transmits a reply for incoming data
tnhnrl 74:d281aaef9766 1548
tnhnrl 74:d281aaef9766 1549 // int current_packet_number = 1;
tnhnrl 74:d281aaef9766 1550 // int last_packet_number = -1;
tnhnrl 74:d281aaef9766 1551 // int break_transmission = 0;
tnhnrl 74:d281aaef9766 1552
tnhnrl 74:d281aaef9766 1553 int counter = 0;
tnhnrl 74:d281aaef9766 1554
tnhnrl 74:d281aaef9766 1555 while(1) {
tnhnrl 74:d281aaef9766 1556 wait(0.25); //runs at 4 Hz
tnhnrl 74:d281aaef9766 1557
tnhnrl 74:d281aaef9766 1558 checkForIncomingData();
tnhnrl 74:d281aaef9766 1559
tnhnrl 74:d281aaef9766 1560 //xbee().printf("\n\rDEBUG: _confirmed_packet_number%d\n\r", _confirmed_packet_number);
tnhnrl 74:d281aaef9766 1561
tnhnrl 74:d281aaef9766 1562 counter++;
tnhnrl 74:d281aaef9766 1563
CodyMarquardt 99:9d0849f5fcd7 1564 sendReply(); //bad name, should call it send request or something CAM
tnhnrl 74:d281aaef9766 1565
tnhnrl 74:d281aaef9766 1566 if (_end_sequence_transmission)
tnhnrl 74:d281aaef9766 1567 break;
tnhnrl 74:d281aaef9766 1568 }
tnhnrl 74:d281aaef9766 1569
tnhnrl 74:d281aaef9766 1570 closeLogFile();
tnhnrl 74:d281aaef9766 1571 }
tnhnrl 74:d281aaef9766 1572
tnhnrl 62:d502889e74f1 1573 void MbedLogger::receiveMissionDataWithFSM() {
tnhnrl 62:d502889e74f1 1574 checkForIncomingData();
tnhnrl 62:d502889e74f1 1575
tnhnrl 62:d502889e74f1 1576 // Example: send reply 75 65 00 00
tnhnrl 62:d502889e74f1 1577 // Example: send reply 75 65 00 01
tnhnrl 62:d502889e74f1 1578 }
tnhnrl 62:d502889e74f1 1579
tnhnrl 62:d502889e74f1 1580 void MbedLogger::receiveMissionDataWithTicker() {
tnhnrl 62:d502889e74f1 1581 openNewMissionFile(); //sequence.txt file opened
tnhnrl 62:d502889e74f1 1582
tnhnrl 62:d502889e74f1 1583 _mbed_receive_ticker.attach(callback(this, &MbedLogger::activateReceivePacket), 0.5);
tnhnrl 62:d502889e74f1 1584
tnhnrl 74:d281aaef9766 1585 xbee().printf("\n\r02/09/2018 MbedLogger receiveMissionData Beginning to receive sequence data...\n\r");
tnhnrl 62:d502889e74f1 1586
tnhnrl 62:d502889e74f1 1587 resetReplyPacket(); //reset the reply packet
tnhnrl 62:d502889e74f1 1588
tnhnrl 62:d502889e74f1 1589 //idea for stopping this if data not being received
tnhnrl 62:d502889e74f1 1590 int current_packet_number = 0;
tnhnrl 62:d502889e74f1 1591 int last_packet_number = -1;
tnhnrl 62:d502889e74f1 1592 int break_transmission = 0;
tnhnrl 62:d502889e74f1 1593
tnhnrl 62:d502889e74f1 1594 while(1) {
tnhnrl 62:d502889e74f1 1595 //runs at 10 hz
tnhnrl 62:d502889e74f1 1596 if (_mbed_receive_loop) {
tnhnrl 62:d502889e74f1 1597 if (!checkForIncomingData()) { // run this until it finishes
tnhnrl 62:d502889e74f1 1598 //when you complete data reception, this will become false
tnhnrl 74:d281aaef9766 1599 xbee().printf("\n\rMbedLogger: Data RECEPTION complete.\n\r");
tnhnrl 62:d502889e74f1 1600 _mbed_receive_ticker.detach();
tnhnrl 62:d502889e74f1 1601 break;
tnhnrl 62:d502889e74f1 1602 }
tnhnrl 62:d502889e74f1 1603 else {
tnhnrl 62:d502889e74f1 1604
tnhnrl 62:d502889e74f1 1605 // SEND REPLY 75 65 00 00
tnhnrl 62:d502889e74f1 1606 // SEND REPLY 75 65 00 01
tnhnrl 62:d502889e74f1 1607
tnhnrl 62:d502889e74f1 1608 //check if you keep getting the same thing
tnhnrl 62:d502889e74f1 1609 current_packet_number = sendReply();
tnhnrl 62:d502889e74f1 1610
tnhnrl 74:d281aaef9766 1611 //xbee().printf("DEBUG: current packet number %d (last packet number %d) \n\r", current_packet_number, last_packet_number); //debug
tnhnrl 62:d502889e74f1 1612
tnhnrl 62:d502889e74f1 1613 //let this count up a few times before it exits
tnhnrl 62:d502889e74f1 1614 if (current_packet_number == last_packet_number) {
tnhnrl 62:d502889e74f1 1615 break_transmission++;
tnhnrl 62:d502889e74f1 1616
tnhnrl 62:d502889e74f1 1617 //break transmission after 50 failed attempts (was 100)
tnhnrl 62:d502889e74f1 1618 if (break_transmission >= 50) {
tnhnrl 62:d502889e74f1 1619 closeIncompleteFile(); //close the file
tnhnrl 62:d502889e74f1 1620 _mbed_receive_ticker.detach();
tnhnrl 74:d281aaef9766 1621 xbee().printf("MbedLogger: TRANSMISSION INTERRUPTED!\n\r");
tnhnrl 62:d502889e74f1 1622 break;
tnhnrl 62:d502889e74f1 1623 }
tnhnrl 62:d502889e74f1 1624 }
tnhnrl 62:d502889e74f1 1625 else
tnhnrl 62:d502889e74f1 1626 last_packet_number = current_packet_number;
tnhnrl 62:d502889e74f1 1627 }
tnhnrl 62:d502889e74f1 1628 _mbed_receive_loop = false; // wait until the loop rate timer fires again
tnhnrl 62:d502889e74f1 1629 }
tnhnrl 62:d502889e74f1 1630 }
tnhnrl 62:d502889e74f1 1631 }
tnhnrl 62:d502889e74f1 1632
tnhnrl 62:d502889e74f1 1633 //only do this for the MBED because of the limited file size
tnhnrl 62:d502889e74f1 1634 //write one line to the file (open to write, this will erase all other data) and close it.
tnhnrl 62:d502889e74f1 1635 void MbedLogger::eraseFile() {
tnhnrl 62:d502889e74f1 1636 _fp = fopen(_full_file_path_string.c_str(), "w"); // LOG000.csv
tnhnrl 62:d502889e74f1 1637
tnhnrl 67:c86a4b464682 1638 fprintf(_fp,_heading_string.c_str());
tnhnrl 74:d281aaef9766 1639
tnhnrl 62:d502889e74f1 1640 closeLogFile();
tnhnrl 62:d502889e74f1 1641 }
tnhnrl 62:d502889e74f1 1642
tnhnrl 62:d502889e74f1 1643 void MbedLogger::intCreateDataPacket(int data_buffer[],int payload_length) {
tnhnrl 62:d502889e74f1 1644 // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2
tnhnrl 62:d502889e74f1 1645
tnhnrl 62:d502889e74f1 1646 //check here https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=01c0
tnhnrl 62:d502889e74f1 1647 // ieee 754: http://www6.uniovi.es/~antonio/uned/ieee754/IEEE-754hex32.html
tnhnrl 62:d502889e74f1 1648
tnhnrl 62:d502889e74f1 1649 //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
tnhnrl 62:d502889e74f1 1650 _data_packet.clear();
tnhnrl 62:d502889e74f1 1651
tnhnrl 62:d502889e74f1 1652 //DATA PACKET HEADER
tnhnrl 62:d502889e74f1 1653 _data_packet.push_back(117); //0x75
tnhnrl 62:d502889e74f1 1654 _data_packet.push_back(101); //0x65
tnhnrl 62:d502889e74f1 1655
tnhnrl 62:d502889e74f1 1656 _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form
tnhnrl 62:d502889e74f1 1657 _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form
tnhnrl 62:d502889e74f1 1658
tnhnrl 62:d502889e74f1 1659 _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form
tnhnrl 62:d502889e74f1 1660 _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form
tnhnrl 62:d502889e74f1 1661
tnhnrl 62:d502889e74f1 1662 _data_packet.push_back(_current_line_length);
tnhnrl 62:d502889e74f1 1663
tnhnrl 62:d502889e74f1 1664 //DATA FROM INTEGER ARRAY (read the array)
tnhnrl 62:d502889e74f1 1665 for (int i = 0; i < payload_length; i++) {
tnhnrl 62:d502889e74f1 1666 _data_packet.push_back(data_buffer[i]);
tnhnrl 62:d502889e74f1 1667 }
tnhnrl 62:d502889e74f1 1668
tnhnrl 62:d502889e74f1 1669 //CRC CALCULATIONS BELOW, character version of calculation screws up on null character 0x00, scrapped and using vector of integers
tnhnrl 62:d502889e74f1 1670 int crc_one = calcCrcOne();
tnhnrl 62:d502889e74f1 1671 int crc_two = calcCrcTwo();
tnhnrl 62:d502889e74f1 1672
tnhnrl 62:d502889e74f1 1673 //place the crc bytes into the data packet that is transmitted
tnhnrl 62:d502889e74f1 1674 _data_packet.push_back(crc_one);
tnhnrl 62:d502889e74f1 1675 _data_packet.push_back(crc_two);
tnhnrl 62:d502889e74f1 1676 }
tnhnrl 62:d502889e74f1 1677
tnhnrl 68:8f549749b8ce 1678 void MbedLogger::setTransmitPacketNumber(int packet_number) {
tnhnrl 68:8f549749b8ce 1679 _transmit_packet_num = packet_number;
tnhnrl 69:919ac8d7e023 1680
tnhnrl 69:919ac8d7e023 1681 //also needed to reset a boolean flag on the transmit
tnhnrl 69:919ac8d7e023 1682 _fsm_transmit_complete = false;
tnhnrl 73:f6f378311c8d 1683
tnhnrl 73:f6f378311c8d 1684 _end_transmit_packet = false;
tnhnrl 72:250b2665755c 1685 }
tnhnrl 72:250b2665755c 1686
tnhnrl 72:250b2665755c 1687 int MbedLogger::currentPacketNumber() {
tnhnrl 72:250b2665755c 1688 return _packet_number;
tnhnrl 74:d281aaef9766 1689 }