3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
main.cpp@81:996c0a3319b4, 2017-05-11 (annotated)
- Committer:
- aburch1
- Date:
- Thu May 11 14:34:53 2017 +0000
- Revision:
- 81:996c0a3319b4
- Parent:
- 80:959151952153
- Child:
- 82:668b51a39148
Changed the logger to use char arrays of a size 256 and fixed some issues that cropped up from this. Moved all printing statements over to use the logger
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
FairyMental | 36:19d3f752f9c3 | 1 | |
Jonathan Austin |
0:2757d7abb7d9 | 2 | #include "mbed.h" |
martinsimpson | 32:260a288be58f | 3 | #include "rtos.h" |
aburch1 | 74:749727490f44 | 4 | #include "hts221.h" |
aburch1 | 74:749727490f44 | 5 | #include "LPS25H.h" |
aburch1 | 74:749727490f44 | 6 | |
aburch1 | 81:996c0a3319b4 | 7 | #include "MessageLogger.h" |
aburch1 | 74:749727490f44 | 8 | #include "CircularArray.h" |
aburch1 | 74:749727490f44 | 9 | #include "FakeSensor.h" |
aburch1 | 74:749727490f44 | 10 | |
FairyMental | 43:3983059e0d91 | 11 | #include <string.h> |
FairyMental | 34:09ed07f2acba | 12 | #include <stdio.h> |
FairyMental | 34:09ed07f2acba | 13 | #include <ctype.h> |
FairyMental | 41:d222c043c96d | 14 | #include <iostream> |
aburch1 | 74:749727490f44 | 15 | #include <sstream> |
martinsimpson | 32:260a288be58f | 16 | |
FairyMental | 36:19d3f752f9c3 | 17 | #define SIGNAL_doMeasure 1 |
aburch1 | 75:b44645bbf2d2 | 18 | #define SIGNAL_printMessage 2 |
aburch1 | 81:996c0a3319b4 | 19 | #define SIGNAL_terminate 3 |
FairyMental | 36:19d3f752f9c3 | 20 | #define SWITCH1_RELEASE 90 |
Netaphous | 65:3723d2729b68 | 21 | #define BUFFER_SIZE 120 |
FairyMental | 34:09ed07f2acba | 22 | |
FairyMental | 57:dfcdda1e42b6 | 23 | // |
FairyMental | 57:dfcdda1e42b6 | 24 | // MBED DECLARATIONS |
FairyMental | 57:dfcdda1e42b6 | 25 | // |
martinsimpson | 32:260a288be58f | 26 | DigitalOut myled(LED1); |
Netaphous | 50:c07e968b9582 | 27 | DigitalIn onBoardSwitch(USER_BUTTON); |
martinsimpson | 32:260a288be58f | 28 | I2C i2c2(I2C_SDA, I2C_SCL); |
Netaphous | 50:c07e968b9582 | 29 | |
FairyMental | 57:dfcdda1e42b6 | 30 | // |
FairyMental | 57:dfcdda1e42b6 | 31 | // SENSOR DECLARATIONS |
Netaphous | 50:c07e968b9582 | 32 | // MAKE SURE ONE OF THESE IS COMMENTED OUT |
Netaphous | 50:c07e968b9582 | 33 | // Real sensor |
aburch1 | 75:b44645bbf2d2 | 34 | // LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
aburch1 | 75:b44645bbf2d2 | 35 | // HTS221 measurer(I2C_SDA, I2C_SCL); |
Netaphous | 50:c07e968b9582 | 36 | // Fake sensor |
aburch1 | 75:b44645bbf2d2 | 37 | FakeBarometer barometer(1029.0, 1031.0); |
aburch1 | 75:b44645bbf2d2 | 38 | FakeMeasurer measurer(20.0, 25.0, 30.0, 50.0); |
Netaphous | 50:c07e968b9582 | 39 | |
FairyMental | 57:dfcdda1e42b6 | 40 | // |
FairyMental | 57:dfcdda1e42b6 | 41 | // THREADS DECLARATION |
FairyMental | 57:dfcdda1e42b6 | 42 | // |
FairyMental | 37:00775e368a71 | 43 | Thread *produceThread; |
FairyMental | 37:00775e368a71 | 44 | Thread *measureThread; |
FairyMental | 39:618ad21e2b34 | 45 | Thread *consumeThread; |
aburch1 | 74:749727490f44 | 46 | Thread *loggingThread; |
FairyMental | 58:7fc6e3e4d746 | 47 | Ticker timer; |
FairyMental | 58:7fc6e3e4d746 | 48 | Ticker realTimeDate; |
FairyMental | 57:dfcdda1e42b6 | 49 | // |
FairyMental | 57:dfcdda1e42b6 | 50 | // GLOBAL VARIABLES |
FairyMental | 57:dfcdda1e42b6 | 51 | // |
FairyMental | 34:09ed07f2acba | 52 | Mail<Measure, 16> mail_box; |
FairyMental | 46:0de1f3c7d118 | 53 | LocalDate *localDate; |
aburch1 | 74:749727490f44 | 54 | //Mail<> |
FairyMental | 49:83bea7fb2728 | 55 | bool logging = true; |
aburch1 | 81:996c0a3319b4 | 56 | double sampleRate = 15; |
aburch1 | 81:996c0a3319b4 | 57 | char temp[256]; |
FairyMental | 49:83bea7fb2728 | 58 | |
aburch1 | 74:749727490f44 | 59 | // Logging objects |
aburch1 | 75:b44645bbf2d2 | 60 | MessageLogger logger; |
aburch1 | 74:749727490f44 | 61 | |
aburch1 | 81:996c0a3319b4 | 62 | CircularArray buffer(BUFFER_SIZE, &logger); |
FairyMental | 57:dfcdda1e42b6 | 63 | // |
FairyMental | 57:dfcdda1e42b6 | 64 | // Called by a TICKER |
FairyMental | 57:dfcdda1e42b6 | 65 | // Adds 1 second every second to the clock |
FairyMental | 46:0de1f3c7d118 | 66 | void RealTimeDate() |
FairyMental | 46:0de1f3c7d118 | 67 | { |
FairyMental | 46:0de1f3c7d118 | 68 | localDate->TickSecond(); |
FairyMental | 46:0de1f3c7d118 | 69 | } |
FairyMental | 58:7fc6e3e4d746 | 70 | |
FairyMental | 58:7fc6e3e4d746 | 71 | // |
FairyMental | 58:7fc6e3e4d746 | 72 | // Ticker that signals the measureThread to do a measure |
FairyMental | 58:7fc6e3e4d746 | 73 | // |
FairyMental | 58:7fc6e3e4d746 | 74 | void SendSignalDoMeasure() |
FairyMental | 58:7fc6e3e4d746 | 75 | { |
FairyMental | 58:7fc6e3e4d746 | 76 | if(logging == true) |
FairyMental | 58:7fc6e3e4d746 | 77 | measureThread->signal_set(SIGNAL_doMeasure); |
FairyMental | 58:7fc6e3e4d746 | 78 | } |
FairyMental | 58:7fc6e3e4d746 | 79 | |
FairyMental | 57:dfcdda1e42b6 | 80 | // |
FairyMental | 57:dfcdda1e42b6 | 81 | // SIGNALED BY Ticker at a frequency of <T> Hz |
FairyMental | 57:dfcdda1e42b6 | 82 | // Reads values from sensor board, sends over through mail queue |
aburch1 | 79:4e6b53eb678b | 83 | void MeasureThread() |
aburch1 | 79:4e6b53eb678b | 84 | { |
aburch1 | 81:996c0a3319b4 | 85 | double temperature , humidity, pressure; |
FairyMental | 35:484e384f9bf1 | 86 | |
FairyMental | 36:19d3f752f9c3 | 87 | while(true) |
FairyMental | 36:19d3f752f9c3 | 88 | { |
aburch1 | 79:4e6b53eb678b | 89 | temperature = 0; |
aburch1 | 79:4e6b53eb678b | 90 | humidity = 0; |
aburch1 | 79:4e6b53eb678b | 91 | pressure = 0; |
FairyMental | 57:dfcdda1e42b6 | 92 | //Await signal from ticker |
FairyMental | 36:19d3f752f9c3 | 93 | Thread::signal_wait(SIGNAL_doMeasure); |
FairyMental | 57:dfcdda1e42b6 | 94 | |
FairyMental | 36:19d3f752f9c3 | 95 | Measure *measure = mail_box.alloc(); |
FairyMental | 36:19d3f752f9c3 | 96 | if (measure == NULL) |
FairyMental | 36:19d3f752f9c3 | 97 | { |
aburch1 | 81:996c0a3319b4 | 98 | logger.SendError("Out of memory\n\r"); |
FairyMental | 36:19d3f752f9c3 | 99 | return; |
FairyMental | 36:19d3f752f9c3 | 100 | } |
FairyMental | 34:09ed07f2acba | 101 | |
FairyMental | 57:dfcdda1e42b6 | 102 | //Read and fill in data |
FairyMental | 36:19d3f752f9c3 | 103 | measurer.ReadTempHumi(&temperature,&humidity); |
FairyMental | 36:19d3f752f9c3 | 104 | barometer.get(); |
FairyMental | 36:19d3f752f9c3 | 105 | pressure = barometer.pressure(); |
FairyMental | 47:468a89d62c23 | 106 | |
FairyMental | 36:19d3f752f9c3 | 107 | measure->temperature = temperature; |
FairyMental | 36:19d3f752f9c3 | 108 | measure->humidity = humidity; |
FairyMental | 36:19d3f752f9c3 | 109 | measure->pressure = pressure; |
Netaphous | 67:8d0e88172e2a | 110 | measure->date.setValues(localDate); |
FairyMental | 36:19d3f752f9c3 | 111 | |
FairyMental | 57:dfcdda1e42b6 | 112 | osStatus stat = mail_box.put(measure); |
martinsimpson | 32:260a288be58f | 113 | |
FairyMental | 36:19d3f752f9c3 | 114 | //Check if succesful |
FairyMental | 36:19d3f752f9c3 | 115 | if (stat == osErrorResource) { |
aburch1 | 81:996c0a3319b4 | 116 | snprintf(temp, 256, "queue->put() Error code: %4Xh, Resource not available\r\n", stat); |
aburch1 | 81:996c0a3319b4 | 117 | logger.SendError(temp); |
FairyMental | 36:19d3f752f9c3 | 118 | mail_box.free(measure); |
FairyMental | 36:19d3f752f9c3 | 119 | return; |
FairyMental | 36:19d3f752f9c3 | 120 | } |
FairyMental | 34:09ed07f2acba | 121 | } |
FairyMental | 34:09ed07f2acba | 122 | } |
FairyMental | 34:09ed07f2acba | 123 | |
FairyMental | 57:dfcdda1e42b6 | 124 | // |
FairyMental | 57:dfcdda1e42b6 | 125 | // Receives data through mail queue, then adds it to the global declared list |
FairyMental | 57:dfcdda1e42b6 | 126 | // A.K.A. Producer Thread |
FairyMental | 37:00775e368a71 | 127 | void ProducerThread() |
FairyMental | 34:09ed07f2acba | 128 | { |
FairyMental | 57:dfcdda1e42b6 | 129 | while (true) |
FairyMental | 57:dfcdda1e42b6 | 130 | { |
FairyMental | 34:09ed07f2acba | 131 | //Block on the queue |
FairyMental | 34:09ed07f2acba | 132 | osEvent evt = mail_box.get(); |
FairyMental | 34:09ed07f2acba | 133 | |
FairyMental | 34:09ed07f2acba | 134 | //Check status |
FairyMental | 34:09ed07f2acba | 135 | if (evt.status == osEventMail) { |
FairyMental | 39:618ad21e2b34 | 136 | |
FairyMental | 57:dfcdda1e42b6 | 137 | Measure *measure = (Measure*)evt.value.p; |
FairyMental | 47:468a89d62c23 | 138 | Measure msr(measure->date,measure->temperature, measure->humidity,measure->pressure); |
Netaphous | 65:3723d2729b68 | 139 | |
Netaphous | 65:3723d2729b68 | 140 | // Changed to use circlar buffer rather than list buffer |
Netaphous | 65:3723d2729b68 | 141 | buffer.pushValue(msr); |
FairyMental | 34:09ed07f2acba | 142 | mail_box.free(measure); |
FairyMental | 34:09ed07f2acba | 143 | } else { |
aburch1 | 81:996c0a3319b4 | 144 | snprintf(temp, 256, "ERROR: %x\n\r", evt.status); |
aburch1 | 81:996c0a3319b4 | 145 | logger.SendError(temp); |
aburch1 | 81:996c0a3319b4 | 146 | } |
FairyMental | 57:dfcdda1e42b6 | 147 | } |
FairyMental | 34:09ed07f2acba | 148 | } |
Netaphous | 69:72f237750d85 | 149 | int i; |
FairyMental | 57:dfcdda1e42b6 | 150 | // |
FairyMental | 57:dfcdda1e42b6 | 151 | // Compares two char arrays and returns result |
FairyMental | 57:dfcdda1e42b6 | 152 | // Param1: First char Array / pointer |
FairyMental | 57:dfcdda1e42b6 | 153 | // Param2: Second char Array / pointer |
FairyMental | 57:dfcdda1e42b6 | 154 | // Param3. Size of the smallest char arrays (between param1 and param2) |
FairyMental | 57:dfcdda1e42b6 | 155 | // Return: "-1" IF NOT EQUAL |
FairyMental | 57:dfcdda1e42b6 | 156 | // "1 " IF EQUAL |
aburch1 | 81:996c0a3319b4 | 157 | bool CompareCommands(char command[],char targetcommand[], int size) |
FairyMental | 42:b1f29874ab70 | 158 | { |
FairyMental | 43:3983059e0d91 | 159 | for(i = 0; i < size; i ++) |
FairyMental | 42:b1f29874ab70 | 160 | { |
FairyMental | 42:b1f29874ab70 | 161 | if(command[i] != targetcommand[i]) |
aburch1 | 81:996c0a3319b4 | 162 | { |
aburch1 | 81:996c0a3319b4 | 163 | return false; |
aburch1 | 81:996c0a3319b4 | 164 | } |
FairyMental | 42:b1f29874ab70 | 165 | } |
aburch1 | 81:996c0a3319b4 | 166 | return true; |
FairyMental | 42:b1f29874ab70 | 167 | } |
aburch1 | 74:749727490f44 | 168 | |
FairyMental | 57:dfcdda1e42b6 | 169 | // |
FairyMental | 57:dfcdda1e42b6 | 170 | // Reads commands through PUTTY and 'consumes the data' accordingly |
FairyMental | 57:dfcdda1e42b6 | 171 | // A.K.A. Consumer Thread |
FairyMental | 39:618ad21e2b34 | 172 | void ConsumeThread() |
FairyMental | 39:618ad21e2b34 | 173 | { |
FairyMental | 57:dfcdda1e42b6 | 174 | //Last character pressed read (last key input) |
FairyMental | 41:d222c043c96d | 175 | char charCmd; |
FairyMental | 57:dfcdda1e42b6 | 176 | //Char array that stores the command after user presses ENTER |
FairyMental | 41:d222c043c96d | 177 | char command[40]; |
FairyMental | 57:dfcdda1e42b6 | 178 | //Current Command Size |
aburch1 | 79:4e6b53eb678b | 179 | int crtChar = 0; |
aburch1 | 81:996c0a3319b4 | 180 | logger.SendMessage("\r\nAwaiting Command: \r\n"); |
FairyMental | 39:618ad21e2b34 | 181 | while(1) |
FairyMental | 39:618ad21e2b34 | 182 | { |
FairyMental | 41:d222c043c96d | 183 | charCmd = NULL; |
FairyMental | 41:d222c043c96d | 184 | charCmd = getchar(); |
aburch1 | 80:959151952153 | 185 | |
FairyMental | 41:d222c043c96d | 186 | if(charCmd != NULL) |
FairyMental | 41:d222c043c96d | 187 | { |
FairyMental | 57:dfcdda1e42b6 | 188 | //If BACKSPACE is pressed, Print "DEL" so it deletes last character typed. |
FairyMental | 57:dfcdda1e42b6 | 189 | if (charCmd == 127 && crtChar > 0 ) |
FairyMental | 43:3983059e0d91 | 190 | { |
aburch1 | 79:4e6b53eb678b | 191 | command[--crtChar] = '\0'; |
FairyMental | 43:3983059e0d91 | 192 | } |
FairyMental | 57:dfcdda1e42b6 | 193 | //If NOT enter AND NOT Backspace is pressed, SAVE the char |
FairyMental | 57:dfcdda1e42b6 | 194 | else if(charCmd != 13 && charCmd != 127) |
FairyMental | 41:d222c043c96d | 195 | { |
FairyMental | 41:d222c043c96d | 196 | command[crtChar++] = charCmd; |
FairyMental | 41:d222c043c96d | 197 | } |
FairyMental | 57:dfcdda1e42b6 | 198 | //If ENTER is pressed, PROCESS it |
FairyMental | 44:b523c9a9dd97 | 199 | else if(charCmd == 13) // If Enter is pressed |
FairyMental | 49:83bea7fb2728 | 200 | { |
FairyMental | 57:dfcdda1e42b6 | 201 | //Get first word of command: |
FairyMental | 43:3983059e0d91 | 202 | char *charPos; |
FairyMental | 43:3983059e0d91 | 203 | charPos = strtok(command," -,"); |
FairyMental | 57:dfcdda1e42b6 | 204 | |
FairyMental | 57:dfcdda1e42b6 | 205 | //Check if it's a "LIST" command |
aburch1 | 81:996c0a3319b4 | 206 | if(CompareCommands(charPos, "read",4)) |
FairyMental | 42:b1f29874ab70 | 207 | { |
FairyMental | 43:3983059e0d91 | 208 | charPos = strtok(NULL," -,"); |
FairyMental | 57:dfcdda1e42b6 | 209 | //Check if it's a "LIST ALL" command |
aburch1 | 81:996c0a3319b4 | 210 | if(CompareCommands(charPos, "all",3)) |
FairyMental | 43:3983059e0d91 | 211 | { |
aburch1 | 81:996c0a3319b4 | 212 | logger.SendMessage("Printing all measures performed so far: \r\n"); |
Netaphous | 65:3723d2729b68 | 213 | |
Netaphous | 65:3723d2729b68 | 214 | // Changed to use circular buffer rather than list buffer |
Netaphous | 65:3723d2729b68 | 215 | buffer.readAll(); |
aburch1 | 81:996c0a3319b4 | 216 | logger.SendMessage("D O N E ! \r\n"); |
FairyMental | 43:3983059e0d91 | 217 | } |
FairyMental | 57:dfcdda1e42b6 | 218 | //Check if it's a "LIST X" command |
FairyMental | 43:3983059e0d91 | 219 | else if(strtol(charPos,NULL,10) != 0) |
FairyMental | 43:3983059e0d91 | 220 | { |
aburch1 | 81:996c0a3319b4 | 221 | int num = atoi(charPos); |
aburch1 | 81:996c0a3319b4 | 222 | //char *temp1; |
aburch1 | 81:996c0a3319b4 | 223 | //strncpy(temp1, "\r\nPrinting ", 31); |
aburch1 | 81:996c0a3319b4 | 224 | //strcat(temp1, charPos); |
aburch1 | 81:996c0a3319b4 | 225 | //strcat(temp1, " measures: \r\n"); |
aburch1 | 80:959151952153 | 226 | |
aburch1 | 81:996c0a3319b4 | 227 | snprintf(temp, 256, "Printing %i measures: \r\n", num); |
aburch1 | 79:4e6b53eb678b | 228 | logger.SendMessage(temp); |
Netaphous | 65:3723d2729b68 | 229 | |
Netaphous | 65:3723d2729b68 | 230 | // Changed to use circular buffer rather than list buffer |
Netaphous | 69:72f237750d85 | 231 | buffer.readX(num); |
aburch1 | 81:996c0a3319b4 | 232 | logger.SendMessage("D O N E ! \r\n"); |
FairyMental | 60:db8c5b7fc548 | 233 | } |
FairyMental | 60:db8c5b7fc548 | 234 | else |
FairyMental | 60:db8c5b7fc548 | 235 | { |
aburch1 | 81:996c0a3319b4 | 236 | logger.SendMessage("Expected parameters: \"all\" | \"n\", where n is a number.\r\n"); |
FairyMental | 43:3983059e0d91 | 237 | } |
FairyMental | 42:b1f29874ab70 | 238 | } |
FairyMental | 57:dfcdda1e42b6 | 239 | //Check if it's a "DELETE" command |
aburch1 | 81:996c0a3319b4 | 240 | else if (CompareCommands(charPos,"delete",6)) |
FairyMental | 44:b523c9a9dd97 | 241 | { |
FairyMental | 44:b523c9a9dd97 | 242 | charPos = strtok(NULL," -,"); |
FairyMental | 57:dfcdda1e42b6 | 243 | //Check if it's a "DELETE ALL" command |
aburch1 | 81:996c0a3319b4 | 244 | if(CompareCommands(charPos,"all",3)) |
FairyMental | 44:b523c9a9dd97 | 245 | { |
aburch1 | 81:996c0a3319b4 | 246 | logger.SendMessage("Deleting all measures performed so far: \r\n"); |
Netaphous | 65:3723d2729b68 | 247 | |
Netaphous | 65:3723d2729b68 | 248 | // Changed to use circular buffer rather than list buffer |
Netaphous | 65:3723d2729b68 | 249 | buffer.deleteAll(); |
aburch1 | 81:996c0a3319b4 | 250 | logger.SendMessage("Elements deleted!\r\n"); |
FairyMental | 44:b523c9a9dd97 | 251 | } |
FairyMental | 57:dfcdda1e42b6 | 252 | //Check if it's a "DELETE X" command |
FairyMental | 44:b523c9a9dd97 | 253 | else if (strtol(charPos,NULL,10) != 0) |
FairyMental | 44:b523c9a9dd97 | 254 | { |
Netaphous | 65:3723d2729b68 | 255 | // Changed to use circular buffer rather than list buffer |
Netaphous | 65:3723d2729b68 | 256 | buffer.deleteX(atoi(charPos)); |
aburch1 | 81:996c0a3319b4 | 257 | logger.SendMessage("Elements deleted!\r\n"); |
FairyMental | 60:db8c5b7fc548 | 258 | } |
FairyMental | 60:db8c5b7fc548 | 259 | else |
FairyMental | 60:db8c5b7fc548 | 260 | { |
aburch1 | 81:996c0a3319b4 | 261 | logger.SendMessage("Expected parameters: \"all\" | \"n\", where n is a number."); |
aburch1 | 81:996c0a3319b4 | 262 | } |
FairyMental | 44:b523c9a9dd97 | 263 | } |
FairyMental | 60:db8c5b7fc548 | 264 | //Check if it's a "STATUS" command |
aburch1 | 81:996c0a3319b4 | 265 | else if (CompareCommands(charPos,"status",6)) |
FairyMental | 45:9a33f2bc2b4e | 266 | { |
FairyMental | 46:0de1f3c7d118 | 267 | char *ptr = localDate->ToString(); |
Netaphous | 65:3723d2729b68 | 268 | |
Netaphous | 65:3723d2729b68 | 269 | // Changed to use circular buffer rather than list buffer |
FairyMental | 49:83bea7fb2728 | 270 | if(logging == true) |
aburch1 | 79:4e6b53eb678b | 271 | { |
aburch1 | 81:996c0a3319b4 | 272 | snprintf(temp, 256, "STATUS: \r\n # of measures: %i \r\n SAMPLING: ON \r\n Current Date: %s \r\n Sample Rate(s): %2.2f \r\n", buffer.getSize(), ptr,sampleRate); |
aburch1 | 79:4e6b53eb678b | 273 | } |
FairyMental | 49:83bea7fb2728 | 274 | else |
aburch1 | 79:4e6b53eb678b | 275 | { |
aburch1 | 81:996c0a3319b4 | 276 | snprintf(temp, 256, "STATUS: \r\n # of measures: %i \r\n SAMPLING: OFF \r\n Current Date: %s \r\n Sample Rate(s): %2.2f \r\n", buffer.getSize(), ptr,sampleRate); |
aburch1 | 79:4e6b53eb678b | 277 | } |
aburch1 | 81:996c0a3319b4 | 278 | logger.SendMessage(temp); |
FairyMental | 45:9a33f2bc2b4e | 279 | } |
FairyMental | 57:dfcdda1e42b6 | 280 | //Check if it's a "SETTIME" command |
aburch1 | 81:996c0a3319b4 | 281 | else if (CompareCommands(charPos,"settime",7)) |
FairyMental | 48:a8219954b3f2 | 282 | { |
FairyMental | 48:a8219954b3f2 | 283 | int h,m,s; |
FairyMental | 57:dfcdda1e42b6 | 284 | //Fetch 1st Param |
aburch1 | 81:996c0a3319b4 | 285 | charPos = strtok(NULL," -,"); |
FairyMental | 48:a8219954b3f2 | 286 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 287 | { |
FairyMental | 48:a8219954b3f2 | 288 | h = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 289 | } |
FairyMental | 57:dfcdda1e42b6 | 290 | //Fech 2nd Param |
aburch1 | 81:996c0a3319b4 | 291 | charPos = strtok(NULL," -,"); |
FairyMental | 48:a8219954b3f2 | 292 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 293 | { |
FairyMental | 48:a8219954b3f2 | 294 | m = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 295 | } |
FairyMental | 57:dfcdda1e42b6 | 296 | //Fetch 3rd Param |
aburch1 | 81:996c0a3319b4 | 297 | charPos = strtok(NULL," -,"); |
FairyMental | 48:a8219954b3f2 | 298 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 299 | { |
FairyMental | 48:a8219954b3f2 | 300 | s = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 301 | } |
FairyMental | 57:dfcdda1e42b6 | 302 | //Check if parameters are valid |
FairyMental | 48:a8219954b3f2 | 303 | if((h>=0 && h < 24) && (m>=0 && m<60) && (s>=0 && s<60)) |
FairyMental | 48:a8219954b3f2 | 304 | { |
FairyMental | 48:a8219954b3f2 | 305 | localDate->hour = h; |
FairyMental | 48:a8219954b3f2 | 306 | localDate->min = m; |
FairyMental | 48:a8219954b3f2 | 307 | localDate->sec = s; |
FairyMental | 60:db8c5b7fc548 | 308 | char *ptr = localDate->ToString(); |
aburch1 | 81:996c0a3319b4 | 309 | snprintf(temp, 256, "Updated Date to: %s \r\n", ptr); |
aburch1 | 81:996c0a3319b4 | 310 | logger.SendMessage(temp); |
FairyMental | 48:a8219954b3f2 | 311 | } |
FairyMental | 57:dfcdda1e42b6 | 312 | //If not valid, prompt user |
FairyMental | 48:a8219954b3f2 | 313 | else |
FairyMental | 48:a8219954b3f2 | 314 | { |
aburch1 | 81:996c0a3319b4 | 315 | logger.SendMessage("\r\nWrong format! please use HH-MM-SS. \r\n"); |
FairyMental | 48:a8219954b3f2 | 316 | } |
FairyMental | 48:a8219954b3f2 | 317 | } |
FairyMental | 57:dfcdda1e42b6 | 318 | //Check if it's a "SETDATE" command |
aburch1 | 81:996c0a3319b4 | 319 | else if (CompareCommands(charPos,"setdate",7)) |
FairyMental | 48:a8219954b3f2 | 320 | { |
FairyMental | 48:a8219954b3f2 | 321 | int d,m,y; |
FairyMental | 57:dfcdda1e42b6 | 322 | //Fetch 1st Parameter |
aburch1 | 81:996c0a3319b4 | 323 | charPos = strtok(NULL," ,-"); |
FairyMental | 48:a8219954b3f2 | 324 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 325 | { |
FairyMental | 48:a8219954b3f2 | 326 | d = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 327 | } |
FairyMental | 57:dfcdda1e42b6 | 328 | //Fetch 2nd Parameter |
aburch1 | 81:996c0a3319b4 | 329 | charPos = strtok(NULL," ,-"); |
FairyMental | 48:a8219954b3f2 | 330 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 331 | { |
FairyMental | 48:a8219954b3f2 | 332 | m = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 333 | } |
FairyMental | 57:dfcdda1e42b6 | 334 | //Fetch 3rd Parameter |
aburch1 | 81:996c0a3319b4 | 335 | charPos = strtok(NULL," ,-"); |
FairyMental | 48:a8219954b3f2 | 336 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 337 | { |
FairyMental | 48:a8219954b3f2 | 338 | y = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 339 | } |
FairyMental | 57:dfcdda1e42b6 | 340 | //Check if parameters are valid |
FairyMental | 48:a8219954b3f2 | 341 | if((d>=0 && d < 31) && (m>=0 && m<13)) |
FairyMental | 48:a8219954b3f2 | 342 | { |
FairyMental | 48:a8219954b3f2 | 343 | localDate->day = d; |
FairyMental | 48:a8219954b3f2 | 344 | localDate->month = m; |
FairyMental | 48:a8219954b3f2 | 345 | localDate->year = y; |
FairyMental | 60:db8c5b7fc548 | 346 | char *ptr = localDate->ToString(); |
aburch1 | 81:996c0a3319b4 | 347 | snprintf(temp, 256, "Updated Date to: %s \r\n", ptr); |
aburch1 | 81:996c0a3319b4 | 348 | logger.SendMessage(temp); |
FairyMental | 48:a8219954b3f2 | 349 | } |
FairyMental | 57:dfcdda1e42b6 | 350 | // Prompt user if they are not. |
FairyMental | 48:a8219954b3f2 | 351 | else |
FairyMental | 48:a8219954b3f2 | 352 | { |
aburch1 | 81:996c0a3319b4 | 353 | logger.SendMessage("Wrong format! please use DD-MM-YYYY. \r\n"); |
FairyMental | 48:a8219954b3f2 | 354 | } |
FairyMental | 48:a8219954b3f2 | 355 | } |
FairyMental | 57:dfcdda1e42b6 | 356 | // Check if it's a "LOGGING" command |
aburch1 | 81:996c0a3319b4 | 357 | else if(CompareCommands(charPos,"state",5)) |
FairyMental | 49:83bea7fb2728 | 358 | { |
FairyMental | 49:83bea7fb2728 | 359 | charPos = strtok(NULL," ,"); |
FairyMental | 57:dfcdda1e42b6 | 360 | //Check if it should be turned ON / OFF |
aburch1 | 81:996c0a3319b4 | 361 | if(CompareCommands(charPos,"on",2)) |
FairyMental | 49:83bea7fb2728 | 362 | { |
FairyMental | 49:83bea7fb2728 | 363 | logging = true; |
aburch1 | 81:996c0a3319b4 | 364 | logger.SendMessage("Sampling turned ON!\r\n"); |
FairyMental | 49:83bea7fb2728 | 365 | } |
aburch1 | 81:996c0a3319b4 | 366 | else if (CompareCommands(charPos,"off",3)) |
FairyMental | 49:83bea7fb2728 | 367 | { |
aburch1 | 79:4e6b53eb678b | 368 | logging = false; |
aburch1 | 81:996c0a3319b4 | 369 | logger.SendMessage("Sampling turned OFF!\r\n"); |
FairyMental | 60:db8c5b7fc548 | 370 | } |
FairyMental | 60:db8c5b7fc548 | 371 | else |
FairyMental | 60:db8c5b7fc548 | 372 | { |
aburch1 | 81:996c0a3319b4 | 373 | logger.SendMessage("Expected parameters: \"on\" | \"off\"\r\n"); |
FairyMental | 49:83bea7fb2728 | 374 | } |
FairyMental | 58:7fc6e3e4d746 | 375 | } |
FairyMental | 68:d3765f93c16a | 376 | // Check if it's a "SETT" command |
aburch1 | 81:996c0a3319b4 | 377 | else if(CompareCommands(charPos,"sett",4)) |
FairyMental | 58:7fc6e3e4d746 | 378 | { |
FairyMental | 58:7fc6e3e4d746 | 379 | charPos = strtok(NULL," ,"); |
aburch1 | 81:996c0a3319b4 | 380 | double auxRate = atof(charPos); |
FairyMental | 68:d3765f93c16a | 381 | // Validate rate |
aburch1 | 81:996c0a3319b4 | 382 | if(auxRate >= 0.1 && |
aburch1 | 81:996c0a3319b4 | 383 | auxRate <= 60) |
FairyMental | 58:7fc6e3e4d746 | 384 | { |
FairyMental | 58:7fc6e3e4d746 | 385 | sampleRate = auxRate; |
FairyMental | 58:7fc6e3e4d746 | 386 | timer.detach(); |
FairyMental | 58:7fc6e3e4d746 | 387 | timer.attach(&SendSignalDoMeasure, sampleRate); |
aburch1 | 81:996c0a3319b4 | 388 | snprintf(temp, 256, "Successfully updated sample rate to: %2.2f .\r\n",sampleRate); |
aburch1 | 81:996c0a3319b4 | 389 | logger.SendMessage(temp); |
FairyMental | 58:7fc6e3e4d746 | 390 | } |
FairyMental | 68:d3765f93c16a | 391 | // if rate is not valid, prompt: |
FairyMental | 58:7fc6e3e4d746 | 392 | else |
FairyMental | 58:7fc6e3e4d746 | 393 | { |
aburch1 | 81:996c0a3319b4 | 394 | logger.SendMessage("Sample rate must be between 0.1 and 60.\r\n"); |
FairyMental | 58:7fc6e3e4d746 | 395 | } |
FairyMental | 58:7fc6e3e4d746 | 396 | } |
FairyMental | 68:d3765f93c16a | 397 | // Check if it's a "HELP" command |
aburch1 | 81:996c0a3319b4 | 398 | else if (CompareCommands(charPos,"help",4) || CompareCommands(charPos,"?",1)) |
aburch1 | 75:b44645bbf2d2 | 399 | { |
aburch1 | 81:996c0a3319b4 | 400 | logger.SendMessage("Available Commands:\r\n"); |
aburch1 | 81:996c0a3319b4 | 401 | logger.SendMessage(" read <ALL|N> - Read ALL or N first measures.\r\n"); |
aburch1 | 81:996c0a3319b4 | 402 | logger.SendMessage(" delete <ALL|N> - Delete ALL or N first measures.\r\n"); |
aburch1 | 81:996c0a3319b4 | 403 | logger.SendMessage(" setdate <DD> <MM> <YYYY> Set current date.\r\n"); |
aburch1 | 81:996c0a3319b4 | 404 | logger.SendMessage(" settime <HH> <MM> <SS> Set current time.\r\n"); |
aburch1 | 81:996c0a3319b4 | 405 | logger.SendMessage(" sett <T> Set sample rate (in seconds).\r\n"); |
aburch1 | 81:996c0a3319b4 | 406 | logger.SendMessage(" status - Status report of device.\r\n"); |
aburch1 | 81:996c0a3319b4 | 407 | logger.SendMessage(" state - <ON|OFF> - Turn sampling on or OFF.\r\n"); |
aburch1 | 81:996c0a3319b4 | 408 | logger.SendMessage(" logging <ON|OFF> - Turn logging on or OFF.\r\n"); |
aburch1 | 75:b44645bbf2d2 | 409 | } |
FairyMental | 68:d3765f93c16a | 410 | // If command not recognized |
FairyMental | 60:db8c5b7fc548 | 411 | else |
FairyMental | 60:db8c5b7fc548 | 412 | { |
aburch1 | 81:996c0a3319b4 | 413 | logger.SendMessage("Command not recognized. Type \"help\" for more info.\r\n"); |
FairyMental | 60:db8c5b7fc548 | 414 | } |
aburch1 | 81:996c0a3319b4 | 415 | logger.SendMessage("\r\nAwaiting Command:\n\r"); |
FairyMental | 57:dfcdda1e42b6 | 416 | //Clear command! |
FairyMental | 57:dfcdda1e42b6 | 417 | //* NOTE * Setting first char in array to '\0' WILL NOT RESET IT...for some reason. |
FairyMental | 41:d222c043c96d | 418 | int i = 0; |
FairyMental | 41:d222c043c96d | 419 | for(i =0 ; i < crtChar; i++) |
FairyMental | 41:d222c043c96d | 420 | command[i] = ' '; |
FairyMental | 41:d222c043c96d | 421 | command[0] = 0; |
FairyMental | 41:d222c043c96d | 422 | crtChar = 0; |
FairyMental | 41:d222c043c96d | 423 | } |
FairyMental | 41:d222c043c96d | 424 | } |
FairyMental | 39:618ad21e2b34 | 425 | } |
FairyMental | 39:618ad21e2b34 | 426 | } |
aburch1 | 73:cfad270d2f2c | 427 | |
aburch1 | 73:cfad270d2f2c | 428 | void LoggingThread() |
aburch1 | 73:cfad270d2f2c | 429 | { |
aburch1 | 75:b44645bbf2d2 | 430 | //Thread::wait(300000); |
aburch1 | 73:cfad270d2f2c | 431 | // ARRON: TODO |
aburch1 | 73:cfad270d2f2c | 432 | |
aburch1 | 73:cfad270d2f2c | 433 | // - Printing messages |
aburch1 | 73:cfad270d2f2c | 434 | // - Out of memory |
aburch1 | 73:cfad270d2f2c | 435 | // - Current status |
aburch1 | 73:cfad270d2f2c | 436 | // - Display time every X seconds/minutes |
aburch1 | 73:cfad270d2f2c | 437 | |
aburch1 | 73:cfad270d2f2c | 438 | // Some queue system holding lines to print |
aburch1 | 73:cfad270d2f2c | 439 | // If the queue has something to print, print it. |
aburch1 | 75:b44645bbf2d2 | 440 | |
aburch1 | 75:b44645bbf2d2 | 441 | while(true) |
aburch1 | 75:b44645bbf2d2 | 442 | { |
aburch1 | 81:996c0a3319b4 | 443 | Thread::signal_wait(SIGNAL_printMessage); |
aburch1 | 81:996c0a3319b4 | 444 | |
aburch1 | 75:b44645bbf2d2 | 445 | if(logger.GetError()) |
aburch1 | 75:b44645bbf2d2 | 446 | { |
aburch1 | 75:b44645bbf2d2 | 447 | // Kill EVERYTHING |
aburch1 | 76:ee1f132e5744 | 448 | //mbed_reset(); |
aburch1 | 76:ee1f132e5744 | 449 | //NVIC_SystemReset(); |
aburch1 | 75:b44645bbf2d2 | 450 | } |
aburch1 | 75:b44645bbf2d2 | 451 | else if(!logger.GetMessage()) |
aburch1 | 75:b44645bbf2d2 | 452 | { |
aburch1 | 76:ee1f132e5744 | 453 | |
aburch1 | 75:b44645bbf2d2 | 454 | } |
aburch1 | 75:b44645bbf2d2 | 455 | |
aburch1 | 75:b44645bbf2d2 | 456 | } |
aburch1 | 73:cfad270d2f2c | 457 | } |
FairyMental | 34:09ed07f2acba | 458 | |
FairyMental | 34:09ed07f2acba | 459 | // Main thread |
FairyMental | 34:09ed07f2acba | 460 | int main() { |
FairyMental | 34:09ed07f2acba | 461 | |
FairyMental | 57:dfcdda1e42b6 | 462 | //Initialize all stuff you need here: |
FairyMental | 34:09ed07f2acba | 463 | measurer.init(); |
FairyMental | 34:09ed07f2acba | 464 | measurer.calib(); |
Netaphous | 54:53ee2d07d684 | 465 | |
FairyMental | 46:0de1f3c7d118 | 466 | localDate = new LocalDate(); |
aburch1 | 81:996c0a3319b4 | 467 | //Start message |
FairyMental | 34:09ed07f2acba | 468 | |
FairyMental | 34:09ed07f2acba | 469 | //Hook up timer interrupt |
FairyMental | 58:7fc6e3e4d746 | 470 | timer.attach(&SendSignalDoMeasure, sampleRate); |
FairyMental | 46:0de1f3c7d118 | 471 | realTimeDate.attach(&RealTimeDate,1.0); |
FairyMental | 34:09ed07f2acba | 472 | |
FairyMental | 57:dfcdda1e42b6 | 473 | //Run Threads |
aburch1 | 80:959151952153 | 474 | loggingThread = new Thread(); |
aburch1 | 80:959151952153 | 475 | loggingThread->start(LoggingThread); |
aburch1 | 80:959151952153 | 476 | |
aburch1 | 80:959151952153 | 477 | logger.SetThread(loggingThread); |
aburch1 | 80:959151952153 | 478 | |
FairyMental | 37:00775e368a71 | 479 | produceThread = new Thread(); |
FairyMental | 37:00775e368a71 | 480 | produceThread->start(ProducerThread); |
FairyMental | 37:00775e368a71 | 481 | measureThread = new Thread(); |
FairyMental | 37:00775e368a71 | 482 | measureThread->start(MeasureThread); |
FairyMental | 39:618ad21e2b34 | 483 | consumeThread = new Thread(); |
FairyMental | 39:618ad21e2b34 | 484 | consumeThread->start(ConsumeThread); |
aburch1 | 74:749727490f44 | 485 | |
aburch1 | 81:996c0a3319b4 | 486 | logger.SendMessage("\r\n--- W E L C O M E --\r\n"); |
FairyMental | 34:09ed07f2acba | 487 | |
aburch1 | 75:b44645bbf2d2 | 488 | while(true) |
martinsimpson | 32:260a288be58f | 489 | { |
aburch1 | 73:cfad270d2f2c | 490 | // Is there a sleep method that could be used instead or waiting and awaking every 3 seconds? |
aburch1 | 81:996c0a3319b4 | 491 | Thread::signal_wait(SIGNAL_terminate); |
FairyMental | 68:d3765f93c16a | 492 | } |
Netaphous | 70:ee19a73ed215 | 493 | } |