fuck this
Dependencies: BMP280
Serial.cpp@25:a2aedb498b27, 2018-01-10 (annotated)
- Committer:
- mwthewsey
- Date:
- Wed Jan 10 03:57:59 2018 +0000
- Revision:
- 25:a2aedb498b27
- Parent:
- 21:6e733076f49c
Final Submission
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mwthewsey | 10:261f2b69c4c7 | 1 | #include "Serial.h" |
mwthewsey | 10:261f2b69c4c7 | 2 | #include "mbed.h" |
mwthewsey | 10:261f2b69c4c7 | 3 | #include "Sampling.h" |
mwthewsey | 10:261f2b69c4c7 | 4 | #include "TimeInterface.h" |
mwthewsey | 21:6e733076f49c | 5 | #include "Logging.h" |
mwthewsey | 12:03589f1d5c30 | 6 | |
mwthewsey | 10:261f2b69c4c7 | 7 | #include <string> |
mwthewsey | 10:261f2b69c4c7 | 8 | |
mwthewsey | 10:261f2b69c4c7 | 9 | Thread SerialThread; |
mwthewsey | 10:261f2b69c4c7 | 10 | |
mwthewsey | 21:6e733076f49c | 11 | //Variables |
mwthewsey | 10:261f2b69c4c7 | 12 | int DateTimeVar[3]; |
mwthewsey | 21:6e733076f49c | 13 | bool SerialState; |
mwthewsey | 10:261f2b69c4c7 | 14 | char InputBufferText[128]; |
mwthewsey | 10:261f2b69c4c7 | 15 | unsigned short InputBufferNum; |
mwthewsey | 10:261f2b69c4c7 | 16 | unsigned short internalIndex; |
mwthewsey | 21:6e733076f49c | 17 | |
mwthewsey | 21:6e733076f49c | 18 | //float tempReadingsSerial[BUFFERSIZE] = {}; |
mwthewsey | 21:6e733076f49c | 19 | //float presReadingsSerial[BUFFERSIZE] = {}; |
mwthewsey | 21:6e733076f49c | 20 | //float LDRReadingsSerial[BUFFERSIZE] = {}; |
mwthewsey | 21:6e733076f49c | 21 | //time_t timeReadingsSerial[BUFFERSIZE] = {}; |
mwthewsey | 10:261f2b69c4c7 | 22 | |
mwthewsey | 10:261f2b69c4c7 | 23 | |
mwthewsey | 10:261f2b69c4c7 | 24 | void SerialStart(void) |
mwthewsey | 10:261f2b69c4c7 | 25 | { |
mwthewsey | 21:6e733076f49c | 26 | SerialThread.start(&SerialCode); //Start thread running |
mwthewsey | 21:6e733076f49c | 27 | Thread SerialThread(osPriorityNormal, 32 * 1024); //Normal priority with a 32k stack size |
mwthewsey | 21:6e733076f49c | 28 | PC.attach(&RXInterruptISR); //Attach interrupt function to hardware interrupt |
mwthewsey | 10:261f2b69c4c7 | 29 | } |
mwthewsey | 10:261f2b69c4c7 | 30 | |
mwthewsey | 10:261f2b69c4c7 | 31 | |
mwthewsey | 10:261f2b69c4c7 | 32 | void RXInterruptISR(void) |
mwthewsey | 10:261f2b69c4c7 | 33 | { |
mwthewsey | 21:6e733076f49c | 34 | SerialThread.signal_set(1); //Set thread signal to start SerialCode |
mwthewsey | 13:41c394fa932c | 35 | PC.attach(NULL); //Disable interrupt |
mwthewsey | 10:261f2b69c4c7 | 36 | } |
mwthewsey | 10:261f2b69c4c7 | 37 | |
mwthewsey | 10:261f2b69c4c7 | 38 | |
mwthewsey | 10:261f2b69c4c7 | 39 | void SerialCode(void) |
mwthewsey | 10:261f2b69c4c7 | 40 | { |
mwthewsey | 10:261f2b69c4c7 | 41 | while(true) { |
mwthewsey | 10:261f2b69c4c7 | 42 | Thread::signal_wait(1); //Wait untill signal set |
mwthewsey | 10:261f2b69c4c7 | 43 | PC.scanf("%s", InputBufferText); //Scan serial |
mwthewsey | 10:261f2b69c4c7 | 44 | PC.printf("Command: %s\n\r", InputBufferText); //Print input string. Readback |
mwthewsey | 10:261f2b69c4c7 | 45 | |
mwthewsey | 10:261f2b69c4c7 | 46 | string InputBufferString(InputBufferText); //Convert array of char to string |
mwthewsey | 10:261f2b69c4c7 | 47 | |
mwthewsey | 10:261f2b69c4c7 | 48 | |
mwthewsey | 10:261f2b69c4c7 | 49 | if (InputBufferString == "test") { |
mwthewsey | 21:6e733076f49c | 50 | //Test function |
mwthewsey | 10:261f2b69c4c7 | 51 | PC.printf("testsuccess\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 52 | |
mwthewsey | 10:261f2b69c4c7 | 53 | |
mwthewsey | 10:261f2b69c4c7 | 54 | } else if (InputBufferString == "help") { |
mwthewsey | 21:6e733076f49c | 55 | //Help function |
mwthewsey | 10:261f2b69c4c7 | 56 | PC.printf("Here is an usefull list of commands:\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 57 | PC.printf(" readall\n\r deleteall\n\r read <n>\n\r delete <n>\n\r setdate <dd><mm><yyyy>\n\r settime <hh><mm><ss>\n\r sett <T>\n\r state <x>\n\r logging <x>\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 58 | |
mwthewsey | 10:261f2b69c4c7 | 59 | |
mwthewsey | 10:261f2b69c4c7 | 60 | } else if (InputBufferString == "readall") { |
mwthewsey | 21:6e733076f49c | 61 | //Reads all samples in buffers and prints oldest first |
mwthewsey | 21:6e733076f49c | 62 | Sampling(false); //Stop sampling |
mwthewsey | 10:261f2b69c4c7 | 63 | internalIndex = currentIndex; //InternalIndex for incrementing out data |
mwthewsey | 10:261f2b69c4c7 | 64 | PC.printf("Printing all %d reccords\n\r",BUFFERSIZE); |
mwthewsey | 10:261f2b69c4c7 | 65 | PC.printf(" Date | Time | Temp | Pressure | Light\n\r"); |
mwthewsey | 21:6e733076f49c | 66 | |
mwthewsey | 10:261f2b69c4c7 | 67 | TakeKeys(true); //Take keys |
mwthewsey | 10:261f2b69c4c7 | 68 | for (short i = 0; i < BUFFERSIZE; i++) { //For loop of length buffersize |
mwthewsey | 11:b538e73841ae | 69 | tm T = ReturnDateTimeStruct(timeReadings[internalIndex]); |
mwthewsey | 10:261f2b69c4c7 | 70 | //InternalIndex was set as newest. We will now decrement to display oldest to newest |
mwthewsey | 12:03589f1d5c30 | 71 | PC.printf(" %4d/%2d/%2d %2d:%2d:%2d %2.2f %2.2f %2.2f\n\r",T.tm_year,T.tm_mon,T.tm_mday,T.tm_hour,T.tm_min,T.tm_sec,tempReadings[internalIndex],presReadings[internalIndex],LDRReadings[internalIndex]); |
mwthewsey | 11:b538e73841ae | 72 | //PC.printf("%d\n\r",T.tm_year); |
mwthewsey | 10:261f2b69c4c7 | 73 | internalIndex = IndexDecrement(internalIndex); //Decrement internal index |
mwthewsey | 10:261f2b69c4c7 | 74 | } |
mwthewsey | 21:6e733076f49c | 75 | |
mwthewsey | 10:261f2b69c4c7 | 76 | TakeKeys(false); //Return keys |
mwthewsey | 12:03589f1d5c30 | 77 | Sampling(true); //Start sampling |
mwthewsey | 10:261f2b69c4c7 | 78 | |
mwthewsey | 10:261f2b69c4c7 | 79 | |
mwthewsey | 10:261f2b69c4c7 | 80 | } else if (InputBufferString == "deleteall") { |
mwthewsey | 21:6e733076f49c | 81 | //Deletes all reccords |
mwthewsey | 10:261f2b69c4c7 | 82 | |
mwthewsey | 21:6e733076f49c | 83 | Sampling(false);//Stop sampling |
mwthewsey | 10:261f2b69c4c7 | 84 | |
mwthewsey | 10:261f2b69c4c7 | 85 | TakeKeys(true); //Take keys |
mwthewsey | 10:261f2b69c4c7 | 86 | |
mwthewsey | 11:b538e73841ae | 87 | memset(tempReadings, 0.0, BUFFERSIZE * sizeof(float)); //Fill array with 0s |
mwthewsey | 11:b538e73841ae | 88 | memset(presReadings, 0.0, BUFFERSIZE * sizeof(float)); |
mwthewsey | 11:b538e73841ae | 89 | memset(LDRReadings, 0.0, BUFFERSIZE * sizeof(float)); |
mwthewsey | 11:b538e73841ae | 90 | memset(timeReadings, 0.0, BUFFERSIZE * sizeof(float)); |
mwthewsey | 10:261f2b69c4c7 | 91 | |
mwthewsey | 21:6e733076f49c | 92 | nextIndex = 0; //Reset Index |
mwthewsey | 10:261f2b69c4c7 | 93 | currentIndex = 0; |
mwthewsey | 10:261f2b69c4c7 | 94 | oldestIndex = 0; |
mwthewsey | 10:261f2b69c4c7 | 95 | |
mwthewsey | 10:261f2b69c4c7 | 96 | firstSample = true; |
mwthewsey | 10:261f2b69c4c7 | 97 | |
mwthewsey | 10:261f2b69c4c7 | 98 | TakeKeys(false); //Return keys |
mwthewsey | 10:261f2b69c4c7 | 99 | |
mwthewsey | 10:261f2b69c4c7 | 100 | Sampling(true); //Start sampling |
mwthewsey | 10:261f2b69c4c7 | 101 | |
mwthewsey | 10:261f2b69c4c7 | 102 | PC.printf("Deleted %d records\n\r",BUFFERSIZE); |
mwthewsey | 10:261f2b69c4c7 | 103 | |
mwthewsey | 10:261f2b69c4c7 | 104 | |
mwthewsey | 10:261f2b69c4c7 | 105 | } else if (InputBufferString == "read") { |
mwthewsey | 21:6e733076f49c | 106 | //Reads past <n> records |
mwthewsey | 10:261f2b69c4c7 | 107 | PC.printf("How many records would you like to view:\n\r"); |
mwthewsey | 21:6e733076f49c | 108 | PC.attach(&RXInterruptISR); //Enable interrupt |
mwthewsey | 10:261f2b69c4c7 | 109 | Thread::signal_wait(1); //Wait untill signal set |
mwthewsey | 10:261f2b69c4c7 | 110 | |
mwthewsey | 10:261f2b69c4c7 | 111 | PC.scanf("%d", &InputBufferNum); //Scan serial |
mwthewsey | 10:261f2b69c4c7 | 112 | |
mwthewsey | 10:261f2b69c4c7 | 113 | if (InputBufferNum >= BUFFERSIZE) { //If requested number of samples is greater than buffersize |
mwthewsey | 10:261f2b69c4c7 | 114 | InputBufferNum = BUFFERSIZE; //Then set number of samples equal to buffersize |
mwthewsey | 10:261f2b69c4c7 | 115 | } |
mwthewsey | 10:261f2b69c4c7 | 116 | |
mwthewsey | 10:261f2b69c4c7 | 117 | internalIndex = currentIndex; //InternalIndex for incrementing out data |
mwthewsey | 10:261f2b69c4c7 | 118 | PC.printf("Showing the newest %d records:\n\r", InputBufferNum); |
mwthewsey | 10:261f2b69c4c7 | 119 | PC.printf(" Date | Time | Temp | Pressure | Light\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 120 | TakeKeys(true); //Take keys |
mwthewsey | 10:261f2b69c4c7 | 121 | |
mwthewsey | 21:6e733076f49c | 122 | for (short i = 0; i < InputBufferNum; i++) { //For loop of length InputBufferNum |
mwthewsey | 12:03589f1d5c30 | 123 | tm T = ReturnDateTimeStruct(timeReadings[internalIndex]); |
mwthewsey | 12:03589f1d5c30 | 124 | PC.printf(" %4d/%2d/%2d %2d:%2d:%2d %2.2f %2.2f %2.2f\n\r",T.tm_year,T.tm_mon,T.tm_mday,T.tm_hour,T.tm_min,T.tm_sec,tempReadings[internalIndex],presReadings[internalIndex],LDRReadings[internalIndex]); |
mwthewsey | 10:261f2b69c4c7 | 125 | internalIndex = IndexDecrement(internalIndex); //Decrement internal index |
mwthewsey | 10:261f2b69c4c7 | 126 | } |
mwthewsey | 10:261f2b69c4c7 | 127 | TakeKeys(false); //Return keys |
mwthewsey | 10:261f2b69c4c7 | 128 | |
mwthewsey | 10:261f2b69c4c7 | 129 | |
mwthewsey | 10:261f2b69c4c7 | 130 | } else if (InputBufferString == "delete") { |
mwthewsey | 21:6e733076f49c | 131 | //Deletes oldest <n> records |
mwthewsey | 10:261f2b69c4c7 | 132 | PC.printf("How many records would you like to delete:\n\r"); |
mwthewsey | 13:41c394fa932c | 133 | PC.attach(&RXInterruptISR); //Enable interrupt |
mwthewsey | 10:261f2b69c4c7 | 134 | Thread::signal_wait(1); //Wait untill signal set |
mwthewsey | 10:261f2b69c4c7 | 135 | |
mwthewsey | 10:261f2b69c4c7 | 136 | PC.scanf("%d", &InputBufferNum); //Scan serial |
mwthewsey | 10:261f2b69c4c7 | 137 | Sampling(false); //Stop sampling |
mwthewsey | 10:261f2b69c4c7 | 138 | TakeKeys(true); //Take keys |
mwthewsey | 10:261f2b69c4c7 | 139 | |
mwthewsey | 10:261f2b69c4c7 | 140 | for (int i = 0; i < InputBufferNum; i++) { |
mwthewsey | 10:261f2b69c4c7 | 141 | tempReadings[oldestIndex] = 0; //Fill array with 0s |
mwthewsey | 10:261f2b69c4c7 | 142 | presReadings[oldestIndex] = 0; |
mwthewsey | 10:261f2b69c4c7 | 143 | LDRReadings[oldestIndex] = 0; |
mwthewsey | 11:b538e73841ae | 144 | timeReadings[oldestIndex] = 0; |
mwthewsey | 10:261f2b69c4c7 | 145 | |
mwthewsey | 21:6e733076f49c | 146 | oldestIndex = IndexIncrement(oldestIndex); //Increment index |
mwthewsey | 10:261f2b69c4c7 | 147 | } |
mwthewsey | 10:261f2b69c4c7 | 148 | |
mwthewsey | 10:261f2b69c4c7 | 149 | TakeKeys(false); //Return keys |
mwthewsey | 10:261f2b69c4c7 | 150 | Sampling(true); //Start sampling |
mwthewsey | 10:261f2b69c4c7 | 151 | PC.printf("Deleated %d records", InputBufferNum); //Scan serial |
mwthewsey | 10:261f2b69c4c7 | 152 | |
mwthewsey | 10:261f2b69c4c7 | 153 | |
mwthewsey | 10:261f2b69c4c7 | 154 | } else if (InputBufferString == "setdate") { |
mwthewsey | 21:6e733076f49c | 155 | //Set date YYYY/MM/DD |
mwthewsey | 10:261f2b69c4c7 | 156 | Sampling(false); //Stop sampling |
mwthewsey | 10:261f2b69c4c7 | 157 | PC.printf("Setting Date\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 158 | |
mwthewsey | 21:6e733076f49c | 159 | for (int i = 0; i < 3; i++) { //Changes message on each iteration |
mwthewsey | 10:261f2b69c4c7 | 160 | if (i==0) { |
mwthewsey | 10:261f2b69c4c7 | 161 | PC.printf("Input Year <YYYY>:\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 162 | } else if(i==1) { |
mwthewsey | 10:261f2b69c4c7 | 163 | PC.printf("Input Month <MM>:\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 164 | } else if (i==2) { |
mwthewsey | 10:261f2b69c4c7 | 165 | PC.printf("Input Date <DD>:\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 166 | } |
mwthewsey | 10:261f2b69c4c7 | 167 | |
mwthewsey | 21:6e733076f49c | 168 | PC.attach(&RXInterruptISR); //Enable interrupt |
mwthewsey | 10:261f2b69c4c7 | 169 | Thread::signal_wait(1); //Wait untill signal set |
mwthewsey | 10:261f2b69c4c7 | 170 | |
mwthewsey | 10:261f2b69c4c7 | 171 | PC.scanf("%d", &DateTimeVar[i]); //Scan serial |
mwthewsey | 10:261f2b69c4c7 | 172 | } |
mwthewsey | 10:261f2b69c4c7 | 173 | |
mwthewsey | 21:6e733076f49c | 174 | SetDate(DateTimeVar[2], DateTimeVar[1], DateTimeVar[0]); //Set date |
mwthewsey | 10:261f2b69c4c7 | 175 | PC.printf("Date set to : %d/%d/%d",DateTimeVar[2], DateTimeVar[1], DateTimeVar[0]); |
mwthewsey | 10:261f2b69c4c7 | 176 | Sampling(true); //Stop sampling |
mwthewsey | 10:261f2b69c4c7 | 177 | |
mwthewsey | 10:261f2b69c4c7 | 178 | |
mwthewsey | 10:261f2b69c4c7 | 179 | } else if (InputBufferString == "settime") { |
mwthewsey | 21:6e733076f49c | 180 | //Set time hh/mm/ss |
mwthewsey | 10:261f2b69c4c7 | 181 | Sampling(false); //Stop sampling |
mwthewsey | 10:261f2b69c4c7 | 182 | PC.printf("Setting Time\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 183 | |
mwthewsey | 21:6e733076f49c | 184 | for (int i = 0; i < 3; i++) { //Changes message on each iteration |
mwthewsey | 10:261f2b69c4c7 | 185 | if (i==0) { |
mwthewsey | 10:261f2b69c4c7 | 186 | PC.printf("Input Hour <hh>:\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 187 | } else if(i==1) { |
mwthewsey | 10:261f2b69c4c7 | 188 | PC.printf("Input Minute <mm>:\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 189 | } else if (i==2) { |
mwthewsey | 10:261f2b69c4c7 | 190 | PC.printf("Input Seccond <ss>:\n\r"); |
mwthewsey | 10:261f2b69c4c7 | 191 | } |
mwthewsey | 10:261f2b69c4c7 | 192 | |
mwthewsey | 21:6e733076f49c | 193 | PC.attach(&RXInterruptISR); //Enable interrupt |
mwthewsey | 10:261f2b69c4c7 | 194 | Thread::signal_wait(1); //Wait untill signal set |
mwthewsey | 10:261f2b69c4c7 | 195 | |
mwthewsey | 10:261f2b69c4c7 | 196 | PC.scanf("%d", &DateTimeVar[i]); //Scan serial |
mwthewsey | 10:261f2b69c4c7 | 197 | } |
mwthewsey | 10:261f2b69c4c7 | 198 | |
mwthewsey | 21:6e733076f49c | 199 | SetTime(DateTimeVar[0], DateTimeVar[1], DateTimeVar[2]); //Set time |
mwthewsey | 10:261f2b69c4c7 | 200 | PC.printf("Time set to : %d:%d:%d",DateTimeVar[0], DateTimeVar[1], DateTimeVar[2]); |
mwthewsey | 10:261f2b69c4c7 | 201 | Sampling(true); //Stop sampling |
mwthewsey | 10:261f2b69c4c7 | 202 | |
mwthewsey | 10:261f2b69c4c7 | 203 | |
mwthewsey | 10:261f2b69c4c7 | 204 | } else if (InputBufferString == "sett") { |
mwthewsey | 21:6e733076f49c | 205 | //Set sample rate |
mwthewsey | 21:6e733076f49c | 206 | PC.printf("What would you like to set the sample rate to:\n\r"); |
mwthewsey | 21:6e733076f49c | 207 | PC.attach(&RXInterruptISR); //Enable interrupt |
mwthewsey | 21:6e733076f49c | 208 | Thread::signal_wait(1); //Wait untill signal set |
mwthewsey | 21:6e733076f49c | 209 | |
mwthewsey | 21:6e733076f49c | 210 | PC.scanf("%d", &SAMPLERATE); //Scan serial |
mwthewsey | 21:6e733076f49c | 211 | |
mwthewsey | 21:6e733076f49c | 212 | ConfigThreadsAndIR(); //Reattach ticker |
mwthewsey | 21:6e733076f49c | 213 | |
mwthewsey | 21:6e733076f49c | 214 | PC.printf("Sample rate set to: %d\n\r",SAMPLERATE); |
mwthewsey | 10:261f2b69c4c7 | 215 | |
mwthewsey | 10:261f2b69c4c7 | 216 | |
mwthewsey | 10:261f2b69c4c7 | 217 | } else if (InputBufferString == "state") { |
mwthewsey | 21:6e733076f49c | 218 | //Toggle sampling |
mwthewsey | 21:6e733076f49c | 219 | SerialState = !SerialState; //Toggle state |
mwthewsey | 21:6e733076f49c | 220 | if (SerialState) { |
mwthewsey | 21:6e733076f49c | 221 | Sampling(true); //Start sampling |
mwthewsey | 21:6e733076f49c | 222 | PC.printf("Sampling started\n\r"); |
mwthewsey | 21:6e733076f49c | 223 | } else { |
mwthewsey | 21:6e733076f49c | 224 | Sampling(false);//Stop sampling |
mwthewsey | 21:6e733076f49c | 225 | PC.printf("Sampling stopped\n\r"); |
mwthewsey | 21:6e733076f49c | 226 | } |
mwthewsey | 21:6e733076f49c | 227 | |
mwthewsey | 10:261f2b69c4c7 | 228 | |
mwthewsey | 10:261f2b69c4c7 | 229 | |
mwthewsey | 10:261f2b69c4c7 | 230 | } else if (InputBufferString == "logging") { |
mwthewsey | 21:6e733076f49c | 231 | //Toggle logging |
mwthewsey | 21:6e733076f49c | 232 | logging = !logging; //Toggle state |
mwthewsey | 21:6e733076f49c | 233 | if (logging) { |
mwthewsey | 21:6e733076f49c | 234 | PC.printf("Logging started"); |
mwthewsey | 21:6e733076f49c | 235 | } else { |
mwthewsey | 21:6e733076f49c | 236 | PC.printf("Logging stopped"); |
mwthewsey | 10:261f2b69c4c7 | 237 | } |
mwthewsey | 10:261f2b69c4c7 | 238 | } |
mwthewsey | 10:261f2b69c4c7 | 239 | |
mwthewsey | 10:261f2b69c4c7 | 240 | |
mwthewsey | 13:41c394fa932c | 241 | PC.attach(&RXInterruptISR); //Enable interrupt |
mwthewsey | 10:261f2b69c4c7 | 242 | } |
mwthewsey | 10:261f2b69c4c7 | 243 | } |