Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MODSERIAL FATFileSystem
MbedLogger/MbedLogger.hpp@49:47ffa4feb6db, 2018-02-15 (annotated)
- Committer:
- tnhnrl
- Date:
- Thu Feb 15 23:07:25 2018 +0000
- Revision:
- 49:47ffa4feb6db
- Parent:
- 46:030be9f5c793
Working pool-tested code. Rudder disabled (some weird coupling issue).
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tnhnrl | 46:030be9f5c793 | 1 | #ifndef MBEDLOGGER_HPP |
| tnhnrl | 46:030be9f5c793 | 2 | #define MBEDLOGGER_HPP |
| tnhnrl | 46:030be9f5c793 | 3 | |
| tnhnrl | 46:030be9f5c793 | 4 | #include "mbed.h" |
| tnhnrl | 46:030be9f5c793 | 5 | #include <string> |
| tnhnrl | 46:030be9f5c793 | 6 | using namespace std; |
| tnhnrl | 46:030be9f5c793 | 7 | |
| tnhnrl | 46:030be9f5c793 | 8 | #include <vector> |
| tnhnrl | 46:030be9f5c793 | 9 | |
| tnhnrl | 46:030be9f5c793 | 10 | enum { |
| tnhnrl | 46:030be9f5c793 | 11 | HEADER_117, |
| tnhnrl | 46:030be9f5c793 | 12 | HEADER_101, |
| tnhnrl | 46:030be9f5c793 | 13 | PACKET_NUM, |
| tnhnrl | 46:030be9f5c793 | 14 | TOTAL_NUM_PACKETS, |
| tnhnrl | 46:030be9f5c793 | 15 | PACKET_SIZE, |
| tnhnrl | 46:030be9f5c793 | 16 | END_TRANSMISSION, |
| tnhnrl | 46:030be9f5c793 | 17 | TRANSMIT_PACKET_1, |
| tnhnrl | 46:030be9f5c793 | 18 | TRANSMIT_PACKET_2, |
| tnhnrl | 46:030be9f5c793 | 19 | PACKET_CRC_ONE, |
| tnhnrl | 46:030be9f5c793 | 20 | PACKET_CRC_TWO, |
| tnhnrl | 46:030be9f5c793 | 21 | RANDOM_CHAR |
| tnhnrl | 46:030be9f5c793 | 22 | }; |
| tnhnrl | 46:030be9f5c793 | 23 | |
| tnhnrl | 46:030be9f5c793 | 24 | class MbedLogger { |
| tnhnrl | 46:030be9f5c793 | 25 | public: |
| tnhnrl | 46:030be9f5c793 | 26 | MbedLogger(string file_system_input_string); //constructor |
| tnhnrl | 46:030be9f5c793 | 27 | |
| tnhnrl | 46:030be9f5c793 | 28 | void createTestLog(); //test log function |
| tnhnrl | 46:030be9f5c793 | 29 | |
| tnhnrl | 46:030be9f5c793 | 30 | void appendLogFile(int current_state, int option); //check if you have orphaned file pointers before this (file should not be open already) |
| tnhnrl | 49:47ffa4feb6db | 31 | |
| tnhnrl | 46:030be9f5c793 | 32 | //save float array to file (append each time) |
| tnhnrl | 46:030be9f5c793 | 33 | void saveDataToFile(int input_state, float *input); |
| tnhnrl | 46:030be9f5c793 | 34 | |
| tnhnrl | 46:030be9f5c793 | 35 | //REVISED METHOD to save data (same as OpenLog) |
| tnhnrl | 46:030be9f5c793 | 36 | void recordData(int current_state); |
| tnhnrl | 46:030be9f5c793 | 37 | |
| tnhnrl | 46:030be9f5c793 | 38 | //print the directory to the screen |
| tnhnrl | 46:030be9f5c793 | 39 | void printMbedDirectory(); |
| tnhnrl | 46:030be9f5c793 | 40 | |
| tnhnrl | 46:030be9f5c793 | 41 | //print the current MBED log file |
| tnhnrl | 46:030be9f5c793 | 42 | void printCurrentLogFile(); |
| tnhnrl | 46:030be9f5c793 | 43 | |
| tnhnrl | 46:030be9f5c793 | 44 | //open file for transmit |
| tnhnrl | 46:030be9f5c793 | 45 | void openFileForTransmit(); |
| tnhnrl | 46:030be9f5c793 | 46 | |
| tnhnrl | 46:030be9f5c793 | 47 | //transmit data when receive confirmation |
| tnhnrl | 46:030be9f5c793 | 48 | void transmitCurrentLogFileLine(bool next_line); |
| tnhnrl | 46:030be9f5c793 | 49 | |
| tnhnrl | 46:030be9f5c793 | 50 | //transmit data when receive confirmation |
| tnhnrl | 46:030be9f5c793 | 51 | //void transmitCurrentLogFile(); |
| tnhnrl | 46:030be9f5c793 | 52 | |
| tnhnrl | 46:030be9f5c793 | 53 | // Create the data packet |
| tnhnrl | 46:030be9f5c793 | 54 | void createDataPacket(); |
| tnhnrl | 46:030be9f5c793 | 55 | |
| tnhnrl | 46:030be9f5c793 | 56 | // Transmit the data packet |
| tnhnrl | 46:030be9f5c793 | 57 | void transmitDataPacket(); |
| tnhnrl | 46:030be9f5c793 | 58 | |
| tnhnrl | 46:030be9f5c793 | 59 | // read transmit packet from Python program |
| tnhnrl | 46:030be9f5c793 | 60 | int readTransmitPacket(); |
| tnhnrl | 46:030be9f5c793 | 61 | |
| tnhnrl | 46:030be9f5c793 | 62 | // REOPEN LINE READER |
| tnhnrl | 46:030be9f5c793 | 63 | void reOpenLineReader(); |
| tnhnrl | 46:030be9f5c793 | 64 | |
| tnhnrl | 46:030be9f5c793 | 65 | // OPEN LINE READER |
| tnhnrl | 46:030be9f5c793 | 66 | bool openLineReader(); |
| tnhnrl | 46:030be9f5c793 | 67 | |
| tnhnrl | 46:030be9f5c793 | 68 | //read packet |
| tnhnrl | 46:030be9f5c793 | 69 | void readPacketInSeries(); |
| tnhnrl | 46:030be9f5c793 | 70 | |
| tnhnrl | 46:030be9f5c793 | 71 | //get the number of packets |
| tnhnrl | 46:030be9f5c793 | 72 | void getNumberOfPacketsInCurrentLog(); |
| tnhnrl | 46:030be9f5c793 | 73 | |
| tnhnrl | 46:030be9f5c793 | 74 | // END TRANSMISSION AND CLOSE FILE |
| tnhnrl | 46:030be9f5c793 | 75 | void endTransmissionCloseFile(); |
| tnhnrl | 46:030be9f5c793 | 76 | |
| tnhnrl | 46:030be9f5c793 | 77 | //open a file to write to it |
| tnhnrl | 46:030be9f5c793 | 78 | void openWriteFile(); |
| tnhnrl | 46:030be9f5c793 | 79 | |
| tnhnrl | 46:030be9f5c793 | 80 | bool checkForIncomingData(); |
| tnhnrl | 46:030be9f5c793 | 81 | |
| tnhnrl | 46:030be9f5c793 | 82 | void endReceiveData(); |
| tnhnrl | 46:030be9f5c793 | 83 | |
| tnhnrl | 46:030be9f5c793 | 84 | void copyFileExample(); |
| tnhnrl | 46:030be9f5c793 | 85 | |
| tnhnrl | 46:030be9f5c793 | 86 | int calcCrcOne(); //used only when creating the data packet, apparently you need to pass array size to function |
| tnhnrl | 46:030be9f5c793 | 87 | |
| tnhnrl | 46:030be9f5c793 | 88 | int calcCrcTwo(); //used only when creating the data packet, apparently you need to pass array size to function |
| tnhnrl | 46:030be9f5c793 | 89 | |
| tnhnrl | 46:030be9f5c793 | 90 | int calcCrcOneString (string string_input); |
| tnhnrl | 46:030be9f5c793 | 91 | |
| tnhnrl | 46:030be9f5c793 | 92 | int calcCrcTwoString (string string_input); |
| tnhnrl | 46:030be9f5c793 | 93 | |
| tnhnrl | 46:030be9f5c793 | 94 | //NEW for logging actual time |
| tnhnrl | 46:030be9f5c793 | 95 | void setLogTime(); //set a local time |
| tnhnrl | 46:030be9f5c793 | 96 | |
| tnhnrl | 46:030be9f5c793 | 97 | int getSystemTime(); //parse the time to record to the log file |
| tnhnrl | 46:030be9f5c793 | 98 | |
| tnhnrl | 46:030be9f5c793 | 99 | //NEW |
| tnhnrl | 46:030be9f5c793 | 100 | int sendReply(); |
| tnhnrl | 46:030be9f5c793 | 101 | void sendReplyPacketNumber(int packet_number); |
| tnhnrl | 46:030be9f5c793 | 102 | |
| tnhnrl | 46:030be9f5c793 | 103 | void resetReplyPacket(); |
| tnhnrl | 46:030be9f5c793 | 104 | |
| tnhnrl | 46:030be9f5c793 | 105 | void openNewMissionFile(); |
| tnhnrl | 46:030be9f5c793 | 106 | |
| tnhnrl | 46:030be9f5c793 | 107 | void setDataCounter(int input_counter); |
| tnhnrl | 46:030be9f5c793 | 108 | |
| tnhnrl | 46:030be9f5c793 | 109 | void closeIncompleteFile(); |
| tnhnrl | 46:030be9f5c793 | 110 | |
| tnhnrl | 46:030be9f5c793 | 111 | void initializeLogFile(); |
| tnhnrl | 46:030be9f5c793 | 112 | |
| tnhnrl | 46:030be9f5c793 | 113 | int fileTransmitState(); |
| tnhnrl | 46:030be9f5c793 | 114 | |
| tnhnrl | 46:030be9f5c793 | 115 | int testGetFileSize(); |
| tnhnrl | 46:030be9f5c793 | 116 | |
| tnhnrl | 46:030be9f5c793 | 117 | int getFileSize(string filename); |
| tnhnrl | 46:030be9f5c793 | 118 | |
| tnhnrl | 46:030be9f5c793 | 119 | void createEmptyLog(); |
| tnhnrl | 46:030be9f5c793 | 120 | |
| tnhnrl | 46:030be9f5c793 | 121 | int debugFileState(); |
| tnhnrl | 46:030be9f5c793 | 122 | |
| tnhnrl | 46:030be9f5c793 | 123 | //TICKER AND FUNCTION TO TRANSMIT DATA |
| tnhnrl | 46:030be9f5c793 | 124 | void activateTransmitPacket(); |
| tnhnrl | 46:030be9f5c793 | 125 | void transmitDataWithTicker(); |
| tnhnrl | 46:030be9f5c793 | 126 | |
| tnhnrl | 46:030be9f5c793 | 127 | //TICKER AND FUNCTION TO RECEIVE DATA |
| tnhnrl | 46:030be9f5c793 | 128 | void activateReceivePacket(); |
| tnhnrl | 46:030be9f5c793 | 129 | void receiveMissionDataWithTicker(); |
| tnhnrl | 46:030be9f5c793 | 130 | |
| tnhnrl | 46:030be9f5c793 | 131 | void closeLogFile(); //this sets pointer to null and checks if it is closed otherwise |
| tnhnrl | 46:030be9f5c793 | 132 | |
| tnhnrl | 46:030be9f5c793 | 133 | void specifyFileForTransmit(string input_string); |
| tnhnrl | 46:030be9f5c793 | 134 | void transmitFileFromDirectory(int file_number); |
| tnhnrl | 46:030be9f5c793 | 135 | void accessMbedDirectory(); |
| tnhnrl | 46:030be9f5c793 | 136 | |
| tnhnrl | 46:030be9f5c793 | 137 | int getFilePointerState(); |
| tnhnrl | 46:030be9f5c793 | 138 | void testToggleFilePointer(); |
| tnhnrl | 46:030be9f5c793 | 139 | |
| tnhnrl | 46:030be9f5c793 | 140 | int getNumberOfPackets(); |
| tnhnrl | 46:030be9f5c793 | 141 | |
| tnhnrl | 46:030be9f5c793 | 142 | void readTransmitPacketOneChar(); |
| tnhnrl | 46:030be9f5c793 | 143 | |
| tnhnrl | 46:030be9f5c793 | 144 | bool isTransmissionComplete(); |
| tnhnrl | 46:030be9f5c793 | 145 | int currentPacketNumber(); |
| tnhnrl | 49:47ffa4feb6db | 146 | void eraseFile(); |
| tnhnrl | 46:030be9f5c793 | 147 | |
| tnhnrl | 46:030be9f5c793 | 148 | private: |
| tnhnrl | 46:030be9f5c793 | 149 | int _f; //file name number |
| tnhnrl | 46:030be9f5c793 | 150 | int _file_number; |
| tnhnrl | 46:030be9f5c793 | 151 | |
| tnhnrl | 49:47ffa4feb6db | 152 | char _file_name[256]; //file name placeholder |
| tnhnrl | 49:47ffa4feb6db | 153 | char _line_buffer[256]; //line buffer used to read file line by line |
| tnhnrl | 46:030be9f5c793 | 154 | |
| tnhnrl | 49:47ffa4feb6db | 155 | FILE *_fp; //the file is a class member variable |
| tnhnrl | 46:030be9f5c793 | 156 | |
| tnhnrl | 46:030be9f5c793 | 157 | vector <int> _data_packet; |
| tnhnrl | 46:030be9f5c793 | 158 | |
| tnhnrl | 46:030be9f5c793 | 159 | std::vector<int>::iterator _it; |
| tnhnrl | 46:030be9f5c793 | 160 | |
| tnhnrl | 46:030be9f5c793 | 161 | int _current_line_length; |
| tnhnrl | 46:030be9f5c793 | 162 | |
| tnhnrl | 46:030be9f5c793 | 163 | int _packet_number; |
| tnhnrl | 46:030be9f5c793 | 164 | int _total_number_of_packets; |
| tnhnrl | 46:030be9f5c793 | 165 | string _log_file_string; |
| tnhnrl | 46:030be9f5c793 | 166 | |
| tnhnrl | 46:030be9f5c793 | 167 | int _reply_byte; |
| tnhnrl | 46:030be9f5c793 | 168 | int _reply_byte2; |
| tnhnrl | 46:030be9f5c793 | 169 | int _reply_byte3; |
| tnhnrl | 46:030be9f5c793 | 170 | int _reply_byte4; |
| tnhnrl | 46:030be9f5c793 | 171 | int _previous_reply_byte; |
| tnhnrl | 46:030be9f5c793 | 172 | |
| tnhnrl | 46:030be9f5c793 | 173 | bool _file_transmission; //breaks out of loop in State Machine |
| tnhnrl | 46:030be9f5c793 | 174 | |
| tnhnrl | 46:030be9f5c793 | 175 | float _data_log[11]; //for logging all of the data from the outer and inner loops and so on |
| tnhnrl | 46:030be9f5c793 | 176 | |
| tnhnrl | 46:030be9f5c793 | 177 | int _confirmed_packet_number; |
| tnhnrl | 46:030be9f5c793 | 178 | |
| tnhnrl | 46:030be9f5c793 | 179 | bool _still_transmitting_data; |
| tnhnrl | 46:030be9f5c793 | 180 | |
| tnhnrl | 46:030be9f5c793 | 181 | int _transmit_counter; |
| tnhnrl | 46:030be9f5c793 | 182 | |
| tnhnrl | 46:030be9f5c793 | 183 | int _file_transmission_state; //needed to test out what's going on with receiver |
| tnhnrl | 46:030be9f5c793 | 184 | |
| tnhnrl | 46:030be9f5c793 | 185 | int _number_of_packets[2]; |
| tnhnrl | 46:030be9f5c793 | 186 | |
| tnhnrl | 46:030be9f5c793 | 187 | Ticker _mbed_transmit_ticker; |
| tnhnrl | 46:030be9f5c793 | 188 | Ticker _mbed_receive_ticker; |
| tnhnrl | 46:030be9f5c793 | 189 | |
| tnhnrl | 46:030be9f5c793 | 190 | volatile bool _mbed_transmit_loop; |
| tnhnrl | 46:030be9f5c793 | 191 | volatile bool _mbed_receive_loop; |
| tnhnrl | 46:030be9f5c793 | 192 | |
| tnhnrl | 46:030be9f5c793 | 193 | string _file_system_string; |
| tnhnrl | 46:030be9f5c793 | 194 | string _full_file_path_string; |
| tnhnrl | 46:030be9f5c793 | 195 | |
| tnhnrl | 46:030be9f5c793 | 196 | volatile bool _file_transmission_complete; |
| tnhnrl | 46:030be9f5c793 | 197 | }; |
| tnhnrl | 46:030be9f5c793 | 198 | |
| tnhnrl | 46:030be9f5c793 | 199 | #endif |