most functionality to splashdwon, find neutral and start mission. short timeouts still in code for testing, will adjust to go directly to sit_idle after splashdown

Dependencies:   mbed MODSERIAL FATFileSystem

Committer:
tnhnrl
Date:
Thu Feb 15 03:07:16 2018 +0000
Revision:
47:fb3c7929d3f3
Parent:
46:030be9f5c793
Child:
48:20e681885161
everything except sd card loader...what's up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tnhnrl 46:030be9f5c793 1 #include "MbedLogger.hpp"
tnhnrl 46:030be9f5c793 2 #include "StaticDefs.hpp"
tnhnrl 46:030be9f5c793 3
tnhnrl 46:030be9f5c793 4 MbedLogger::MbedLogger(string file_system_input_string) {
tnhnrl 46:030be9f5c793 5 _file_system_string = file_system_input_string;
tnhnrl 46:030be9f5c793 6
tnhnrl 46:030be9f5c793 7 _full_file_path_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 8
tnhnrl 46:030be9f5c793 9 _f = 0;
tnhnrl 46:030be9f5c793 10 _file_number = 0;
tnhnrl 46:030be9f5c793 11
tnhnrl 46:030be9f5c793 12 _file_transmission = true;
tnhnrl 46:030be9f5c793 13
tnhnrl 46:030be9f5c793 14 _confirmed_packet_number = 0; //must set this to zero
tnhnrl 46:030be9f5c793 15
tnhnrl 46:030be9f5c793 16 _transmit_counter = 0;
tnhnrl 46:030be9f5c793 17
tnhnrl 46:030be9f5c793 18 _file_transmission_state = -1;
tnhnrl 46:030be9f5c793 19
tnhnrl 46:030be9f5c793 20 _total_number_of_packets = 0;
tnhnrl 46:030be9f5c793 21
tnhnrl 46:030be9f5c793 22 _mbed_transmit_loop = false;
tnhnrl 46:030be9f5c793 23
tnhnrl 46:030be9f5c793 24 _SD_file_name_string = "";
tnhnrl 46:030be9f5c793 25
tnhnrl 46:030be9f5c793 26 _file_transmission_complete = false;
tnhnrl 46:030be9f5c793 27 }
tnhnrl 46:030be9f5c793 28
tnhnrl 46:030be9f5c793 29 //this function has to be called for the time to function correctly
tnhnrl 46:030be9f5c793 30 void MbedLogger::setLogTime() {
tnhnrl 47:fb3c7929d3f3 31 pc().printf("\n%s log time set.\n\r", _file_system_string.c_str());
tnhnrl 46:030be9f5c793 32 set_time(1518467832); // Set RTC time to Mon, 12 FEB 2018 15:37
tnhnrl 46:030be9f5c793 33 }
tnhnrl 46:030be9f5c793 34
tnhnrl 46:030be9f5c793 35 //in the future create the ability to set the start time
tnhnrl 46:030be9f5c793 36 int MbedLogger::getSystemTime() {
tnhnrl 46:030be9f5c793 37 time_t seconds = time(NULL); // Time as seconds since January 1, 1970
tnhnrl 46:030be9f5c793 38
tnhnrl 46:030be9f5c793 39 return seconds;
tnhnrl 46:030be9f5c793 40 }
tnhnrl 46:030be9f5c793 41
tnhnrl 47:fb3c7929d3f3 42 //idea to copy SD file on system down-time
tnhnrl 46:030be9f5c793 43 void MbedLogger::copySDFile() {
tnhnrl 46:030be9f5c793 44 }
tnhnrl 46:030be9f5c793 45
tnhnrl 46:030be9f5c793 46 //function is a work in progress
tnhnrl 46:030be9f5c793 47 void MbedLogger::openSDFile() {
tnhnrl 46:030be9f5c793 48 //create a file for writing to it (overwrites existing file) (do not close it until other operations are complete)
tnhnrl 46:030be9f5c793 49
tnhnrl 46:030be9f5c793 50 //write this as a string...
tnhnrl 46:030be9f5c793 51 char char_buffer[50];
tnhnrl 46:030be9f5c793 52 sprintf(char_buffer, "L%d.csv", getSystemTime());
tnhnrl 46:030be9f5c793 53
tnhnrl 46:030be9f5c793 54
tnhnrl 46:030be9f5c793 55 _SD_file_name_string = _file_system_string + char_buffer;
tnhnrl 46:030be9f5c793 56 _fp = fopen(_SD_file_name_string.c_str(), "w");
tnhnrl 46:030be9f5c793 57
tnhnrl 46:030be9f5c793 58 //write the header
tnhnrl 46:030be9f5c793 59 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\n");
tnhnrl 46:030be9f5c793 60
tnhnrl 46:030be9f5c793 61 // added pitchRate_degs (degrees per second)
tnhnrl 46:030be9f5c793 62 // added depthRate_fps (feet per second)
tnhnrl 46:030be9f5c793 63
tnhnrl 46:030be9f5c793 64 pc().printf("SD CARD FILE IS %s\n\r", _SD_file_name_string.c_str());
tnhnrl 46:030be9f5c793 65 }
tnhnrl 46:030be9f5c793 66 void MbedLogger::recordData(int current_state) {
tnhnrl 46:030be9f5c793 67 int data_log_int = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp
tnhnrl 46:030be9f5c793 68 //_data_log[0] = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp
tnhnrl 46:030be9f5c793 69 _data_log[1] = depthLoop().getCommand(); //depth command
tnhnrl 46:030be9f5c793 70 _data_log[2] = depthLoop().getPosition(); //depth reading
tnhnrl 46:030be9f5c793 71 _data_log[3] = pitchLoop().getCommand(); //pitch command
tnhnrl 46:030be9f5c793 72 _data_log[4] = pitchLoop().getPosition(); //pitch reading
tnhnrl 46:030be9f5c793 73 _data_log[5] = bce().getSetPosition_mm(); //BCE command
tnhnrl 46:030be9f5c793 74 _data_log[6] = bce().getPosition_mm(); //BCE reading
tnhnrl 46:030be9f5c793 75 _data_log[7] = batt().getSetPosition_mm(); //Batt command
tnhnrl 46:030be9f5c793 76 _data_log[8] = batt().getPosition_mm(); //Batt reading
tnhnrl 46:030be9f5c793 77 _data_log[9] = pitchLoop().getVelocity(); // pitchRate_degs (degrees per second)
tnhnrl 46:030be9f5c793 78 _data_log[10] = depthLoop().getVelocity(); // depthRate_fps (feet per second)
tnhnrl 46:030be9f5c793 79
tnhnrl 46:030be9f5c793 80 //check what the current state is and create that string
tnhnrl 46:030be9f5c793 81 string string_state;
tnhnrl 46:030be9f5c793 82 if (current_state == SIT_IDLE)
tnhnrl 46:030be9f5c793 83 string_state = "SIT_IDLE";
tnhnrl 46:030be9f5c793 84 else if (current_state == FIND_NEUTRAL)
tnhnrl 46:030be9f5c793 85 string_state = "FIND_NEUTRAL";
tnhnrl 46:030be9f5c793 86 else if (current_state == DIVE)
tnhnrl 46:030be9f5c793 87 string_state = "DIVE";
tnhnrl 46:030be9f5c793 88 else if (current_state == RISE)
tnhnrl 46:030be9f5c793 89 string_state = "RISE";
tnhnrl 46:030be9f5c793 90 else if (current_state == FLOAT_LEVEL)
tnhnrl 46:030be9f5c793 91 string_state = "FLOAT_LEVEL";
tnhnrl 46:030be9f5c793 92 else if (current_state == FLOAT_BROADCAST)
tnhnrl 46:030be9f5c793 93 string_state = "FLOAT_BROADCAST";
tnhnrl 46:030be9f5c793 94 else if (current_state == EMERGENCY_CLIMB)
tnhnrl 46:030be9f5c793 95 string_state = "EMERGENCY_CLIMB";
tnhnrl 46:030be9f5c793 96 else if (current_state == MULTI_DIVE)
tnhnrl 46:030be9f5c793 97 string_state = "MULTI_DIVE";
tnhnrl 46:030be9f5c793 98 else if (current_state == MULTI_RISE)
tnhnrl 46:030be9f5c793 99 string_state = "MULTI_RISE";
tnhnrl 46:030be9f5c793 100 else if (current_state == KEYBOARD)
tnhnrl 46:030be9f5c793 101 string_state = "KEYBOARD";
tnhnrl 46:030be9f5c793 102
tnhnrl 46:030be9f5c793 103 //record the string state, integer state, and then the data
tnhnrl 46:030be9f5c793 104 fprintf(_fp, "%s,%d,",string_state.c_str(),current_state);
tnhnrl 46:030be9f5c793 105 fprintf(_fp, "%d,",data_log_int);
tnhnrl 46:030be9f5c793 106 fprintf(_fp, "%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f,%0.1f\n",_data_log[1],
tnhnrl 46:030be9f5c793 107 _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]);
tnhnrl 46:030be9f5c793 108 }
tnhnrl 46:030be9f5c793 109
tnhnrl 46:030be9f5c793 110 void MbedLogger::printMbedDirectory() {
tnhnrl 46:030be9f5c793 111 DIR *dir;
tnhnrl 46:030be9f5c793 112 struct dirent *dp; //dirent.h is the format of directory entries
tnhnrl 46:030be9f5c793 113 int log_found =0,loop=0;
tnhnrl 46:030be9f5c793 114 long int temp=0;
tnhnrl 46:030be9f5c793 115
tnhnrl 46:030be9f5c793 116 //char * char_pointer;
tnhnrl 46:030be9f5c793 117 char * numstart;
tnhnrl 46:030be9f5c793 118 //char *numstop;
tnhnrl 46:030be9f5c793 119
tnhnrl 46:030be9f5c793 120 pc().printf("\n\rPrinting out the directory of device %s\n\r", _file_system_string.c_str());
tnhnrl 46:030be9f5c793 121
tnhnrl 46:030be9f5c793 122 if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) {
tnhnrl 46:030be9f5c793 123 pc().printf("MBED directory could not be opened\r\n");
tnhnrl 46:030be9f5c793 124 }
tnhnrl 46:030be9f5c793 125 else
tnhnrl 46:030be9f5c793 126 {
tnhnrl 46:030be9f5c793 127 while ( NULL != (dp = readdir( dir )) )
tnhnrl 46:030be9f5c793 128 {
tnhnrl 46:030be9f5c793 129 if(strncmp(dp->d_name,"LOG",3)==0)
tnhnrl 46:030be9f5c793 130 {
tnhnrl 46:030be9f5c793 131 log_found=1;
tnhnrl 46:030be9f5c793 132 /*numstart = dp->d_name; //Look for third character (do safety)
tnhnrl 46:030be9f5c793 133 numstop = strchr(dp->d_name,'.');
tnhnrl 46:030be9f5c793 134 if(numstart!=NULL&&numstop!=NULL)
tnhnrl 46:030be9f5c793 135 {
tnhnrl 46:030be9f5c793 136 temp=numstop-numstart;
tnhnrl 46:030be9f5c793 137 }
tnhnrl 46:030be9f5c793 138 else
tnhnrl 46:030be9f5c793 139 log_found=0; //Something is not right. Ignore
tnhnrl 46:030be9f5c793 140 */
tnhnrl 46:030be9f5c793 141 numstart=dp->d_name+3;
tnhnrl 46:030be9f5c793 142 temp=strtol(numstart,NULL,10); //add in check to see if this is null (start logs at one)
tnhnrl 46:030be9f5c793 143 }
tnhnrl 46:030be9f5c793 144 else
tnhnrl 46:030be9f5c793 145 log_found=0;
tnhnrl 46:030be9f5c793 146 pc().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp);
tnhnrl 46:030be9f5c793 147
tnhnrl 46:030be9f5c793 148 loop++;
tnhnrl 46:030be9f5c793 149 }
tnhnrl 46:030be9f5c793 150 }
tnhnrl 46:030be9f5c793 151 }
tnhnrl 46:030be9f5c793 152
tnhnrl 46:030be9f5c793 153 void MbedLogger::printCurrentLogFile() {
tnhnrl 46:030be9f5c793 154 //open the file for reading
tnhnrl 46:030be9f5c793 155 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 156
tnhnrl 46:030be9f5c793 157 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 46:030be9f5c793 158
tnhnrl 46:030be9f5c793 159 // http://people.cs.uchicago.edu/~dmfranklin/tutorials/fgets.txt
tnhnrl 46:030be9f5c793 160
tnhnrl 46:030be9f5c793 161 //while not end of file, read through line by line???
tnhnrl 46:030be9f5c793 162 char buffer[500];
tnhnrl 46:030be9f5c793 163
tnhnrl 46:030be9f5c793 164 //read the file line by line (and print each line)
tnhnrl 46:030be9f5c793 165 pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\n\r",_file_number);
tnhnrl 46:030be9f5c793 166 while (!feof(_fp)) {
tnhnrl 46:030be9f5c793 167 // read in the line and make sure it was successful
tnhnrl 46:030be9f5c793 168 if (fgets(buffer,500,_fp) != NULL) {
tnhnrl 46:030be9f5c793 169 pc().printf("%s\r",buffer);
tnhnrl 46:030be9f5c793 170 }
tnhnrl 46:030be9f5c793 171 }
tnhnrl 46:030be9f5c793 172
tnhnrl 46:030be9f5c793 173 //close the file
tnhnrl 46:030be9f5c793 174 closeLogFile();
tnhnrl 46:030be9f5c793 175 pc().printf("\n\rLog file closed.\n\r");
tnhnrl 46:030be9f5c793 176 }
tnhnrl 46:030be9f5c793 177
tnhnrl 46:030be9f5c793 178 void MbedLogger::openFileForTransmit() {
tnhnrl 46:030be9f5c793 179 pc().printf("\n\ropenFileForTransmit\n\r");
tnhnrl 46:030be9f5c793 180 //open the current file to read the contents
tnhnrl 46:030be9f5c793 181 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 182
tnhnrl 46:030be9f5c793 183 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 46:030be9f5c793 184
tnhnrl 46:030be9f5c793 185 _file_transmission = true;
tnhnrl 46:030be9f5c793 186 }
tnhnrl 46:030be9f5c793 187
tnhnrl 46:030be9f5c793 188 void MbedLogger::transmitCurrentLogFileLine(bool next_line) {
tnhnrl 46:030be9f5c793 189 vector <int> current_packet;
tnhnrl 46:030be9f5c793 190
tnhnrl 46:030be9f5c793 191 pc().printf("\n\rtransmitCurrentLogFile\n\r");
tnhnrl 46:030be9f5c793 192
tnhnrl 46:030be9f5c793 193 //while not end of file, read through line by line???
tnhnrl 46:030be9f5c793 194
tnhnrl 46:030be9f5c793 195 //transmit the file line by line (reads one line)
tnhnrl 46:030be9f5c793 196 //if you get the command to send a new line, go to the next line
tnhnrl 46:030be9f5c793 197 if (next_line) {
tnhnrl 46:030be9f5c793 198 if (fgets(_line_buffer,256,_fp) != NULL) {
tnhnrl 46:030be9f5c793 199 pc().printf("%s\r",_line_buffer);
tnhnrl 46:030be9f5c793 200 }
tnhnrl 46:030be9f5c793 201 }
tnhnrl 46:030be9f5c793 202
tnhnrl 46:030be9f5c793 203 // GET BUFFER LENGTH
tnhnrl 46:030be9f5c793 204 _current_line_length = strlen(_line_buffer);
tnhnrl 46:030be9f5c793 205
tnhnrl 46:030be9f5c793 206 //DEBUG PRINT TO SCREEN
tnhnrl 46:030be9f5c793 207 pc().printf("line_buffer size: %d %<%s>\r",_current_line_length,_line_buffer);
tnhnrl 46:030be9f5c793 208 }
tnhnrl 46:030be9f5c793 209
tnhnrl 46:030be9f5c793 210 void MbedLogger::createDataPacket() {
tnhnrl 46:030be9f5c793 211 //pc().printf("createDataPacket\n\r");
tnhnrl 46:030be9f5c793 212
tnhnrl 46:030be9f5c793 213 // packet is 7565 0001 FFFF DATA DATA DATA ... CRC1 CRC2
tnhnrl 46:030be9f5c793 214
tnhnrl 46:030be9f5c793 215 //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
tnhnrl 46:030be9f5c793 216 _data_packet.clear();
tnhnrl 46:030be9f5c793 217
tnhnrl 46:030be9f5c793 218 //DATA PACKET HEADER
tnhnrl 46:030be9f5c793 219 _data_packet.push_back(117); //0x75
tnhnrl 46:030be9f5c793 220 _data_packet.push_back(101); //0x65
tnhnrl 46:030be9f5c793 221
tnhnrl 46:030be9f5c793 222 _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form
tnhnrl 46:030be9f5c793 223 _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form
tnhnrl 46:030be9f5c793 224
tnhnrl 46:030be9f5c793 225 _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form
tnhnrl 46:030be9f5c793 226 _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form
tnhnrl 46:030be9f5c793 227
tnhnrl 46:030be9f5c793 228 _data_packet.push_back(_current_line_length);
tnhnrl 46:030be9f5c793 229
tnhnrl 46:030be9f5c793 230 //DATA FROM LINE READ (read string character by chracter)
tnhnrl 46:030be9f5c793 231 for (int i = 0; i < _current_line_length; i++) {
tnhnrl 46:030be9f5c793 232 _data_packet.push_back(_line_buffer[i]);
tnhnrl 46:030be9f5c793 233 }
tnhnrl 46:030be9f5c793 234
tnhnrl 46:030be9f5c793 235 //CRC CALCULATIONS BELOW
tnhnrl 46:030be9f5c793 236 //character version of calculation screws up on null character 0x00, scrapped and using vector of integers
tnhnrl 46:030be9f5c793 237
tnhnrl 46:030be9f5c793 238 int crc_one = calcCrcOne();
tnhnrl 46:030be9f5c793 239 int crc_two = calcCrcTwo();
tnhnrl 46:030be9f5c793 240
tnhnrl 46:030be9f5c793 241 //place the crc bytes into the data packet that is transmitted
tnhnrl 46:030be9f5c793 242 _data_packet.push_back(crc_one);
tnhnrl 46:030be9f5c793 243 _data_packet.push_back(crc_two);
tnhnrl 46:030be9f5c793 244 }
tnhnrl 46:030be9f5c793 245
tnhnrl 46:030be9f5c793 246 //should i save it as a string and transmit the string? next iteration maybe
tnhnrl 46:030be9f5c793 247
tnhnrl 46:030be9f5c793 248 void MbedLogger::transmitDataPacket() {
tnhnrl 46:030be9f5c793 249 //WRITE the data (in bytes) to the serial port
tnhnrl 46:030be9f5c793 250 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) {
tnhnrl 46:030be9f5c793 251 pc().putc(*_it); //send integers over serial port one byte at a time
tnhnrl 46:030be9f5c793 252 }
tnhnrl 46:030be9f5c793 253 }
tnhnrl 46:030be9f5c793 254
tnhnrl 46:030be9f5c793 255 // REACH ONE CHAR AT A TIME (EACH ITERATION OF STATE MACHINE...)
tnhnrl 46:030be9f5c793 256
tnhnrl 46:030be9f5c793 257 void MbedLogger::readTransmitPacketOneChar() {
tnhnrl 46:030be9f5c793 258 _file_transmission = true;
tnhnrl 46:030be9f5c793 259 _file_transmission_state = 0;
tnhnrl 46:030be9f5c793 260
tnhnrl 46:030be9f5c793 261 //first check if you're receiving data, then read four bytes
tnhnrl 46:030be9f5c793 262
tnhnrl 46:030be9f5c793 263 static int transmit_state = HEADER_117; //for state machine
tnhnrl 46:030be9f5c793 264
tnhnrl 46:030be9f5c793 265 int incoming_byte = -1;
tnhnrl 46:030be9f5c793 266
tnhnrl 46:030be9f5c793 267 int requested_packet_number = -1;
tnhnrl 46:030be9f5c793 268
tnhnrl 46:030be9f5c793 269 static int inside_while_loop = 1;
tnhnrl 46:030be9f5c793 270
tnhnrl 46:030be9f5c793 271 static char input_packet[4];
tnhnrl 46:030be9f5c793 272
tnhnrl 46:030be9f5c793 273 static int transmit_crc_one = 0;
tnhnrl 46:030be9f5c793 274 static int transmit_crc_two = 0;
tnhnrl 46:030be9f5c793 275
tnhnrl 46:030be9f5c793 276 static int random_packet_counter = 0;
tnhnrl 46:030be9f5c793 277
tnhnrl 46:030be9f5c793 278 if (pc().readable()) { //don't rely on pc readable being open all the time
tnhnrl 46:030be9f5c793 279 incoming_byte = pc().getc();
tnhnrl 46:030be9f5c793 280 led2() = !led2();
tnhnrl 46:030be9f5c793 281
tnhnrl 46:030be9f5c793 282 }
tnhnrl 46:030be9f5c793 283
tnhnrl 46:030be9f5c793 284 switch(transmit_state) {
tnhnrl 46:030be9f5c793 285
tnhnrl 46:030be9f5c793 286 case (HEADER_117):
tnhnrl 46:030be9f5c793 287 random_packet_counter = 0;
tnhnrl 46:030be9f5c793 288 //pc().printf("SC %x (%d)\n\r", incoming_byte, transmit_state);
tnhnrl 46:030be9f5c793 289 //continue processing
tnhnrl 46:030be9f5c793 290 if (incoming_byte == 30){ //1E
tnhnrl 46:030be9f5c793 291 //pc().printf("byte one \n\r");
tnhnrl 46:030be9f5c793 292 transmit_state = HEADER_101;
tnhnrl 46:030be9f5c793 293 led3() = !led3();
tnhnrl 46:030be9f5c793 294
tnhnrl 46:030be9f5c793 295 random_packet_counter = 0; //successful read
tnhnrl 46:030be9f5c793 296 }
tnhnrl 46:030be9f5c793 297 //end transmission
tnhnrl 46:030be9f5c793 298 else if (incoming_byte == 16) {
tnhnrl 46:030be9f5c793 299 transmit_state = END_TRANSMISSION;
tnhnrl 46:030be9f5c793 300 random_packet_counter = 0; //successful read
tnhnrl 46:030be9f5c793 301 }
tnhnrl 46:030be9f5c793 302 else {
tnhnrl 46:030be9f5c793 303 transmit_state = HEADER_117; //go back to checking the first packet
tnhnrl 46:030be9f5c793 304
tnhnrl 46:030be9f5c793 305 random_packet_counter++; //UNSUCCESSFUL READ
tnhnrl 46:030be9f5c793 306
tnhnrl 46:030be9f5c793 307 if (random_packet_counter > 10) {
tnhnrl 46:030be9f5c793 308 transmit_state = END_TRANSMISSION;
tnhnrl 46:030be9f5c793 309 }
tnhnrl 46:030be9f5c793 310 }
tnhnrl 46:030be9f5c793 311 break;
tnhnrl 46:030be9f5c793 312
tnhnrl 46:030be9f5c793 313 case (HEADER_101):
tnhnrl 46:030be9f5c793 314 if(incoming_byte == 31) { //1F
tnhnrl 46:030be9f5c793 315 //pc().printf("byte two \n\r");
tnhnrl 46:030be9f5c793 316 transmit_state = TRANSMIT_PACKET_1;
tnhnrl 46:030be9f5c793 317 led4() = !led4();
tnhnrl 46:030be9f5c793 318 }
tnhnrl 46:030be9f5c793 319 break;
tnhnrl 46:030be9f5c793 320
tnhnrl 46:030be9f5c793 321 case (TRANSMIT_PACKET_1):
tnhnrl 46:030be9f5c793 322 if (incoming_byte >= 0) {
tnhnrl 46:030be9f5c793 323 input_packet[0] = 30;
tnhnrl 46:030be9f5c793 324 input_packet[1] = 31;
tnhnrl 46:030be9f5c793 325 input_packet[2] = incoming_byte;
tnhnrl 46:030be9f5c793 326
tnhnrl 46:030be9f5c793 327 transmit_state = TRANSMIT_PACKET_2;
tnhnrl 46:030be9f5c793 328 _reply_byte3 = incoming_byte;
tnhnrl 46:030be9f5c793 329 //pc().printf("byte three \n\r");
tnhnrl 46:030be9f5c793 330 }
tnhnrl 46:030be9f5c793 331 break;
tnhnrl 46:030be9f5c793 332
tnhnrl 46:030be9f5c793 333 case (TRANSMIT_PACKET_2):
tnhnrl 46:030be9f5c793 334 if (incoming_byte >= 0) {
tnhnrl 46:030be9f5c793 335 input_packet[3] = incoming_byte;
tnhnrl 46:030be9f5c793 336 _reply_byte4 = incoming_byte;
tnhnrl 46:030be9f5c793 337 //pc().printf("byte four \n\r");
tnhnrl 46:030be9f5c793 338 }
tnhnrl 46:030be9f5c793 339 transmit_state = PACKET_CRC_ONE;
tnhnrl 46:030be9f5c793 340
tnhnrl 46:030be9f5c793 341 break;
tnhnrl 46:030be9f5c793 342
tnhnrl 46:030be9f5c793 343 case (PACKET_CRC_ONE):
tnhnrl 46:030be9f5c793 344 transmit_crc_one = calcCrcOneString(input_packet);
tnhnrl 46:030be9f5c793 345 transmit_state = PACKET_CRC_TWO;
tnhnrl 46:030be9f5c793 346 break;
tnhnrl 46:030be9f5c793 347
tnhnrl 46:030be9f5c793 348 case (PACKET_CRC_TWO):
tnhnrl 46:030be9f5c793 349 transmit_crc_two = calcCrcTwoString(input_packet);
tnhnrl 46:030be9f5c793 350 transmit_state = HEADER_117;
tnhnrl 46:030be9f5c793 351
tnhnrl 46:030be9f5c793 352
tnhnrl 46:030be9f5c793 353
tnhnrl 46:030be9f5c793 354 requested_packet_number = _reply_byte3 * 256 + _reply_byte4; //compute the numbers 0 through 65535 with the two bytes
tnhnrl 46:030be9f5c793 355
tnhnrl 46:030be9f5c793 356 //pc().printf("req packet number is %d\n\r", requested_packet_number);
tnhnrl 46:030be9f5c793 357
tnhnrl 46:030be9f5c793 358 if (requested_packet_number != _previous_reply_byte) { //CHANGE THE NAME TO SOMETHING NOT BYTE!
tnhnrl 46:030be9f5c793 359 //MUST SET THE PACKET NUMBER
tnhnrl 46:030be9f5c793 360 _packet_number = requested_packet_number;
tnhnrl 46:030be9f5c793 361
tnhnrl 46:030be9f5c793 362 // DO NOT READ IF THE CURRENT REQUEST IS PAST THE TOTAL # OF PACKETS (lines of the file)
tnhnrl 46:030be9f5c793 363 if (_packet_number <= _total_number_of_packets) {
tnhnrl 46:030be9f5c793 364 readPacketInSeries();
tnhnrl 46:030be9f5c793 365 createDataPacket();
tnhnrl 46:030be9f5c793 366 }
tnhnrl 46:030be9f5c793 367
tnhnrl 46:030be9f5c793 368 _previous_reply_byte = requested_packet_number; //RECORD THIS BYTE to prevent new packets being created
tnhnrl 46:030be9f5c793 369
tnhnrl 46:030be9f5c793 370 //pc().printf("DEBUG IF transmitDataPacket\n\r");
tnhnrl 46:030be9f5c793 371 transmitDataPacket(); //continuously transmit current packet until it tells you to do the next packet (response from Python program)
tnhnrl 46:030be9f5c793 372 }
tnhnrl 46:030be9f5c793 373
tnhnrl 46:030be9f5c793 374 else {
tnhnrl 46:030be9f5c793 375 //inside_while_loopc().printf("DEBUG: TRANSMIT SAME PACKET #%d\n\r", _packet_number);
tnhnrl 46:030be9f5c793 376 transmitDataPacket();
tnhnrl 46:030be9f5c793 377 //pc().printf("DEBUG ELSE transmitDataPacket\n\r");
tnhnrl 46:030be9f5c793 378 }
tnhnrl 46:030be9f5c793 379
tnhnrl 46:030be9f5c793 380 break;
tnhnrl 46:030be9f5c793 381
tnhnrl 46:030be9f5c793 382 case END_TRANSMISSION:
tnhnrl 46:030be9f5c793 383 _file_transmission_complete = true; //this triggers an exit on the state machine
tnhnrl 46:030be9f5c793 384 break;
tnhnrl 46:030be9f5c793 385 } //end of switch statement
tnhnrl 46:030be9f5c793 386
tnhnrl 46:030be9f5c793 387 //once you're outside of the while loop
tnhnrl 46:030be9f5c793 388 inside_while_loop = true; //for next iteration
tnhnrl 46:030be9f5c793 389
tnhnrl 46:030be9f5c793 390 /* pc().printf("DEBUG: (readTransmitPacket) Outside of while loop\n\r"); */
tnhnrl 46:030be9f5c793 391 }
tnhnrl 46:030be9f5c793 392
tnhnrl 46:030be9f5c793 393 int MbedLogger::readTransmitPacket() {
tnhnrl 46:030be9f5c793 394 _file_transmission = true;
tnhnrl 46:030be9f5c793 395 _file_transmission_state = 0;
tnhnrl 46:030be9f5c793 396
tnhnrl 46:030be9f5c793 397 //first check if you're receiving data, then read four bytes
tnhnrl 46:030be9f5c793 398
tnhnrl 46:030be9f5c793 399 int transmit_state = 0; //for state machine
tnhnrl 46:030be9f5c793 400
tnhnrl 46:030be9f5c793 401 int incoming_byte = -1;
tnhnrl 46:030be9f5c793 402
tnhnrl 46:030be9f5c793 403 int requested_packet_number = -1;
tnhnrl 46:030be9f5c793 404
tnhnrl 46:030be9f5c793 405 static int inside_while_loop = 1;
tnhnrl 46:030be9f5c793 406
tnhnrl 46:030be9f5c793 407 while (inside_while_loop) {
tnhnrl 46:030be9f5c793 408 if (pc().readable()) { //don't rely on pc readable being open all the time
tnhnrl 46:030be9f5c793 409
tnhnrl 46:030be9f5c793 410 incoming_byte = pc().getc();
tnhnrl 46:030be9f5c793 411
tnhnrl 46:030be9f5c793 412 switch(transmit_state) {
tnhnrl 46:030be9f5c793 413
tnhnrl 46:030be9f5c793 414 case (HEADER_117):
tnhnrl 46:030be9f5c793 415 //continue processing
tnhnrl 46:030be9f5c793 416 if (incoming_byte == 117){
tnhnrl 46:030be9f5c793 417 transmit_state = HEADER_101;
tnhnrl 46:030be9f5c793 418 }
tnhnrl 46:030be9f5c793 419 //end transmission
tnhnrl 46:030be9f5c793 420 else if (incoming_byte == 16) {
tnhnrl 46:030be9f5c793 421 transmit_state = END_TRANSMISSION;
tnhnrl 46:030be9f5c793 422 }
tnhnrl 46:030be9f5c793 423 else {
tnhnrl 46:030be9f5c793 424 transmit_state = HEADER_117;
tnhnrl 46:030be9f5c793 425 }
tnhnrl 46:030be9f5c793 426 break;
tnhnrl 46:030be9f5c793 427
tnhnrl 46:030be9f5c793 428 case (HEADER_101):
tnhnrl 46:030be9f5c793 429 if(incoming_byte == 101) {
tnhnrl 46:030be9f5c793 430 transmit_state = TRANSMIT_PACKET_1;
tnhnrl 46:030be9f5c793 431 }
tnhnrl 46:030be9f5c793 432 break;
tnhnrl 46:030be9f5c793 433
tnhnrl 46:030be9f5c793 434 case (TRANSMIT_PACKET_1):
tnhnrl 46:030be9f5c793 435 if (incoming_byte >= 0) {
tnhnrl 46:030be9f5c793 436 transmit_state = TRANSMIT_PACKET_2;
tnhnrl 46:030be9f5c793 437 _reply_byte3 = incoming_byte;
tnhnrl 46:030be9f5c793 438 }
tnhnrl 46:030be9f5c793 439 break;
tnhnrl 46:030be9f5c793 440
tnhnrl 46:030be9f5c793 441 case (TRANSMIT_PACKET_2):
tnhnrl 46:030be9f5c793 442 if (incoming_byte >= 0) {
tnhnrl 46:030be9f5c793 443 _reply_byte4 = incoming_byte;
tnhnrl 46:030be9f5c793 444 }
tnhnrl 46:030be9f5c793 445
tnhnrl 46:030be9f5c793 446 requested_packet_number = _reply_byte3 * 256 + _reply_byte4; //compute the numbers 0 through 65535 with the two bytes
tnhnrl 46:030be9f5c793 447
tnhnrl 46:030be9f5c793 448 if (requested_packet_number != _previous_reply_byte) { //CHANGE THE NAME TO SOMETHING NOT BYTE!
tnhnrl 46:030be9f5c793 449 //MUST SET THE PACKET NUMBER
tnhnrl 46:030be9f5c793 450 _packet_number = requested_packet_number;
tnhnrl 46:030be9f5c793 451
tnhnrl 46:030be9f5c793 452 readPacketInSeries();
tnhnrl 46:030be9f5c793 453 createDataPacket();
tnhnrl 46:030be9f5c793 454 _previous_reply_byte = requested_packet_number; //RECORD THIS BYTE to prevent new packets being created
tnhnrl 46:030be9f5c793 455
tnhnrl 46:030be9f5c793 456 transmitDataPacket(); //continuously transmit current packet until it tells you to do the next packet (response from Python program)
tnhnrl 46:030be9f5c793 457 }
tnhnrl 46:030be9f5c793 458
tnhnrl 46:030be9f5c793 459 else {
tnhnrl 46:030be9f5c793 460 //inside_while_loopc().printf("DEBUG: TRANSMIT SAME PACKET #%d\n\r", _packet_number);
tnhnrl 46:030be9f5c793 461 transmitDataPacket();
tnhnrl 46:030be9f5c793 462 }
tnhnrl 46:030be9f5c793 463
tnhnrl 46:030be9f5c793 464 //TRANSMIT_PACKET_2
tnhnrl 46:030be9f5c793 465 inside_while_loop = 0; //exit the while loop with this
tnhnrl 46:030be9f5c793 466 pc().printf("(TRANSMIT_PACKET_2)reached inside_while_loop = 0\n\r"); //DEBUG
tnhnrl 46:030be9f5c793 467 break;
tnhnrl 46:030be9f5c793 468 } //end of switch statement
tnhnrl 46:030be9f5c793 469 } //end of while loop
tnhnrl 46:030be9f5c793 470
tnhnrl 46:030be9f5c793 471 // else {
tnhnrl 46:030be9f5c793 472 // //pc().printf("pc not readable \n\r");
tnhnrl 46:030be9f5c793 473 // }
tnhnrl 46:030be9f5c793 474 //
tnhnrl 46:030be9f5c793 475 }
tnhnrl 46:030be9f5c793 476
tnhnrl 46:030be9f5c793 477 //once you're outside of the while loop
tnhnrl 46:030be9f5c793 478 inside_while_loop = true; //for next iteration
tnhnrl 46:030be9f5c793 479
tnhnrl 46:030be9f5c793 480 pc().printf("DEBUG: (readTransmitPacket) Outside of while loop\n\r");
tnhnrl 46:030be9f5c793 481 return false;
tnhnrl 46:030be9f5c793 482 }
tnhnrl 46:030be9f5c793 483
tnhnrl 46:030be9f5c793 484 void MbedLogger::reOpenLineReader() {
tnhnrl 46:030be9f5c793 485 //open a new one
tnhnrl 46:030be9f5c793 486 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 487
tnhnrl 46:030be9f5c793 488 _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read
tnhnrl 46:030be9f5c793 489
tnhnrl 46:030be9f5c793 490 //check if this actually worked...
tnhnrl 46:030be9f5c793 491 if (!_fp) {
tnhnrl 46:030be9f5c793 492 pc().printf("ERROR: Log file could not be opened\n\r");
tnhnrl 46:030be9f5c793 493 }
tnhnrl 46:030be9f5c793 494 else {
tnhnrl 46:030be9f5c793 495 pc().printf("Current Log file (LOG000.csv) was opened.\n\r");
tnhnrl 46:030be9f5c793 496 }
tnhnrl 46:030be9f5c793 497 }
tnhnrl 46:030be9f5c793 498
tnhnrl 46:030be9f5c793 499 bool MbedLogger::openLineReader() {
tnhnrl 46:030be9f5c793 500 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 501
tnhnrl 46:030be9f5c793 502 _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read
tnhnrl 46:030be9f5c793 503
tnhnrl 46:030be9f5c793 504 //check if this actually worked...
tnhnrl 46:030be9f5c793 505 if (!_fp) {
tnhnrl 46:030be9f5c793 506 //pc().printf("ERROR: Log file could not be opened\n\r");
tnhnrl 46:030be9f5c793 507 return false;
tnhnrl 46:030be9f5c793 508 }
tnhnrl 46:030be9f5c793 509 else {
tnhnrl 46:030be9f5c793 510 //pc().printf("Current Log file (LOG000.csv) was opened.\n\r");
tnhnrl 46:030be9f5c793 511 return true;
tnhnrl 46:030be9f5c793 512 }
tnhnrl 46:030be9f5c793 513 }
tnhnrl 46:030be9f5c793 514
tnhnrl 46:030be9f5c793 515 //VERIFIED THIS IS WORKING
tnhnrl 46:030be9f5c793 516 void MbedLogger::readPacketInSeries(){
tnhnrl 46:030be9f5c793 517 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 46:030be9f5c793 518
tnhnrl 46:030be9f5c793 519 //pc().printf("readPacketInSeries pointer: %p\n\r", _fp);
tnhnrl 46:030be9f5c793 520
tnhnrl 46:030be9f5c793 521 //read the current line in the file
tnhnrl 46:030be9f5c793 522 fgets(_line_buffer, 256, _fp); //reads the line of characters until you reach a newline character
tnhnrl 46:030be9f5c793 523
tnhnrl 46:030be9f5c793 524 //RECORD THE STRING LENGTH
tnhnrl 46:030be9f5c793 525 _current_line_length = strlen(_line_buffer);
tnhnrl 46:030be9f5c793 526 //pc().printf("_current_line_length: %D\n\r", _current_line_length);
tnhnrl 46:030be9f5c793 527 }
tnhnrl 46:030be9f5c793 528
tnhnrl 46:030be9f5c793 529 void MbedLogger::getNumberOfPacketsInCurrentLog() {
tnhnrl 46:030be9f5c793 530 _file_transmission_complete = false; //not sure this is working correctly
tnhnrl 46:030be9f5c793 531 _packet_number = 0;
tnhnrl 46:030be9f5c793 532
tnhnrl 46:030be9f5c793 533 int ch;
tnhnrl 46:030be9f5c793 534
tnhnrl 46:030be9f5c793 535 _total_number_of_packets = 0; //clear this each time you read the file
tnhnrl 46:030be9f5c793 536
tnhnrl 46:030be9f5c793 537 //if this is null, use the default, else use the new file
tnhnrl 46:030be9f5c793 538
tnhnrl 46:030be9f5c793 539 if (_fp == NULL) {
tnhnrl 46:030be9f5c793 540 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 541
tnhnrl 46:030be9f5c793 542 _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read
tnhnrl 46:030be9f5c793 543 }
tnhnrl 46:030be9f5c793 544 else { //else, use the file that is already open...
tnhnrl 46:030be9f5c793 545 fseek(_fp, 0, SEEK_SET); // SEEK_SET is the beginning of file
tnhnrl 46:030be9f5c793 546 }
tnhnrl 46:030be9f5c793 547
tnhnrl 46:030be9f5c793 548 while (EOF != (ch=getc(_fp))) {
tnhnrl 46:030be9f5c793 549 if ('\n' == ch)
tnhnrl 46:030be9f5c793 550 _total_number_of_packets++; // records the number of new lines to determine how many packets to send
tnhnrl 46:030be9f5c793 551 }
tnhnrl 46:030be9f5c793 552
tnhnrl 46:030be9f5c793 553 pc().printf("There are %d lines/packets in the file.\n\r", _total_number_of_packets);
tnhnrl 46:030be9f5c793 554
tnhnrl 46:030be9f5c793 555
tnhnrl 46:030be9f5c793 556 //move the FILE pointer back to the start
tnhnrl 46:030be9f5c793 557 fseek(_fp, 0, SEEK_SET); // SEEK_SET is the beginning of file
tnhnrl 46:030be9f5c793 558
tnhnrl 46:030be9f5c793 559 _file_transmission = true; //preparing to transmit files from MBED to Python
tnhnrl 46:030be9f5c793 560 }
tnhnrl 46:030be9f5c793 561
tnhnrl 46:030be9f5c793 562 void MbedLogger::endTransmissionCloseFile() {
tnhnrl 46:030be9f5c793 563 // if the file pointer is null, the file was not opened in the first place
tnhnrl 46:030be9f5c793 564 if (!_fp) {
tnhnrl 46:030be9f5c793 565 pc().printf("\n endTransmissionCloseFile: FILE WAS NOT OPENED!\n\r");
tnhnrl 46:030be9f5c793 566 }
tnhnrl 46:030be9f5c793 567 else {
tnhnrl 46:030be9f5c793 568 pc().printf("\n endTransmissionCloseFile: FILE FOUND AND CLOSED!\n\r");
tnhnrl 46:030be9f5c793 569 closeLogFile();
tnhnrl 46:030be9f5c793 570 }
tnhnrl 46:030be9f5c793 571
tnhnrl 46:030be9f5c793 572 _file_transmission = false;
tnhnrl 46:030be9f5c793 573 }
tnhnrl 46:030be9f5c793 574
tnhnrl 46:030be9f5c793 575 void MbedLogger::openWriteFile() {
tnhnrl 46:030be9f5c793 576 pc().printf("Opening file for reception.\n\r");
tnhnrl 46:030be9f5c793 577
tnhnrl 46:030be9f5c793 578 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 579
tnhnrl 46:030be9f5c793 580 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 46:030be9f5c793 581 }
tnhnrl 46:030be9f5c793 582
tnhnrl 46:030be9f5c793 583 // function checks for incoming data (receiver function) from a Python program that transmits a file
tnhnrl 46:030be9f5c793 584 bool MbedLogger::checkForIncomingData() {
tnhnrl 46:030be9f5c793 585 int receive_packet_number;
tnhnrl 46:030be9f5c793 586 int receive_total_number_packets;
tnhnrl 46:030be9f5c793 587 int receive_packet_size;
tnhnrl 46:030be9f5c793 588
tnhnrl 46:030be9f5c793 589 bool data_transmission_complete = false;
tnhnrl 46:030be9f5c793 590
tnhnrl 46:030be9f5c793 591 int incoming_byte;
tnhnrl 46:030be9f5c793 592
tnhnrl 46:030be9f5c793 593 char char_buffer[256] = {}; //create empty buffer
tnhnrl 46:030be9f5c793 594
tnhnrl 46:030be9f5c793 595 //starting state
tnhnrl 46:030be9f5c793 596 int process_state = HEADER_117;
tnhnrl 46:030be9f5c793 597
tnhnrl 46:030be9f5c793 598 //variables for processing data below
tnhnrl 46:030be9f5c793 599 int checksum_one = -1;
tnhnrl 46:030be9f5c793 600 int checksum_two = -1;
tnhnrl 46:030be9f5c793 601
tnhnrl 46:030be9f5c793 602 int calculated_crc_one = -1 ;
tnhnrl 46:030be9f5c793 603 int calculated_crc_two = -1;
tnhnrl 46:030be9f5c793 604
tnhnrl 46:030be9f5c793 605 int i = 5;
tnhnrl 46:030be9f5c793 606 int serial_timeout = 0;
tnhnrl 46:030be9f5c793 607
tnhnrl 46:030be9f5c793 608 while (pc().readable() && !data_transmission_complete) {
tnhnrl 46:030be9f5c793 609 incoming_byte = pc().getc(); //getc returns an unsigned char cast to an int
tnhnrl 46:030be9f5c793 610 //pc().printf("DEBUG: State 0\n\r");
tnhnrl 46:030be9f5c793 611
tnhnrl 46:030be9f5c793 612 switch(process_state) {
tnhnrl 46:030be9f5c793 613 case HEADER_117:
tnhnrl 46:030be9f5c793 614 //continue processing
tnhnrl 46:030be9f5c793 615 if (incoming_byte == 117){
tnhnrl 46:030be9f5c793 616 process_state = HEADER_101;
tnhnrl 46:030be9f5c793 617 //pc().printf("DEBUG: Case 117\n\r");
tnhnrl 46:030be9f5c793 618 }
tnhnrl 46:030be9f5c793 619 //end transmission
tnhnrl 46:030be9f5c793 620 else if (incoming_byte == 16) {
tnhnrl 46:030be9f5c793 621 process_state = END_TRANSMISSION;
tnhnrl 46:030be9f5c793 622 pc().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r");
tnhnrl 46:030be9f5c793 623 }
tnhnrl 46:030be9f5c793 624 else {
tnhnrl 46:030be9f5c793 625 process_state = HEADER_117; // ???
tnhnrl 46:030be9f5c793 626 //pc().printf("DEBUG: State Header 117\n\r");
tnhnrl 46:030be9f5c793 627 }
tnhnrl 46:030be9f5c793 628 break;
tnhnrl 46:030be9f5c793 629
tnhnrl 46:030be9f5c793 630 case HEADER_101:
tnhnrl 46:030be9f5c793 631 if(incoming_byte == 101) {
tnhnrl 46:030be9f5c793 632 process_state = PACKET_NUM;
tnhnrl 46:030be9f5c793 633 //pc().printf("DEBUG: Case 101\n\r");
tnhnrl 46:030be9f5c793 634 }
tnhnrl 46:030be9f5c793 635 break;
tnhnrl 46:030be9f5c793 636
tnhnrl 46:030be9f5c793 637 case PACKET_NUM:
tnhnrl 46:030be9f5c793 638 receive_packet_number = incoming_byte;
tnhnrl 46:030be9f5c793 639 process_state = TOTAL_NUM_PACKETS;
tnhnrl 46:030be9f5c793 640 //pc().printf("DEBUG: Case PACKET_NUM\n\r");
tnhnrl 46:030be9f5c793 641 break;
tnhnrl 46:030be9f5c793 642
tnhnrl 46:030be9f5c793 643 case TOTAL_NUM_PACKETS:
tnhnrl 46:030be9f5c793 644 receive_total_number_packets = incoming_byte;
tnhnrl 46:030be9f5c793 645 process_state = PACKET_SIZE;
tnhnrl 46:030be9f5c793 646 //pc().printf("DEBUG: Case TOTAL_NUM_PACKETS\n\r");
tnhnrl 46:030be9f5c793 647 break;
tnhnrl 46:030be9f5c793 648
tnhnrl 46:030be9f5c793 649 case PACKET_SIZE:
tnhnrl 46:030be9f5c793 650 receive_packet_size = incoming_byte;
tnhnrl 46:030be9f5c793 651 //pc().printf("DEBUG: Case PACKET_SIZE\n\r");
tnhnrl 46:030be9f5c793 652
tnhnrl 46:030be9f5c793 653 //write the header stuff to it
tnhnrl 46:030be9f5c793 654 char_buffer[0] = 117;
tnhnrl 46:030be9f5c793 655 char_buffer[1] = 101;
tnhnrl 46:030be9f5c793 656 char_buffer[2] = receive_packet_number;
tnhnrl 46:030be9f5c793 657 char_buffer[3] = receive_total_number_packets;
tnhnrl 46:030be9f5c793 658 char_buffer[4] = receive_packet_size;
tnhnrl 46:030be9f5c793 659
tnhnrl 46:030be9f5c793 660 //pc().printf("DEBUG: after header\n\r");
tnhnrl 46:030be9f5c793 661
tnhnrl 46:030be9f5c793 662 //char temp_char = NULL; //TEST IDEA 1/12/2018
tnhnrl 46:030be9f5c793 663
tnhnrl 46:030be9f5c793 664 //process packet data
tnhnrl 46:030be9f5c793 665 //pc().printf("DEBUG: receive_packet_size %d\n\r", receive_packet_size);
tnhnrl 46:030be9f5c793 666 //append for larger data sizes, FF FF
tnhnrl 46:030be9f5c793 667
tnhnrl 46:030be9f5c793 668 // IF YOU GET AN INTERRUPTED DATA STREAM YOU NEED TO BREAK OUT OF THE LOOP
tnhnrl 46:030be9f5c793 669 i = 5;
tnhnrl 46:030be9f5c793 670 serial_timeout = 0;
tnhnrl 46:030be9f5c793 671
tnhnrl 46:030be9f5c793 672 while (true) {
tnhnrl 46:030be9f5c793 673 if (pc().readable()) {
tnhnrl 46:030be9f5c793 674 char_buffer[i] = pc().getc();
tnhnrl 46:030be9f5c793 675 i++;
tnhnrl 46:030be9f5c793 676
tnhnrl 46:030be9f5c793 677 serial_timeout = 0; //reset the timeout
tnhnrl 46:030be9f5c793 678
tnhnrl 46:030be9f5c793 679 //pc().printf("pc readable?\n\r");
tnhnrl 46:030be9f5c793 680 }
tnhnrl 46:030be9f5c793 681 else {
tnhnrl 46:030be9f5c793 682 serial_timeout++;
tnhnrl 46:030be9f5c793 683 //pc().printf("serial_timeout %d\n\r", serial_timeout);
tnhnrl 46:030be9f5c793 684 }
tnhnrl 46:030be9f5c793 685
tnhnrl 46:030be9f5c793 686 // FULL PACKET RECEIVED
tnhnrl 46:030be9f5c793 687 if (i >= receive_packet_size+5) { //cannot do this properly with a for loop
tnhnrl 46:030be9f5c793 688 //pc().printf("i is %d\n\r", i);
tnhnrl 46:030be9f5c793 689
tnhnrl 46:030be9f5c793 690 checksum_one = pc().getc();
tnhnrl 46:030be9f5c793 691 checksum_two = pc().getc();
tnhnrl 46:030be9f5c793 692
tnhnrl 46:030be9f5c793 693 calculated_crc_one = calcCrcOneString(char_buffer);
tnhnrl 46:030be9f5c793 694 calculated_crc_two = calcCrcTwoString(char_buffer);
tnhnrl 46:030be9f5c793 695
tnhnrl 46:030be9f5c793 696 //if both checksums work...
tnhnrl 46:030be9f5c793 697 if ((calculated_crc_one == checksum_one) and (calculated_crc_two == checksum_two)) {
tnhnrl 46:030be9f5c793 698 //print a subset of the character array by starting at 5 (after header)
tnhnrl 46:030be9f5c793 699 fprintf(_fp, "%s", char_buffer+5);
tnhnrl 46:030be9f5c793 700
tnhnrl 46:030be9f5c793 701 _confirmed_packet_number = receive_packet_number;
tnhnrl 46:030be9f5c793 702 }
tnhnrl 46:030be9f5c793 703
tnhnrl 46:030be9f5c793 704 process_state = HEADER_117;
tnhnrl 46:030be9f5c793 705
tnhnrl 46:030be9f5c793 706 break;
tnhnrl 46:030be9f5c793 707 }
tnhnrl 46:030be9f5c793 708
tnhnrl 46:030be9f5c793 709 if (serial_timeout >= 10000) {
tnhnrl 46:030be9f5c793 710 //pc().printf("break serial_timeout %d\n\r", serial_timeout);
tnhnrl 46:030be9f5c793 711 break;
tnhnrl 46:030be9f5c793 712 }
tnhnrl 46:030be9f5c793 713 }
tnhnrl 46:030be9f5c793 714 break;
tnhnrl 46:030be9f5c793 715
tnhnrl 46:030be9f5c793 716 case END_TRANSMISSION:
tnhnrl 46:030be9f5c793 717 if (pc().getc() == 16) {
tnhnrl 46:030be9f5c793 718 pc().printf("TROY, you're here. 1. \n\r");
tnhnrl 46:030be9f5c793 719
tnhnrl 46:030be9f5c793 720 if (pc().getc() == 16) {
tnhnrl 46:030be9f5c793 721 pc().printf("TROY, you're here. 2. \n\r");
tnhnrl 46:030be9f5c793 722
tnhnrl 46:030be9f5c793 723 endReceiveData();
tnhnrl 46:030be9f5c793 724 }
tnhnrl 46:030be9f5c793 725 }
tnhnrl 46:030be9f5c793 726
tnhnrl 46:030be9f5c793 727 pc().printf("TROY, you're here. 5. \n\r");
tnhnrl 46:030be9f5c793 728
tnhnrl 46:030be9f5c793 729 //process_state = HEADER_117; //don't do this unless the check is wrong
tnhnrl 46:030be9f5c793 730 pc().printf("END_TRANSMISSION process_state is %d\n\r", process_state); //should be 5 (debug) 02/06/2018
tnhnrl 46:030be9f5c793 731 data_transmission_complete = true;
tnhnrl 46:030be9f5c793 732 break;
tnhnrl 46:030be9f5c793 733 }//END OF SWITCH
tnhnrl 46:030be9f5c793 734
tnhnrl 46:030be9f5c793 735 if (data_transmission_complete) {
tnhnrl 46:030be9f5c793 736 pc().printf("if statement data_transmission_complete ??? \n\r");
tnhnrl 46:030be9f5c793 737 break; //out of while loop
tnhnrl 46:030be9f5c793 738 }
tnhnrl 46:030be9f5c793 739 } // while loop
tnhnrl 46:030be9f5c793 740
tnhnrl 46:030be9f5c793 741 //pc().printf("checkForIncomingData done???\n\r"); //debug
tnhnrl 46:030be9f5c793 742
tnhnrl 46:030be9f5c793 743 if (data_transmission_complete)
tnhnrl 46:030be9f5c793 744 return false; //tell state machine class that this is done (not transmitting, false)
tnhnrl 46:030be9f5c793 745 else {
tnhnrl 46:030be9f5c793 746 return true;
tnhnrl 46:030be9f5c793 747 }
tnhnrl 46:030be9f5c793 748 }
tnhnrl 46:030be9f5c793 749
tnhnrl 46:030be9f5c793 750 int MbedLogger::sendReply() {
tnhnrl 46:030be9f5c793 751 //being explicit in what's being transmitted
tnhnrl 46:030be9f5c793 752
tnhnrl 46:030be9f5c793 753 //change this method to be more explicit later
tnhnrl 46:030be9f5c793 754
tnhnrl 46:030be9f5c793 755 //integer vector _data_packet is used here, cleared fist just in case
tnhnrl 46:030be9f5c793 756
tnhnrl 46:030be9f5c793 757 _data_packet.clear(); //same data packet for transmission
tnhnrl 46:030be9f5c793 758 _data_packet.push_back(117);
tnhnrl 46:030be9f5c793 759 _data_packet.push_back(101);
tnhnrl 46:030be9f5c793 760
tnhnrl 46:030be9f5c793 761 //_confirmed_packet_number comes from the packet number that is sent from the Python program
tnhnrl 46:030be9f5c793 762 _data_packet.push_back(_confirmed_packet_number / 256); //packet number only changed when confirmed
tnhnrl 46:030be9f5c793 763 _data_packet.push_back(_confirmed_packet_number % 256); //split into first and second byte
tnhnrl 46:030be9f5c793 764
tnhnrl 46:030be9f5c793 765 //compute checksums
tnhnrl 46:030be9f5c793 766
tnhnrl 46:030be9f5c793 767 int receiver_crc_one = calcCrcOne();
tnhnrl 46:030be9f5c793 768 int receiver_crc_two = calcCrcTwo();
tnhnrl 46:030be9f5c793 769
tnhnrl 46:030be9f5c793 770 _data_packet.push_back(receiver_crc_one);
tnhnrl 46:030be9f5c793 771 _data_packet.push_back(receiver_crc_two);
tnhnrl 46:030be9f5c793 772
tnhnrl 46:030be9f5c793 773 //transmit this packet
tnhnrl 46:030be9f5c793 774 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) {
tnhnrl 46:030be9f5c793 775 pc().putc(*_it); //send integers over serial port one byte at a time
tnhnrl 46:030be9f5c793 776 }
tnhnrl 46:030be9f5c793 777
tnhnrl 46:030be9f5c793 778 //change process methodology later...
tnhnrl 46:030be9f5c793 779
tnhnrl 46:030be9f5c793 780 return _confirmed_packet_number;
tnhnrl 46:030be9f5c793 781 }
tnhnrl 46:030be9f5c793 782
tnhnrl 46:030be9f5c793 783 //void MbedLogger::sendReplyPacketNumber(int packet_number) {
tnhnrl 46:030be9f5c793 784 // //being explicit in what's being transmitted
tnhnrl 46:030be9f5c793 785 // pc().putc(117);
tnhnrl 46:030be9f5c793 786 // pc().putc(101);
tnhnrl 46:030be9f5c793 787 // pc().putc(packet_number / 256); //this removes the trailing byte
tnhnrl 46:030be9f5c793 788 // pc().putc(packet_number % 256); //remainder gives you second byte (removes first byte)
tnhnrl 46:030be9f5c793 789 // pc().putc(packet_number);
tnhnrl 46:030be9f5c793 790 //
tnhnrl 46:030be9f5c793 791 // //not sure a checksum is needed here, I'll test behavior
tnhnrl 46:030be9f5c793 792 // //example is a reply of 75 65 03 03 03 (telling Python program you received packet # 3)
tnhnrl 46:030be9f5c793 793 //}
tnhnrl 46:030be9f5c793 794
tnhnrl 46:030be9f5c793 795 //DLE character * 4 ==> 10 10 10 10
tnhnrl 46:030be9f5c793 796 void MbedLogger::endReceiveData() {
tnhnrl 46:030be9f5c793 797 closeLogFile(); //close the file here
tnhnrl 46:030be9f5c793 798 pc().printf("endReceiveData closed the file and ended transmission\n\r");
tnhnrl 46:030be9f5c793 799 }
tnhnrl 46:030be9f5c793 800
tnhnrl 46:030be9f5c793 801 int MbedLogger::calcCrcOne() {
tnhnrl 46:030be9f5c793 802 //can't initialize the table in the constructor in c++
tnhnrl 46:030be9f5c793 803 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 46:030be9f5c793 804
tnhnrl 46:030be9f5c793 805 int crc = 0;
tnhnrl 46:030be9f5c793 806 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++)
tnhnrl 46:030be9f5c793 807 crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 46:030be9f5c793 808
tnhnrl 46:030be9f5c793 809 return crc / 256; //second-to-last byte
tnhnrl 46:030be9f5c793 810 }
tnhnrl 46:030be9f5c793 811
tnhnrl 46:030be9f5c793 812 int MbedLogger::calcCrcTwo() {
tnhnrl 46:030be9f5c793 813 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 46:030be9f5c793 814
tnhnrl 46:030be9f5c793 815 int crc = 0;
tnhnrl 46:030be9f5c793 816 for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++)
tnhnrl 46:030be9f5c793 817 crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 46:030be9f5c793 818
tnhnrl 46:030be9f5c793 819 //pc().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_array.length(), crc % 256);
tnhnrl 46:030be9f5c793 820
tnhnrl 46:030be9f5c793 821 return crc % 256; //last byte
tnhnrl 46:030be9f5c793 822 }
tnhnrl 46:030be9f5c793 823
tnhnrl 46:030be9f5c793 824 void MbedLogger::resetReplyPacket() {
tnhnrl 46:030be9f5c793 825 _confirmed_packet_number = 0;
tnhnrl 46:030be9f5c793 826 }
tnhnrl 46:030be9f5c793 827
tnhnrl 46:030be9f5c793 828 void MbedLogger::openNewMissionFile() {
tnhnrl 46:030be9f5c793 829 pc().printf("Opening Mission file (sequence.txt) for reception.\n\r");
tnhnrl 46:030be9f5c793 830 string file_name_string = _file_system_string + "sequence.txt";
tnhnrl 46:030be9f5c793 831
tnhnrl 46:030be9f5c793 832 pc().printf("openNewMissionFile: %s\n\r", file_name_string.c_str());
tnhnrl 46:030be9f5c793 833
tnhnrl 46:030be9f5c793 834 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 46:030be9f5c793 835 }
tnhnrl 46:030be9f5c793 836
tnhnrl 46:030be9f5c793 837 void MbedLogger::setDataCounter(int input_counter) {
tnhnrl 46:030be9f5c793 838 _transmit_counter = input_counter;
tnhnrl 46:030be9f5c793 839 }
tnhnrl 46:030be9f5c793 840
tnhnrl 46:030be9f5c793 841 void MbedLogger::closeIncompleteFile() {
tnhnrl 46:030be9f5c793 842 fprintf(_fp, "TRANSMISSION INTERRUPTED!"); //write this warning to the file
tnhnrl 46:030be9f5c793 843 closeLogFile(); //close file
tnhnrl 46:030be9f5c793 844 }
tnhnrl 46:030be9f5c793 845
tnhnrl 46:030be9f5c793 846 void MbedLogger::appendLogFile(int current_state, int option) {
tnhnrl 46:030be9f5c793 847 //option one means write to file
tnhnrl 46:030be9f5c793 848
tnhnrl 46:030be9f5c793 849 if (option == 1) {
tnhnrl 46:030be9f5c793 850 if (!_fp) { //if not present
tnhnrl 46:030be9f5c793 851 _fp = fopen(_full_file_path_string.c_str(), "a");
tnhnrl 46:030be9f5c793 852 }
tnhnrl 46:030be9f5c793 853
tnhnrl 46:030be9f5c793 854 //record data using the recordData function (takes in the state integer)
tnhnrl 46:030be9f5c793 855 recordData(current_state);
tnhnrl 46:030be9f5c793 856 }
tnhnrl 46:030be9f5c793 857
tnhnrl 46:030be9f5c793 858 else {
tnhnrl 46:030be9f5c793 859 closeLogFile();
tnhnrl 46:030be9f5c793 860 }
tnhnrl 46:030be9f5c793 861 }
tnhnrl 46:030be9f5c793 862
tnhnrl 47:fb3c7929d3f3 863 // initialize and close the file
tnhnrl 46:030be9f5c793 864 void MbedLogger::initializeLogFile() {
tnhnrl 46:030be9f5c793 865 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 866
tnhnrl 46:030be9f5c793 867 //try to open this file...
tnhnrl 46:030be9f5c793 868 _fp = fopen(file_name_string.c_str(), "r");
tnhnrl 46:030be9f5c793 869 //closeLogFile();
tnhnrl 46:030be9f5c793 870
tnhnrl 46:030be9f5c793 871 //if the file is empty, create this.
tnhnrl 46:030be9f5c793 872 if (!_fp) {
tnhnrl 46:030be9f5c793 873 _fp = fopen(file_name_string.c_str(), "w"); //write,print,close
tnhnrl 46:030be9f5c793 874 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 46:030be9f5c793 875 closeLogFile();
tnhnrl 46:030be9f5c793 876 }
tnhnrl 46:030be9f5c793 877 else
tnhnrl 46:030be9f5c793 878 closeLogFile(); //close the opened read file
tnhnrl 46:030be9f5c793 879 }
tnhnrl 46:030be9f5c793 880
tnhnrl 46:030be9f5c793 881 int MbedLogger::fileTransmitState() {
tnhnrl 46:030be9f5c793 882 return _file_transmission_state;
tnhnrl 46:030be9f5c793 883 }
tnhnrl 46:030be9f5c793 884
tnhnrl 46:030be9f5c793 885 int MbedLogger::calcCrcOneString (string input_string) {
tnhnrl 46:030be9f5c793 886 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 46:030be9f5c793 887
tnhnrl 46:030be9f5c793 888 int crc = 0;
tnhnrl 46:030be9f5c793 889 for (unsigned int i = 0; i < input_string.length(); i++) {
tnhnrl 46:030be9f5c793 890 //convert each character to an integer
tnhnrl 46:030be9f5c793 891 int character_to_integer = input_string[i]; //correct
tnhnrl 46:030be9f5c793 892
tnhnrl 46:030be9f5c793 893 crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 46:030be9f5c793 894 }
tnhnrl 46:030be9f5c793 895
tnhnrl 46:030be9f5c793 896 //pc().printf("DEBUG: calcCrcOne string length: %d crc: %d\n\r", input_string.length(), crc/256);
tnhnrl 46:030be9f5c793 897
tnhnrl 46:030be9f5c793 898 return crc / 256; //second-to-last byte
tnhnrl 46:030be9f5c793 899 }
tnhnrl 46:030be9f5c793 900
tnhnrl 46:030be9f5c793 901 int MbedLogger::calcCrcTwoString (string input_string) {
tnhnrl 46:030be9f5c793 902 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 46:030be9f5c793 903
tnhnrl 46:030be9f5c793 904 int crc = 0;
tnhnrl 46:030be9f5c793 905 for (unsigned int i = 0; i < input_string.length(); i++) {
tnhnrl 46:030be9f5c793 906 //convert each character to an integer
tnhnrl 46:030be9f5c793 907 int character_to_integer = input_string[i]; //correct
tnhnrl 46:030be9f5c793 908
tnhnrl 46:030be9f5c793 909 crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF;
tnhnrl 46:030be9f5c793 910 }
tnhnrl 46:030be9f5c793 911
tnhnrl 46:030be9f5c793 912 //pc().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_string.length(), crc % 256);
tnhnrl 46:030be9f5c793 913
tnhnrl 46:030be9f5c793 914 return crc % 256; //last byte
tnhnrl 46:030be9f5c793 915 }
tnhnrl 46:030be9f5c793 916
tnhnrl 46:030be9f5c793 917 int MbedLogger::testGetFileSize() {
tnhnrl 46:030be9f5c793 918 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 919
tnhnrl 46:030be9f5c793 920 _fp = fopen(file_name_string.c_str(), "rb"); //open the file for reading as a binary file
tnhnrl 46:030be9f5c793 921
tnhnrl 46:030be9f5c793 922 fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file)
tnhnrl 46:030be9f5c793 923 unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file.
tnhnrl 46:030be9f5c793 924 fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary
tnhnrl 46:030be9f5c793 925
tnhnrl 46:030be9f5c793 926 closeLogFile(); //can probably just close the file pointer and not worry about position
tnhnrl 46:030be9f5c793 927
tnhnrl 46:030be9f5c793 928 pc().printf("(%s) LOG000.csv file size is %d\n\r", _file_system_string.c_str(), file_size);
tnhnrl 46:030be9f5c793 929
tnhnrl 46:030be9f5c793 930 if (file_size == 0)
tnhnrl 46:030be9f5c793 931 createEmptyLog(); //this is not actually a completely empty log
tnhnrl 46:030be9f5c793 932
tnhnrl 46:030be9f5c793 933 return file_size;
tnhnrl 46:030be9f5c793 934
tnhnrl 46:030be9f5c793 935 // http://www.cplusplus.com/reference/cstdio/ftell/
tnhnrl 46:030be9f5c793 936 // https://os.mbed.com/questions/1260/How-to-read-the-LocalFileSystem-filesize/
tnhnrl 46:030be9f5c793 937 }
tnhnrl 46:030be9f5c793 938
tnhnrl 46:030be9f5c793 939 void MbedLogger::createEmptyLog() {
tnhnrl 46:030be9f5c793 940 string file_name_string = _file_system_string + "LOG000.csv";
tnhnrl 46:030be9f5c793 941
tnhnrl 46:030be9f5c793 942 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 46:030be9f5c793 943 fprintf(_fp, "EMPTY LOG\n"); //just write this string to the log (processing needs a file size that is not zero)
tnhnrl 46:030be9f5c793 944 closeLogFile();
tnhnrl 46:030be9f5c793 945 }
tnhnrl 46:030be9f5c793 946
tnhnrl 46:030be9f5c793 947 int MbedLogger::getFileSize(string filename) {
tnhnrl 46:030be9f5c793 948 // fixed the const char * errror:
tnhnrl 46:030be9f5c793 949 // https://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char
tnhnrl 46:030be9f5c793 950 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 46:030be9f5c793 951 //http://www.cplusplus.com/reference/string/string/c_str/
tnhnrl 46:030be9f5c793 952
tnhnrl 46:030be9f5c793 953 _fp = fopen(filename.c_str(), "rb"); //open the file for reading as a binary file
tnhnrl 46:030be9f5c793 954
tnhnrl 46:030be9f5c793 955 fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file)
tnhnrl 46:030be9f5c793 956 unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file.
tnhnrl 46:030be9f5c793 957 fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary
tnhnrl 46:030be9f5c793 958
tnhnrl 46:030be9f5c793 959 closeLogFile(); //can probably just close the file pointer and not worry about position
tnhnrl 46:030be9f5c793 960
tnhnrl 46:030be9f5c793 961 pc().printf("%s file size is %d\n\r", filename.c_str(), file_size);
tnhnrl 46:030be9f5c793 962
tnhnrl 46:030be9f5c793 963 return file_size;
tnhnrl 46:030be9f5c793 964 }
tnhnrl 46:030be9f5c793 965
tnhnrl 46:030be9f5c793 966 int MbedLogger::debugFileState() {
tnhnrl 46:030be9f5c793 967 pc().printf("What is _fp right now? %p\n\r", _fp); //pointer notation
tnhnrl 46:030be9f5c793 968
tnhnrl 46:030be9f5c793 969 if (_fp)
tnhnrl 46:030be9f5c793 970 return 1; //file pointer does exist
tnhnrl 46:030be9f5c793 971 else
tnhnrl 46:030be9f5c793 972 return 0; //file pointer does not exist
tnhnrl 46:030be9f5c793 973 }
tnhnrl 46:030be9f5c793 974
tnhnrl 46:030be9f5c793 975 void MbedLogger::activateTransmitPacket() {
tnhnrl 46:030be9f5c793 976 _mbed_transmit_loop = true;
tnhnrl 46:030be9f5c793 977 }
tnhnrl 46:030be9f5c793 978
tnhnrl 46:030be9f5c793 979 void MbedLogger::transmitDataWithTicker() {
tnhnrl 46:030be9f5c793 980 getNumberOfPacketsInCurrentLog();
tnhnrl 46:030be9f5c793 981
tnhnrl 46:030be9f5c793 982 //activate ticker
tnhnrl 46:030be9f5c793 983 _mbed_transmit_ticker.attach(callback(this, &MbedLogger::activateTransmitPacket), 0.1); // 10 hz ticker
tnhnrl 46:030be9f5c793 984
tnhnrl 46:030be9f5c793 985 int transmit_data_counter = 0; //used for timeout
tnhnrl 46:030be9f5c793 986
tnhnrl 46:030be9f5c793 987 //transmit data with ticker until complete or times out
tnhnrl 46:030be9f5c793 988 while(1) {
tnhnrl 46:030be9f5c793 989 //runs at 10 hz with ticker
tnhnrl 46:030be9f5c793 990 if (_mbed_transmit_loop) {
tnhnrl 46:030be9f5c793 991 led4() = !led4(); //flash LED 4
tnhnrl 46:030be9f5c793 992
tnhnrl 46:030be9f5c793 993 // transmit_data_counter = checkForTransmitPacket(); // run this until it finishes (function returns an integer)
tnhnrl 46:030be9f5c793 994 _mbed_transmit_loop = false; // wait until the loop rate timer fires again (TICKER)
tnhnrl 46:030be9f5c793 995
tnhnrl 46:030be9f5c793 996 if (transmit_data_counter >= 20) { //was 50
tnhnrl 46:030be9f5c793 997 _file_transmission = false; //file transmission complete
tnhnrl 46:030be9f5c793 998 }
tnhnrl 46:030be9f5c793 999
tnhnrl 46:030be9f5c793 1000 if (!_file_transmission) {
tnhnrl 46:030be9f5c793 1001
tnhnrl 46:030be9f5c793 1002 pc().printf("\n\r(_file_transmission == false) Stopped transmitting?\n\r");
tnhnrl 46:030be9f5c793 1003
tnhnrl 46:030be9f5c793 1004 setDataCounter(0); //reset the counter in the mbed logger
tnhnrl 46:030be9f5c793 1005
tnhnrl 46:030be9f5c793 1006 _mbed_transmit_ticker.detach();
tnhnrl 46:030be9f5c793 1007
tnhnrl 46:030be9f5c793 1008 break; //break out of the while loop when transmission over
tnhnrl 46:030be9f5c793 1009 }
tnhnrl 46:030be9f5c793 1010 }
tnhnrl 46:030be9f5c793 1011 } //end of while loop
tnhnrl 46:030be9f5c793 1012 }
tnhnrl 46:030be9f5c793 1013
tnhnrl 46:030be9f5c793 1014 void MbedLogger::specifyFileForTransmit(string input_string) {
tnhnrl 46:030be9f5c793 1015 pc().printf("specifyFileForTransmit\n\r");
tnhnrl 46:030be9f5c793 1016
tnhnrl 46:030be9f5c793 1017 string file_string = _file_system_string + input_string;
tnhnrl 46:030be9f5c793 1018
tnhnrl 46:030be9f5c793 1019 pc().printf("file_string is <%s>\n\r", file_string.c_str());
tnhnrl 46:030be9f5c793 1020
tnhnrl 46:030be9f5c793 1021 //open this file to read
tnhnrl 46:030be9f5c793 1022 _fp = fopen(file_string.c_str(), "r");
tnhnrl 46:030be9f5c793 1023
tnhnrl 46:030be9f5c793 1024 //transmit that file
tnhnrl 46:030be9f5c793 1025 transmitDataWithTicker();
tnhnrl 46:030be9f5c793 1026
tnhnrl 46:030be9f5c793 1027 _file_transmission = true;
tnhnrl 46:030be9f5c793 1028 }
tnhnrl 46:030be9f5c793 1029
tnhnrl 46:030be9f5c793 1030 void MbedLogger::transmitFileFromDirectory( int file_number ) {
tnhnrl 46:030be9f5c793 1031 file_number = file_number + 1; //to get the correct number
tnhnrl 46:030be9f5c793 1032
tnhnrl 46:030be9f5c793 1033 DIR *dir;
tnhnrl 46:030be9f5c793 1034 struct dirent *dp; //dirent.h is the format of directory entries
tnhnrl 46:030be9f5c793 1035 int log_found =0, loop=1; //start file numbers at 1
tnhnrl 46:030be9f5c793 1036 long int temp=0;
tnhnrl 46:030be9f5c793 1037
tnhnrl 46:030be9f5c793 1038 // char * char_pointer;
tnhnrl 46:030be9f5c793 1039 // char * numstart, *numstop;
tnhnrl 46:030be9f5c793 1040
tnhnrl 46:030be9f5c793 1041 if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) {
tnhnrl 46:030be9f5c793 1042 pc().printf("MBED directory could not be opened\r\n");
tnhnrl 46:030be9f5c793 1043 }
tnhnrl 46:030be9f5c793 1044 else
tnhnrl 46:030be9f5c793 1045 {
tnhnrl 46:030be9f5c793 1046 while ( NULL != (dp = readdir( dir )) )
tnhnrl 46:030be9f5c793 1047 {
tnhnrl 46:030be9f5c793 1048 pc().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp);
tnhnrl 46:030be9f5c793 1049
tnhnrl 46:030be9f5c793 1050 //process current file if it matches the file number
tnhnrl 46:030be9f5c793 1051 if (file_number == loop) {
tnhnrl 46:030be9f5c793 1052 char * current_file_name = dp->d_name; //pointer to this char array
tnhnrl 46:030be9f5c793 1053
tnhnrl 46:030be9f5c793 1054 specifyFileForTransmit(current_file_name);
tnhnrl 46:030be9f5c793 1055
tnhnrl 46:030be9f5c793 1056 break;
tnhnrl 46:030be9f5c793 1057 }
tnhnrl 46:030be9f5c793 1058
tnhnrl 46:030be9f5c793 1059 loop++;
tnhnrl 46:030be9f5c793 1060 }
tnhnrl 46:030be9f5c793 1061 }
tnhnrl 46:030be9f5c793 1062 }
tnhnrl 46:030be9f5c793 1063
tnhnrl 46:030be9f5c793 1064 void MbedLogger::accessMbedDirectory() {
tnhnrl 46:030be9f5c793 1065 printMbedDirectory();
tnhnrl 46:030be9f5c793 1066
tnhnrl 46:030be9f5c793 1067 pc().printf("Type in the number of the file you want to transmit.\n\r");
tnhnrl 46:030be9f5c793 1068
tnhnrl 46:030be9f5c793 1069 char message[42];
tnhnrl 46:030be9f5c793 1070
tnhnrl 46:030be9f5c793 1071 pc().scanf("%41s", message);
tnhnrl 46:030be9f5c793 1072
tnhnrl 46:030be9f5c793 1073 pc().printf("Input received!\n\r");
tnhnrl 46:030be9f5c793 1074
tnhnrl 46:030be9f5c793 1075 //check if char array is an integer
tnhnrl 46:030be9f5c793 1076 char* conversion_pointer;
tnhnrl 46:030be9f5c793 1077 long converted = strtol(message, &conversion_pointer, 10);
tnhnrl 46:030be9f5c793 1078
tnhnrl 46:030be9f5c793 1079 if (*conversion_pointer) {
tnhnrl 46:030be9f5c793 1080 //conversion failed because the input was not a number
tnhnrl 46:030be9f5c793 1081 pc().printf("NOT A VALID FILE NUMBER!\n\r");
tnhnrl 46:030be9f5c793 1082 }
tnhnrl 46:030be9f5c793 1083 else {
tnhnrl 46:030be9f5c793 1084 //conversion worked!
tnhnrl 46:030be9f5c793 1085 pc().printf("You chose file number: %d\n\r", converted);
tnhnrl 46:030be9f5c793 1086
tnhnrl 46:030be9f5c793 1087 // transmit the file
tnhnrl 46:030be9f5c793 1088 transmitFileFromDirectory(converted);
tnhnrl 46:030be9f5c793 1089 }
tnhnrl 46:030be9f5c793 1090 }
tnhnrl 46:030be9f5c793 1091
tnhnrl 46:030be9f5c793 1092 void MbedLogger::closeLogFile() {
tnhnrl 46:030be9f5c793 1093 if (_fp == NULL){
tnhnrl 46:030be9f5c793 1094 pc().printf("MbedLogger: (%s) LOG FILE WAS ALREADY CLOSED!\n\r", _file_system_string.c_str());
tnhnrl 46:030be9f5c793 1095 }
tnhnrl 46:030be9f5c793 1096
tnhnrl 46:030be9f5c793 1097 else {
tnhnrl 46:030be9f5c793 1098 pc().printf("MbedLogger: (%s) CLOSING LOG FILE!\n\r", _file_system_string.c_str());
tnhnrl 46:030be9f5c793 1099
tnhnrl 46:030be9f5c793 1100 //close file
tnhnrl 46:030be9f5c793 1101 fclose(_fp);
tnhnrl 46:030be9f5c793 1102
tnhnrl 46:030be9f5c793 1103 _fp = NULL; //set pointer to zero
tnhnrl 46:030be9f5c793 1104 }
tnhnrl 46:030be9f5c793 1105 }
tnhnrl 46:030be9f5c793 1106
tnhnrl 46:030be9f5c793 1107 void MbedLogger::closeSDLogFile() {
tnhnrl 46:030be9f5c793 1108 if (_fp == NULL){
tnhnrl 46:030be9f5c793 1109 pc().printf("MbedLogger: (%s) SD LOG FILE WAS ALREADY CLOSED!\n\r", _SD_file_name_string.c_str());
tnhnrl 46:030be9f5c793 1110 }
tnhnrl 46:030be9f5c793 1111
tnhnrl 46:030be9f5c793 1112 else {
tnhnrl 46:030be9f5c793 1113 pc().printf("MbedLogger: (%s) CLOSING SD LOG FILE!\n\r", _SD_file_name_string.c_str());
tnhnrl 46:030be9f5c793 1114
tnhnrl 46:030be9f5c793 1115 //close file
tnhnrl 46:030be9f5c793 1116 fclose(_fp);
tnhnrl 46:030be9f5c793 1117
tnhnrl 46:030be9f5c793 1118 _fp = NULL; //set pointer to zero
tnhnrl 46:030be9f5c793 1119 }
tnhnrl 46:030be9f5c793 1120 }
tnhnrl 46:030be9f5c793 1121
tnhnrl 46:030be9f5c793 1122 void MbedLogger::activateReceivePacket() {
tnhnrl 46:030be9f5c793 1123 _mbed_receive_loop = true;
tnhnrl 46:030be9f5c793 1124 }
tnhnrl 46:030be9f5c793 1125
tnhnrl 46:030be9f5c793 1126 void MbedLogger::receiveMissionDataWithTicker() {
tnhnrl 46:030be9f5c793 1127 openNewMissionFile(); //sequence.txt file opened
tnhnrl 46:030be9f5c793 1128
tnhnrl 46:030be9f5c793 1129 _mbed_receive_ticker.attach(callback(this, &MbedLogger::activateReceivePacket), 0.1);
tnhnrl 46:030be9f5c793 1130
tnhnrl 46:030be9f5c793 1131 pc().printf("\n\r02/09/2018 MbedLogger receiveMissionData Beginning to receive sequence data...\n\r");
tnhnrl 46:030be9f5c793 1132
tnhnrl 46:030be9f5c793 1133 resetReplyPacket(); //reset the reply packet
tnhnrl 46:030be9f5c793 1134
tnhnrl 46:030be9f5c793 1135 //idea for stopping this if data not being received
tnhnrl 46:030be9f5c793 1136 int current_packet_number = 0;
tnhnrl 46:030be9f5c793 1137 int last_packet_number = -1;
tnhnrl 46:030be9f5c793 1138 int break_transmission = 0;
tnhnrl 46:030be9f5c793 1139
tnhnrl 46:030be9f5c793 1140 while(1) {
tnhnrl 46:030be9f5c793 1141 //runs at 10 hz
tnhnrl 46:030be9f5c793 1142 if (_mbed_receive_loop) {
tnhnrl 46:030be9f5c793 1143 if (!checkForIncomingData()) { // run this until it finishes
tnhnrl 46:030be9f5c793 1144 //when you complete data reception, this will become false
tnhnrl 46:030be9f5c793 1145 pc().printf("\n\rMbedLogger: Data RECEPTION complete.\n\r");
tnhnrl 46:030be9f5c793 1146 _mbed_receive_ticker.detach();
tnhnrl 46:030be9f5c793 1147 break;
tnhnrl 46:030be9f5c793 1148 }
tnhnrl 46:030be9f5c793 1149 else {
tnhnrl 46:030be9f5c793 1150 //check if you keep getting the same thing
tnhnrl 46:030be9f5c793 1151 current_packet_number = sendReply();
tnhnrl 46:030be9f5c793 1152
tnhnrl 46:030be9f5c793 1153 //pc().printf("DEBUG: current packet number %d (last packet number %d) \n\r", current_packet_number, last_packet_number); //debug
tnhnrl 46:030be9f5c793 1154
tnhnrl 46:030be9f5c793 1155 //let this count up a few times before it exits
tnhnrl 46:030be9f5c793 1156 if (current_packet_number == last_packet_number) {
tnhnrl 46:030be9f5c793 1157 break_transmission++;
tnhnrl 46:030be9f5c793 1158
tnhnrl 46:030be9f5c793 1159 //break transmission after 50 failed attempts (was 100)
tnhnrl 46:030be9f5c793 1160 if (break_transmission >= 50) {
tnhnrl 46:030be9f5c793 1161 closeIncompleteFile(); //close the file
tnhnrl 46:030be9f5c793 1162 _mbed_receive_ticker.detach();
tnhnrl 46:030be9f5c793 1163 pc().printf("MbedLogger: TRANSMISSION INTERRUPTED!\n\r");
tnhnrl 46:030be9f5c793 1164 break;
tnhnrl 46:030be9f5c793 1165 }
tnhnrl 46:030be9f5c793 1166 }
tnhnrl 46:030be9f5c793 1167 else
tnhnrl 46:030be9f5c793 1168 last_packet_number = current_packet_number;
tnhnrl 46:030be9f5c793 1169 }
tnhnrl 46:030be9f5c793 1170 _mbed_receive_loop = false; // wait until the loop rate timer fires again
tnhnrl 46:030be9f5c793 1171 }
tnhnrl 46:030be9f5c793 1172 }
tnhnrl 46:030be9f5c793 1173 }
tnhnrl 46:030be9f5c793 1174
tnhnrl 46:030be9f5c793 1175 int MbedLogger::getFilePointerState() {
tnhnrl 46:030be9f5c793 1176 if (_fp)
tnhnrl 46:030be9f5c793 1177 return 1; //file is not null (has an address), open
tnhnrl 46:030be9f5c793 1178 else
tnhnrl 46:030be9f5c793 1179 return 0; //file is null, closed
tnhnrl 46:030be9f5c793 1180 }
tnhnrl 46:030be9f5c793 1181
tnhnrl 46:030be9f5c793 1182 void MbedLogger::testToggleFilePointer() {
tnhnrl 46:030be9f5c793 1183 pc().printf("testToggleFilePointer\n\r");
tnhnrl 46:030be9f5c793 1184
tnhnrl 46:030be9f5c793 1185 string file_name_string = _file_system_string + "example99.txt";
tnhnrl 46:030be9f5c793 1186
tnhnrl 46:030be9f5c793 1187 if (_fp)
tnhnrl 46:030be9f5c793 1188 _fp = NULL;
tnhnrl 46:030be9f5c793 1189 else
tnhnrl 46:030be9f5c793 1190 _fp = fopen(file_name_string.c_str(), "w");
tnhnrl 46:030be9f5c793 1191 }
tnhnrl 46:030be9f5c793 1192
tnhnrl 46:030be9f5c793 1193 int MbedLogger::getNumberOfPackets() {
tnhnrl 46:030be9f5c793 1194 return _total_number_of_packets;
tnhnrl 46:030be9f5c793 1195 }
tnhnrl 46:030be9f5c793 1196
tnhnrl 46:030be9f5c793 1197 bool MbedLogger::isTransmissionComplete() {
tnhnrl 46:030be9f5c793 1198 return _file_transmission_complete;
tnhnrl 46:030be9f5c793 1199 }
tnhnrl 46:030be9f5c793 1200
tnhnrl 46:030be9f5c793 1201 int MbedLogger::currentPacketNumber() {
tnhnrl 46:030be9f5c793 1202 return _packet_number;
tnhnrl 46:030be9f5c793 1203 }