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
MbedLogger/MbedLogger.cpp@63:6cb0405fc6e6, 2018-06-18 (annotated)
- Committer:
- tnhnrl
- Date:
- Mon Jun 18 21:02:55 2018 +0000
- Revision:
- 63:6cb0405fc6e6
- Parent:
- 62:d502889e74f1
- Child:
- 67:c86a4b464682
Version with code updates on multi-dive (still testing) and PID inputs and fixed data transmissions so it's working very quickly now on USB.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tnhnrl | 62:d502889e74f1 | 1 | #include "MbedLogger.hpp" |
tnhnrl | 62:d502889e74f1 | 2 | #include "StaticDefs.hpp" |
tnhnrl | 62:d502889e74f1 | 3 | |
tnhnrl | 62:d502889e74f1 | 4 | //Timer t; //used to test time to create packet //timing debug |
tnhnrl | 62:d502889e74f1 | 5 | |
tnhnrl | 62:d502889e74f1 | 6 | MbedLogger::MbedLogger(string file_system_input_string) { |
tnhnrl | 62:d502889e74f1 | 7 | _file_system_string = file_system_input_string; |
tnhnrl | 62:d502889e74f1 | 8 | _full_file_path_string = _file_system_string + "LOG000.csv"; //use multiple logs in the future? (after file size is too large) |
tnhnrl | 62:d502889e74f1 | 9 | _file_number = 0; |
tnhnrl | 62:d502889e74f1 | 10 | _file_transmission = true; |
tnhnrl | 62:d502889e74f1 | 11 | _confirmed_packet_number = 0; //must set this to zero |
tnhnrl | 62:d502889e74f1 | 12 | _transmit_counter = 0; |
tnhnrl | 62:d502889e74f1 | 13 | _file_transmission_state = -1; |
tnhnrl | 62:d502889e74f1 | 14 | _total_number_of_packets = 0; |
tnhnrl | 62:d502889e74f1 | 15 | _mbed_transmit_loop = false; |
tnhnrl | 62:d502889e74f1 | 16 | _received_filename = ""; |
tnhnrl | 63:6cb0405fc6e6 | 17 | |
tnhnrl | 63:6cb0405fc6e6 | 18 | _log_file_line_counter = 0; //used to set timer in finite state machine based on size of log |
tnhnrl | 62:d502889e74f1 | 19 | } |
tnhnrl | 62:d502889e74f1 | 20 | |
tnhnrl | 62:d502889e74f1 | 21 | //this function has to be called for the time to function correctly |
tnhnrl | 62:d502889e74f1 | 22 | void MbedLogger::setLogTime() { |
tnhnrl | 62:d502889e74f1 | 23 | pc().printf("\n%s log time set.\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 24 | set_time(1518467832); // Set RTC time to Mon, 12 FEB 2018 15:37 |
tnhnrl | 62:d502889e74f1 | 25 | } |
tnhnrl | 62:d502889e74f1 | 26 | |
tnhnrl | 62:d502889e74f1 | 27 | //in the future create the ability to set the start time |
tnhnrl | 62:d502889e74f1 | 28 | int MbedLogger::getSystemTime() { |
tnhnrl | 62:d502889e74f1 | 29 | time_t seconds = time(NULL); // Time as seconds since January 1, 1970 |
tnhnrl | 62:d502889e74f1 | 30 | |
tnhnrl | 62:d502889e74f1 | 31 | return seconds; |
tnhnrl | 62:d502889e74f1 | 32 | } |
tnhnrl | 62:d502889e74f1 | 33 | |
tnhnrl | 62:d502889e74f1 | 34 | void MbedLogger::recordData(int current_state) { |
tnhnrl | 62:d502889e74f1 | 35 | int data_log_int = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp |
tnhnrl | 62:d502889e74f1 | 36 | //_data_log[0] = mbedLogger().getSystemTime(); //read the system timer to get unix timestamp |
tnhnrl | 62:d502889e74f1 | 37 | _data_log[1] = depthLoop().getCommand(); //depth command |
tnhnrl | 62:d502889e74f1 | 38 | _data_log[2] = depthLoop().getPosition(); //depth reading |
tnhnrl | 62:d502889e74f1 | 39 | _data_log[3] = pitchLoop().getCommand(); //pitch command |
tnhnrl | 62:d502889e74f1 | 40 | _data_log[4] = pitchLoop().getPosition(); //pitch reading |
tnhnrl | 62:d502889e74f1 | 41 | _data_log[5] = bce().getSetPosition_mm(); //BCE command |
tnhnrl | 62:d502889e74f1 | 42 | _data_log[6] = bce().getPosition_mm(); //BCE reading |
tnhnrl | 62:d502889e74f1 | 43 | _data_log[7] = batt().getSetPosition_mm(); //Batt command |
tnhnrl | 62:d502889e74f1 | 44 | _data_log[8] = batt().getPosition_mm(); //Batt reading |
tnhnrl | 62:d502889e74f1 | 45 | _data_log[9] = pitchLoop().getVelocity(); // pitchRate_degs (degrees per second) |
tnhnrl | 62:d502889e74f1 | 46 | _data_log[10] = depthLoop().getVelocity(); // depthRate_fps (feet per second) |
tnhnrl | 62:d502889e74f1 | 47 | |
tnhnrl | 62:d502889e74f1 | 48 | //check what the current state is and create that string |
tnhnrl | 62:d502889e74f1 | 49 | string string_state; |
tnhnrl | 62:d502889e74f1 | 50 | if (current_state == SIT_IDLE) |
tnhnrl | 62:d502889e74f1 | 51 | string_state = "SIT_IDLE"; |
tnhnrl | 62:d502889e74f1 | 52 | else if (current_state == FIND_NEUTRAL) |
tnhnrl | 62:d502889e74f1 | 53 | string_state = "FIND_NEUTRAL"; |
tnhnrl | 62:d502889e74f1 | 54 | else if (current_state == DIVE) |
tnhnrl | 62:d502889e74f1 | 55 | string_state = "DIVE"; |
tnhnrl | 62:d502889e74f1 | 56 | else if (current_state == RISE) |
tnhnrl | 62:d502889e74f1 | 57 | string_state = "RISE"; |
tnhnrl | 62:d502889e74f1 | 58 | else if (current_state == FLOAT_LEVEL) |
tnhnrl | 62:d502889e74f1 | 59 | string_state = "FLOAT_LEVEL"; |
tnhnrl | 62:d502889e74f1 | 60 | else if (current_state == FLOAT_BROADCAST) |
tnhnrl | 62:d502889e74f1 | 61 | string_state = "FLOAT_BROADCAST"; |
tnhnrl | 62:d502889e74f1 | 62 | else if (current_state == EMERGENCY_CLIMB) |
tnhnrl | 62:d502889e74f1 | 63 | string_state = "EMERGENCY_CLIMB"; |
tnhnrl | 62:d502889e74f1 | 64 | else if (current_state == MULTI_DIVE) |
tnhnrl | 62:d502889e74f1 | 65 | string_state = "MULTI_DIVE"; |
tnhnrl | 62:d502889e74f1 | 66 | else if (current_state == MULTI_RISE) |
tnhnrl | 62:d502889e74f1 | 67 | string_state = "MULTI_RISE"; |
tnhnrl | 62:d502889e74f1 | 68 | else if (current_state == KEYBOARD) |
tnhnrl | 62:d502889e74f1 | 69 | string_state = "KEYBOARD"; |
tnhnrl | 62:d502889e74f1 | 70 | else if (current_state == CHECK_TUNING) |
tnhnrl | 63:6cb0405fc6e6 | 71 | string_state = "CHECK_TUNING"; |
tnhnrl | 63:6cb0405fc6e6 | 72 | else if (current_state == POSITION_DIVE) |
tnhnrl | 63:6cb0405fc6e6 | 73 | string_state = "POSITION_DIVE"; |
tnhnrl | 63:6cb0405fc6e6 | 74 | else if (current_state == POSITION_RISE) |
tnhnrl | 63:6cb0405fc6e6 | 75 | string_state = "POSITION_RISE"; |
tnhnrl | 63:6cb0405fc6e6 | 76 | |
tnhnrl | 63:6cb0405fc6e6 | 77 | |
tnhnrl | 63:6cb0405fc6e6 | 78 | |
tnhnrl | 63:6cb0405fc6e6 | 79 | // else if (current_state == PITCH_TUNER_DEPTH) |
tnhnrl | 63:6cb0405fc6e6 | 80 | // string_state = "PITCH_TUNER_DEPTH"; |
tnhnrl | 63:6cb0405fc6e6 | 81 | // else if (current_state == PITCH_TUNER_RUN) |
tnhnrl | 63:6cb0405fc6e6 | 82 | // string_state = "PITCH_TUNER_RUN"; |
tnhnrl | 62:d502889e74f1 | 83 | |
tnhnrl | 62:d502889e74f1 | 84 | //record the string state, integer state, and then the data |
tnhnrl | 62:d502889e74f1 | 85 | fprintf(_fp, "%s,%d,",string_state.c_str(),current_state); |
tnhnrl | 62:d502889e74f1 | 86 | fprintf(_fp, "%d,",data_log_int); |
tnhnrl | 62:d502889e74f1 | 87 | 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 | 62:d502889e74f1 | 88 | _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 | 62:d502889e74f1 | 89 | } |
tnhnrl | 62:d502889e74f1 | 90 | |
tnhnrl | 62:d502889e74f1 | 91 | void MbedLogger::printMbedDirectory() { |
tnhnrl | 62:d502889e74f1 | 92 | DIR *dir; |
tnhnrl | 62:d502889e74f1 | 93 | struct dirent *dp; //dirent.h is the format of directory entries |
tnhnrl | 62:d502889e74f1 | 94 | int log_found =0,loop=0; |
tnhnrl | 62:d502889e74f1 | 95 | long int temp=0; |
tnhnrl | 62:d502889e74f1 | 96 | |
tnhnrl | 62:d502889e74f1 | 97 | //char * char_pointer; |
tnhnrl | 62:d502889e74f1 | 98 | char * numstart; |
tnhnrl | 62:d502889e74f1 | 99 | //char *numstop; |
tnhnrl | 62:d502889e74f1 | 100 | |
tnhnrl | 62:d502889e74f1 | 101 | pc().printf("\n\rPrinting out the directory of device %s\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 102 | |
tnhnrl | 62:d502889e74f1 | 103 | if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) { |
tnhnrl | 62:d502889e74f1 | 104 | pc().printf("MBED directory could not be opened\r\n"); |
tnhnrl | 62:d502889e74f1 | 105 | } |
tnhnrl | 62:d502889e74f1 | 106 | else |
tnhnrl | 62:d502889e74f1 | 107 | { |
tnhnrl | 62:d502889e74f1 | 108 | while ( NULL != (dp = readdir( dir )) ) |
tnhnrl | 62:d502889e74f1 | 109 | { |
tnhnrl | 62:d502889e74f1 | 110 | if(strncmp(dp->d_name,"LOG",3)==0) |
tnhnrl | 62:d502889e74f1 | 111 | { |
tnhnrl | 62:d502889e74f1 | 112 | log_found=1; |
tnhnrl | 62:d502889e74f1 | 113 | /*numstart = dp->d_name; //Look for third character (do safety) |
tnhnrl | 62:d502889e74f1 | 114 | numstop = strchr(dp->d_name,'.'); |
tnhnrl | 62:d502889e74f1 | 115 | if(numstart!=NULL&&numstop!=NULL) |
tnhnrl | 62:d502889e74f1 | 116 | { |
tnhnrl | 62:d502889e74f1 | 117 | temp=numstop-numstart; |
tnhnrl | 62:d502889e74f1 | 118 | } |
tnhnrl | 62:d502889e74f1 | 119 | else |
tnhnrl | 62:d502889e74f1 | 120 | log_found=0; //Something is not right. Ignore |
tnhnrl | 62:d502889e74f1 | 121 | */ |
tnhnrl | 62:d502889e74f1 | 122 | numstart=dp->d_name+3; |
tnhnrl | 62:d502889e74f1 | 123 | temp=strtol(numstart,NULL,10); //add in check to see if this is null (start logs at one) |
tnhnrl | 62:d502889e74f1 | 124 | } |
tnhnrl | 62:d502889e74f1 | 125 | else |
tnhnrl | 62:d502889e74f1 | 126 | log_found=0; |
tnhnrl | 62:d502889e74f1 | 127 | pc().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp); |
tnhnrl | 62:d502889e74f1 | 128 | |
tnhnrl | 62:d502889e74f1 | 129 | loop++; |
tnhnrl | 62:d502889e74f1 | 130 | } |
tnhnrl | 62:d502889e74f1 | 131 | } |
tnhnrl | 62:d502889e74f1 | 132 | } |
tnhnrl | 62:d502889e74f1 | 133 | |
tnhnrl | 62:d502889e74f1 | 134 | void MbedLogger::printCurrentLogFile() { |
tnhnrl | 62:d502889e74f1 | 135 | //open the file for reading |
tnhnrl | 62:d502889e74f1 | 136 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 137 | |
tnhnrl | 63:6cb0405fc6e6 | 138 | _log_file_line_counter = 0; |
tnhnrl | 62:d502889e74f1 | 139 | |
tnhnrl | 62:d502889e74f1 | 140 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 62:d502889e74f1 | 141 | |
tnhnrl | 62:d502889e74f1 | 142 | char buffer[500]; |
tnhnrl | 62:d502889e74f1 | 143 | |
tnhnrl | 62:d502889e74f1 | 144 | //read the file line-by-line and print that to the screen |
tnhnrl | 62:d502889e74f1 | 145 | pc().printf("\n\rCURRENT MBED LOG FILE /local/Log%03d.csv:\n\n\r",_file_number); |
tnhnrl | 62:d502889e74f1 | 146 | while (!feof(_fp)) { |
tnhnrl | 62:d502889e74f1 | 147 | // read in the line and make sure it was successful |
tnhnrl | 62:d502889e74f1 | 148 | if (fgets(buffer,500,_fp) != NULL) { //stops at new line |
tnhnrl | 62:d502889e74f1 | 149 | pc().printf("%s\r",buffer); |
tnhnrl | 63:6cb0405fc6e6 | 150 | _log_file_line_counter++; |
tnhnrl | 62:d502889e74f1 | 151 | } |
tnhnrl | 62:d502889e74f1 | 152 | } |
tnhnrl | 62:d502889e74f1 | 153 | |
tnhnrl | 62:d502889e74f1 | 154 | //fgets stops when either (n-1) characters are read, the newline character is read, |
tnhnrl | 62:d502889e74f1 | 155 | // or the end-of-file is reached |
tnhnrl | 62:d502889e74f1 | 156 | |
tnhnrl | 62:d502889e74f1 | 157 | //close the file |
tnhnrl | 62:d502889e74f1 | 158 | closeLogFile(); |
tnhnrl | 63:6cb0405fc6e6 | 159 | pc().printf("\n\rLog file closed. Lines in log file: %d.\n\r", _log_file_line_counter); |
tnhnrl | 62:d502889e74f1 | 160 | } |
tnhnrl | 62:d502889e74f1 | 161 | |
tnhnrl | 62:d502889e74f1 | 162 | void MbedLogger::openFileForTransmit() { |
tnhnrl | 62:d502889e74f1 | 163 | pc().printf("\n\ropenFileForTransmit\n\r"); |
tnhnrl | 62:d502889e74f1 | 164 | //open the current file to read the contents |
tnhnrl | 62:d502889e74f1 | 165 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 166 | |
tnhnrl | 62:d502889e74f1 | 167 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 62:d502889e74f1 | 168 | |
tnhnrl | 62:d502889e74f1 | 169 | _file_transmission = true; |
tnhnrl | 62:d502889e74f1 | 170 | } |
tnhnrl | 62:d502889e74f1 | 171 | |
tnhnrl | 62:d502889e74f1 | 172 | void MbedLogger::transmitCurrentLogFileLine(bool next_line) { |
tnhnrl | 62:d502889e74f1 | 173 | vector <int> current_packet; |
tnhnrl | 62:d502889e74f1 | 174 | |
tnhnrl | 62:d502889e74f1 | 175 | pc().printf("\n\rtransmitCurrentLogFile\n\r"); |
tnhnrl | 62:d502889e74f1 | 176 | |
tnhnrl | 62:d502889e74f1 | 177 | //while not end of file, read through line by line??? |
tnhnrl | 62:d502889e74f1 | 178 | |
tnhnrl | 62:d502889e74f1 | 179 | //transmit the file line by line (reads one line) |
tnhnrl | 62:d502889e74f1 | 180 | //if you get the command to send a new line, go to the next line |
tnhnrl | 62:d502889e74f1 | 181 | if (next_line) { |
tnhnrl | 62:d502889e74f1 | 182 | if (fgets(_line_buffer,256,_fp) != NULL) { |
tnhnrl | 62:d502889e74f1 | 183 | pc().printf("%s\r",_line_buffer); |
tnhnrl | 62:d502889e74f1 | 184 | } |
tnhnrl | 62:d502889e74f1 | 185 | } |
tnhnrl | 62:d502889e74f1 | 186 | |
tnhnrl | 62:d502889e74f1 | 187 | // GET BUFFER LENGTH |
tnhnrl | 62:d502889e74f1 | 188 | _current_line_length = strlen(_line_buffer); |
tnhnrl | 62:d502889e74f1 | 189 | |
tnhnrl | 62:d502889e74f1 | 190 | //DEBUG PRINT TO SCREEN |
tnhnrl | 62:d502889e74f1 | 191 | pc().printf("line_buffer size: %d %<%s>\r",_current_line_length,_line_buffer); |
tnhnrl | 62:d502889e74f1 | 192 | } |
tnhnrl | 62:d502889e74f1 | 193 | |
tnhnrl | 62:d502889e74f1 | 194 | void MbedLogger::createDataPacket() { |
tnhnrl | 62:d502889e74f1 | 195 | // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2 |
tnhnrl | 62:d502889e74f1 | 196 | |
tnhnrl | 62:d502889e74f1 | 197 | //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0. |
tnhnrl | 62:d502889e74f1 | 198 | _data_packet.clear(); |
tnhnrl | 62:d502889e74f1 | 199 | |
tnhnrl | 62:d502889e74f1 | 200 | //DATA PACKET HEADER |
tnhnrl | 62:d502889e74f1 | 201 | _data_packet.push_back(117); //0x75 |
tnhnrl | 62:d502889e74f1 | 202 | _data_packet.push_back(101); //0x65 |
tnhnrl | 62:d502889e74f1 | 203 | |
tnhnrl | 62:d502889e74f1 | 204 | _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form |
tnhnrl | 62:d502889e74f1 | 205 | _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form |
tnhnrl | 62:d502889e74f1 | 206 | |
tnhnrl | 62:d502889e74f1 | 207 | _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form |
tnhnrl | 62:d502889e74f1 | 208 | _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form |
tnhnrl | 62:d502889e74f1 | 209 | |
tnhnrl | 62:d502889e74f1 | 210 | _data_packet.push_back(_current_line_length); |
tnhnrl | 62:d502889e74f1 | 211 | |
tnhnrl | 62:d502889e74f1 | 212 | pc().printf("DEBUG: Current line buffer: %s\n\r", _line_buffer); //debug |
tnhnrl | 62:d502889e74f1 | 213 | |
tnhnrl | 62:d502889e74f1 | 214 | //DATA FROM LINE READ (read string character by chracter) |
tnhnrl | 62:d502889e74f1 | 215 | for (int i = 0; i < _current_line_length; i++) { |
tnhnrl | 62:d502889e74f1 | 216 | _data_packet.push_back(_line_buffer[i]); |
tnhnrl | 62:d502889e74f1 | 217 | } |
tnhnrl | 62:d502889e74f1 | 218 | |
tnhnrl | 62:d502889e74f1 | 219 | //CRC CALCULATIONS BELOW |
tnhnrl | 62:d502889e74f1 | 220 | //character version of calculation screws up on null character 0x00, scrapped and using vector of integers |
tnhnrl | 62:d502889e74f1 | 221 | |
tnhnrl | 62:d502889e74f1 | 222 | int crc_one = calcCrcOne(); |
tnhnrl | 62:d502889e74f1 | 223 | int crc_two = calcCrcTwo(); |
tnhnrl | 62:d502889e74f1 | 224 | |
tnhnrl | 62:d502889e74f1 | 225 | //place the crc bytes into the data packet that is transmitted |
tnhnrl | 62:d502889e74f1 | 226 | _data_packet.push_back(crc_one); |
tnhnrl | 62:d502889e74f1 | 227 | _data_packet.push_back(crc_two); |
tnhnrl | 62:d502889e74f1 | 228 | } |
tnhnrl | 62:d502889e74f1 | 229 | |
tnhnrl | 62:d502889e74f1 | 230 | //should i save it as a string and transmit the string? next iteration maybe |
tnhnrl | 62:d502889e74f1 | 231 | |
tnhnrl | 62:d502889e74f1 | 232 | void MbedLogger::transmitDataPacket() { |
tnhnrl | 62:d502889e74f1 | 233 | //WRITE the data (in bytes) to the serial port |
tnhnrl | 62:d502889e74f1 | 234 | for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) { |
tnhnrl | 62:d502889e74f1 | 235 | pc().putc(*_it); //send integers over serial port one byte at a time |
tnhnrl | 62:d502889e74f1 | 236 | } |
tnhnrl | 62:d502889e74f1 | 237 | } |
tnhnrl | 62:d502889e74f1 | 238 | |
tnhnrl | 62:d502889e74f1 | 239 | // test to see if transmitDataPacket is working slower than you think. |
tnhnrl | 62:d502889e74f1 | 240 | |
tnhnrl | 62:d502889e74f1 | 241 | // REACH ONE CHAR AT A TIME (EACH ITERATION OF STATE MACHINE...) |
tnhnrl | 62:d502889e74f1 | 242 | |
tnhnrl | 63:6cb0405fc6e6 | 243 | void MbedLogger::readTransmitPacketOneChar() { |
tnhnrl | 63:6cb0405fc6e6 | 244 | led2() = !led2(); |
tnhnrl | 63:6cb0405fc6e6 | 245 | |
tnhnrl | 62:d502889e74f1 | 246 | static int transmit_state = HEADER_117; //state in switch statement |
tnhnrl | 62:d502889e74f1 | 247 | int incoming_byte = -1; //reset each time a character is read |
tnhnrl | 62:d502889e74f1 | 248 | int requested_packet_number = -1; //reset each time a character is read |
tnhnrl | 62:d502889e74f1 | 249 | static int input_packet[4]; //changed from char in previous iteration 03/28/2018 |
tnhnrl | 62:d502889e74f1 | 250 | static int transmit_crc_one = 0; //hold crc values until they're reset with calculations |
tnhnrl | 62:d502889e74f1 | 251 | static int transmit_crc_two = 0; |
tnhnrl | 62:d502889e74f1 | 252 | |
tnhnrl | 62:d502889e74f1 | 253 | |
tnhnrl | 63:6cb0405fc6e6 | 254 | while (pc().readable()) { |
tnhnrl | 63:6cb0405fc6e6 | 255 | incoming_byte = pc().getc(); |
tnhnrl | 63:6cb0405fc6e6 | 256 | |
tnhnrl | 63:6cb0405fc6e6 | 257 | led3() = !led3(); |
tnhnrl | 62:d502889e74f1 | 258 | |
tnhnrl | 63:6cb0405fc6e6 | 259 | switch(transmit_state) { |
tnhnrl | 63:6cb0405fc6e6 | 260 | case HEADER_117: |
tnhnrl | 63:6cb0405fc6e6 | 261 | //continue processing |
tnhnrl | 63:6cb0405fc6e6 | 262 | if (incoming_byte == 30){ //1E |
tnhnrl | 63:6cb0405fc6e6 | 263 | transmit_state = HEADER_101; |
tnhnrl | 63:6cb0405fc6e6 | 264 | } |
tnhnrl | 63:6cb0405fc6e6 | 265 | //did not receive byte 1 |
tnhnrl | 63:6cb0405fc6e6 | 266 | else { |
tnhnrl | 63:6cb0405fc6e6 | 267 | transmit_state = HEADER_117; //go back to checking the first packet |
tnhnrl | 63:6cb0405fc6e6 | 268 | } |
tnhnrl | 63:6cb0405fc6e6 | 269 | break; |
tnhnrl | 63:6cb0405fc6e6 | 270 | |
tnhnrl | 63:6cb0405fc6e6 | 271 | case HEADER_101: |
tnhnrl | 63:6cb0405fc6e6 | 272 | if(incoming_byte == 31) { //1F |
tnhnrl | 63:6cb0405fc6e6 | 273 | transmit_state = TRANSMIT_PACKET_1; |
tnhnrl | 63:6cb0405fc6e6 | 274 | } |
tnhnrl | 63:6cb0405fc6e6 | 275 | break; |
tnhnrl | 62:d502889e74f1 | 276 | |
tnhnrl | 63:6cb0405fc6e6 | 277 | case TRANSMIT_PACKET_1: |
tnhnrl | 63:6cb0405fc6e6 | 278 | if (incoming_byte >= 0) { |
tnhnrl | 63:6cb0405fc6e6 | 279 | input_packet[0] = 30; |
tnhnrl | 63:6cb0405fc6e6 | 280 | input_packet[1] = 31; |
tnhnrl | 63:6cb0405fc6e6 | 281 | input_packet[2] = incoming_byte; |
tnhnrl | 63:6cb0405fc6e6 | 282 | |
tnhnrl | 63:6cb0405fc6e6 | 283 | transmit_state = TRANSMIT_PACKET_2; |
tnhnrl | 63:6cb0405fc6e6 | 284 | //_reply_byte3 = incoming_byte; |
tnhnrl | 63:6cb0405fc6e6 | 285 | } |
tnhnrl | 63:6cb0405fc6e6 | 286 | break; |
tnhnrl | 63:6cb0405fc6e6 | 287 | |
tnhnrl | 63:6cb0405fc6e6 | 288 | case TRANSMIT_PACKET_2: |
tnhnrl | 62:d502889e74f1 | 289 | |
tnhnrl | 63:6cb0405fc6e6 | 290 | if (incoming_byte >= 0) { |
tnhnrl | 63:6cb0405fc6e6 | 291 | input_packet[3] = incoming_byte; |
tnhnrl | 63:6cb0405fc6e6 | 292 | //_reply_byte4 = incoming_byte; |
tnhnrl | 63:6cb0405fc6e6 | 293 | } |
tnhnrl | 63:6cb0405fc6e6 | 294 | transmit_state = PACKET_CRC_ONE; |
tnhnrl | 62:d502889e74f1 | 295 | |
tnhnrl | 63:6cb0405fc6e6 | 296 | break; |
tnhnrl | 62:d502889e74f1 | 297 | |
tnhnrl | 63:6cb0405fc6e6 | 298 | case (PACKET_CRC_ONE): |
tnhnrl | 63:6cb0405fc6e6 | 299 | transmit_crc_one = calcCrcOneArray(input_packet, 4); //calc CRC 1 from the input packet (size 4) |
tnhnrl | 63:6cb0405fc6e6 | 300 | |
tnhnrl | 63:6cb0405fc6e6 | 301 | if (incoming_byte == transmit_crc_one) { |
tnhnrl | 63:6cb0405fc6e6 | 302 | transmit_state = PACKET_CRC_TWO; |
tnhnrl | 62:d502889e74f1 | 303 | } |
tnhnrl | 62:d502889e74f1 | 304 | |
tnhnrl | 62:d502889e74f1 | 305 | else |
tnhnrl | 63:6cb0405fc6e6 | 306 | transmit_state = HEADER_117; |
tnhnrl | 63:6cb0405fc6e6 | 307 | //or state remains the same? |
tnhnrl | 62:d502889e74f1 | 308 | |
tnhnrl | 63:6cb0405fc6e6 | 309 | break; |
tnhnrl | 62:d502889e74f1 | 310 | |
tnhnrl | 63:6cb0405fc6e6 | 311 | case (PACKET_CRC_TWO): |
tnhnrl | 63:6cb0405fc6e6 | 312 | transmit_state = HEADER_117; |
tnhnrl | 63:6cb0405fc6e6 | 313 | transmit_crc_two = calcCrcTwoArray(input_packet, 4); //calc CRC 2 from the input packet (size 4) |
tnhnrl | 63:6cb0405fc6e6 | 314 | |
tnhnrl | 63:6cb0405fc6e6 | 315 | //check if CRC TWO is correct (then send full packet) |
tnhnrl | 63:6cb0405fc6e6 | 316 | if (incoming_byte == transmit_crc_two) { |
tnhnrl | 63:6cb0405fc6e6 | 317 | requested_packet_number = input_packet[2] * 256 + input_packet[3]; //compute the numbers 0 through 65535 with the two bytes |
tnhnrl | 63:6cb0405fc6e6 | 318 | |
tnhnrl | 63:6cb0405fc6e6 | 319 | if (requested_packet_number != _previous_reply_byte) { //CHANGE THE NAME TO SOMETHING NOT BYTE! |
tnhnrl | 63:6cb0405fc6e6 | 320 | //MUST SET THE PACKET NUMBER |
tnhnrl | 63:6cb0405fc6e6 | 321 | _packet_number = requested_packet_number; |
tnhnrl | 63:6cb0405fc6e6 | 322 | |
tnhnrl | 63:6cb0405fc6e6 | 323 | // DO NOT READ IF THE CURRENT REQUEST IS PAST THE TOTAL # OF PACKETS (lines of the file) |
tnhnrl | 63:6cb0405fc6e6 | 324 | if (_packet_number <= _total_number_of_packets) { |
tnhnrl | 63:6cb0405fc6e6 | 325 | readPacketInSeries(); |
tnhnrl | 63:6cb0405fc6e6 | 326 | createDataPacket(); |
tnhnrl | 63:6cb0405fc6e6 | 327 | } |
tnhnrl | 63:6cb0405fc6e6 | 328 | |
tnhnrl | 63:6cb0405fc6e6 | 329 | _previous_reply_byte = requested_packet_number; //RECORD THIS BYTE to prevent new packets being created |
tnhnrl | 63:6cb0405fc6e6 | 330 | |
tnhnrl | 63:6cb0405fc6e6 | 331 | transmitDataPacket(); //continuously transmit current packet until it tells you to do the next packet (response from Python program) |
tnhnrl | 63:6cb0405fc6e6 | 332 | } |
tnhnrl | 63:6cb0405fc6e6 | 333 | |
tnhnrl | 63:6cb0405fc6e6 | 334 | else |
tnhnrl | 63:6cb0405fc6e6 | 335 | transmitDataPacket(); //if you get the same packet request, send the same packet |
tnhnrl | 63:6cb0405fc6e6 | 336 | |
tnhnrl | 63:6cb0405fc6e6 | 337 | } //end of checksum (incoming_byte) if statement |
tnhnrl | 63:6cb0405fc6e6 | 338 | |
tnhnrl | 63:6cb0405fc6e6 | 339 | break; |
tnhnrl | 63:6cb0405fc6e6 | 340 | } //switch statement complete |
tnhnrl | 63:6cb0405fc6e6 | 341 | } //while statement complete |
tnhnrl | 62:d502889e74f1 | 342 | } |
tnhnrl | 62:d502889e74f1 | 343 | |
tnhnrl | 62:d502889e74f1 | 344 | int MbedLogger::readTransmitPacket() { |
tnhnrl | 62:d502889e74f1 | 345 | _file_transmission = true; |
tnhnrl | 62:d502889e74f1 | 346 | _file_transmission_state = 0; |
tnhnrl | 62:d502889e74f1 | 347 | |
tnhnrl | 62:d502889e74f1 | 348 | //first check if you're receiving data, then read four bytes |
tnhnrl | 62:d502889e74f1 | 349 | |
tnhnrl | 62:d502889e74f1 | 350 | int transmit_state = 0; //for state machine |
tnhnrl | 62:d502889e74f1 | 351 | |
tnhnrl | 62:d502889e74f1 | 352 | int incoming_byte = -1; |
tnhnrl | 62:d502889e74f1 | 353 | |
tnhnrl | 62:d502889e74f1 | 354 | int requested_packet_number = -1; |
tnhnrl | 62:d502889e74f1 | 355 | |
tnhnrl | 62:d502889e74f1 | 356 | static int inside_while_loop = 1; |
tnhnrl | 62:d502889e74f1 | 357 | |
tnhnrl | 62:d502889e74f1 | 358 | while (inside_while_loop) { |
tnhnrl | 62:d502889e74f1 | 359 | if (pc().readable()) { //don't rely on pc readable being open all the time |
tnhnrl | 62:d502889e74f1 | 360 | |
tnhnrl | 62:d502889e74f1 | 361 | incoming_byte = pc().getc(); |
tnhnrl | 62:d502889e74f1 | 362 | |
tnhnrl | 62:d502889e74f1 | 363 | switch(transmit_state) { |
tnhnrl | 62:d502889e74f1 | 364 | |
tnhnrl | 62:d502889e74f1 | 365 | case (HEADER_117): |
tnhnrl | 62:d502889e74f1 | 366 | //continue processing |
tnhnrl | 62:d502889e74f1 | 367 | if (incoming_byte == 117){ |
tnhnrl | 62:d502889e74f1 | 368 | transmit_state = HEADER_101; |
tnhnrl | 62:d502889e74f1 | 369 | } |
tnhnrl | 62:d502889e74f1 | 370 | //end transmission |
tnhnrl | 62:d502889e74f1 | 371 | else if (incoming_byte == 16) { |
tnhnrl | 62:d502889e74f1 | 372 | transmit_state = END_TRANSMISSION; |
tnhnrl | 62:d502889e74f1 | 373 | } |
tnhnrl | 62:d502889e74f1 | 374 | else { |
tnhnrl | 62:d502889e74f1 | 375 | transmit_state = HEADER_117; |
tnhnrl | 62:d502889e74f1 | 376 | } |
tnhnrl | 62:d502889e74f1 | 377 | break; |
tnhnrl | 62:d502889e74f1 | 378 | |
tnhnrl | 62:d502889e74f1 | 379 | case (HEADER_101): |
tnhnrl | 62:d502889e74f1 | 380 | if(incoming_byte == 101) { |
tnhnrl | 62:d502889e74f1 | 381 | transmit_state = TRANSMIT_PACKET_1; |
tnhnrl | 62:d502889e74f1 | 382 | } |
tnhnrl | 62:d502889e74f1 | 383 | break; |
tnhnrl | 62:d502889e74f1 | 384 | |
tnhnrl | 62:d502889e74f1 | 385 | case (TRANSMIT_PACKET_1): |
tnhnrl | 62:d502889e74f1 | 386 | if (incoming_byte >= 0) { |
tnhnrl | 62:d502889e74f1 | 387 | transmit_state = TRANSMIT_PACKET_2; |
tnhnrl | 62:d502889e74f1 | 388 | _reply_byte3 = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 389 | } |
tnhnrl | 62:d502889e74f1 | 390 | break; |
tnhnrl | 62:d502889e74f1 | 391 | |
tnhnrl | 62:d502889e74f1 | 392 | case (TRANSMIT_PACKET_2): |
tnhnrl | 62:d502889e74f1 | 393 | if (incoming_byte >= 0) { |
tnhnrl | 62:d502889e74f1 | 394 | _reply_byte4 = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 395 | } |
tnhnrl | 62:d502889e74f1 | 396 | |
tnhnrl | 62:d502889e74f1 | 397 | requested_packet_number = _reply_byte3 * 256 + _reply_byte4; //compute the numbers 0 through 65535 with the two bytes |
tnhnrl | 62:d502889e74f1 | 398 | |
tnhnrl | 62:d502889e74f1 | 399 | if (requested_packet_number != _previous_reply_byte) { //CHANGE THE NAME TO SOMETHING NOT BYTE! |
tnhnrl | 62:d502889e74f1 | 400 | //MUST SET THE PACKET NUMBER |
tnhnrl | 62:d502889e74f1 | 401 | _packet_number = requested_packet_number; |
tnhnrl | 62:d502889e74f1 | 402 | |
tnhnrl | 62:d502889e74f1 | 403 | readPacketInSeries(); |
tnhnrl | 62:d502889e74f1 | 404 | createDataPacket(); |
tnhnrl | 62:d502889e74f1 | 405 | _previous_reply_byte = requested_packet_number; //RECORD THIS BYTE to prevent new packets being created |
tnhnrl | 62:d502889e74f1 | 406 | } |
tnhnrl | 62:d502889e74f1 | 407 | |
tnhnrl | 62:d502889e74f1 | 408 | //continuously transmit current packet until it tells you to do the next packet (response from Python program) |
tnhnrl | 62:d502889e74f1 | 409 | transmitDataPacket(); |
tnhnrl | 62:d502889e74f1 | 410 | |
tnhnrl | 62:d502889e74f1 | 411 | //TRANSMIT_PACKET_2 |
tnhnrl | 62:d502889e74f1 | 412 | inside_while_loop = 0; //exit the while loop with this |
tnhnrl | 62:d502889e74f1 | 413 | pc().printf("(TRANSMIT_PACKET_2)reached inside_while_loop = 0\n\r"); //DEBUG |
tnhnrl | 62:d502889e74f1 | 414 | break; |
tnhnrl | 62:d502889e74f1 | 415 | } //end of switch statement |
tnhnrl | 62:d502889e74f1 | 416 | } //end of while loop |
tnhnrl | 62:d502889e74f1 | 417 | |
tnhnrl | 62:d502889e74f1 | 418 | // else { |
tnhnrl | 62:d502889e74f1 | 419 | // //pc().printf("pc not readable \n\r"); |
tnhnrl | 62:d502889e74f1 | 420 | // } |
tnhnrl | 62:d502889e74f1 | 421 | // |
tnhnrl | 62:d502889e74f1 | 422 | } |
tnhnrl | 62:d502889e74f1 | 423 | |
tnhnrl | 62:d502889e74f1 | 424 | //once you're outside of the while loop |
tnhnrl | 62:d502889e74f1 | 425 | inside_while_loop = true; //for next iteration |
tnhnrl | 62:d502889e74f1 | 426 | |
tnhnrl | 62:d502889e74f1 | 427 | pc().printf("DEBUG: (readTransmitPacket) Outside of while loop\n\r"); |
tnhnrl | 62:d502889e74f1 | 428 | return false; |
tnhnrl | 62:d502889e74f1 | 429 | } |
tnhnrl | 62:d502889e74f1 | 430 | |
tnhnrl | 62:d502889e74f1 | 431 | void MbedLogger::reOpenLineReader() { |
tnhnrl | 62:d502889e74f1 | 432 | //open a new one |
tnhnrl | 62:d502889e74f1 | 433 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 434 | |
tnhnrl | 62:d502889e74f1 | 435 | _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read |
tnhnrl | 62:d502889e74f1 | 436 | |
tnhnrl | 62:d502889e74f1 | 437 | //check if this actually worked... |
tnhnrl | 62:d502889e74f1 | 438 | if (!_fp) { |
tnhnrl | 62:d502889e74f1 | 439 | pc().printf("ERROR: Log file could not be opened\n\r"); |
tnhnrl | 62:d502889e74f1 | 440 | } |
tnhnrl | 62:d502889e74f1 | 441 | else { |
tnhnrl | 62:d502889e74f1 | 442 | pc().printf("Current Log file (LOG000.csv) was opened.\n\r"); |
tnhnrl | 62:d502889e74f1 | 443 | } |
tnhnrl | 62:d502889e74f1 | 444 | } |
tnhnrl | 62:d502889e74f1 | 445 | |
tnhnrl | 62:d502889e74f1 | 446 | bool MbedLogger::openLineReader() { |
tnhnrl | 62:d502889e74f1 | 447 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 448 | |
tnhnrl | 62:d502889e74f1 | 449 | _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read |
tnhnrl | 62:d502889e74f1 | 450 | |
tnhnrl | 62:d502889e74f1 | 451 | //check if this actually worked... |
tnhnrl | 62:d502889e74f1 | 452 | if (!_fp) { |
tnhnrl | 62:d502889e74f1 | 453 | //pc().printf("ERROR: Log file could not be opened\n\r"); |
tnhnrl | 62:d502889e74f1 | 454 | return false; |
tnhnrl | 62:d502889e74f1 | 455 | } |
tnhnrl | 62:d502889e74f1 | 456 | else { |
tnhnrl | 62:d502889e74f1 | 457 | //pc().printf("Current Log file (LOG000.csv) was opened.\n\r"); |
tnhnrl | 62:d502889e74f1 | 458 | return true; |
tnhnrl | 62:d502889e74f1 | 459 | } |
tnhnrl | 62:d502889e74f1 | 460 | } |
tnhnrl | 62:d502889e74f1 | 461 | |
tnhnrl | 62:d502889e74f1 | 462 | //VERIFIED THIS IS WORKING |
tnhnrl | 62:d502889e74f1 | 463 | void MbedLogger::readPacketInSeries(){ |
tnhnrl | 62:d502889e74f1 | 464 | 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 | 465 | |
tnhnrl | 62:d502889e74f1 | 466 | //read the current line in the file |
tnhnrl | 62:d502889e74f1 | 467 | fgets(_line_buffer, 256, _fp); //reads the line of characters until you reach a newline character |
tnhnrl | 62:d502889e74f1 | 468 | |
tnhnrl | 62:d502889e74f1 | 469 | //RECORD THE STRING LENGTH |
tnhnrl | 62:d502889e74f1 | 470 | _current_line_length = strlen(_line_buffer); |
tnhnrl | 62:d502889e74f1 | 471 | } |
tnhnrl | 62:d502889e74f1 | 472 | |
tnhnrl | 62:d502889e74f1 | 473 | void MbedLogger::getNumberOfPacketsInCurrentLog() { |
tnhnrl | 62:d502889e74f1 | 474 | _packet_number = 0; |
tnhnrl | 62:d502889e74f1 | 475 | |
tnhnrl | 62:d502889e74f1 | 476 | int ch; |
tnhnrl | 62:d502889e74f1 | 477 | |
tnhnrl | 62:d502889e74f1 | 478 | _total_number_of_packets = 0; //clear this each time you read the file |
tnhnrl | 62:d502889e74f1 | 479 | |
tnhnrl | 62:d502889e74f1 | 480 | //if this is null, use the default, else use the new file |
tnhnrl | 62:d502889e74f1 | 481 | |
tnhnrl | 62:d502889e74f1 | 482 | if (_fp == NULL) { |
tnhnrl | 62:d502889e74f1 | 483 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 484 | |
tnhnrl | 62:d502889e74f1 | 485 | _fp = fopen(file_name_string.c_str(), "r"); //open the log file to read |
tnhnrl | 62:d502889e74f1 | 486 | } |
tnhnrl | 62:d502889e74f1 | 487 | else { //else, use the file that is already open... |
tnhnrl | 62:d502889e74f1 | 488 | fseek(_fp, 0, SEEK_SET); // SEEK_SET is the beginning of file |
tnhnrl | 62:d502889e74f1 | 489 | } |
tnhnrl | 62:d502889e74f1 | 490 | |
tnhnrl | 62:d502889e74f1 | 491 | while (EOF != (ch=getc(_fp))) { |
tnhnrl | 62:d502889e74f1 | 492 | if ('\n' == ch) |
tnhnrl | 62:d502889e74f1 | 493 | _total_number_of_packets++; // records the number of new lines to determine how many packets to send |
tnhnrl | 62:d502889e74f1 | 494 | } |
tnhnrl | 62:d502889e74f1 | 495 | |
tnhnrl | 62:d502889e74f1 | 496 | //move the FILE pointer back to the start |
tnhnrl | 62:d502889e74f1 | 497 | fseek(_fp, 0, SEEK_SET); // SEEK_SET is the beginning of file |
tnhnrl | 62:d502889e74f1 | 498 | |
tnhnrl | 62:d502889e74f1 | 499 | _file_transmission = true; //preparing to transmit files from MBED to Python |
tnhnrl | 62:d502889e74f1 | 500 | } |
tnhnrl | 62:d502889e74f1 | 501 | |
tnhnrl | 62:d502889e74f1 | 502 | void MbedLogger::endTransmissionCloseFile() { |
tnhnrl | 62:d502889e74f1 | 503 | // if the file pointer is null, the file was not opened in the first place |
tnhnrl | 62:d502889e74f1 | 504 | if (!_fp) { |
tnhnrl | 62:d502889e74f1 | 505 | pc().printf("\n endTransmissionCloseFile: FILE WAS NOT OPENED!\n\r"); |
tnhnrl | 62:d502889e74f1 | 506 | } |
tnhnrl | 62:d502889e74f1 | 507 | else { |
tnhnrl | 62:d502889e74f1 | 508 | pc().printf("\n endTransmissionCloseFile: FILE FOUND AND CLOSED!\n\r"); |
tnhnrl | 62:d502889e74f1 | 509 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 510 | } |
tnhnrl | 62:d502889e74f1 | 511 | |
tnhnrl | 62:d502889e74f1 | 512 | _file_transmission = false; |
tnhnrl | 62:d502889e74f1 | 513 | } |
tnhnrl | 62:d502889e74f1 | 514 | |
tnhnrl | 62:d502889e74f1 | 515 | void MbedLogger::openWriteFile() { |
tnhnrl | 62:d502889e74f1 | 516 | pc().printf("Opening file for reception.\n\r"); |
tnhnrl | 62:d502889e74f1 | 517 | |
tnhnrl | 62:d502889e74f1 | 518 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 519 | |
tnhnrl | 62:d502889e74f1 | 520 | _fp = fopen(file_name_string.c_str(), "w"); |
tnhnrl | 62:d502889e74f1 | 521 | } |
tnhnrl | 62:d502889e74f1 | 522 | |
tnhnrl | 62:d502889e74f1 | 523 | |
tnhnrl | 62:d502889e74f1 | 524 | //weird bug noticed on 5/25/2018 where if you're not sending data the function is not completing |
tnhnrl | 62:d502889e74f1 | 525 | |
tnhnrl | 62:d502889e74f1 | 526 | |
tnhnrl | 62:d502889e74f1 | 527 | |
tnhnrl | 62:d502889e74f1 | 528 | |
tnhnrl | 62:d502889e74f1 | 529 | // function checks for incoming data (receiver function) from a Python program that transmits a file |
tnhnrl | 62:d502889e74f1 | 530 | // current limit is a file that has 255 lines of data |
tnhnrl | 62:d502889e74f1 | 531 | bool MbedLogger::checkForIncomingData() { |
tnhnrl | 62:d502889e74f1 | 532 | led1() = !led1(); |
tnhnrl | 62:d502889e74f1 | 533 | |
tnhnrl | 62:d502889e74f1 | 534 | int receive_packet_number; |
tnhnrl | 62:d502889e74f1 | 535 | int receive_total_number_packets; |
tnhnrl | 62:d502889e74f1 | 536 | int receive_packet_size; |
tnhnrl | 62:d502889e74f1 | 537 | |
tnhnrl | 62:d502889e74f1 | 538 | bool data_transmission_complete = false; |
tnhnrl | 62:d502889e74f1 | 539 | |
tnhnrl | 62:d502889e74f1 | 540 | int incoming_byte; |
tnhnrl | 62:d502889e74f1 | 541 | |
tnhnrl | 62:d502889e74f1 | 542 | char char_buffer[256] = {}; //create empty buffer |
tnhnrl | 62:d502889e74f1 | 543 | |
tnhnrl | 62:d502889e74f1 | 544 | //starting state |
tnhnrl | 62:d502889e74f1 | 545 | int process_state = HEADER_117; |
tnhnrl | 62:d502889e74f1 | 546 | |
tnhnrl | 62:d502889e74f1 | 547 | //variables for processing data below |
tnhnrl | 62:d502889e74f1 | 548 | int checksum_one = -1; |
tnhnrl | 62:d502889e74f1 | 549 | int checksum_two = -1; |
tnhnrl | 62:d502889e74f1 | 550 | |
tnhnrl | 62:d502889e74f1 | 551 | int i = 5; |
tnhnrl | 62:d502889e74f1 | 552 | int serial_timeout = 0; |
tnhnrl | 62:d502889e74f1 | 553 | |
tnhnrl | 62:d502889e74f1 | 554 | while (pc().readable() && !data_transmission_complete) { |
tnhnrl | 62:d502889e74f1 | 555 | incoming_byte = pc().getc(); //getc returns an unsigned char cast to an int |
tnhnrl | 62:d502889e74f1 | 556 | //pc().printf("DEBUG: State 0\n\r"); |
tnhnrl | 62:d502889e74f1 | 557 | |
tnhnrl | 62:d502889e74f1 | 558 | switch(process_state) { |
tnhnrl | 62:d502889e74f1 | 559 | case HEADER_117: |
tnhnrl | 62:d502889e74f1 | 560 | //continue processing |
tnhnrl | 62:d502889e74f1 | 561 | if (incoming_byte == 117){ |
tnhnrl | 62:d502889e74f1 | 562 | process_state = HEADER_101; |
tnhnrl | 62:d502889e74f1 | 563 | //pc().printf("DEBUG: Case 117\n\r"); |
tnhnrl | 62:d502889e74f1 | 564 | } |
tnhnrl | 62:d502889e74f1 | 565 | //end transmission |
tnhnrl | 62:d502889e74f1 | 566 | else if (incoming_byte == 16) { |
tnhnrl | 62:d502889e74f1 | 567 | process_state = END_TRANSMISSION; |
tnhnrl | 62:d502889e74f1 | 568 | pc().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r"); |
tnhnrl | 62:d502889e74f1 | 569 | } |
tnhnrl | 62:d502889e74f1 | 570 | else { |
tnhnrl | 62:d502889e74f1 | 571 | process_state = HEADER_117; // ??? |
tnhnrl | 62:d502889e74f1 | 572 | //pc().printf("DEBUG: State Header 117\n\r"); |
tnhnrl | 62:d502889e74f1 | 573 | } |
tnhnrl | 62:d502889e74f1 | 574 | break; |
tnhnrl | 62:d502889e74f1 | 575 | |
tnhnrl | 62:d502889e74f1 | 576 | case HEADER_101: |
tnhnrl | 62:d502889e74f1 | 577 | if(incoming_byte == 101) { |
tnhnrl | 62:d502889e74f1 | 578 | process_state = PACKET_NUM; |
tnhnrl | 62:d502889e74f1 | 579 | //pc().printf("DEBUG: Case 101\n\r"); |
tnhnrl | 62:d502889e74f1 | 580 | } |
tnhnrl | 62:d502889e74f1 | 581 | break; |
tnhnrl | 62:d502889e74f1 | 582 | |
tnhnrl | 62:d502889e74f1 | 583 | case PACKET_NUM: |
tnhnrl | 62:d502889e74f1 | 584 | receive_packet_number = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 585 | process_state = TOTAL_NUM_PACKETS; |
tnhnrl | 62:d502889e74f1 | 586 | //pc().printf("DEBUG: Case PACKET_NUM\n\r"); |
tnhnrl | 62:d502889e74f1 | 587 | break; |
tnhnrl | 62:d502889e74f1 | 588 | |
tnhnrl | 62:d502889e74f1 | 589 | case TOTAL_NUM_PACKETS: |
tnhnrl | 62:d502889e74f1 | 590 | receive_total_number_packets = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 591 | process_state = PACKET_SIZE; |
tnhnrl | 62:d502889e74f1 | 592 | //pc().printf("DEBUG: Case TOTAL_NUM_PACKETS\n\r"); |
tnhnrl | 62:d502889e74f1 | 593 | break; |
tnhnrl | 62:d502889e74f1 | 594 | |
tnhnrl | 62:d502889e74f1 | 595 | case PACKET_SIZE: |
tnhnrl | 62:d502889e74f1 | 596 | receive_packet_size = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 597 | //pc().printf("DEBUG: Case PACKET_SIZE\n\r"); |
tnhnrl | 62:d502889e74f1 | 598 | |
tnhnrl | 62:d502889e74f1 | 599 | //write the header stuff to it |
tnhnrl | 62:d502889e74f1 | 600 | char_buffer[0] = 117; |
tnhnrl | 62:d502889e74f1 | 601 | char_buffer[1] = 101; |
tnhnrl | 62:d502889e74f1 | 602 | char_buffer[2] = receive_packet_number; |
tnhnrl | 62:d502889e74f1 | 603 | char_buffer[3] = receive_total_number_packets; |
tnhnrl | 62:d502889e74f1 | 604 | char_buffer[4] = receive_packet_size; |
tnhnrl | 62:d502889e74f1 | 605 | |
tnhnrl | 62:d502889e74f1 | 606 | // tests confirmed that packet number is zero, number of packets is 12, packet size is 12 |
tnhnrl | 62:d502889e74f1 | 607 | //pc().printf("char_buffer 2/3/4: %d %d %d\n\r", receive_packet_number,receive_total_number_packets,receive_packet_size); |
tnhnrl | 62:d502889e74f1 | 608 | |
tnhnrl | 62:d502889e74f1 | 609 | led4() = !led4(); |
tnhnrl | 62:d502889e74f1 | 610 | |
tnhnrl | 62:d502889e74f1 | 611 | //process packet data, future version will append for larger data sizes, 0xFFFF |
tnhnrl | 62:d502889e74f1 | 612 | |
tnhnrl | 62:d502889e74f1 | 613 | // IF YOU GET AN INTERRUPTED DATA STREAM YOU NEED TO BREAK OUT OF THE LOOP |
tnhnrl | 62:d502889e74f1 | 614 | i = 5; |
tnhnrl | 62:d502889e74f1 | 615 | serial_timeout = 0; |
tnhnrl | 62:d502889e74f1 | 616 | |
tnhnrl | 62:d502889e74f1 | 617 | while (true) { |
tnhnrl | 62:d502889e74f1 | 618 | if (pc().readable()) { |
tnhnrl | 62:d502889e74f1 | 619 | char_buffer[i] = pc().getc(); //read all of the data packets |
tnhnrl | 62:d502889e74f1 | 620 | i++; |
tnhnrl | 62:d502889e74f1 | 621 | |
tnhnrl | 62:d502889e74f1 | 622 | serial_timeout = 0; //reset the timeout |
tnhnrl | 62:d502889e74f1 | 623 | } |
tnhnrl | 62:d502889e74f1 | 624 | else { |
tnhnrl | 62:d502889e74f1 | 625 | serial_timeout++; |
tnhnrl | 62:d502889e74f1 | 626 | } |
tnhnrl | 62:d502889e74f1 | 627 | |
tnhnrl | 62:d502889e74f1 | 628 | // When full data packet is received... |
tnhnrl | 62:d502889e74f1 | 629 | if (i >= receive_packet_size+5) { //cannot do this properly with a for loop |
tnhnrl | 62:d502889e74f1 | 630 | //get checksum bytes |
tnhnrl | 62:d502889e74f1 | 631 | checksum_one = pc().getc(); |
tnhnrl | 62:d502889e74f1 | 632 | checksum_two = pc().getc(); |
tnhnrl | 62:d502889e74f1 | 633 | |
tnhnrl | 62:d502889e74f1 | 634 | //calculate the CRC from the header and data bytes using _data_packet (vector) |
tnhnrl | 62:d502889e74f1 | 635 | //found out calculating crc with string was dropping empty or null spaces |
tnhnrl | 62:d502889e74f1 | 636 | _data_packet.clear(); //clear it just in case |
tnhnrl | 62:d502889e74f1 | 637 | |
tnhnrl | 62:d502889e74f1 | 638 | for (int a = 0; a < receive_packet_size+5; a++) { |
tnhnrl | 62:d502889e74f1 | 639 | _data_packet.push_back(char_buffer[a]); //push the character array into the buffer |
tnhnrl | 62:d502889e74f1 | 640 | } |
tnhnrl | 62:d502889e74f1 | 641 | |
tnhnrl | 62:d502889e74f1 | 642 | //calculate the CRC using the vector (strings will cut off null characters) |
tnhnrl | 62:d502889e74f1 | 643 | int calc_crc_one = calcCrcOne(); |
tnhnrl | 62:d502889e74f1 | 644 | int calc_crc_two = calcCrcTwo(); |
tnhnrl | 62:d502889e74f1 | 645 | |
tnhnrl | 62:d502889e74f1 | 646 | //pc().printf("DEBUG: calc crc 1: %d, crc 2: %d\n\r", calc_crc_one, calc_crc_two); |
tnhnrl | 62:d502889e74f1 | 647 | |
tnhnrl | 62:d502889e74f1 | 648 | // first confirm that the checksum is correct |
tnhnrl | 62:d502889e74f1 | 649 | if ((calc_crc_one == checksum_one) and (calc_crc_two == checksum_two)) { |
tnhnrl | 62:d502889e74f1 | 650 | // CHECKSUM CORRECT & get the filename from the first packet (check that you receive first packet) |
tnhnrl | 62:d502889e74f1 | 651 | if (receive_packet_number == 0) { |
tnhnrl | 62:d502889e74f1 | 652 | char temp_char[receive_packet_size+1]; //temp array for memcpy |
tnhnrl | 62:d502889e74f1 | 653 | memset(&temp_char[0], 0, sizeof(temp_char)); //clear full array (or get random characters) |
tnhnrl | 62:d502889e74f1 | 654 | strncpy(temp_char, char_buffer + 5 /* Offset */, receive_packet_size*sizeof(char) /* Length */); //memcpy introduced random characters |
tnhnrl | 62:d502889e74f1 | 655 | |
tnhnrl | 62:d502889e74f1 | 656 | //have to terminate the string with '\0' |
tnhnrl | 62:d502889e74f1 | 657 | temp_char[receive_packet_size] = '\0'; |
tnhnrl | 62:d502889e74f1 | 658 | |
tnhnrl | 62:d502889e74f1 | 659 | //pc().printf("\n\rDEBUG: ------ Filename? <<%s>>\n\r", temp_char); |
tnhnrl | 62:d502889e74f1 | 660 | _received_filename = temp_char; |
tnhnrl | 62:d502889e74f1 | 661 | |
tnhnrl | 62:d502889e74f1 | 662 | //pc().printf("\n\rDEBUG: _received_filename <<%s>>\n\r", _received_filename); |
tnhnrl | 62:d502889e74f1 | 663 | |
tnhnrl | 62:d502889e74f1 | 664 | //open a file for writing |
tnhnrl | 62:d502889e74f1 | 665 | openReceiveFile(_received_filename); |
tnhnrl | 62:d502889e74f1 | 666 | |
tnhnrl | 62:d502889e74f1 | 667 | //send a reply to Python transmit program |
tnhnrl | 62:d502889e74f1 | 668 | sendReply(); |
tnhnrl | 62:d502889e74f1 | 669 | |
tnhnrl | 62:d502889e74f1 | 670 | led3() = 1; |
tnhnrl | 62:d502889e74f1 | 671 | |
tnhnrl | 62:d502889e74f1 | 672 | // even if correct CRC, counter prevents the program from writing the same packet twice |
tnhnrl | 62:d502889e74f1 | 673 | _confirmed_packet_number++; |
tnhnrl | 62:d502889e74f1 | 674 | } |
tnhnrl | 62:d502889e74f1 | 675 | |
tnhnrl | 62:d502889e74f1 | 676 | // check if the packet that you're receiving (receive_packet_number) has been received already... |
tnhnrl | 62:d502889e74f1 | 677 | |
tnhnrl | 62:d502889e74f1 | 678 | //CHECKSUM CORRECT & packet numbers that are 1 through N packets |
tnhnrl | 62:d502889e74f1 | 679 | else if (receive_packet_number == _confirmed_packet_number){ |
tnhnrl | 62:d502889e74f1 | 680 | //save the data (char buffer) to the file if both checksums work... |
tnhnrl | 62:d502889e74f1 | 681 | |
tnhnrl | 62:d502889e74f1 | 682 | // when a packet is received (successfully) send a reply |
tnhnrl | 62:d502889e74f1 | 683 | sendReply(); |
tnhnrl | 62:d502889e74f1 | 684 | |
tnhnrl | 62:d502889e74f1 | 685 | // write correct data to file |
tnhnrl | 62:d502889e74f1 | 686 | fprintf(_fp, "%s", char_buffer+5); |
tnhnrl | 62:d502889e74f1 | 687 | |
tnhnrl | 62:d502889e74f1 | 688 | // even if correct CRC, counter prevents the program from writing the same packet twice |
tnhnrl | 62:d502889e74f1 | 689 | _confirmed_packet_number++; |
tnhnrl | 62:d502889e74f1 | 690 | } |
tnhnrl | 62:d502889e74f1 | 691 | |
tnhnrl | 62:d502889e74f1 | 692 | //clear the variables |
tnhnrl | 62:d502889e74f1 | 693 | checksum_one = -1; |
tnhnrl | 62:d502889e74f1 | 694 | checksum_two = -1; |
tnhnrl | 62:d502889e74f1 | 695 | } |
tnhnrl | 62:d502889e74f1 | 696 | |
tnhnrl | 62:d502889e74f1 | 697 | process_state = HEADER_117; |
tnhnrl | 62:d502889e74f1 | 698 | |
tnhnrl | 62:d502889e74f1 | 699 | break; |
tnhnrl | 62:d502889e74f1 | 700 | } |
tnhnrl | 62:d502889e74f1 | 701 | |
tnhnrl | 62:d502889e74f1 | 702 | //counter breaks out of the loop if no data received |
tnhnrl | 62:d502889e74f1 | 703 | if (serial_timeout >= 10000) { |
tnhnrl | 62:d502889e74f1 | 704 | //pc().printf("break serial_timeout %d\n\r", serial_timeout); |
tnhnrl | 62:d502889e74f1 | 705 | break; |
tnhnrl | 62:d502889e74f1 | 706 | } |
tnhnrl | 62:d502889e74f1 | 707 | } |
tnhnrl | 62:d502889e74f1 | 708 | break; |
tnhnrl | 62:d502889e74f1 | 709 | |
tnhnrl | 62:d502889e74f1 | 710 | case END_TRANSMISSION: |
tnhnrl | 62:d502889e74f1 | 711 | if (pc().getc() == 16) { |
tnhnrl | 62:d502889e74f1 | 712 | pc().printf("DEBUG: END_TRANSMISSION REACHED: 1. \n\r"); |
tnhnrl | 62:d502889e74f1 | 713 | |
tnhnrl | 62:d502889e74f1 | 714 | if (pc().getc() == 16) { |
tnhnrl | 62:d502889e74f1 | 715 | pc().printf("DEBUG: END_TRANSMISSION REACHED: 2. \n\r"); |
tnhnrl | 62:d502889e74f1 | 716 | |
tnhnrl | 62:d502889e74f1 | 717 | endReceiveData(); |
tnhnrl | 62:d502889e74f1 | 718 | } |
tnhnrl | 62:d502889e74f1 | 719 | } |
tnhnrl | 62:d502889e74f1 | 720 | |
tnhnrl | 62:d502889e74f1 | 721 | pc().printf("DEBUG: END_TRANSMISSION REACHED: 5. \n\r"); |
tnhnrl | 62:d502889e74f1 | 722 | |
tnhnrl | 62:d502889e74f1 | 723 | //process_state = HEADER_117; //don't do this unless the check is wrong |
tnhnrl | 62:d502889e74f1 | 724 | pc().printf("END_TRANSMISSION process_state is %d\n\r", process_state); //should be 5 (debug) 02/06/2018 |
tnhnrl | 62:d502889e74f1 | 725 | data_transmission_complete = true; |
tnhnrl | 62:d502889e74f1 | 726 | break; |
tnhnrl | 62:d502889e74f1 | 727 | }//END OF SWITCH |
tnhnrl | 62:d502889e74f1 | 728 | |
tnhnrl | 62:d502889e74f1 | 729 | if (data_transmission_complete) { |
tnhnrl | 62:d502889e74f1 | 730 | pc().printf("DEBUG: checkForIncomingData data_transmission_complete \n\r"); |
tnhnrl | 62:d502889e74f1 | 731 | break; //out of while loop |
tnhnrl | 62:d502889e74f1 | 732 | } |
tnhnrl | 62:d502889e74f1 | 733 | } // while loop |
tnhnrl | 62:d502889e74f1 | 734 | |
tnhnrl | 62:d502889e74f1 | 735 | led3() = !led3(); |
tnhnrl | 62:d502889e74f1 | 736 | |
tnhnrl | 62:d502889e74f1 | 737 | if (data_transmission_complete) |
tnhnrl | 62:d502889e74f1 | 738 | return false; //tell state machine class that this is done (not transmitting, false) |
tnhnrl | 62:d502889e74f1 | 739 | else { |
tnhnrl | 62:d502889e74f1 | 740 | return true; |
tnhnrl | 62:d502889e74f1 | 741 | } |
tnhnrl | 62:d502889e74f1 | 742 | } |
tnhnrl | 62:d502889e74f1 | 743 | |
tnhnrl | 62:d502889e74f1 | 744 | void MbedLogger::checkForPythonCommand() { |
tnhnrl | 62:d502889e74f1 | 745 | // STARTING STATE |
tnhnrl | 62:d502889e74f1 | 746 | static int process_state = HEADER_117; |
tnhnrl | 62:d502889e74f1 | 747 | |
tnhnrl | 62:d502889e74f1 | 748 | static string command_string = ""; |
tnhnrl | 62:d502889e74f1 | 749 | |
tnhnrl | 62:d502889e74f1 | 750 | int incoming_byte; |
tnhnrl | 62:d502889e74f1 | 751 | |
tnhnrl | 62:d502889e74f1 | 752 | if ( pc().readable() ) { |
tnhnrl | 62:d502889e74f1 | 753 | led1() = !led1(); |
tnhnrl | 62:d502889e74f1 | 754 | incoming_byte = pc().getc(); //getc returns an unsigned char cast to an int |
tnhnrl | 62:d502889e74f1 | 755 | |
tnhnrl | 62:d502889e74f1 | 756 | switch(process_state) { |
tnhnrl | 62:d502889e74f1 | 757 | case HEADER_117: |
tnhnrl | 62:d502889e74f1 | 758 | //continue processing |
tnhnrl | 62:d502889e74f1 | 759 | if (incoming_byte == 117){ |
tnhnrl | 63:6cb0405fc6e6 | 760 | //had led two here |
tnhnrl | 62:d502889e74f1 | 761 | process_state = HEADER_101; |
tnhnrl | 62:d502889e74f1 | 762 | pc().printf("DEBUG: Case 117\n\r"); |
tnhnrl | 62:d502889e74f1 | 763 | } |
tnhnrl | 62:d502889e74f1 | 764 | //end transmission |
tnhnrl | 62:d502889e74f1 | 765 | else if (incoming_byte == 16) { |
tnhnrl | 62:d502889e74f1 | 766 | process_state = END_TRANSMISSION; |
tnhnrl | 62:d502889e74f1 | 767 | pc().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r"); |
tnhnrl | 62:d502889e74f1 | 768 | } |
tnhnrl | 62:d502889e74f1 | 769 | else { |
tnhnrl | 62:d502889e74f1 | 770 | process_state = HEADER_117; // ??? |
tnhnrl | 62:d502889e74f1 | 771 | //pc().printf("DEBUG: State Header 117\n\r"); |
tnhnrl | 62:d502889e74f1 | 772 | } |
tnhnrl | 62:d502889e74f1 | 773 | break; |
tnhnrl | 62:d502889e74f1 | 774 | |
tnhnrl | 62:d502889e74f1 | 775 | case HEADER_101: |
tnhnrl | 62:d502889e74f1 | 776 | if(incoming_byte == 101) { |
tnhnrl | 62:d502889e74f1 | 777 | led3() = !led3(); |
tnhnrl | 62:d502889e74f1 | 778 | process_state = COMMAND_ONE; |
tnhnrl | 62:d502889e74f1 | 779 | pc().printf("DEBUG: Case 101\n\r"); |
tnhnrl | 62:d502889e74f1 | 780 | } |
tnhnrl | 62:d502889e74f1 | 781 | break; |
tnhnrl | 62:d502889e74f1 | 782 | case COMMAND_ONE: |
tnhnrl | 62:d502889e74f1 | 783 | command_string += (char)incoming_byte; |
tnhnrl | 62:d502889e74f1 | 784 | process_state = COMMAND_TWO; |
tnhnrl | 62:d502889e74f1 | 785 | pc().printf("DEBUG: COMMAND: [%s]\n\r", command_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 786 | break; |
tnhnrl | 62:d502889e74f1 | 787 | case COMMAND_TWO: |
tnhnrl | 62:d502889e74f1 | 788 | command_string += (char)incoming_byte; |
tnhnrl | 62:d502889e74f1 | 789 | process_state = HEADER_117; |
tnhnrl | 62:d502889e74f1 | 790 | pc().printf("DEBUG: COMMAND: [%s]\n\r", command_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 791 | |
tnhnrl | 62:d502889e74f1 | 792 | // COMMAND |
tnhnrl | 63:6cb0405fc6e6 | 793 | // if ( command_string.find("CT") == 0 ) { //find returns the position of the string |
tnhnrl | 63:6cb0405fc6e6 | 794 | // stateMachine().setFSMCommand(CHECK_TUNING); |
tnhnrl | 63:6cb0405fc6e6 | 795 | // } |
tnhnrl | 63:6cb0405fc6e6 | 796 | // else if ( command_string.find("FN") == 0 ) { //find returns the position of the string |
tnhnrl | 63:6cb0405fc6e6 | 797 | // stateMachine().setFSMCommand(FIND_NEUTRAL); |
tnhnrl | 63:6cb0405fc6e6 | 798 | // } |
tnhnrl | 63:6cb0405fc6e6 | 799 | // else if ( command_string.find("DV") == 0 ) { //find returns the position of the string |
tnhnrl | 63:6cb0405fc6e6 | 800 | // stateMachine().setFSMCommand(DIVE); |
tnhnrl | 63:6cb0405fc6e6 | 801 | // } |
tnhnrl | 62:d502889e74f1 | 802 | |
tnhnrl | 62:d502889e74f1 | 803 | command_string = ""; |
tnhnrl | 62:d502889e74f1 | 804 | |
tnhnrl | 62:d502889e74f1 | 805 | // COMMAND |
tnhnrl | 62:d502889e74f1 | 806 | |
tnhnrl | 62:d502889e74f1 | 807 | break; |
tnhnrl | 62:d502889e74f1 | 808 | } |
tnhnrl | 62:d502889e74f1 | 809 | // |
tnhnrl | 62:d502889e74f1 | 810 | // case PACKET_NUM: |
tnhnrl | 62:d502889e74f1 | 811 | // receive_packet_number = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 812 | // process_state = TOTAL_NUM_PACKETS; |
tnhnrl | 62:d502889e74f1 | 813 | // //pc().printf("DEBUG: Case PACKET_NUM\n\r"); |
tnhnrl | 62:d502889e74f1 | 814 | // break; |
tnhnrl | 62:d502889e74f1 | 815 | } |
tnhnrl | 62:d502889e74f1 | 816 | //else { |
tnhnrl | 62:d502889e74f1 | 817 | // return false; |
tnhnrl | 62:d502889e74f1 | 818 | // } |
tnhnrl | 62:d502889e74f1 | 819 | // |
tnhnrl | 62:d502889e74f1 | 820 | // switch(process_state) { |
tnhnrl | 62:d502889e74f1 | 821 | // case HEADER_117: |
tnhnrl | 62:d502889e74f1 | 822 | // //continue processing |
tnhnrl | 62:d502889e74f1 | 823 | // if (incoming_byte == 101){ |
tnhnrl | 62:d502889e74f1 | 824 | // process_state = HEADER_101; |
tnhnrl | 62:d502889e74f1 | 825 | // pc().printf("DEBUG: Case 101\n\r"); |
tnhnrl | 62:d502889e74f1 | 826 | // } |
tnhnrl | 62:d502889e74f1 | 827 | // //end transmission |
tnhnrl | 62:d502889e74f1 | 828 | // else if (incoming_byte == 16) { |
tnhnrl | 62:d502889e74f1 | 829 | // process_state = END_TRANSMISSION; |
tnhnrl | 62:d502889e74f1 | 830 | // pc().printf("DEBUG: State 16 (END_TRANSMISSION)\n\r"); |
tnhnrl | 62:d502889e74f1 | 831 | // } |
tnhnrl | 62:d502889e74f1 | 832 | // else { |
tnhnrl | 62:d502889e74f1 | 833 | // process_state = HEADER_117; // ??? |
tnhnrl | 62:d502889e74f1 | 834 | // //pc().printf("DEBUG: State Header 117\n\r"); |
tnhnrl | 62:d502889e74f1 | 835 | // } |
tnhnrl | 62:d502889e74f1 | 836 | // break; |
tnhnrl | 62:d502889e74f1 | 837 | // |
tnhnrl | 62:d502889e74f1 | 838 | // case HEADER_101: |
tnhnrl | 62:d502889e74f1 | 839 | // if(incoming_byte == 117) { |
tnhnrl | 62:d502889e74f1 | 840 | // process_state = PACKET_NUM; |
tnhnrl | 62:d502889e74f1 | 841 | // pc().printf("DEBUG: Case 117\n\r"); |
tnhnrl | 62:d502889e74f1 | 842 | // } |
tnhnrl | 62:d502889e74f1 | 843 | // break; |
tnhnrl | 62:d502889e74f1 | 844 | // |
tnhnrl | 62:d502889e74f1 | 845 | // case PACKET_NUM: |
tnhnrl | 62:d502889e74f1 | 846 | // receive_packet_number = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 847 | // process_state = TOTAL_NUM_PACKETS; |
tnhnrl | 62:d502889e74f1 | 848 | // //pc().printf("DEBUG: Case PACKET_NUM\n\r"); |
tnhnrl | 62:d502889e74f1 | 849 | // break; |
tnhnrl | 62:d502889e74f1 | 850 | // |
tnhnrl | 62:d502889e74f1 | 851 | // case TOTAL_NUM_PACKETS: |
tnhnrl | 62:d502889e74f1 | 852 | // receive_total_number_packets = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 853 | // process_state = PACKET_SIZE; |
tnhnrl | 62:d502889e74f1 | 854 | // //pc().printf("DEBUG: Case TOTAL_NUM_PACKETS\n\r"); |
tnhnrl | 62:d502889e74f1 | 855 | // break; |
tnhnrl | 62:d502889e74f1 | 856 | // |
tnhnrl | 62:d502889e74f1 | 857 | // case PACKET_SIZE: |
tnhnrl | 62:d502889e74f1 | 858 | // receive_packet_size = incoming_byte; |
tnhnrl | 62:d502889e74f1 | 859 | // //pc().printf("DEBUG: Case PACKET_SIZE\n\r"); |
tnhnrl | 62:d502889e74f1 | 860 | // |
tnhnrl | 62:d502889e74f1 | 861 | // //write the header stuff to it |
tnhnrl | 62:d502889e74f1 | 862 | // char_buffer[0] = 117; |
tnhnrl | 62:d502889e74f1 | 863 | // char_buffer[1] = 101; |
tnhnrl | 62:d502889e74f1 | 864 | // char_buffer[2] = receive_packet_number; |
tnhnrl | 62:d502889e74f1 | 865 | // char_buffer[3] = receive_total_number_packets; |
tnhnrl | 62:d502889e74f1 | 866 | // char_buffer[4] = receive_packet_size; |
tnhnrl | 62:d502889e74f1 | 867 | // |
tnhnrl | 62:d502889e74f1 | 868 | // break; |
tnhnrl | 62:d502889e74f1 | 869 | // |
tnhnrl | 62:d502889e74f1 | 870 | // case END_TRANSMISSION: |
tnhnrl | 62:d502889e74f1 | 871 | // if (pc().getc() == 16) { |
tnhnrl | 62:d502889e74f1 | 872 | // pc().printf("DEBUG: END_TRANSMISSION REACHED: 1. \n\r"); |
tnhnrl | 62:d502889e74f1 | 873 | // |
tnhnrl | 62:d502889e74f1 | 874 | // if (pc().getc() == 16) { |
tnhnrl | 62:d502889e74f1 | 875 | // pc().printf("DEBUG: END_TRANSMISSION REACHED: 2. \n\r"); |
tnhnrl | 62:d502889e74f1 | 876 | // |
tnhnrl | 62:d502889e74f1 | 877 | // endReceiveData(); |
tnhnrl | 62:d502889e74f1 | 878 | // } |
tnhnrl | 62:d502889e74f1 | 879 | // } |
tnhnrl | 62:d502889e74f1 | 880 | // |
tnhnrl | 62:d502889e74f1 | 881 | // pc().printf("DEBUG: END_TRANSMISSION REACHED: 5. \n\r"); |
tnhnrl | 62:d502889e74f1 | 882 | // |
tnhnrl | 62:d502889e74f1 | 883 | // //process_state = HEADER_117; //don't do this unless the check is wrong |
tnhnrl | 62:d502889e74f1 | 884 | // pc().printf("END_TRANSMISSION process_state is %d\n\r", process_state); //should be 5 (debug) 02/06/2018 |
tnhnrl | 62:d502889e74f1 | 885 | // data_transmission_complete = true; |
tnhnrl | 62:d502889e74f1 | 886 | // break; |
tnhnrl | 62:d502889e74f1 | 887 | // }//END OF SWITCH STATEMENT |
tnhnrl | 62:d502889e74f1 | 888 | // |
tnhnrl | 62:d502889e74f1 | 889 | // return true; |
tnhnrl | 62:d502889e74f1 | 890 | } |
tnhnrl | 62:d502889e74f1 | 891 | |
tnhnrl | 62:d502889e74f1 | 892 | int MbedLogger::sendReply() { |
tnhnrl | 62:d502889e74f1 | 893 | //being explicit in what's being transmitted |
tnhnrl | 62:d502889e74f1 | 894 | |
tnhnrl | 62:d502889e74f1 | 895 | //change this method to be more explicit later |
tnhnrl | 62:d502889e74f1 | 896 | |
tnhnrl | 62:d502889e74f1 | 897 | //integer vector _data_packet is used here, cleared fist just in case |
tnhnrl | 62:d502889e74f1 | 898 | |
tnhnrl | 62:d502889e74f1 | 899 | _data_packet.clear(); //same data packet for transmission |
tnhnrl | 62:d502889e74f1 | 900 | _data_packet.push_back(117); |
tnhnrl | 62:d502889e74f1 | 901 | _data_packet.push_back(101); |
tnhnrl | 62:d502889e74f1 | 902 | |
tnhnrl | 62:d502889e74f1 | 903 | //_confirmed_packet_number comes from the packet number that is sent from the Python program |
tnhnrl | 62:d502889e74f1 | 904 | _data_packet.push_back(_confirmed_packet_number / 256); //packet number only changed when confirmed |
tnhnrl | 62:d502889e74f1 | 905 | _data_packet.push_back(_confirmed_packet_number % 256); //split into first and second byte |
tnhnrl | 62:d502889e74f1 | 906 | |
tnhnrl | 62:d502889e74f1 | 907 | //compute checksums |
tnhnrl | 62:d502889e74f1 | 908 | |
tnhnrl | 62:d502889e74f1 | 909 | int receiver_crc_one = calcCrcOne(); |
tnhnrl | 62:d502889e74f1 | 910 | int receiver_crc_two = calcCrcTwo(); |
tnhnrl | 62:d502889e74f1 | 911 | |
tnhnrl | 62:d502889e74f1 | 912 | _data_packet.push_back(receiver_crc_one); |
tnhnrl | 62:d502889e74f1 | 913 | _data_packet.push_back(receiver_crc_two); |
tnhnrl | 62:d502889e74f1 | 914 | |
tnhnrl | 62:d502889e74f1 | 915 | //transmit this packet |
tnhnrl | 62:d502889e74f1 | 916 | for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) { |
tnhnrl | 62:d502889e74f1 | 917 | pc().putc(*_it); //send integers over serial port one byte at a time |
tnhnrl | 62:d502889e74f1 | 918 | } |
tnhnrl | 62:d502889e74f1 | 919 | |
tnhnrl | 62:d502889e74f1 | 920 | //change process methodology later... |
tnhnrl | 62:d502889e74f1 | 921 | |
tnhnrl | 62:d502889e74f1 | 922 | return _confirmed_packet_number; |
tnhnrl | 62:d502889e74f1 | 923 | } |
tnhnrl | 62:d502889e74f1 | 924 | |
tnhnrl | 62:d502889e74f1 | 925 | void MbedLogger::endReceiveData() { //DLE character * 4 ==> 10 10 10 10 |
tnhnrl | 62:d502889e74f1 | 926 | closeLogFile(); //close the file here |
tnhnrl | 62:d502889e74f1 | 927 | pc().printf("endReceiveData closed the file and ended transmission\n\r"); |
tnhnrl | 62:d502889e74f1 | 928 | } |
tnhnrl | 62:d502889e74f1 | 929 | |
tnhnrl | 62:d502889e74f1 | 930 | //calculate the crc with an integer array |
tnhnrl | 62:d502889e74f1 | 931 | int MbedLogger::calcCrcOneArray(int *input_array, int array_length) { |
tnhnrl | 62:d502889e74f1 | 932 | //can't initialize the table in the constructor in c++ |
tnhnrl | 62:d502889e74f1 | 933 | 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 | 934 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 935 | for (int z = 0; z < array_length; z++) { |
tnhnrl | 62:d502889e74f1 | 936 | crc = (crc_table[(input_array[z] ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 937 | } |
tnhnrl | 62:d502889e74f1 | 938 | return crc / 256; //second-to-last byte |
tnhnrl | 62:d502889e74f1 | 939 | } |
tnhnrl | 62:d502889e74f1 | 940 | |
tnhnrl | 62:d502889e74f1 | 941 | int MbedLogger::calcCrcTwoArray(int *input_array, int array_length) { |
tnhnrl | 62:d502889e74f1 | 942 | //can't initialize the table in the constructor in c++ |
tnhnrl | 62:d502889e74f1 | 943 | 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 | 944 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 945 | for (int z = 0; z < array_length; z++) { |
tnhnrl | 62:d502889e74f1 | 946 | crc = (crc_table[(input_array[z] ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 947 | } |
tnhnrl | 62:d502889e74f1 | 948 | return crc % 256; //second-to-last byte |
tnhnrl | 62:d502889e74f1 | 949 | } |
tnhnrl | 62:d502889e74f1 | 950 | |
tnhnrl | 62:d502889e74f1 | 951 | int MbedLogger::calcCrcOne() { |
tnhnrl | 62:d502889e74f1 | 952 | //can't initialize the table in the constructor in c++ |
tnhnrl | 62:d502889e74f1 | 953 | 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 | 954 | |
tnhnrl | 62:d502889e74f1 | 955 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 956 | for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) |
tnhnrl | 62:d502889e74f1 | 957 | crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 958 | |
tnhnrl | 62:d502889e74f1 | 959 | return crc / 256; //second-to-last byte |
tnhnrl | 62:d502889e74f1 | 960 | } |
tnhnrl | 62:d502889e74f1 | 961 | |
tnhnrl | 62:d502889e74f1 | 962 | int MbedLogger::calcCrcTwo() { |
tnhnrl | 62:d502889e74f1 | 963 | 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 | 964 | |
tnhnrl | 62:d502889e74f1 | 965 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 966 | for (_it=_data_packet.begin(); _it < _data_packet.end(); _it++) |
tnhnrl | 62:d502889e74f1 | 967 | crc = (crc_table[(*_it ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 968 | |
tnhnrl | 62:d502889e74f1 | 969 | //pc().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_array.length(), crc % 256); |
tnhnrl | 62:d502889e74f1 | 970 | |
tnhnrl | 62:d502889e74f1 | 971 | return crc % 256; //last byte |
tnhnrl | 62:d502889e74f1 | 972 | } |
tnhnrl | 62:d502889e74f1 | 973 | |
tnhnrl | 62:d502889e74f1 | 974 | void MbedLogger::resetReplyPacket() { |
tnhnrl | 62:d502889e74f1 | 975 | _confirmed_packet_number = 0; |
tnhnrl | 62:d502889e74f1 | 976 | } |
tnhnrl | 62:d502889e74f1 | 977 | |
tnhnrl | 62:d502889e74f1 | 978 | void MbedLogger::openNewMissionFile() { |
tnhnrl | 62:d502889e74f1 | 979 | pc().printf("Opening Mission file (sequence.txt) for reception.\n\r"); |
tnhnrl | 62:d502889e74f1 | 980 | string filename_string = _file_system_string + "sequence.txt"; |
tnhnrl | 62:d502889e74f1 | 981 | |
tnhnrl | 62:d502889e74f1 | 982 | pc().printf("openNewMissionFile: %s\n\r", filename_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 983 | |
tnhnrl | 62:d502889e74f1 | 984 | _fp = fopen(filename_string.c_str(), "w"); |
tnhnrl | 62:d502889e74f1 | 985 | } |
tnhnrl | 62:d502889e74f1 | 986 | |
tnhnrl | 62:d502889e74f1 | 987 | void MbedLogger::openReceiveFile(string filename) { |
tnhnrl | 62:d502889e74f1 | 988 | string filename_string = _file_system_string + filename; //example "sequence.txt" |
tnhnrl | 62:d502889e74f1 | 989 | |
tnhnrl | 62:d502889e74f1 | 990 | _fp = fopen(filename_string.c_str(), "w"); //open a file for writing |
tnhnrl | 62:d502889e74f1 | 991 | } |
tnhnrl | 62:d502889e74f1 | 992 | |
tnhnrl | 62:d502889e74f1 | 993 | void MbedLogger::setDataCounter(int input_counter) { |
tnhnrl | 62:d502889e74f1 | 994 | _transmit_counter = input_counter; |
tnhnrl | 62:d502889e74f1 | 995 | } |
tnhnrl | 62:d502889e74f1 | 996 | |
tnhnrl | 62:d502889e74f1 | 997 | void MbedLogger::closeIncompleteFile() { |
tnhnrl | 62:d502889e74f1 | 998 | fprintf(_fp, "TRANSMISSION INTERRUPTED!"); //write this warning to the file |
tnhnrl | 62:d502889e74f1 | 999 | closeLogFile(); //close file |
tnhnrl | 62:d502889e74f1 | 1000 | } |
tnhnrl | 62:d502889e74f1 | 1001 | |
tnhnrl | 62:d502889e74f1 | 1002 | void MbedLogger::appendLogFile(int current_state, int option) { |
tnhnrl | 62:d502889e74f1 | 1003 | //option one means write to file |
tnhnrl | 62:d502889e74f1 | 1004 | |
tnhnrl | 62:d502889e74f1 | 1005 | if (option == 1) { |
tnhnrl | 62:d502889e74f1 | 1006 | if (!_fp) { //if not present |
tnhnrl | 62:d502889e74f1 | 1007 | _fp = fopen(_full_file_path_string.c_str(), "a"); |
tnhnrl | 62:d502889e74f1 | 1008 | } |
tnhnrl | 62:d502889e74f1 | 1009 | |
tnhnrl | 62:d502889e74f1 | 1010 | //record data using the recordData function (takes in the state integer) |
tnhnrl | 62:d502889e74f1 | 1011 | recordData(current_state); |
tnhnrl | 62:d502889e74f1 | 1012 | } |
tnhnrl | 62:d502889e74f1 | 1013 | |
tnhnrl | 62:d502889e74f1 | 1014 | else { |
tnhnrl | 62:d502889e74f1 | 1015 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 1016 | } |
tnhnrl | 62:d502889e74f1 | 1017 | } |
tnhnrl | 62:d502889e74f1 | 1018 | |
tnhnrl | 62:d502889e74f1 | 1019 | // initialize and close the file |
tnhnrl | 62:d502889e74f1 | 1020 | // log file freezes at 0x0000006c |
tnhnrl | 62:d502889e74f1 | 1021 | void MbedLogger::initializeLogFile() { |
tnhnrl | 62:d502889e74f1 | 1022 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 1023 | pc().printf("%s file system init\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1024 | |
tnhnrl | 62:d502889e74f1 | 1025 | //try to open this file... |
tnhnrl | 62:d502889e74f1 | 1026 | _fp = fopen(file_name_string.c_str(), "r"); |
tnhnrl | 62:d502889e74f1 | 1027 | |
tnhnrl | 62:d502889e74f1 | 1028 | //if the file is empty, create this. |
tnhnrl | 62:d502889e74f1 | 1029 | if (!_fp) { |
tnhnrl | 62:d502889e74f1 | 1030 | _fp = fopen(file_name_string.c_str(), "w"); //write,print,close |
tnhnrl | 62:d502889e74f1 | 1031 | 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 | 62:d502889e74f1 | 1032 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 1033 | } |
tnhnrl | 62:d502889e74f1 | 1034 | else |
tnhnrl | 62:d502889e74f1 | 1035 | closeLogFile(); //close the opened read file |
tnhnrl | 62:d502889e74f1 | 1036 | } |
tnhnrl | 62:d502889e74f1 | 1037 | |
tnhnrl | 62:d502889e74f1 | 1038 | int MbedLogger::fileTransmitState() { |
tnhnrl | 62:d502889e74f1 | 1039 | return _file_transmission_state; |
tnhnrl | 62:d502889e74f1 | 1040 | } |
tnhnrl | 62:d502889e74f1 | 1041 | |
tnhnrl | 62:d502889e74f1 | 1042 | int MbedLogger::calcCrcOneString (string input_string) { |
tnhnrl | 62:d502889e74f1 | 1043 | 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 | 1044 | |
tnhnrl | 62:d502889e74f1 | 1045 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 1046 | for (unsigned int i = 0; i < input_string.length(); i++) { |
tnhnrl | 62:d502889e74f1 | 1047 | //convert each character to an integer |
tnhnrl | 62:d502889e74f1 | 1048 | int character_to_integer = input_string[i]; //correct |
tnhnrl | 62:d502889e74f1 | 1049 | |
tnhnrl | 62:d502889e74f1 | 1050 | crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 1051 | } |
tnhnrl | 62:d502889e74f1 | 1052 | |
tnhnrl | 62:d502889e74f1 | 1053 | //pc().printf("DEBUG: calcCrcOne string length: %d crc: %d\n\r", input_string.length(), crc/256); |
tnhnrl | 62:d502889e74f1 | 1054 | |
tnhnrl | 62:d502889e74f1 | 1055 | return crc / 256; //second-to-last byte |
tnhnrl | 62:d502889e74f1 | 1056 | } |
tnhnrl | 62:d502889e74f1 | 1057 | |
tnhnrl | 62:d502889e74f1 | 1058 | int MbedLogger::calcCrcTwoString (string input_string) { |
tnhnrl | 62:d502889e74f1 | 1059 | 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 | 1060 | |
tnhnrl | 62:d502889e74f1 | 1061 | int crc = 0; |
tnhnrl | 62:d502889e74f1 | 1062 | for (unsigned int i = 0; i < input_string.length(); i++) { |
tnhnrl | 62:d502889e74f1 | 1063 | //convert each character to an integer |
tnhnrl | 62:d502889e74f1 | 1064 | int character_to_integer = input_string[i]; //correct |
tnhnrl | 62:d502889e74f1 | 1065 | |
tnhnrl | 62:d502889e74f1 | 1066 | crc = (crc_table[(character_to_integer ^ crc) & 0xff] ^ (crc >> 8)) & 0xFFFF; |
tnhnrl | 62:d502889e74f1 | 1067 | } |
tnhnrl | 62:d502889e74f1 | 1068 | |
tnhnrl | 62:d502889e74f1 | 1069 | //pc().printf("DEBUG: calcCrcTwo string length: %d crc: %d\n\r", input_string.length(), crc % 256); |
tnhnrl | 62:d502889e74f1 | 1070 | |
tnhnrl | 62:d502889e74f1 | 1071 | return crc % 256; //last byte |
tnhnrl | 62:d502889e74f1 | 1072 | } |
tnhnrl | 62:d502889e74f1 | 1073 | |
tnhnrl | 62:d502889e74f1 | 1074 | int MbedLogger::testGetFileSize() { |
tnhnrl | 62:d502889e74f1 | 1075 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 1076 | |
tnhnrl | 62:d502889e74f1 | 1077 | _fp = fopen(file_name_string.c_str(), "rb"); //open the file for reading as a binary file |
tnhnrl | 62:d502889e74f1 | 1078 | |
tnhnrl | 62:d502889e74f1 | 1079 | fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file) |
tnhnrl | 62:d502889e74f1 | 1080 | unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file. |
tnhnrl | 62:d502889e74f1 | 1081 | fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary |
tnhnrl | 62:d502889e74f1 | 1082 | |
tnhnrl | 62:d502889e74f1 | 1083 | closeLogFile(); //can probably just close the file pointer and not worry about position |
tnhnrl | 62:d502889e74f1 | 1084 | |
tnhnrl | 62:d502889e74f1 | 1085 | pc().printf("(%s) LOG000.csv file size is %d\n\r", _file_system_string.c_str(), file_size); |
tnhnrl | 62:d502889e74f1 | 1086 | |
tnhnrl | 62:d502889e74f1 | 1087 | if (file_size == 0) |
tnhnrl | 62:d502889e74f1 | 1088 | createEmptyLog(); //this is not actually a completely empty log |
tnhnrl | 62:d502889e74f1 | 1089 | |
tnhnrl | 62:d502889e74f1 | 1090 | return file_size; |
tnhnrl | 62:d502889e74f1 | 1091 | |
tnhnrl | 62:d502889e74f1 | 1092 | // http://www.cplusplus.com/reference/cstdio/ftell/ |
tnhnrl | 62:d502889e74f1 | 1093 | // https://os.mbed.com/questions/1260/How-to-read-the-LocalFileSystem-filesize/ |
tnhnrl | 62:d502889e74f1 | 1094 | } |
tnhnrl | 62:d502889e74f1 | 1095 | |
tnhnrl | 62:d502889e74f1 | 1096 | void MbedLogger::createEmptyLog() { |
tnhnrl | 62:d502889e74f1 | 1097 | string file_name_string = _file_system_string + "LOG000.csv"; |
tnhnrl | 62:d502889e74f1 | 1098 | |
tnhnrl | 62:d502889e74f1 | 1099 | _fp = fopen(file_name_string.c_str(), "w"); |
tnhnrl | 62:d502889e74f1 | 1100 | fprintf(_fp, "EMPTY LOG\n"); //just write this string to the log (processing needs a file size that is not zero) |
tnhnrl | 62:d502889e74f1 | 1101 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 1102 | } |
tnhnrl | 62:d502889e74f1 | 1103 | |
tnhnrl | 62:d502889e74f1 | 1104 | int MbedLogger::getFileSize(string filename) { |
tnhnrl | 62:d502889e74f1 | 1105 | // fixed the const char * errror: |
tnhnrl | 62:d502889e74f1 | 1106 | // https://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char |
tnhnrl | 62:d502889e74f1 | 1107 | 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 | 1108 | //http://www.cplusplus.com/reference/string/string/c_str/ |
tnhnrl | 62:d502889e74f1 | 1109 | |
tnhnrl | 62:d502889e74f1 | 1110 | _fp = fopen(filename.c_str(), "rb"); //open the file for reading as a binary file |
tnhnrl | 62:d502889e74f1 | 1111 | |
tnhnrl | 62:d502889e74f1 | 1112 | fseek(_fp, 0, SEEK_END); //SEEK_END is a constant in cstdio (end of the file) |
tnhnrl | 62:d502889e74f1 | 1113 | unsigned int file_size = ftell(_fp); //For binary streams, this is the number of bytes from the beginning of the file. |
tnhnrl | 62:d502889e74f1 | 1114 | fseek(_fp, 0, SEEK_SET); //SEEK_SET is hte beginning of the file, not sure this is necessary |
tnhnrl | 62:d502889e74f1 | 1115 | |
tnhnrl | 62:d502889e74f1 | 1116 | closeLogFile(); //can probably just close the file pointer and not worry about position |
tnhnrl | 62:d502889e74f1 | 1117 | |
tnhnrl | 62:d502889e74f1 | 1118 | pc().printf("%s file size is %d\n\r", filename.c_str(), file_size); |
tnhnrl | 62:d502889e74f1 | 1119 | |
tnhnrl | 62:d502889e74f1 | 1120 | return file_size; |
tnhnrl | 62:d502889e74f1 | 1121 | } |
tnhnrl | 62:d502889e74f1 | 1122 | |
tnhnrl | 62:d502889e74f1 | 1123 | int MbedLogger::debugFileState() { |
tnhnrl | 62:d502889e74f1 | 1124 | pc().printf("What is _fp right now? %p\n\r", _fp); //pointer notation |
tnhnrl | 62:d502889e74f1 | 1125 | |
tnhnrl | 62:d502889e74f1 | 1126 | if (_fp) |
tnhnrl | 62:d502889e74f1 | 1127 | return 1; //file pointer does exist |
tnhnrl | 62:d502889e74f1 | 1128 | else |
tnhnrl | 62:d502889e74f1 | 1129 | return 0; //file pointer does not exist |
tnhnrl | 62:d502889e74f1 | 1130 | } |
tnhnrl | 62:d502889e74f1 | 1131 | |
tnhnrl | 62:d502889e74f1 | 1132 | void MbedLogger::specifyFileForTransmit(string input_string) { |
tnhnrl | 62:d502889e74f1 | 1133 | pc().printf("specifyFileForTransmit\n\r"); |
tnhnrl | 62:d502889e74f1 | 1134 | |
tnhnrl | 62:d502889e74f1 | 1135 | string file_string = _file_system_string + input_string; |
tnhnrl | 62:d502889e74f1 | 1136 | |
tnhnrl | 62:d502889e74f1 | 1137 | pc().printf("file_string is <%s>\n\r", file_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1138 | |
tnhnrl | 62:d502889e74f1 | 1139 | //open this file to read |
tnhnrl | 62:d502889e74f1 | 1140 | _fp = fopen(file_string.c_str(), "r"); |
tnhnrl | 62:d502889e74f1 | 1141 | |
tnhnrl | 62:d502889e74f1 | 1142 | //transmit that file |
tnhnrl | 62:d502889e74f1 | 1143 | //transmitDataWithTicker(); //replaced ticker |
tnhnrl | 62:d502889e74f1 | 1144 | |
tnhnrl | 62:d502889e74f1 | 1145 | _file_transmission = true; |
tnhnrl | 62:d502889e74f1 | 1146 | } |
tnhnrl | 62:d502889e74f1 | 1147 | |
tnhnrl | 62:d502889e74f1 | 1148 | void MbedLogger::transmitFileFromDirectory( int file_number ) { |
tnhnrl | 62:d502889e74f1 | 1149 | file_number = file_number + 1; //to get the correct number |
tnhnrl | 62:d502889e74f1 | 1150 | |
tnhnrl | 62:d502889e74f1 | 1151 | DIR *dir; |
tnhnrl | 62:d502889e74f1 | 1152 | struct dirent *dp; //dirent.h is the format of directory entries |
tnhnrl | 62:d502889e74f1 | 1153 | int log_found =0, loop=1; //start file numbers at 1 |
tnhnrl | 62:d502889e74f1 | 1154 | long int temp=0; |
tnhnrl | 62:d502889e74f1 | 1155 | |
tnhnrl | 62:d502889e74f1 | 1156 | // char * char_pointer; |
tnhnrl | 62:d502889e74f1 | 1157 | // char * numstart, *numstop; |
tnhnrl | 62:d502889e74f1 | 1158 | |
tnhnrl | 62:d502889e74f1 | 1159 | if ( NULL == (dir = opendir( _file_system_string.c_str() )) ) { |
tnhnrl | 62:d502889e74f1 | 1160 | pc().printf("MBED directory could not be opened\r\n"); |
tnhnrl | 62:d502889e74f1 | 1161 | } |
tnhnrl | 62:d502889e74f1 | 1162 | else |
tnhnrl | 62:d502889e74f1 | 1163 | { |
tnhnrl | 62:d502889e74f1 | 1164 | while ( NULL != (dp = readdir( dir )) ) |
tnhnrl | 62:d502889e74f1 | 1165 | { |
tnhnrl | 62:d502889e74f1 | 1166 | pc().printf( "%d. %s (log file: %d, %d)\r\n", loop, dp->d_name,log_found,temp); |
tnhnrl | 62:d502889e74f1 | 1167 | |
tnhnrl | 62:d502889e74f1 | 1168 | //process current file if it matches the file number |
tnhnrl | 62:d502889e74f1 | 1169 | if (file_number == loop) { |
tnhnrl | 62:d502889e74f1 | 1170 | char * current_file_name = dp->d_name; //pointer to this char array |
tnhnrl | 62:d502889e74f1 | 1171 | |
tnhnrl | 62:d502889e74f1 | 1172 | specifyFileForTransmit(current_file_name); |
tnhnrl | 62:d502889e74f1 | 1173 | |
tnhnrl | 62:d502889e74f1 | 1174 | break; |
tnhnrl | 62:d502889e74f1 | 1175 | } |
tnhnrl | 62:d502889e74f1 | 1176 | |
tnhnrl | 62:d502889e74f1 | 1177 | loop++; |
tnhnrl | 62:d502889e74f1 | 1178 | } |
tnhnrl | 62:d502889e74f1 | 1179 | } |
tnhnrl | 62:d502889e74f1 | 1180 | } |
tnhnrl | 62:d502889e74f1 | 1181 | |
tnhnrl | 62:d502889e74f1 | 1182 | void MbedLogger::accessMbedDirectory() { |
tnhnrl | 62:d502889e74f1 | 1183 | printMbedDirectory(); |
tnhnrl | 62:d502889e74f1 | 1184 | |
tnhnrl | 62:d502889e74f1 | 1185 | pc().printf("Type in the number of the file you want to transmit.\n\r"); |
tnhnrl | 62:d502889e74f1 | 1186 | |
tnhnrl | 62:d502889e74f1 | 1187 | char message[42]; |
tnhnrl | 62:d502889e74f1 | 1188 | |
tnhnrl | 62:d502889e74f1 | 1189 | pc().scanf("%41s", message); |
tnhnrl | 62:d502889e74f1 | 1190 | |
tnhnrl | 62:d502889e74f1 | 1191 | pc().printf("Input received!\n\r"); |
tnhnrl | 62:d502889e74f1 | 1192 | |
tnhnrl | 62:d502889e74f1 | 1193 | //check if char array is an integer |
tnhnrl | 62:d502889e74f1 | 1194 | char* conversion_pointer; |
tnhnrl | 62:d502889e74f1 | 1195 | long converted = strtol(message, &conversion_pointer, 10); |
tnhnrl | 62:d502889e74f1 | 1196 | |
tnhnrl | 62:d502889e74f1 | 1197 | if (*conversion_pointer) { |
tnhnrl | 62:d502889e74f1 | 1198 | //conversion failed because the input was not a number |
tnhnrl | 62:d502889e74f1 | 1199 | pc().printf("NOT A VALID FILE NUMBER!\n\r"); |
tnhnrl | 62:d502889e74f1 | 1200 | } |
tnhnrl | 62:d502889e74f1 | 1201 | else { |
tnhnrl | 62:d502889e74f1 | 1202 | //conversion worked! |
tnhnrl | 62:d502889e74f1 | 1203 | pc().printf("You chose file number: %d\n\r", converted); |
tnhnrl | 62:d502889e74f1 | 1204 | |
tnhnrl | 62:d502889e74f1 | 1205 | // transmit the file |
tnhnrl | 62:d502889e74f1 | 1206 | transmitFileFromDirectory(converted); |
tnhnrl | 62:d502889e74f1 | 1207 | } |
tnhnrl | 62:d502889e74f1 | 1208 | } |
tnhnrl | 62:d502889e74f1 | 1209 | |
tnhnrl | 62:d502889e74f1 | 1210 | void MbedLogger::closeLogFile() { |
tnhnrl | 62:d502889e74f1 | 1211 | if (_fp == NULL){ |
tnhnrl | 62:d502889e74f1 | 1212 | pc().printf("MbedLogger: (%s) LOG FILE WAS ALREADY CLOSED!\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1213 | } |
tnhnrl | 62:d502889e74f1 | 1214 | |
tnhnrl | 62:d502889e74f1 | 1215 | else { |
tnhnrl | 62:d502889e74f1 | 1216 | pc().printf("MbedLogger: (%s) CLOSING LOG FILE!\n\r", _file_system_string.c_str()); |
tnhnrl | 62:d502889e74f1 | 1217 | |
tnhnrl | 62:d502889e74f1 | 1218 | //close file |
tnhnrl | 62:d502889e74f1 | 1219 | fclose(_fp); |
tnhnrl | 62:d502889e74f1 | 1220 | |
tnhnrl | 62:d502889e74f1 | 1221 | _fp = NULL; //set pointer to zero |
tnhnrl | 62:d502889e74f1 | 1222 | } |
tnhnrl | 62:d502889e74f1 | 1223 | } |
tnhnrl | 62:d502889e74f1 | 1224 | |
tnhnrl | 62:d502889e74f1 | 1225 | void MbedLogger::activateReceivePacket() { |
tnhnrl | 62:d502889e74f1 | 1226 | _mbed_receive_loop = true; |
tnhnrl | 62:d502889e74f1 | 1227 | } |
tnhnrl | 62:d502889e74f1 | 1228 | |
tnhnrl | 62:d502889e74f1 | 1229 | void MbedLogger::receiveMissionDataWithFSM() { |
tnhnrl | 62:d502889e74f1 | 1230 | led4() = !led4(); |
tnhnrl | 62:d502889e74f1 | 1231 | |
tnhnrl | 62:d502889e74f1 | 1232 | checkForIncomingData(); |
tnhnrl | 62:d502889e74f1 | 1233 | |
tnhnrl | 62:d502889e74f1 | 1234 | // Example: send reply 75 65 00 00 |
tnhnrl | 62:d502889e74f1 | 1235 | // Example: send reply 75 65 00 01 |
tnhnrl | 62:d502889e74f1 | 1236 | } |
tnhnrl | 62:d502889e74f1 | 1237 | |
tnhnrl | 62:d502889e74f1 | 1238 | void MbedLogger::receiveMissionDataWithTicker() { |
tnhnrl | 62:d502889e74f1 | 1239 | openNewMissionFile(); //sequence.txt file opened |
tnhnrl | 62:d502889e74f1 | 1240 | |
tnhnrl | 62:d502889e74f1 | 1241 | _mbed_receive_ticker.attach(callback(this, &MbedLogger::activateReceivePacket), 0.5); |
tnhnrl | 62:d502889e74f1 | 1242 | |
tnhnrl | 62:d502889e74f1 | 1243 | pc().printf("\n\r02/09/2018 MbedLogger receiveMissionData Beginning to receive sequence data...\n\r"); |
tnhnrl | 62:d502889e74f1 | 1244 | |
tnhnrl | 62:d502889e74f1 | 1245 | resetReplyPacket(); //reset the reply packet |
tnhnrl | 62:d502889e74f1 | 1246 | |
tnhnrl | 62:d502889e74f1 | 1247 | //idea for stopping this if data not being received |
tnhnrl | 62:d502889e74f1 | 1248 | int current_packet_number = 0; |
tnhnrl | 62:d502889e74f1 | 1249 | int last_packet_number = -1; |
tnhnrl | 62:d502889e74f1 | 1250 | int break_transmission = 0; |
tnhnrl | 62:d502889e74f1 | 1251 | |
tnhnrl | 62:d502889e74f1 | 1252 | while(1) { |
tnhnrl | 62:d502889e74f1 | 1253 | //runs at 10 hz |
tnhnrl | 62:d502889e74f1 | 1254 | if (_mbed_receive_loop) { |
tnhnrl | 62:d502889e74f1 | 1255 | if (!checkForIncomingData()) { // run this until it finishes |
tnhnrl | 62:d502889e74f1 | 1256 | //when you complete data reception, this will become false |
tnhnrl | 62:d502889e74f1 | 1257 | pc().printf("\n\rMbedLogger: Data RECEPTION complete.\n\r"); |
tnhnrl | 62:d502889e74f1 | 1258 | _mbed_receive_ticker.detach(); |
tnhnrl | 62:d502889e74f1 | 1259 | break; |
tnhnrl | 62:d502889e74f1 | 1260 | } |
tnhnrl | 62:d502889e74f1 | 1261 | else { |
tnhnrl | 62:d502889e74f1 | 1262 | |
tnhnrl | 62:d502889e74f1 | 1263 | // SEND REPLY 75 65 00 00 |
tnhnrl | 62:d502889e74f1 | 1264 | // SEND REPLY 75 65 00 01 |
tnhnrl | 62:d502889e74f1 | 1265 | |
tnhnrl | 62:d502889e74f1 | 1266 | //check if you keep getting the same thing |
tnhnrl | 62:d502889e74f1 | 1267 | current_packet_number = sendReply(); |
tnhnrl | 62:d502889e74f1 | 1268 | |
tnhnrl | 62:d502889e74f1 | 1269 | //pc().printf("DEBUG: current packet number %d (last packet number %d) \n\r", current_packet_number, last_packet_number); //debug |
tnhnrl | 62:d502889e74f1 | 1270 | |
tnhnrl | 62:d502889e74f1 | 1271 | //let this count up a few times before it exits |
tnhnrl | 62:d502889e74f1 | 1272 | if (current_packet_number == last_packet_number) { |
tnhnrl | 62:d502889e74f1 | 1273 | break_transmission++; |
tnhnrl | 62:d502889e74f1 | 1274 | |
tnhnrl | 62:d502889e74f1 | 1275 | //break transmission after 50 failed attempts (was 100) |
tnhnrl | 62:d502889e74f1 | 1276 | if (break_transmission >= 50) { |
tnhnrl | 62:d502889e74f1 | 1277 | closeIncompleteFile(); //close the file |
tnhnrl | 62:d502889e74f1 | 1278 | _mbed_receive_ticker.detach(); |
tnhnrl | 62:d502889e74f1 | 1279 | pc().printf("MbedLogger: TRANSMISSION INTERRUPTED!\n\r"); |
tnhnrl | 62:d502889e74f1 | 1280 | break; |
tnhnrl | 62:d502889e74f1 | 1281 | } |
tnhnrl | 62:d502889e74f1 | 1282 | } |
tnhnrl | 62:d502889e74f1 | 1283 | else |
tnhnrl | 62:d502889e74f1 | 1284 | last_packet_number = current_packet_number; |
tnhnrl | 62:d502889e74f1 | 1285 | } |
tnhnrl | 62:d502889e74f1 | 1286 | _mbed_receive_loop = false; // wait until the loop rate timer fires again |
tnhnrl | 62:d502889e74f1 | 1287 | } |
tnhnrl | 62:d502889e74f1 | 1288 | } |
tnhnrl | 62:d502889e74f1 | 1289 | } |
tnhnrl | 62:d502889e74f1 | 1290 | |
tnhnrl | 62:d502889e74f1 | 1291 | int MbedLogger::getFilePointerState() { |
tnhnrl | 62:d502889e74f1 | 1292 | if (_fp) |
tnhnrl | 62:d502889e74f1 | 1293 | return 1; //file is not null (has an address), open |
tnhnrl | 62:d502889e74f1 | 1294 | else |
tnhnrl | 62:d502889e74f1 | 1295 | return 0; //file is null, closed |
tnhnrl | 62:d502889e74f1 | 1296 | } |
tnhnrl | 62:d502889e74f1 | 1297 | |
tnhnrl | 62:d502889e74f1 | 1298 | void MbedLogger::testToggleFilePointer() { |
tnhnrl | 62:d502889e74f1 | 1299 | pc().printf("testToggleFilePointer\n\r"); |
tnhnrl | 62:d502889e74f1 | 1300 | |
tnhnrl | 62:d502889e74f1 | 1301 | string file_name_string = _file_system_string + "example99.txt"; |
tnhnrl | 62:d502889e74f1 | 1302 | |
tnhnrl | 62:d502889e74f1 | 1303 | if (_fp) |
tnhnrl | 62:d502889e74f1 | 1304 | _fp = NULL; |
tnhnrl | 62:d502889e74f1 | 1305 | else |
tnhnrl | 62:d502889e74f1 | 1306 | _fp = fopen(file_name_string.c_str(), "w"); |
tnhnrl | 62:d502889e74f1 | 1307 | } |
tnhnrl | 62:d502889e74f1 | 1308 | |
tnhnrl | 62:d502889e74f1 | 1309 | int MbedLogger::getNumberOfPackets() { |
tnhnrl | 62:d502889e74f1 | 1310 | return _total_number_of_packets; |
tnhnrl | 62:d502889e74f1 | 1311 | } |
tnhnrl | 62:d502889e74f1 | 1312 | |
tnhnrl | 62:d502889e74f1 | 1313 | int MbedLogger::currentPacketNumber() { |
tnhnrl | 62:d502889e74f1 | 1314 | return _packet_number; |
tnhnrl | 62:d502889e74f1 | 1315 | } |
tnhnrl | 62:d502889e74f1 | 1316 | |
tnhnrl | 62:d502889e74f1 | 1317 | //only do this for the MBED because of the limited file size |
tnhnrl | 62:d502889e74f1 | 1318 | //write one line to the file (open to write, this will erase all other data) and close it. |
tnhnrl | 62:d502889e74f1 | 1319 | void MbedLogger::eraseFile() { |
tnhnrl | 62:d502889e74f1 | 1320 | _fp = fopen(_full_file_path_string.c_str(), "w"); // LOG000.csv |
tnhnrl | 62:d502889e74f1 | 1321 | |
tnhnrl | 62:d502889e74f1 | 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\n(file erased)\n"); |
tnhnrl | 62:d502889e74f1 | 1323 | |
tnhnrl | 62:d502889e74f1 | 1324 | closeLogFile(); |
tnhnrl | 62:d502889e74f1 | 1325 | } |
tnhnrl | 62:d502889e74f1 | 1326 | |
tnhnrl | 62:d502889e74f1 | 1327 | void MbedLogger::intCreateDataPacket(int data_buffer[],int payload_length) { |
tnhnrl | 62:d502889e74f1 | 1328 | // packet is 7565 0001 FFFF EEEE CC DATA DATA DATA ... CRC1 CRC2 |
tnhnrl | 62:d502889e74f1 | 1329 | |
tnhnrl | 62:d502889e74f1 | 1330 | //check here https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=01c0 |
tnhnrl | 62:d502889e74f1 | 1331 | // ieee 754: http://www6.uniovi.es/~antonio/uned/ieee754/IEEE-754hex32.html |
tnhnrl | 62:d502889e74f1 | 1332 | |
tnhnrl | 62:d502889e74f1 | 1333 | //CLEAR: Removes all elements from the vector (which are destroyed), leaving the container with a size of 0. |
tnhnrl | 62:d502889e74f1 | 1334 | _data_packet.clear(); |
tnhnrl | 62:d502889e74f1 | 1335 | |
tnhnrl | 62:d502889e74f1 | 1336 | //DATA PACKET HEADER |
tnhnrl | 62:d502889e74f1 | 1337 | _data_packet.push_back(117); //0x75 |
tnhnrl | 62:d502889e74f1 | 1338 | _data_packet.push_back(101); //0x65 |
tnhnrl | 62:d502889e74f1 | 1339 | |
tnhnrl | 62:d502889e74f1 | 1340 | _data_packet.push_back(_packet_number/256); //current packet number in 0x#### form |
tnhnrl | 62:d502889e74f1 | 1341 | _data_packet.push_back(_packet_number%256); //current packet number in 0x#### form |
tnhnrl | 62:d502889e74f1 | 1342 | |
tnhnrl | 62:d502889e74f1 | 1343 | _data_packet.push_back(_total_number_of_packets/256); //total number of packets, 0x#### form |
tnhnrl | 62:d502889e74f1 | 1344 | _data_packet.push_back(_total_number_of_packets%256); //total number of packets, 0x#### form |
tnhnrl | 62:d502889e74f1 | 1345 | |
tnhnrl | 62:d502889e74f1 | 1346 | _data_packet.push_back(_current_line_length); |
tnhnrl | 62:d502889e74f1 | 1347 | |
tnhnrl | 62:d502889e74f1 | 1348 | //DATA FROM INTEGER ARRAY (read the array) |
tnhnrl | 62:d502889e74f1 | 1349 | for (int i = 0; i < payload_length; i++) { |
tnhnrl | 62:d502889e74f1 | 1350 | _data_packet.push_back(data_buffer[i]); |
tnhnrl | 62:d502889e74f1 | 1351 | } |
tnhnrl | 62:d502889e74f1 | 1352 | |
tnhnrl | 62:d502889e74f1 | 1353 | //CRC CALCULATIONS BELOW, character version of calculation screws up on null character 0x00, scrapped and using vector of integers |
tnhnrl | 62:d502889e74f1 | 1354 | int crc_one = calcCrcOne(); |
tnhnrl | 62:d502889e74f1 | 1355 | int crc_two = calcCrcTwo(); |
tnhnrl | 62:d502889e74f1 | 1356 | |
tnhnrl | 62:d502889e74f1 | 1357 | //place the crc bytes into the data packet that is transmitted |
tnhnrl | 62:d502889e74f1 | 1358 | _data_packet.push_back(crc_one); |
tnhnrl | 62:d502889e74f1 | 1359 | _data_packet.push_back(crc_two); |
tnhnrl | 62:d502889e74f1 | 1360 | } |
tnhnrl | 62:d502889e74f1 | 1361 | |
tnhnrl | 62:d502889e74f1 | 1362 | void MbedLogger::sendStatus() { |
tnhnrl | 62:d502889e74f1 | 1363 | led3() = !led3(); |
tnhnrl | 62:d502889e74f1 | 1364 | //readPacketInSeries() //uses this functionality |
tnhnrl | 62:d502889e74f1 | 1365 | |
tnhnrl | 62:d502889e74f1 | 1366 | //get data depth, pitch, heading, timer |
tnhnrl | 62:d502889e74f1 | 1367 | |
tnhnrl | 62:d502889e74f1 | 1368 | //store the data as a hex char array |
tnhnrl | 62:d502889e74f1 | 1369 | char hex_char_depth[256]; |
tnhnrl | 62:d502889e74f1 | 1370 | char hex_char_pitch[256]; |
tnhnrl | 62:d502889e74f1 | 1371 | char hex_char_heading[256]; |
tnhnrl | 62:d502889e74f1 | 1372 | char hex_char_timer[256]; |
tnhnrl | 62:d502889e74f1 | 1373 | |
tnhnrl | 62:d502889e74f1 | 1374 | string string_hex_buffer = ""; //buffer to store the hex data as a string |
tnhnrl | 62:d502889e74f1 | 1375 | string string_temp; // ELIMINATE THIS ASAP |
tnhnrl | 62:d502889e74f1 | 1376 | |
tnhnrl | 62:d502889e74f1 | 1377 | float depth_value = depthLoop().getPosition(); |
tnhnrl | 62:d502889e74f1 | 1378 | float pitch_value = imu().getPitch(); |
tnhnrl | 62:d502889e74f1 | 1379 | float heading_value = imu().getHeading(); |
tnhnrl | 63:6cb0405fc6e6 | 1380 | //float timer_value = stateMachine().getTimer(); |
tnhnrl | 63:6cb0405fc6e6 | 1381 | float timer_value = 123.123; |
tnhnrl | 62:d502889e74f1 | 1382 | |
tnhnrl | 62:d502889e74f1 | 1383 | sprintf(hex_char_depth, "%08X" , *(unsigned int*)&depth_value); |
tnhnrl | 62:d502889e74f1 | 1384 | sprintf(hex_char_pitch, "%08X" , *(unsigned int*)&pitch_value); |
tnhnrl | 62:d502889e74f1 | 1385 | sprintf(hex_char_heading, "%08X" , *(unsigned int*)&heading_value); |
tnhnrl | 62:d502889e74f1 | 1386 | sprintf(hex_char_timer, "%08X" , *(unsigned int*)&timer_value); |
tnhnrl | 62:d502889e74f1 | 1387 | |
tnhnrl | 62:d502889e74f1 | 1388 | //store the integer values |
tnhnrl | 62:d502889e74f1 | 1389 | int hex_to_int[128]; |
tnhnrl | 62:d502889e74f1 | 1390 | |
tnhnrl | 62:d502889e74f1 | 1391 | //read through hex string and break apart into chunks and save the decimal values |
tnhnrl | 62:d502889e74f1 | 1392 | int hex_counter = 0; |
tnhnrl | 62:d502889e74f1 | 1393 | |
tnhnrl | 62:d502889e74f1 | 1394 | ////////////////////////// |
tnhnrl | 62:d502889e74f1 | 1395 | string_hex_buffer = hex_char_depth; |
tnhnrl | 62:d502889e74f1 | 1396 | |
tnhnrl | 62:d502889e74f1 | 1397 | for (int i = 0; i < 8; i = i+2) { |
tnhnrl | 62:d502889e74f1 | 1398 | //substring starts at x substr(x, length of substring) |
tnhnrl | 62:d502889e74f1 | 1399 | string_temp = string_hex_buffer.substr(i,2); // get substring from string (converted from char array) |
tnhnrl | 62:d502889e74f1 | 1400 | |
tnhnrl | 62:d502889e74f1 | 1401 | //convoluted, fix this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
tnhnrl | 62:d502889e74f1 | 1402 | // copying the contents of the string to char array |
tnhnrl | 62:d502889e74f1 | 1403 | char temp_char_array[256]; |
tnhnrl | 62:d502889e74f1 | 1404 | strcpy(temp_char_array, string_temp.c_str()); |
tnhnrl | 62:d502889e74f1 | 1405 | |
tnhnrl | 62:d502889e74f1 | 1406 | //store char array hex value as decimal value |
tnhnrl | 62:d502889e74f1 | 1407 | int decimal_value; |
tnhnrl | 62:d502889e74f1 | 1408 | sscanf(temp_char_array,"%x",&decimal_value); |
tnhnrl | 62:d502889e74f1 | 1409 | |
tnhnrl | 62:d502889e74f1 | 1410 | hex_to_int[hex_counter] = decimal_value; |
tnhnrl | 62:d502889e74f1 | 1411 | hex_counter++; //iterate to keep up with for loop |
tnhnrl | 62:d502889e74f1 | 1412 | |
tnhnrl | 62:d502889e74f1 | 1413 | //clear the string each time |
tnhnrl | 62:d502889e74f1 | 1414 | //string_temp = ""; |
tnhnrl | 62:d502889e74f1 | 1415 | //cout << "string_temp CLEARED? is " << string_temp << endl; |
tnhnrl | 62:d502889e74f1 | 1416 | //cout << "string_temp is " << string_temp << " and the value is " << decimal_value << endl; |
tnhnrl | 62:d502889e74f1 | 1417 | } |
tnhnrl | 62:d502889e74f1 | 1418 | |
tnhnrl | 62:d502889e74f1 | 1419 | string_hex_buffer = hex_char_pitch; |
tnhnrl | 62:d502889e74f1 | 1420 | |
tnhnrl | 62:d502889e74f1 | 1421 | for (int i = 0; i < 8; i = i+2) { |
tnhnrl | 62:d502889e74f1 | 1422 | //substring starts at x substr(x, length of substring) |
tnhnrl | 62:d502889e74f1 | 1423 | string_temp = string_hex_buffer.substr(i,2); |
tnhnrl | 62:d502889e74f1 | 1424 | |
tnhnrl | 62:d502889e74f1 | 1425 | //convoluted, fix this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
tnhnrl | 62:d502889e74f1 | 1426 | // copying the contents of the string to char array |
tnhnrl | 62:d502889e74f1 | 1427 | char temp_char_array[256]; |
tnhnrl | 62:d502889e74f1 | 1428 | strcpy(temp_char_array, string_temp.c_str()); |
tnhnrl | 62:d502889e74f1 | 1429 | |
tnhnrl | 62:d502889e74f1 | 1430 | //store char array hex value as decimal value |
tnhnrl | 62:d502889e74f1 | 1431 | int decimal_value; |
tnhnrl | 62:d502889e74f1 | 1432 | sscanf(temp_char_array,"%x",&decimal_value); |
tnhnrl | 62:d502889e74f1 | 1433 | |
tnhnrl | 62:d502889e74f1 | 1434 | hex_to_int[hex_counter] = decimal_value; |
tnhnrl | 62:d502889e74f1 | 1435 | hex_counter++; //iterate to keep up with for loop |
tnhnrl | 62:d502889e74f1 | 1436 | |
tnhnrl | 62:d502889e74f1 | 1437 | //clear the string each time |
tnhnrl | 62:d502889e74f1 | 1438 | //string_temp = ""; |
tnhnrl | 62:d502889e74f1 | 1439 | //cout << "string_temp CLEARED? is " << string_temp << endl; |
tnhnrl | 62:d502889e74f1 | 1440 | //cout << "string_temp is " << string_temp << " and the value is " << decimal_value << endl; |
tnhnrl | 62:d502889e74f1 | 1441 | } |
tnhnrl | 62:d502889e74f1 | 1442 | |
tnhnrl | 62:d502889e74f1 | 1443 | string_hex_buffer = hex_char_heading; |
tnhnrl | 62:d502889e74f1 | 1444 | |
tnhnrl | 62:d502889e74f1 | 1445 | for (int i = 0; i < 8; i = i+2) { |
tnhnrl | 62:d502889e74f1 | 1446 | //substring starts at x substr(x, length of substring) |
tnhnrl | 62:d502889e74f1 | 1447 | string_temp = string_hex_buffer.substr(i,2); |
tnhnrl | 62:d502889e74f1 | 1448 | |
tnhnrl | 62:d502889e74f1 | 1449 | //convoluted, fix this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
tnhnrl | 62:d502889e74f1 | 1450 | // copying the contents of the string to char array |
tnhnrl | 62:d502889e74f1 | 1451 | char temp_char_array[256]; |
tnhnrl | 62:d502889e74f1 | 1452 | strcpy(temp_char_array, string_temp.c_str()); |
tnhnrl | 62:d502889e74f1 | 1453 | |
tnhnrl | 62:d502889e74f1 | 1454 | //store char array hex value as decimal value |
tnhnrl | 62:d502889e74f1 | 1455 | int decimal_value; |
tnhnrl | 62:d502889e74f1 | 1456 | sscanf(temp_char_array,"%x",&decimal_value); |
tnhnrl | 62:d502889e74f1 | 1457 | |
tnhnrl | 62:d502889e74f1 | 1458 | hex_to_int[hex_counter] = decimal_value; |
tnhnrl | 62:d502889e74f1 | 1459 | hex_counter++; //iterate to keep up with for loop |
tnhnrl | 62:d502889e74f1 | 1460 | |
tnhnrl | 62:d502889e74f1 | 1461 | //clear the string each time |
tnhnrl | 62:d502889e74f1 | 1462 | //string_temp = ""; |
tnhnrl | 62:d502889e74f1 | 1463 | //cout << "string_temp CLEARED? is " << string_temp << endl; |
tnhnrl | 62:d502889e74f1 | 1464 | //cout << "string_temp is " << string_temp << " and the value is " << decimal_value << endl; |
tnhnrl | 62:d502889e74f1 | 1465 | } |
tnhnrl | 62:d502889e74f1 | 1466 | |
tnhnrl | 62:d502889e74f1 | 1467 | string_hex_buffer = hex_char_timer; |
tnhnrl | 62:d502889e74f1 | 1468 | |
tnhnrl | 62:d502889e74f1 | 1469 | for (int i = 0; i < 8; i = i+2) { |
tnhnrl | 62:d502889e74f1 | 1470 | //substring starts at x substr(x, length of substring) |
tnhnrl | 62:d502889e74f1 | 1471 | string_temp = string_hex_buffer.substr(i,2); |
tnhnrl | 62:d502889e74f1 | 1472 | |
tnhnrl | 62:d502889e74f1 | 1473 | //convoluted, fix this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
tnhnrl | 62:d502889e74f1 | 1474 | // copying the contents of the string to char array |
tnhnrl | 62:d502889e74f1 | 1475 | char temp_char_array[256]; |
tnhnrl | 62:d502889e74f1 | 1476 | strcpy(temp_char_array, string_temp.c_str()); |
tnhnrl | 62:d502889e74f1 | 1477 | |
tnhnrl | 62:d502889e74f1 | 1478 | //store char array hex value as decimal value |
tnhnrl | 62:d502889e74f1 | 1479 | int decimal_value; |
tnhnrl | 62:d502889e74f1 | 1480 | sscanf(temp_char_array,"%x",&decimal_value); |
tnhnrl | 62:d502889e74f1 | 1481 | |
tnhnrl | 62:d502889e74f1 | 1482 | hex_to_int[hex_counter] = decimal_value; |
tnhnrl | 62:d502889e74f1 | 1483 | hex_counter++; //iterate to keep up with for loop |
tnhnrl | 62:d502889e74f1 | 1484 | |
tnhnrl | 62:d502889e74f1 | 1485 | //clear the string each time |
tnhnrl | 62:d502889e74f1 | 1486 | //string_temp = ""; |
tnhnrl | 62:d502889e74f1 | 1487 | //cout << "string_temp CLEARED? is " << string_temp << endl; |
tnhnrl | 62:d502889e74f1 | 1488 | //cout << "string_temp is " << string_temp << " and the value is " << decimal_value << endl; |
tnhnrl | 62:d502889e74f1 | 1489 | } |
tnhnrl | 62:d502889e74f1 | 1490 | ////////////////////////// |
tnhnrl | 62:d502889e74f1 | 1491 | |
tnhnrl | 62:d502889e74f1 | 1492 | //create new packet based on int array |
tnhnrl | 62:d502889e74f1 | 1493 | _packet_number = 1; // TEST, FIX THIS |
tnhnrl | 62:d502889e74f1 | 1494 | _total_number_of_packets = 1; |
tnhnrl | 62:d502889e74f1 | 1495 | _current_line_length = 16; |
tnhnrl | 62:d502889e74f1 | 1496 | |
tnhnrl | 62:d502889e74f1 | 1497 | intCreateDataPacket(hex_to_int,16); |
tnhnrl | 62:d502889e74f1 | 1498 | |
tnhnrl | 62:d502889e74f1 | 1499 | |
tnhnrl | 62:d502889e74f1 | 1500 | transmitDataPacket(); |
tnhnrl | 63:6cb0405fc6e6 | 1501 | } |
tnhnrl | 63:6cb0405fc6e6 | 1502 | |
tnhnrl | 63:6cb0405fc6e6 | 1503 | int MbedLogger::getLogSize() { |
tnhnrl | 63:6cb0405fc6e6 | 1504 | return _log_file_line_counter; |
tnhnrl | 62:d502889e74f1 | 1505 | } |