![](/media/cache/profiles/acab0116c354964a558e65bdd07ff047.jpg.50x50_q85.jpg)
Syggestions
Dependencies: CheckRTC LPS25H hts221
Fork of ELEC350-extended-referral2 by
main.cpp@34:62aae7d507e2, 2017-04-05 (annotated)
- Committer:
- FairyMental
- Date:
- Wed Apr 05 11:00:20 2017 +0000
- Revision:
- 34:62aae7d507e2
- Parent:
- 33:cffe0ae69aa6
- Child:
- 35:af125862c33e
1st push of project files.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:2757d7abb7d9 | 1 | #include "mbed.h" |
martinsimpson | 32:260a288be58f | 2 | #include "rtos.h" |
FairyMental | 34:62aae7d507e2 | 3 | #include "string.h" |
FairyMental | 34:62aae7d507e2 | 4 | #include <stdio.h> |
FairyMental | 34:62aae7d507e2 | 5 | #include <ctype.h> |
FairyMental | 34:62aae7d507e2 | 6 | #include "hts221.h" |
martinsimpson | 32:260a288be58f | 7 | #include "LPS25H.h" |
martinsimpson | 32:260a288be58f | 8 | |
FairyMental | 34:62aae7d507e2 | 9 | #define SWITCH1_RELEASE 1 |
FairyMental | 34:62aae7d507e2 | 10 | |
FairyMental | 34:62aae7d507e2 | 11 | void thread1(); |
FairyMental | 34:62aae7d507e2 | 12 | |
martinsimpson | 32:260a288be58f | 13 | DigitalOut myled(LED1); |
martinsimpson | 32:260a288be58f | 14 | I2C i2c2(I2C_SDA, I2C_SCL); |
martinsimpson | 32:260a288be58f | 15 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
FairyMental | 34:62aae7d507e2 | 16 | HTS221 measurer(I2C_SDA, I2C_SCL); |
FairyMental | 34:62aae7d507e2 | 17 | DigitalIn onBoardSwitch(USER_BUTTON); |
FairyMental | 34:62aae7d507e2 | 18 | |
martinsimpson | 32:260a288be58f | 19 | |
FairyMental | 34:62aae7d507e2 | 20 | //Threads |
FairyMental | 34:62aae7d507e2 | 21 | Thread *t1; |
FairyMental | 34:62aae7d507e2 | 22 | |
FairyMental | 34:62aae7d507e2 | 23 | //Class type |
FairyMental | 34:62aae7d507e2 | 24 | class Measure { |
FairyMental | 34:62aae7d507e2 | 25 | public: |
FairyMental | 34:62aae7d507e2 | 26 | float temperature; |
FairyMental | 34:62aae7d507e2 | 27 | float humidity; |
FairyMental | 34:62aae7d507e2 | 28 | float pressure; |
FairyMental | 34:62aae7d507e2 | 29 | |
FairyMental | 34:62aae7d507e2 | 30 | //Constructor |
FairyMental | 34:62aae7d507e2 | 31 | Measure(float f, float h, float p) { |
FairyMental | 34:62aae7d507e2 | 32 | temperature = f; |
FairyMental | 34:62aae7d507e2 | 33 | humidity = h; |
FairyMental | 34:62aae7d507e2 | 34 | pressure = p; |
FairyMental | 34:62aae7d507e2 | 35 | } |
FairyMental | 34:62aae7d507e2 | 36 | }; |
FairyMental | 34:62aae7d507e2 | 37 | |
FairyMental | 34:62aae7d507e2 | 38 | //Mail queue |
FairyMental | 34:62aae7d507e2 | 39 | Mail<Measure, 16> mail_box; |
FairyMental | 34:62aae7d507e2 | 40 | |
FairyMental | 34:62aae7d507e2 | 41 | |
FairyMental | 34:62aae7d507e2 | 42 | // Call this on precise intervals |
FairyMental | 34:62aae7d507e2 | 43 | void adcISR() { |
FairyMental | 34:62aae7d507e2 | 44 | |
FairyMental | 34:62aae7d507e2 | 45 | |
FairyMental | 34:62aae7d507e2 | 46 | //Read sample - make a copy |
FairyMental | 34:62aae7d507e2 | 47 | float temperature = 0 , humidity = 0,pressure = 0; |
FairyMental | 34:62aae7d507e2 | 48 | |
FairyMental | 34:62aae7d507e2 | 49 | measurer.ReadTempHumi(&temperature,&humidity); |
FairyMental | 34:62aae7d507e2 | 50 | barometer.get(); |
FairyMental | 34:62aae7d507e2 | 51 | pressure = barometer.pressure(); |
FairyMental | 34:62aae7d507e2 | 52 | |
FairyMental | 34:62aae7d507e2 | 53 | //Allocate a block from the memory pool |
FairyMental | 34:62aae7d507e2 | 54 | Measure *measure = mail_box.alloc(); |
FairyMental | 34:62aae7d507e2 | 55 | if (measure == NULL) { |
FairyMental | 34:62aae7d507e2 | 56 | //Out of memory |
FairyMental | 34:62aae7d507e2 | 57 | printf("Out of memory\n\r"); |
FairyMental | 34:62aae7d507e2 | 58 | return; |
FairyMental | 34:62aae7d507e2 | 59 | } |
FairyMental | 34:62aae7d507e2 | 60 | |
FairyMental | 34:62aae7d507e2 | 61 | //Fill in the data |
FairyMental | 34:62aae7d507e2 | 62 | measure->temperature = temperature; |
FairyMental | 34:62aae7d507e2 | 63 | measure->humidity = humidity; |
FairyMental | 34:62aae7d507e2 | 64 | measure->pressure = pressure; |
FairyMental | 34:62aae7d507e2 | 65 | // printf("%4.2fC %3.1f%% %6.1f \r\n", measure->temperature, measure->humidity,measure->pressure); |
FairyMental | 34:62aae7d507e2 | 66 | |
FairyMental | 34:62aae7d507e2 | 67 | //Write to queue |
FairyMental | 34:62aae7d507e2 | 68 | osStatus stat = mail_box.put(measure); //Note we are sending the "pointer" |
martinsimpson | 32:260a288be58f | 69 | |
FairyMental | 34:62aae7d507e2 | 70 | //Check if succesful |
FairyMental | 34:62aae7d507e2 | 71 | if (stat == osErrorResource) { |
FairyMental | 34:62aae7d507e2 | 72 | printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); |
FairyMental | 34:62aae7d507e2 | 73 | mail_box.free(measure); |
FairyMental | 34:62aae7d507e2 | 74 | return; |
FairyMental | 34:62aae7d507e2 | 75 | } |
FairyMental | 34:62aae7d507e2 | 76 | |
FairyMental | 34:62aae7d507e2 | 77 | } |
FairyMental | 34:62aae7d507e2 | 78 | |
FairyMental | 34:62aae7d507e2 | 79 | //Normal priority thread (consumer) |
FairyMental | 34:62aae7d507e2 | 80 | void thread1() |
FairyMental | 34:62aae7d507e2 | 81 | { |
FairyMental | 34:62aae7d507e2 | 82 | while (true) { |
FairyMental | 34:62aae7d507e2 | 83 | //Block on the queue |
FairyMental | 34:62aae7d507e2 | 84 | osEvent evt = mail_box.get(); |
FairyMental | 34:62aae7d507e2 | 85 | |
FairyMental | 34:62aae7d507e2 | 86 | //Check status |
FairyMental | 34:62aae7d507e2 | 87 | if (evt.status == osEventMail) { |
FairyMental | 34:62aae7d507e2 | 88 | Measure *measure = (Measure*)evt.value.p; //This is the pointer (address) |
FairyMental | 34:62aae7d507e2 | 89 | //Make a copy |
FairyMental | 34:62aae7d507e2 | 90 | printf("Consumer: %4.2fC | %3.1f%% | %6.1f \r\n", measure->temperature, measure->humidity,measure->pressure); |
FairyMental | 34:62aae7d507e2 | 91 | Measure msr(measure->temperature, measure->humidity,measure->pressure); |
FairyMental | 34:62aae7d507e2 | 92 | //We are done with this, so give back the memory to the pool |
FairyMental | 34:62aae7d507e2 | 93 | mail_box.free(measure); |
FairyMental | 34:62aae7d507e2 | 94 | |
FairyMental | 34:62aae7d507e2 | 95 | //Echo to the terminal |
FairyMental | 34:62aae7d507e2 | 96 | |
FairyMental | 34:62aae7d507e2 | 97 | } else { |
FairyMental | 34:62aae7d507e2 | 98 | printf("ERROR: %x\n\r", evt.status); |
FairyMental | 34:62aae7d507e2 | 99 | } |
FairyMental | 34:62aae7d507e2 | 100 | |
FairyMental | 34:62aae7d507e2 | 101 | } //end while |
FairyMental | 34:62aae7d507e2 | 102 | } |
FairyMental | 34:62aae7d507e2 | 103 | |
FairyMental | 34:62aae7d507e2 | 104 | |
FairyMental | 34:62aae7d507e2 | 105 | // Main thread |
FairyMental | 34:62aae7d507e2 | 106 | int main() { |
FairyMental | 34:62aae7d507e2 | 107 | |
FairyMental | 34:62aae7d507e2 | 108 | measurer.init(); |
FairyMental | 34:62aae7d507e2 | 109 | measurer.calib(); |
FairyMental | 34:62aae7d507e2 | 110 | //Start message |
FairyMental | 34:62aae7d507e2 | 111 | printf("Welcome\n"); |
FairyMental | 34:62aae7d507e2 | 112 | |
FairyMental | 34:62aae7d507e2 | 113 | //Hook up timer interrupt |
FairyMental | 34:62aae7d507e2 | 114 | Ticker timer; |
FairyMental | 34:62aae7d507e2 | 115 | timer.attach(&adcISR, 5); |
FairyMental | 34:62aae7d507e2 | 116 | |
FairyMental | 34:62aae7d507e2 | 117 | //Threads |
FairyMental | 34:62aae7d507e2 | 118 | t1 = new Thread(); |
FairyMental | 34:62aae7d507e2 | 119 | t1->start(thread1); |
FairyMental | 34:62aae7d507e2 | 120 | |
FairyMental | 34:62aae7d507e2 | 121 | printf("Main Thread\n"); |
FairyMental | 34:62aae7d507e2 | 122 | while(1) |
martinsimpson | 32:260a288be58f | 123 | { |
FairyMental | 34:62aae7d507e2 | 124 | Thread::wait(3000); |
FairyMental | 34:62aae7d507e2 | 125 | float tempCelsius,humi; |
FairyMental | 34:62aae7d507e2 | 126 | measurer.ReadTempHumi(&tempCelsius, &humi); |
FairyMental | 34:62aae7d507e2 | 127 | barometer.get(); |
FairyMental | 34:62aae7d507e2 | 128 | printf("Main ThreaD: %4.2fC %3.1f%% %6.1f \r\n", tempCelsius, humi,barometer.pressure()); |
FairyMental | 34:62aae7d507e2 | 129 | |
martinsimpson | 32:260a288be58f | 130 | } |
Jonathan Austin |
0:2757d7abb7d9 | 131 | } |
FairyMental | 34:62aae7d507e2 | 132 |