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