![](/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@36:19d3f752f9c3, 2017-04-05 (annotated)
- Committer:
- FairyMental
- Date:
- Wed Apr 05 13:33:32 2017 +0000
- Revision:
- 36:19d3f752f9c3
- Parent:
- 35:484e384f9bf1
- Child:
- 37:00775e368a71
Nick hasn't found a solution yet, implemented thread with a signal to do reading at a specified time.
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 | 34:09ed07f2acba | 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" |
martinsimpson | 32:260a288be58f | 9 | |
FairyMental | 36:19d3f752f9c3 | 10 | #define SIGNAL_doMeasure 1 |
FairyMental | 36:19d3f752f9c3 | 11 | #define SWITCH1_RELEASE 90 |
FairyMental | 34:09ed07f2acba | 12 | |
FairyMental | 34:09ed07f2acba | 13 | void thread1(); |
FairyMental | 34:09ed07f2acba | 14 | |
martinsimpson | 32:260a288be58f | 15 | DigitalOut myled(LED1); |
martinsimpson | 32:260a288be58f | 16 | I2C i2c2(I2C_SDA, I2C_SCL); |
martinsimpson | 32:260a288be58f | 17 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
FairyMental | 34:09ed07f2acba | 18 | HTS221 measurer(I2C_SDA, I2C_SCL); |
FairyMental | 34:09ed07f2acba | 19 | DigitalIn onBoardSwitch(USER_BUTTON); |
FairyMental | 34:09ed07f2acba | 20 | |
martinsimpson | 32:260a288be58f | 21 | |
FairyMental | 34:09ed07f2acba | 22 | //Threads |
FairyMental | 34:09ed07f2acba | 23 | Thread *t1; |
FairyMental | 34:09ed07f2acba | 24 | Thread *t2; |
FairyMental | 34:09ed07f2acba | 25 | int count= 0; |
FairyMental | 34:09ed07f2acba | 26 | //Class type |
FairyMental | 34:09ed07f2acba | 27 | class Measure { |
FairyMental | 34:09ed07f2acba | 28 | public: |
FairyMental | 34:09ed07f2acba | 29 | float temperature; |
FairyMental | 34:09ed07f2acba | 30 | float humidity; |
FairyMental | 34:09ed07f2acba | 31 | float pressure; |
FairyMental | 34:09ed07f2acba | 32 | |
FairyMental | 34:09ed07f2acba | 33 | //Constructor |
FairyMental | 34:09ed07f2acba | 34 | Measure(float f, float h, float p) { |
FairyMental | 34:09ed07f2acba | 35 | temperature = f; |
FairyMental | 34:09ed07f2acba | 36 | humidity = h; |
FairyMental | 34:09ed07f2acba | 37 | pressure = p; |
FairyMental | 34:09ed07f2acba | 38 | } |
FairyMental | 34:09ed07f2acba | 39 | }; |
FairyMental | 34:09ed07f2acba | 40 | |
FairyMental | 34:09ed07f2acba | 41 | //Mail queue |
FairyMental | 34:09ed07f2acba | 42 | Mail<Measure, 16> mail_box; |
FairyMental | 34:09ed07f2acba | 43 | |
FairyMental | 34:09ed07f2acba | 44 | |
FairyMental | 34:09ed07f2acba | 45 | // Call this on precise intervals |
FairyMental | 36:19d3f752f9c3 | 46 | void DoMeasure() { |
FairyMental | 35:484e384f9bf1 | 47 | |
FairyMental | 36:19d3f752f9c3 | 48 | while(true) |
FairyMental | 36:19d3f752f9c3 | 49 | { |
FairyMental | 36:19d3f752f9c3 | 50 | printf("Awaiting signal \r\n\r\n"); |
FairyMental | 36:19d3f752f9c3 | 51 | Thread::signal_wait(SIGNAL_doMeasure); |
FairyMental | 36:19d3f752f9c3 | 52 | //Read sample - make a copy |
FairyMental | 36:19d3f752f9c3 | 53 | float temperature = 0 , humidity = 0,pressure = 0; |
FairyMental | 34:09ed07f2acba | 54 | |
FairyMental | 34:09ed07f2acba | 55 | |
FairyMental | 34:09ed07f2acba | 56 | |
FairyMental | 36:19d3f752f9c3 | 57 | //Allocate a block from the memory pool |
FairyMental | 36:19d3f752f9c3 | 58 | Measure *measure = mail_box.alloc(); |
FairyMental | 36:19d3f752f9c3 | 59 | if (measure == NULL) |
FairyMental | 36:19d3f752f9c3 | 60 | { |
FairyMental | 36:19d3f752f9c3 | 61 | //Out of memory |
FairyMental | 36:19d3f752f9c3 | 62 | printf("Out of memory\n\r"); |
FairyMental | 36:19d3f752f9c3 | 63 | return; |
FairyMental | 36:19d3f752f9c3 | 64 | } |
FairyMental | 34:09ed07f2acba | 65 | |
FairyMental | 36:19d3f752f9c3 | 66 | //Fill in the data |
FairyMental | 36:19d3f752f9c3 | 67 | measurer.ReadTempHumi(&temperature,&humidity); |
FairyMental | 36:19d3f752f9c3 | 68 | barometer.get(); |
FairyMental | 36:19d3f752f9c3 | 69 | pressure = barometer.pressure(); |
FairyMental | 36:19d3f752f9c3 | 70 | |
FairyMental | 34:09ed07f2acba | 71 | |
FairyMental | 36:19d3f752f9c3 | 72 | measure->temperature = temperature; |
FairyMental | 36:19d3f752f9c3 | 73 | measure->humidity = humidity; |
FairyMental | 36:19d3f752f9c3 | 74 | measure->pressure = pressure; |
FairyMental | 36:19d3f752f9c3 | 75 | |
FairyMental | 36:19d3f752f9c3 | 76 | //Write to queue |
FairyMental | 36:19d3f752f9c3 | 77 | osStatus stat = mail_box.put(measure); //Note we are sending the "pointer" |
martinsimpson | 32:260a288be58f | 78 | |
FairyMental | 36:19d3f752f9c3 | 79 | //Check if succesful |
FairyMental | 36:19d3f752f9c3 | 80 | if (stat == osErrorResource) { |
FairyMental | 36:19d3f752f9c3 | 81 | printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); |
FairyMental | 36:19d3f752f9c3 | 82 | mail_box.free(measure); |
FairyMental | 36:19d3f752f9c3 | 83 | return; |
FairyMental | 36:19d3f752f9c3 | 84 | } |
FairyMental | 34:09ed07f2acba | 85 | } |
FairyMental | 34:09ed07f2acba | 86 | } |
FairyMental | 34:09ed07f2acba | 87 | |
FairyMental | 34:09ed07f2acba | 88 | //Normal priority thread (consumer) |
FairyMental | 34:09ed07f2acba | 89 | void thread1() |
FairyMental | 34:09ed07f2acba | 90 | { |
FairyMental | 34:09ed07f2acba | 91 | while (true) { |
FairyMental | 34:09ed07f2acba | 92 | //Block on the queue |
FairyMental | 34:09ed07f2acba | 93 | osEvent evt = mail_box.get(); |
FairyMental | 34:09ed07f2acba | 94 | |
FairyMental | 34:09ed07f2acba | 95 | //Check status |
FairyMental | 34:09ed07f2acba | 96 | if (evt.status == osEventMail) { |
FairyMental | 34:09ed07f2acba | 97 | Measure *measure = (Measure*)evt.value.p; //This is the pointer (address) |
FairyMental | 34:09ed07f2acba | 98 | //Make a copy |
FairyMental | 34:09ed07f2acba | 99 | printf("Consumer: %fC | %f% % | %f \r\n", measure->temperature, measure->humidity,measure->pressure); |
FairyMental | 34:09ed07f2acba | 100 | Measure msr(measure->temperature, measure->humidity,measure->pressure); |
FairyMental | 34:09ed07f2acba | 101 | //We are done with this, so give back the memory to the pool |
FairyMental | 34:09ed07f2acba | 102 | mail_box.free(measure); |
FairyMental | 34:09ed07f2acba | 103 | |
FairyMental | 34:09ed07f2acba | 104 | //Echo to the terminal |
FairyMental | 34:09ed07f2acba | 105 | |
FairyMental | 34:09ed07f2acba | 106 | } else { |
FairyMental | 34:09ed07f2acba | 107 | printf("ERROR: %x\n\r", evt.status); |
FairyMental | 34:09ed07f2acba | 108 | } |
FairyMental | 34:09ed07f2acba | 109 | |
FairyMental | 34:09ed07f2acba | 110 | } //end while |
FairyMental | 34:09ed07f2acba | 111 | } |
FairyMental | 36:19d3f752f9c3 | 112 | void SendSignalDoMeasure() |
FairyMental | 36:19d3f752f9c3 | 113 | { |
FairyMental | 36:19d3f752f9c3 | 114 | t2->signal_set(SIGNAL_doMeasure); |
FairyMental | 36:19d3f752f9c3 | 115 | } |
FairyMental | 34:09ed07f2acba | 116 | |
FairyMental | 34:09ed07f2acba | 117 | // Main thread |
FairyMental | 34:09ed07f2acba | 118 | int main() { |
FairyMental | 34:09ed07f2acba | 119 | |
FairyMental | 34:09ed07f2acba | 120 | measurer.init(); |
FairyMental | 34:09ed07f2acba | 121 | measurer.calib(); |
FairyMental | 34:09ed07f2acba | 122 | //Start message |
FairyMental | 34:09ed07f2acba | 123 | printf("Welcome\n"); |
FairyMental | 34:09ed07f2acba | 124 | |
FairyMental | 34:09ed07f2acba | 125 | //Hook up timer interrupt |
FairyMental | 36:19d3f752f9c3 | 126 | Ticker timer; |
FairyMental | 36:19d3f752f9c3 | 127 | timer.attach(&SendSignalDoMeasure, 5.0); |
FairyMental | 34:09ed07f2acba | 128 | |
FairyMental | 34:09ed07f2acba | 129 | //Threads |
FairyMental | 34:09ed07f2acba | 130 | t1 = new Thread(); |
FairyMental | 34:09ed07f2acba | 131 | t1->start(thread1); |
FairyMental | 34:09ed07f2acba | 132 | t2 = new Thread(); |
FairyMental | 36:19d3f752f9c3 | 133 | t2->start(DoMeasure); |
FairyMental | 34:09ed07f2acba | 134 | |
FairyMental | 34:09ed07f2acba | 135 | printf("Main Thread\n"); |
FairyMental | 34:09ed07f2acba | 136 | while(1) |
martinsimpson | 32:260a288be58f | 137 | { |
FairyMental | 35:484e384f9bf1 | 138 | Thread::wait(3000); |
FairyMental | 34:09ed07f2acba | 139 | float temp,humi; |
FairyMental | 34:09ed07f2acba | 140 | measurer.ReadTempHumi(&temp, &humi); |
FairyMental | 34:09ed07f2acba | 141 | barometer.get(); |
FairyMental | 36:19d3f752f9c3 | 142 | // t2->signal_set(SIGNAL_doMeasure); |
FairyMental | 36:19d3f752f9c3 | 143 | printf("Main Thread Measures: %fC %f %f \r\n", temp, humi,barometer.pressure()); |
FairyMental | 34:09ed07f2acba | 144 | |
martinsimpson | 32:260a288be58f | 145 | } |
Jonathan Austin |
0:2757d7abb7d9 | 146 | } |
FairyMental | 34:09ed07f2acba | 147 |