Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SOFT253_Template_Weather_OS_54 by
main.cpp@67:8d0e88172e2a, 2017-04-10 (annotated)
- Committer:
- Netaphous
- Date:
- Mon Apr 10 01:37:12 2017 +0000
- Revision:
- 67:8d0e88172e2a
- Parent:
- 65:3723d2729b68
- Child:
- 68:d3765f93c16a
Fixed an issue with taking measurements of the current time causing an out of memory error
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 |
Netaphous | 56:4dd780d8fb47 | 28 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
Netaphous | 56:4dd780d8fb47 | 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 | } |
FairyMental | 57:dfcdda1e42b6 | 133 | // |
FairyMental | 57:dfcdda1e42b6 | 134 | // Compares two char arrays and returns result |
FairyMental | 57:dfcdda1e42b6 | 135 | // Param1: First char Array / pointer |
FairyMental | 57:dfcdda1e42b6 | 136 | // Param2: Second char Array / pointer |
FairyMental | 57:dfcdda1e42b6 | 137 | // Param3. Size of the smallest char arrays (between param1 and param2) |
FairyMental | 57:dfcdda1e42b6 | 138 | // Return: "-1" IF NOT EQUAL |
FairyMental | 57:dfcdda1e42b6 | 139 | // "1 " IF EQUAL |
FairyMental | 42:b1f29874ab70 | 140 | int CompareCommands(char command[],char targetcommand[], int size) |
FairyMental | 42:b1f29874ab70 | 141 | { |
FairyMental | 43:3983059e0d91 | 142 | int i; |
FairyMental | 43:3983059e0d91 | 143 | for(i = 0; i < size; i ++) |
FairyMental | 42:b1f29874ab70 | 144 | { |
FairyMental | 42:b1f29874ab70 | 145 | if(command[i] != targetcommand[i]) |
FairyMental | 42:b1f29874ab70 | 146 | return -1; |
FairyMental | 42:b1f29874ab70 | 147 | } |
FairyMental | 42:b1f29874ab70 | 148 | return 1; |
FairyMental | 42:b1f29874ab70 | 149 | } |
FairyMental | 57:dfcdda1e42b6 | 150 | // |
FairyMental | 57:dfcdda1e42b6 | 151 | // Reads commands through PUTTY and 'consumes the data' accordingly |
FairyMental | 57:dfcdda1e42b6 | 152 | // A.K.A. Consumer Thread |
FairyMental | 39:618ad21e2b34 | 153 | void ConsumeThread() |
FairyMental | 39:618ad21e2b34 | 154 | { |
FairyMental | 57:dfcdda1e42b6 | 155 | //Last character pressed read (last key input) |
FairyMental | 41:d222c043c96d | 156 | char charCmd; |
FairyMental | 57:dfcdda1e42b6 | 157 | //Char array that stores the command after user presses ENTER |
FairyMental | 41:d222c043c96d | 158 | char command[40]; |
FairyMental | 57:dfcdda1e42b6 | 159 | //Current Command Size |
FairyMental | 41:d222c043c96d | 160 | int crtChar = 0; |
FairyMental | 41:d222c043c96d | 161 | printf("\r\nAwaiting command:\r\n"); |
FairyMental | 39:618ad21e2b34 | 162 | while(1) |
FairyMental | 39:618ad21e2b34 | 163 | { |
FairyMental | 41:d222c043c96d | 164 | charCmd = NULL; |
FairyMental | 41:d222c043c96d | 165 | charCmd = getchar(); |
FairyMental | 41:d222c043c96d | 166 | if(charCmd != NULL) |
FairyMental | 41:d222c043c96d | 167 | { |
FairyMental | 57:dfcdda1e42b6 | 168 | //If BACKSPACE is pressed, Print "DEL" so it deletes last character typed. |
FairyMental | 57:dfcdda1e42b6 | 169 | if (charCmd == 127 && crtChar > 0 ) |
FairyMental | 43:3983059e0d91 | 170 | { |
FairyMental | 43:3983059e0d91 | 171 | printf("%c",charCmd); |
FairyMental | 43:3983059e0d91 | 172 | command[--crtChar] = '\0'; |
FairyMental | 43:3983059e0d91 | 173 | } |
FairyMental | 57:dfcdda1e42b6 | 174 | //If NOT enter AND NOT Backspace is pressed, SAVE the char |
FairyMental | 57:dfcdda1e42b6 | 175 | else if(charCmd != 13 && charCmd != 127) |
FairyMental | 41:d222c043c96d | 176 | { |
FairyMental | 41:d222c043c96d | 177 | command[crtChar++] = charCmd; |
FairyMental | 41:d222c043c96d | 178 | printf("%c",charCmd); |
FairyMental | 41:d222c043c96d | 179 | } |
FairyMental | 57:dfcdda1e42b6 | 180 | //If ENTER is pressed, PROCESS it |
FairyMental | 44:b523c9a9dd97 | 181 | else if(charCmd == 13) // If Enter is pressed |
FairyMental | 49:83bea7fb2728 | 182 | { |
FairyMental | 57:dfcdda1e42b6 | 183 | //Get first word of command: |
FairyMental | 43:3983059e0d91 | 184 | char *charPos; |
FairyMental | 43:3983059e0d91 | 185 | charPos = strtok(command," -,"); |
FairyMental | 57:dfcdda1e42b6 | 186 | |
FairyMental | 57:dfcdda1e42b6 | 187 | //Check if it's a "LIST" command |
FairyMental | 60:db8c5b7fc548 | 188 | if(CompareCommands(charPos, "read",4) == 1) |
FairyMental | 42:b1f29874ab70 | 189 | { |
FairyMental | 43:3983059e0d91 | 190 | charPos = strtok(NULL," -,"); |
FairyMental | 57:dfcdda1e42b6 | 191 | //Check if it's a "LIST ALL" command |
FairyMental | 43:3983059e0d91 | 192 | if(CompareCommands(charPos, "all",3) == 1) |
FairyMental | 43:3983059e0d91 | 193 | { |
FairyMental | 44:b523c9a9dd97 | 194 | printf("\r\n Printing all measures performed so far: \r\n"); |
Netaphous | 65:3723d2729b68 | 195 | |
Netaphous | 65:3723d2729b68 | 196 | // Changed to use circular buffer rather than list buffer |
Netaphous | 65:3723d2729b68 | 197 | buffer.readAll(); |
FairyMental | 60:db8c5b7fc548 | 198 | printf("\r\nD O N E ! \r\n"); |
FairyMental | 43:3983059e0d91 | 199 | } |
FairyMental | 57:dfcdda1e42b6 | 200 | //Check if it's a "LIST X" command |
FairyMental | 43:3983059e0d91 | 201 | else if(strtol(charPos,NULL,10) != 0) |
FairyMental | 43:3983059e0d91 | 202 | { |
FairyMental | 60:db8c5b7fc548 | 203 | printf("\r\n Printing %i measures: \r\n",atoi(charPos)); |
Netaphous | 65:3723d2729b68 | 204 | |
Netaphous | 65:3723d2729b68 | 205 | // Changed to use circular buffer rather than list buffer |
Netaphous | 65:3723d2729b68 | 206 | buffer.readX(atoi(charPos)); |
FairyMental | 60:db8c5b7fc548 | 207 | printf("\r\nD O N E ! \r\n"); |
FairyMental | 60:db8c5b7fc548 | 208 | } |
FairyMental | 60:db8c5b7fc548 | 209 | else |
FairyMental | 60:db8c5b7fc548 | 210 | { |
FairyMental | 60:db8c5b7fc548 | 211 | printf("Expected parameters: \"all\" | \"n\", where n is a number."); |
FairyMental | 43:3983059e0d91 | 212 | } |
FairyMental | 42:b1f29874ab70 | 213 | } |
FairyMental | 57:dfcdda1e42b6 | 214 | //Check if it's a "DELETE" command |
FairyMental | 44:b523c9a9dd97 | 215 | else if (CompareCommands(charPos,"delete",6) == 1) |
FairyMental | 44:b523c9a9dd97 | 216 | { |
FairyMental | 44:b523c9a9dd97 | 217 | charPos = strtok(NULL," -,"); |
FairyMental | 57:dfcdda1e42b6 | 218 | //Check if it's a "DELETE ALL" command |
FairyMental | 44:b523c9a9dd97 | 219 | if(CompareCommands(charPos,"all",3) == 1) |
FairyMental | 44:b523c9a9dd97 | 220 | { |
FairyMental | 44:b523c9a9dd97 | 221 | printf("\r\n Deleting all measures performed so far: \r\n"); |
Netaphous | 65:3723d2729b68 | 222 | |
Netaphous | 65:3723d2729b68 | 223 | // Changed to use circular buffer rather than list buffer |
Netaphous | 65:3723d2729b68 | 224 | buffer.deleteAll(); |
FairyMental | 60:db8c5b7fc548 | 225 | printf("\r\nElements deleted!\r\n"); |
FairyMental | 44:b523c9a9dd97 | 226 | } |
FairyMental | 57:dfcdda1e42b6 | 227 | //Check if it's a "DELETE X" command |
FairyMental | 44:b523c9a9dd97 | 228 | else if (strtol(charPos,NULL,10) != 0) |
FairyMental | 44:b523c9a9dd97 | 229 | { |
Netaphous | 65:3723d2729b68 | 230 | // Changed to use circular buffer rather than list buffer |
Netaphous | 65:3723d2729b68 | 231 | buffer.deleteX(atoi(charPos)); |
FairyMental | 60:db8c5b7fc548 | 232 | printf("\r\nElements deleted!\r\n"); |
FairyMental | 60:db8c5b7fc548 | 233 | } |
FairyMental | 60:db8c5b7fc548 | 234 | else |
FairyMental | 60:db8c5b7fc548 | 235 | { |
FairyMental | 60:db8c5b7fc548 | 236 | printf("Expected parameters: \"all\" | \"n\", where n is a number."); |
FairyMental | 44:b523c9a9dd97 | 237 | } |
FairyMental | 44:b523c9a9dd97 | 238 | |
FairyMental | 44:b523c9a9dd97 | 239 | } |
FairyMental | 60:db8c5b7fc548 | 240 | //Check if it's a "STATUS" command |
FairyMental | 60:db8c5b7fc548 | 241 | else if (CompareCommands(charPos,"status",6) == 1) |
FairyMental | 45:9a33f2bc2b4e | 242 | { |
FairyMental | 46:0de1f3c7d118 | 243 | char *ptr = localDate->ToString(); |
Netaphous | 65:3723d2729b68 | 244 | |
Netaphous | 65:3723d2729b68 | 245 | // Changed to use circular buffer rather than list buffer |
FairyMental | 49:83bea7fb2728 | 246 | if(logging == true) |
Netaphous | 65:3723d2729b68 | 247 | 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 | 248 | else |
Netaphous | 65:3723d2729b68 | 249 | 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 | 250 | } |
FairyMental | 57:dfcdda1e42b6 | 251 | //Check if it's a "SETTIME" command |
FairyMental | 48:a8219954b3f2 | 252 | else if (CompareCommands(charPos,"settime",7) == 1) |
FairyMental | 48:a8219954b3f2 | 253 | { |
FairyMental | 48:a8219954b3f2 | 254 | int h,m,s; |
FairyMental | 57:dfcdda1e42b6 | 255 | //Fetch 1st Param |
FairyMental | 48:a8219954b3f2 | 256 | charPos = strtok(NULL," ,"); |
FairyMental | 48:a8219954b3f2 | 257 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 258 | { |
FairyMental | 48:a8219954b3f2 | 259 | h = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 260 | } |
FairyMental | 57:dfcdda1e42b6 | 261 | //Fech 2nd Param |
FairyMental | 48:a8219954b3f2 | 262 | charPos = strtok(NULL," ,"); |
FairyMental | 48:a8219954b3f2 | 263 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 264 | { |
FairyMental | 48:a8219954b3f2 | 265 | m = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 266 | } |
FairyMental | 57:dfcdda1e42b6 | 267 | //Fetch 3rd 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 | s = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 272 | } |
FairyMental | 57:dfcdda1e42b6 | 273 | //Check if parameters are valid |
FairyMental | 48:a8219954b3f2 | 274 | if((h>=0 && h < 24) && (m>=0 && m<60) && (s>=0 && s<60)) |
FairyMental | 48:a8219954b3f2 | 275 | { |
FairyMental | 48:a8219954b3f2 | 276 | localDate->hour = h; |
FairyMental | 48:a8219954b3f2 | 277 | localDate->min = m; |
FairyMental | 48:a8219954b3f2 | 278 | localDate->sec = s; |
FairyMental | 60:db8c5b7fc548 | 279 | char *ptr = localDate->ToString(); |
FairyMental | 60:db8c5b7fc548 | 280 | printf("\r\nUpdated Date to: %s \r\n", ptr); |
FairyMental | 48:a8219954b3f2 | 281 | } |
FairyMental | 57:dfcdda1e42b6 | 282 | //If not valid, prompt user |
FairyMental | 48:a8219954b3f2 | 283 | else |
FairyMental | 48:a8219954b3f2 | 284 | { |
FairyMental | 60:db8c5b7fc548 | 285 | printf("\r\nWrong format! please use HH-MM-SS separated by spaces. \r\n"); |
FairyMental | 48:a8219954b3f2 | 286 | } |
FairyMental | 48:a8219954b3f2 | 287 | } |
FairyMental | 57:dfcdda1e42b6 | 288 | //Check if it's a "SETDATE" command |
FairyMental | 48:a8219954b3f2 | 289 | else if (CompareCommands(charPos,"setdate",7) == 1) |
FairyMental | 48:a8219954b3f2 | 290 | { |
FairyMental | 48:a8219954b3f2 | 291 | int d,m,y; |
FairyMental | 57:dfcdda1e42b6 | 292 | //Fetch 1st Parameter |
FairyMental | 48:a8219954b3f2 | 293 | charPos = strtok(NULL," ,"); |
FairyMental | 48:a8219954b3f2 | 294 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 295 | { |
FairyMental | 48:a8219954b3f2 | 296 | d = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 297 | } |
FairyMental | 57:dfcdda1e42b6 | 298 | //Fetch 2nd Parameter |
FairyMental | 48:a8219954b3f2 | 299 | charPos = strtok(NULL," ,"); |
FairyMental | 48:a8219954b3f2 | 300 | if(strtol(charPos,NULL,10) != 0) |
FairyMental | 48:a8219954b3f2 | 301 | { |
FairyMental | 48:a8219954b3f2 | 302 | m = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 303 | } |
FairyMental | 57:dfcdda1e42b6 | 304 | //Fetch 3rd 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 | y = atoi(charPos); |
FairyMental | 48:a8219954b3f2 | 309 | } |
FairyMental | 57:dfcdda1e42b6 | 310 | //Check if parameters are valid |
FairyMental | 48:a8219954b3f2 | 311 | if((d>=0 && d < 31) && (m>=0 && m<13)) |
FairyMental | 48:a8219954b3f2 | 312 | { |
FairyMental | 48:a8219954b3f2 | 313 | localDate->day = d; |
FairyMental | 48:a8219954b3f2 | 314 | localDate->month = m; |
FairyMental | 48:a8219954b3f2 | 315 | localDate->year = y; |
FairyMental | 60:db8c5b7fc548 | 316 | char *ptr = localDate->ToString(); |
FairyMental | 60:db8c5b7fc548 | 317 | printf("\r\nUpdated Date to: %s \r\n", ptr); |
FairyMental | 48:a8219954b3f2 | 318 | } |
FairyMental | 57:dfcdda1e42b6 | 319 | // Prompt user if they are not. |
FairyMental | 48:a8219954b3f2 | 320 | else |
FairyMental | 48:a8219954b3f2 | 321 | { |
FairyMental | 60:db8c5b7fc548 | 322 | printf("\r\nWrong format! please use DD-MM-YYYY separated by spaces. \r\n"); |
FairyMental | 48:a8219954b3f2 | 323 | } |
FairyMental | 48:a8219954b3f2 | 324 | } |
FairyMental | 57:dfcdda1e42b6 | 325 | // Check if it's a "LOGGING" command |
FairyMental | 60:db8c5b7fc548 | 326 | else if(CompareCommands(charPos,"state",5) == 1) |
FairyMental | 49:83bea7fb2728 | 327 | { |
FairyMental | 49:83bea7fb2728 | 328 | charPos = strtok(NULL," ,"); |
FairyMental | 57:dfcdda1e42b6 | 329 | //Check if it should be turned ON / OFF |
FairyMental | 49:83bea7fb2728 | 330 | if(CompareCommands(charPos,"on",2) == 1) |
FairyMental | 49:83bea7fb2728 | 331 | { |
FairyMental | 49:83bea7fb2728 | 332 | logging = true; |
FairyMental | 60:db8c5b7fc548 | 333 | printf("\r\nSampling turned ON!\r\n"); |
FairyMental | 49:83bea7fb2728 | 334 | } |
FairyMental | 49:83bea7fb2728 | 335 | else if (CompareCommands(charPos,"off",3) == 1) |
FairyMental | 49:83bea7fb2728 | 336 | { |
FairyMental | 49:83bea7fb2728 | 337 | logging = false; |
Netaphous | 65:3723d2729b68 | 338 | printf("\r\nSampling turned OFF!\r\n"); |
FairyMental | 60:db8c5b7fc548 | 339 | } |
FairyMental | 60:db8c5b7fc548 | 340 | else |
FairyMental | 60:db8c5b7fc548 | 341 | { |
FairyMental | 60:db8c5b7fc548 | 342 | printf("Expected parameters: \"on\" | \"off\""); |
FairyMental | 49:83bea7fb2728 | 343 | } |
FairyMental | 58:7fc6e3e4d746 | 344 | } |
FairyMental | 58:7fc6e3e4d746 | 345 | else if(CompareCommands(charPos,"sett",4) == 1) |
FairyMental | 58:7fc6e3e4d746 | 346 | { |
FairyMental | 58:7fc6e3e4d746 | 347 | charPos = strtok(NULL," ,"); |
FairyMental | 58:7fc6e3e4d746 | 348 | float auxRate = atof(charPos); |
Netaphous | 67:8d0e88172e2a | 349 | if(auxRate != 0 && auxRate > 0.01 && auxRate <= 60 ) |
FairyMental | 58:7fc6e3e4d746 | 350 | { |
FairyMental | 58:7fc6e3e4d746 | 351 | sampleRate = auxRate; |
FairyMental | 58:7fc6e3e4d746 | 352 | timer.detach(); |
FairyMental | 58:7fc6e3e4d746 | 353 | timer.attach(&SendSignalDoMeasure, sampleRate); |
FairyMental | 58:7fc6e3e4d746 | 354 | printf("\r\nSuccessfully updated sample rate to: %2.2f .\r\n",sampleRate); |
FairyMental | 58:7fc6e3e4d746 | 355 | } |
FairyMental | 58:7fc6e3e4d746 | 356 | else |
FairyMental | 58:7fc6e3e4d746 | 357 | { |
FairyMental | 60:db8c5b7fc548 | 358 | printf("\r\n Sample rate must be between greater than 0.1 or less equal than 60. \r\n"); |
FairyMental | 58:7fc6e3e4d746 | 359 | } |
FairyMental | 58:7fc6e3e4d746 | 360 | } |
FairyMental | 60:db8c5b7fc548 | 361 | else if (CompareCommands(charPos,"help",4) == 1 || CompareCommands(charPos,"?",1) == 1) |
FairyMental | 59:a69cd12dafca | 362 | { |
FairyMental | 59:a69cd12dafca | 363 | printf("\r\nAvailable Commands:\r\n"); |
FairyMental | 59:a69cd12dafca | 364 | printf(" read <ALL|N> - Read ALL or N first measures.\r\n"); |
FairyMental | 59:a69cd12dafca | 365 | printf(" delete <ALL|N> - Delete ALL or N first measures.\r\n"); |
FairyMental | 59:a69cd12dafca | 366 | printf(" setdate <DD> <MM> <YYYY> Set current date.\r\n"); |
FairyMental | 59:a69cd12dafca | 367 | printf(" settime <HH> <MM> <SS> Set current time.\r\n"); |
FairyMental | 59:a69cd12dafca | 368 | printf(" sett <T> Set sample rate (in seconds).\r\n"); |
FairyMental | 60:db8c5b7fc548 | 369 | printf(" status - Status report of device.\r\n"); |
FairyMental | 60:db8c5b7fc548 | 370 | printf(" state - <ON|OFF> - Turn sampling on or OFF.\r\n"); |
FairyMental | 59:a69cd12dafca | 371 | printf(" logging <ON|OFF> - Turn logging on or OFF.\r\n"); |
FairyMental | 59:a69cd12dafca | 372 | } |
FairyMental | 60:db8c5b7fc548 | 373 | else |
FairyMental | 60:db8c5b7fc548 | 374 | { |
FairyMental | 60:db8c5b7fc548 | 375 | printf("\r\n Command not recognized. Type \"help\" for more info.\r\n"); |
FairyMental | 60:db8c5b7fc548 | 376 | } |
FairyMental | 58:7fc6e3e4d746 | 377 | printf("\r\nAwaiting command: \r\n"); |
FairyMental | 57:dfcdda1e42b6 | 378 | //Clear command! |
FairyMental | 57:dfcdda1e42b6 | 379 | //* NOTE * Setting first char in array to '\0' WILL NOT RESET IT...for some reason. |
FairyMental | 41:d222c043c96d | 380 | int i = 0; |
FairyMental | 41:d222c043c96d | 381 | for(i =0 ; i < crtChar; i++) |
FairyMental | 41:d222c043c96d | 382 | command[i] = ' '; |
FairyMental | 41:d222c043c96d | 383 | command[0] = 0; |
FairyMental | 41:d222c043c96d | 384 | crtChar = 0; |
FairyMental | 41:d222c043c96d | 385 | } |
FairyMental | 41:d222c043c96d | 386 | } |
FairyMental | 39:618ad21e2b34 | 387 | } |
FairyMental | 39:618ad21e2b34 | 388 | } |
FairyMental | 34:09ed07f2acba | 389 | |
FairyMental | 34:09ed07f2acba | 390 | // Main thread |
FairyMental | 34:09ed07f2acba | 391 | int main() { |
FairyMental | 34:09ed07f2acba | 392 | |
FairyMental | 57:dfcdda1e42b6 | 393 | //Initialize all stuff you need here: |
FairyMental | 34:09ed07f2acba | 394 | measurer.init(); |
FairyMental | 34:09ed07f2acba | 395 | measurer.calib(); |
Netaphous | 54:53ee2d07d684 | 396 | |
FairyMental | 46:0de1f3c7d118 | 397 | localDate = new LocalDate(); |
FairyMental | 34:09ed07f2acba | 398 | //Start message |
FairyMental | 37:00775e368a71 | 399 | printf("Welcome\r\n"); |
FairyMental | 34:09ed07f2acba | 400 | |
FairyMental | 34:09ed07f2acba | 401 | //Hook up timer interrupt |
FairyMental | 58:7fc6e3e4d746 | 402 | |
FairyMental | 58:7fc6e3e4d746 | 403 | timer.attach(&SendSignalDoMeasure, sampleRate); |
FairyMental | 46:0de1f3c7d118 | 404 | realTimeDate.attach(&RealTimeDate,1.0); |
FairyMental | 34:09ed07f2acba | 405 | |
FairyMental | 57:dfcdda1e42b6 | 406 | //Run Threads |
FairyMental | 37:00775e368a71 | 407 | produceThread = new Thread(); |
FairyMental | 37:00775e368a71 | 408 | produceThread->start(ProducerThread); |
FairyMental | 37:00775e368a71 | 409 | measureThread = new Thread(); |
FairyMental | 37:00775e368a71 | 410 | measureThread->start(MeasureThread); |
FairyMental | 39:618ad21e2b34 | 411 | consumeThread = new Thread(); |
FairyMental | 39:618ad21e2b34 | 412 | consumeThread->start(ConsumeThread); |
FairyMental | 34:09ed07f2acba | 413 | |
FairyMental | 34:09ed07f2acba | 414 | printf("Main Thread\n"); |
FairyMental | 34:09ed07f2acba | 415 | while(1) |
martinsimpson | 32:260a288be58f | 416 | { |
FairyMental | 35:484e384f9bf1 | 417 | Thread::wait(3000); |
FairyMental | 37:00775e368a71 | 418 | // float temp,humi; |
FairyMental | 37:00775e368a71 | 419 | // measurer.ReadTempHumi(&temp, &humi); |
FairyMental | 37:00775e368a71 | 420 | // barometer.get(); |
FairyMental | 36:19d3f752f9c3 | 421 | // t2->signal_set(SIGNAL_doMeasure); |
FairyMental | 37:00775e368a71 | 422 | // printf("Main Thread Measures: %fC %f %f \r\n", temp, humi,barometer.pressure()); |
FairyMental | 34:09ed07f2acba | 423 | |
martinsimpson | 32:260a288be58f | 424 | } |
Jonathan Austin |
0:2757d7abb7d9 | 425 | } |
FairyMental | 34:09ed07f2acba | 426 |