![](/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
- Committer:
- FairyMental
- Date:
- 2017-04-05
- Revision:
- 36:19d3f752f9c3
- Parent:
- 35:484e384f9bf1
- Child:
- 37:00775e368a71
File content as of revision 36:19d3f752f9c3:
#include "mbed.h" #include "rtos.h" #include "string.h" #include <stdio.h> #include <ctype.h> #include "hts221.h" #include "LPS25H.h" #define SIGNAL_doMeasure 1 #define SWITCH1_RELEASE 90 void thread1(); DigitalOut myled(LED1); I2C i2c2(I2C_SDA, I2C_SCL); LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); HTS221 measurer(I2C_SDA, I2C_SCL); DigitalIn onBoardSwitch(USER_BUTTON); //Threads Thread *t1; Thread *t2; int count= 0; //Class type class Measure { public: float temperature; float humidity; float pressure; //Constructor Measure(float f, float h, float p) { temperature = f; humidity = h; pressure = p; } }; //Mail queue Mail<Measure, 16> mail_box; // Call this on precise intervals void DoMeasure() { while(true) { printf("Awaiting signal \r\n\r\n"); Thread::signal_wait(SIGNAL_doMeasure); //Read sample - make a copy float temperature = 0 , humidity = 0,pressure = 0; //Allocate a block from the memory pool Measure *measure = mail_box.alloc(); if (measure == NULL) { //Out of memory printf("Out of memory\n\r"); return; } //Fill in the data measurer.ReadTempHumi(&temperature,&humidity); barometer.get(); pressure = barometer.pressure(); measure->temperature = temperature; measure->humidity = humidity; measure->pressure = pressure; //Write to queue osStatus stat = mail_box.put(measure); //Note we are sending the "pointer" //Check if succesful if (stat == osErrorResource) { printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); mail_box.free(measure); return; } } } //Normal priority thread (consumer) void thread1() { while (true) { //Block on the queue osEvent evt = mail_box.get(); //Check status if (evt.status == osEventMail) { Measure *measure = (Measure*)evt.value.p; //This is the pointer (address) //Make a copy printf("Consumer: %fC | %f% % | %f \r\n", measure->temperature, measure->humidity,measure->pressure); Measure msr(measure->temperature, measure->humidity,measure->pressure); //We are done with this, so give back the memory to the pool mail_box.free(measure); //Echo to the terminal } else { printf("ERROR: %x\n\r", evt.status); } } //end while } void SendSignalDoMeasure() { t2->signal_set(SIGNAL_doMeasure); } // Main thread int main() { measurer.init(); measurer.calib(); //Start message printf("Welcome\n"); //Hook up timer interrupt Ticker timer; timer.attach(&SendSignalDoMeasure, 5.0); //Threads t1 = new Thread(); t1->start(thread1); t2 = new Thread(); t2->start(DoMeasure); printf("Main Thread\n"); while(1) { Thread::wait(3000); float temp,humi; measurer.ReadTempHumi(&temp, &humi); barometer.get(); // t2->signal_set(SIGNAL_doMeasure); printf("Main Thread Measures: %fC %f %f \r\n", temp, humi,barometer.pressure()); } }