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