![](/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@46:0de1f3c7d118, 2017-04-06 (annotated)
- Committer:
- FairyMental
- Date:
- Thu Apr 06 15:31:01 2017 +0000
- Revision:
- 46:0de1f3c7d118
- Parent:
- 45:9a33f2bc2b4e
- Child:
- 47:468a89d62c23
Implmeneted localDate class. It's basically a clock.
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" |
FairyMental | 40:ba083993b481 | 9 | #include "LinkedList.h" |
FairyMental | 41:d222c043c96d | 10 | #include <iostream> |
martinsimpson | 32:260a288be58f | 11 | |
FairyMental | 36:19d3f752f9c3 | 12 | #define SIGNAL_doMeasure 1 |
FairyMental | 36:19d3f752f9c3 | 13 | #define SWITCH1_RELEASE 90 |
FairyMental | 34:09ed07f2acba | 14 | |
FairyMental | 34:09ed07f2acba | 15 | |
martinsimpson | 32:260a288be58f | 16 | DigitalOut myled(LED1); |
martinsimpson | 32:260a288be58f | 17 | I2C i2c2(I2C_SDA, I2C_SCL); |
martinsimpson | 32:260a288be58f | 18 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
FairyMental | 34:09ed07f2acba | 19 | HTS221 measurer(I2C_SDA, I2C_SCL); |
FairyMental | 34:09ed07f2acba | 20 | DigitalIn onBoardSwitch(USER_BUTTON); |
FairyMental | 34:09ed07f2acba | 21 | |
FairyMental | 34:09ed07f2acba | 22 | //Threads |
FairyMental | 37:00775e368a71 | 23 | Thread *produceThread; |
FairyMental | 37:00775e368a71 | 24 | Thread *measureThread; |
FairyMental | 39:618ad21e2b34 | 25 | Thread *consumeThread; |
FairyMental | 38:e626a358e5e3 | 26 | |
FairyMental | 34:09ed07f2acba | 27 | int count= 0; |
FairyMental | 38:e626a358e5e3 | 28 | |
FairyMental | 34:09ed07f2acba | 29 | //Mail queue |
FairyMental | 34:09ed07f2acba | 30 | Mail<Measure, 16> mail_box; |
FairyMental | 34:09ed07f2acba | 31 | |
FairyMental | 39:618ad21e2b34 | 32 | LinkedList *listBuffer; |
FairyMental | 46:0de1f3c7d118 | 33 | LocalDate *localDate; |
FairyMental | 46:0de1f3c7d118 | 34 | void RealTimeDate() |
FairyMental | 46:0de1f3c7d118 | 35 | { |
FairyMental | 46:0de1f3c7d118 | 36 | localDate->TickSecond(); |
FairyMental | 46:0de1f3c7d118 | 37 | |
FairyMental | 46:0de1f3c7d118 | 38 | } |
FairyMental | 37:00775e368a71 | 39 | void MeasureThread() { |
FairyMental | 35:484e384f9bf1 | 40 | |
FairyMental | 36:19d3f752f9c3 | 41 | while(true) |
FairyMental | 36:19d3f752f9c3 | 42 | { |
FairyMental | 36:19d3f752f9c3 | 43 | Thread::signal_wait(SIGNAL_doMeasure); |
FairyMental | 36:19d3f752f9c3 | 44 | //Read sample - make a copy |
FairyMental | 36:19d3f752f9c3 | 45 | float temperature = 0 , humidity = 0,pressure = 0; |
FairyMental | 34:09ed07f2acba | 46 | |
FairyMental | 34:09ed07f2acba | 47 | |
FairyMental | 34:09ed07f2acba | 48 | |
FairyMental | 36:19d3f752f9c3 | 49 | //Allocate a block from the memory pool |
FairyMental | 36:19d3f752f9c3 | 50 | Measure *measure = mail_box.alloc(); |
FairyMental | 36:19d3f752f9c3 | 51 | if (measure == NULL) |
FairyMental | 36:19d3f752f9c3 | 52 | { |
FairyMental | 36:19d3f752f9c3 | 53 | //Out of memory |
FairyMental | 36:19d3f752f9c3 | 54 | printf("Out of memory\n\r"); |
FairyMental | 36:19d3f752f9c3 | 55 | return; |
FairyMental | 36:19d3f752f9c3 | 56 | } |
FairyMental | 34:09ed07f2acba | 57 | |
FairyMental | 36:19d3f752f9c3 | 58 | //Fill in the data |
FairyMental | 36:19d3f752f9c3 | 59 | measurer.ReadTempHumi(&temperature,&humidity); |
FairyMental | 36:19d3f752f9c3 | 60 | barometer.get(); |
FairyMental | 36:19d3f752f9c3 | 61 | pressure = barometer.pressure(); |
FairyMental | 36:19d3f752f9c3 | 62 | |
FairyMental | 34:09ed07f2acba | 63 | |
FairyMental | 36:19d3f752f9c3 | 64 | measure->temperature = temperature; |
FairyMental | 36:19d3f752f9c3 | 65 | measure->humidity = humidity; |
FairyMental | 36:19d3f752f9c3 | 66 | measure->pressure = pressure; |
FairyMental | 36:19d3f752f9c3 | 67 | |
FairyMental | 36:19d3f752f9c3 | 68 | //Write to queue |
FairyMental | 36:19d3f752f9c3 | 69 | osStatus stat = mail_box.put(measure); //Note we are sending the "pointer" |
martinsimpson | 32:260a288be58f | 70 | |
FairyMental | 36:19d3f752f9c3 | 71 | //Check if succesful |
FairyMental | 36:19d3f752f9c3 | 72 | if (stat == osErrorResource) { |
FairyMental | 36:19d3f752f9c3 | 73 | printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); |
FairyMental | 36:19d3f752f9c3 | 74 | mail_box.free(measure); |
FairyMental | 36:19d3f752f9c3 | 75 | return; |
FairyMental | 36:19d3f752f9c3 | 76 | } |
FairyMental | 34:09ed07f2acba | 77 | } |
FairyMental | 34:09ed07f2acba | 78 | } |
FairyMental | 34:09ed07f2acba | 79 | |
FairyMental | 34:09ed07f2acba | 80 | //Normal priority thread (consumer) |
FairyMental | 37:00775e368a71 | 81 | void ProducerThread() |
FairyMental | 34:09ed07f2acba | 82 | { |
FairyMental | 34:09ed07f2acba | 83 | while (true) { |
FairyMental | 34:09ed07f2acba | 84 | //Block on the queue |
FairyMental | 34:09ed07f2acba | 85 | osEvent evt = mail_box.get(); |
FairyMental | 34:09ed07f2acba | 86 | |
FairyMental | 34:09ed07f2acba | 87 | //Check status |
FairyMental | 34:09ed07f2acba | 88 | if (evt.status == osEventMail) { |
FairyMental | 34:09ed07f2acba | 89 | Measure *measure = (Measure*)evt.value.p; //This is the pointer (address) |
FairyMental | 34:09ed07f2acba | 90 | //Make a copy |
FairyMental | 41:d222c043c96d | 91 | // printf("Consumer: %fC | %f% % | %f \r\n", measure->temperature, measure->humidity,measure->pressure); |
FairyMental | 39:618ad21e2b34 | 92 | |
FairyMental | 34:09ed07f2acba | 93 | Measure msr(measure->temperature, measure->humidity,measure->pressure); |
FairyMental | 39:618ad21e2b34 | 94 | listBuffer->addValueEnd(msr); |
FairyMental | 34:09ed07f2acba | 95 | //We are done with this, so give back the memory to the pool |
FairyMental | 34:09ed07f2acba | 96 | mail_box.free(measure); |
FairyMental | 34:09ed07f2acba | 97 | |
FairyMental | 34:09ed07f2acba | 98 | //Echo to the terminal |
FairyMental | 34:09ed07f2acba | 99 | |
FairyMental | 34:09ed07f2acba | 100 | } else { |
FairyMental | 34:09ed07f2acba | 101 | printf("ERROR: %x\n\r", evt.status); |
FairyMental | 34:09ed07f2acba | 102 | } |
FairyMental | 34:09ed07f2acba | 103 | |
FairyMental | 34:09ed07f2acba | 104 | } //end while |
FairyMental | 34:09ed07f2acba | 105 | } |
FairyMental | 42:b1f29874ab70 | 106 | int CompareCommands(char command[],char targetcommand[], int size) |
FairyMental | 42:b1f29874ab70 | 107 | { |
FairyMental | 43:3983059e0d91 | 108 | int i; |
FairyMental | 43:3983059e0d91 | 109 | for(i = 0; i < size; i ++) |
FairyMental | 42:b1f29874ab70 | 110 | { |
FairyMental | 42:b1f29874ab70 | 111 | if(command[i] != targetcommand[i]) |
FairyMental | 42:b1f29874ab70 | 112 | return -1; |
FairyMental | 42:b1f29874ab70 | 113 | |
FairyMental | 42:b1f29874ab70 | 114 | } |
FairyMental | 42:b1f29874ab70 | 115 | return 1; |
FairyMental | 42:b1f29874ab70 | 116 | } |
FairyMental | 39:618ad21e2b34 | 117 | void ConsumeThread() |
FairyMental | 39:618ad21e2b34 | 118 | { |
FairyMental | 41:d222c043c96d | 119 | char charCmd; |
FairyMental | 41:d222c043c96d | 120 | char command[40]; |
FairyMental | 41:d222c043c96d | 121 | int crtChar = 0; |
FairyMental | 41:d222c043c96d | 122 | printf("\r\nAwaiting command:\r\n"); |
FairyMental | 39:618ad21e2b34 | 123 | while(1) |
FairyMental | 39:618ad21e2b34 | 124 | { |
FairyMental | 41:d222c043c96d | 125 | |
FairyMental | 41:d222c043c96d | 126 | charCmd = NULL; |
FairyMental | 41:d222c043c96d | 127 | charCmd = getchar(); |
FairyMental | 41:d222c043c96d | 128 | if(charCmd != NULL) |
FairyMental | 41:d222c043c96d | 129 | { |
FairyMental | 44:b523c9a9dd97 | 130 | if (charCmd == 127 && crtChar > 0 ) // If Backspace is pressed |
FairyMental | 43:3983059e0d91 | 131 | { |
FairyMental | 43:3983059e0d91 | 132 | printf("%c",charCmd); |
FairyMental | 43:3983059e0d91 | 133 | command[--crtChar] = '\0'; |
FairyMental | 43:3983059e0d91 | 134 | } |
FairyMental | 44:b523c9a9dd97 | 135 | else if(charCmd != 13 && charCmd != 127) // If NOT enter AND NOT Backspace is pressed |
FairyMental | 41:d222c043c96d | 136 | { |
FairyMental | 41:d222c043c96d | 137 | command[crtChar++] = charCmd; |
FairyMental | 41:d222c043c96d | 138 | printf("%c",charCmd); |
FairyMental | 41:d222c043c96d | 139 | } |
FairyMental | 44:b523c9a9dd97 | 140 | else if(charCmd == 13) // If Enter is pressed |
FairyMental | 41:d222c043c96d | 141 | { |
FairyMental | 42:b1f29874ab70 | 142 | printf("\r\n\r\nCommand entered: %s\r\n", command); |
FairyMental | 41:d222c043c96d | 143 | |
FairyMental | 43:3983059e0d91 | 144 | // this thing that follows splits a string into multiple strings, just like String.Split(char[] delimiters) |
FairyMental | 43:3983059e0d91 | 145 | // here we will check for commands and parameters :) |
FairyMental | 43:3983059e0d91 | 146 | char *charPos; |
FairyMental | 43:3983059e0d91 | 147 | charPos = strtok(command," -,"); |
FairyMental | 43:3983059e0d91 | 148 | if(CompareCommands(charPos, "list",4) == 1) |
FairyMental | 42:b1f29874ab70 | 149 | { |
FairyMental | 43:3983059e0d91 | 150 | charPos = strtok(NULL," -,"); |
FairyMental | 43:3983059e0d91 | 151 | if(CompareCommands(charPos, "all",3) == 1) |
FairyMental | 43:3983059e0d91 | 152 | { |
FairyMental | 44:b523c9a9dd97 | 153 | printf("\r\n Printing all measures performed so far: \r\n"); |
FairyMental | 43:3983059e0d91 | 154 | listBuffer->ListAll(); |
FairyMental | 44:b523c9a9dd97 | 155 | printf("\r\n D O N E ! \r\n"); |
FairyMental | 43:3983059e0d91 | 156 | } |
FairyMental | 43:3983059e0d91 | 157 | else if(strtol(charPos,NULL,10) != 0) |
FairyMental | 43:3983059e0d91 | 158 | { |
FairyMental | 43:3983059e0d91 | 159 | listBuffer->ListX(atoi(charPos)); |
FairyMental | 44:b523c9a9dd97 | 160 | printf("\r\n D O N E ! \r\n"); |
FairyMental | 43:3983059e0d91 | 161 | } |
FairyMental | 42:b1f29874ab70 | 162 | } |
FairyMental | 44:b523c9a9dd97 | 163 | else if (CompareCommands(charPos,"delete",6) == 1) |
FairyMental | 44:b523c9a9dd97 | 164 | { |
FairyMental | 44:b523c9a9dd97 | 165 | charPos = strtok(NULL," -,"); |
FairyMental | 44:b523c9a9dd97 | 166 | if(CompareCommands(charPos,"all",3) == 1) |
FairyMental | 44:b523c9a9dd97 | 167 | { |
FairyMental | 44:b523c9a9dd97 | 168 | printf("\r\n Deleting all measures performed so far: \r\n"); |
FairyMental | 44:b523c9a9dd97 | 169 | listBuffer->DeleteAll(); |
FairyMental | 44:b523c9a9dd97 | 170 | printf("\r\n D O N E ! \r\n"); |
FairyMental | 44:b523c9a9dd97 | 171 | } |
FairyMental | 44:b523c9a9dd97 | 172 | else if (strtol(charPos,NULL,10) != 0) |
FairyMental | 44:b523c9a9dd97 | 173 | { |
FairyMental | 44:b523c9a9dd97 | 174 | listBuffer->DeleteX(atoi(charPos)); |
FairyMental | 44:b523c9a9dd97 | 175 | printf("\r\n D O N E ! \r\n"); |
FairyMental | 44:b523c9a9dd97 | 176 | } |
FairyMental | 44:b523c9a9dd97 | 177 | |
FairyMental | 44:b523c9a9dd97 | 178 | } |
FairyMental | 45:9a33f2bc2b4e | 179 | else if (CompareCommands(charPos,"status",6) == 1) |
FairyMental | 45:9a33f2bc2b4e | 180 | { |
FairyMental | 46:0de1f3c7d118 | 181 | char *ptr = localDate->ToString(); |
FairyMental | 46:0de1f3c7d118 | 182 | printf("\r\n STATUS: \r\n # of measures: %i \r\n SAMPLING: ON \r\n Current Date: %s \r\n", listBuffer->GetSize(),ptr); |
FairyMental | 45:9a33f2bc2b4e | 183 | } |
FairyMental | 43:3983059e0d91 | 184 | // printf("%s \r\n", charPos); |
FairyMental | 43:3983059e0d91 | 185 | // charPos = strtok(NULL," -,"); |
FairyMental | 43:3983059e0d91 | 186 | |
FairyMental | 42:b1f29874ab70 | 187 | |
FairyMental | 41:d222c043c96d | 188 | printf("Awaiting command: \r\n"); |
FairyMental | 41:d222c043c96d | 189 | int i = 0; |
FairyMental | 41:d222c043c96d | 190 | for(i =0 ; i < crtChar; i++) |
FairyMental | 41:d222c043c96d | 191 | command[i] = ' '; |
FairyMental | 41:d222c043c96d | 192 | command[0] = 0; |
FairyMental | 41:d222c043c96d | 193 | crtChar = 0; |
FairyMental | 41:d222c043c96d | 194 | } |
FairyMental | 41:d222c043c96d | 195 | } |
FairyMental | 39:618ad21e2b34 | 196 | } |
FairyMental | 39:618ad21e2b34 | 197 | } |
FairyMental | 39:618ad21e2b34 | 198 | |
FairyMental | 42:b1f29874ab70 | 199 | |
FairyMental | 36:19d3f752f9c3 | 200 | void SendSignalDoMeasure() |
FairyMental | 36:19d3f752f9c3 | 201 | { |
FairyMental | 37:00775e368a71 | 202 | measureThread->signal_set(SIGNAL_doMeasure); |
FairyMental | 36:19d3f752f9c3 | 203 | } |
FairyMental | 34:09ed07f2acba | 204 | |
FairyMental | 34:09ed07f2acba | 205 | // Main thread |
FairyMental | 34:09ed07f2acba | 206 | int main() { |
FairyMental | 34:09ed07f2acba | 207 | |
FairyMental | 39:618ad21e2b34 | 208 | //Initialize stuff |
FairyMental | 34:09ed07f2acba | 209 | measurer.init(); |
FairyMental | 34:09ed07f2acba | 210 | measurer.calib(); |
FairyMental | 39:618ad21e2b34 | 211 | listBuffer = new LinkedList(); |
FairyMental | 46:0de1f3c7d118 | 212 | localDate = new LocalDate(); |
FairyMental | 34:09ed07f2acba | 213 | //Start message |
FairyMental | 37:00775e368a71 | 214 | printf("Welcome\r\n"); |
FairyMental | 34:09ed07f2acba | 215 | |
FairyMental | 34:09ed07f2acba | 216 | //Hook up timer interrupt |
FairyMental | 36:19d3f752f9c3 | 217 | Ticker timer; |
FairyMental | 44:b523c9a9dd97 | 218 | timer.attach(&SendSignalDoMeasure, 2.0); |
FairyMental | 46:0de1f3c7d118 | 219 | Ticker realTimeDate; |
FairyMental | 46:0de1f3c7d118 | 220 | realTimeDate.attach(&RealTimeDate,1.0); |
FairyMental | 34:09ed07f2acba | 221 | |
FairyMental | 34:09ed07f2acba | 222 | //Threads |
FairyMental | 37:00775e368a71 | 223 | produceThread = new Thread(); |
FairyMental | 37:00775e368a71 | 224 | produceThread->start(ProducerThread); |
FairyMental | 37:00775e368a71 | 225 | measureThread = new Thread(); |
FairyMental | 37:00775e368a71 | 226 | measureThread->start(MeasureThread); |
FairyMental | 39:618ad21e2b34 | 227 | consumeThread = new Thread(); |
FairyMental | 39:618ad21e2b34 | 228 | consumeThread->start(ConsumeThread); |
FairyMental | 34:09ed07f2acba | 229 | |
FairyMental | 34:09ed07f2acba | 230 | printf("Main Thread\n"); |
FairyMental | 34:09ed07f2acba | 231 | while(1) |
martinsimpson | 32:260a288be58f | 232 | { |
FairyMental | 35:484e384f9bf1 | 233 | Thread::wait(3000); |
FairyMental | 37:00775e368a71 | 234 | // float temp,humi; |
FairyMental | 37:00775e368a71 | 235 | // measurer.ReadTempHumi(&temp, &humi); |
FairyMental | 37:00775e368a71 | 236 | // barometer.get(); |
FairyMental | 36:19d3f752f9c3 | 237 | // t2->signal_set(SIGNAL_doMeasure); |
FairyMental | 37:00775e368a71 | 238 | // printf("Main Thread Measures: %fC %f %f \r\n", temp, humi,barometer.pressure()); |
FairyMental | 34:09ed07f2acba | 239 | |
martinsimpson | 32:260a288be58f | 240 | } |
Jonathan Austin |
0:2757d7abb7d9 | 241 | } |
FairyMental | 34:09ed07f2acba | 242 |