3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
Diff: main.cpp
- Revision:
- 36:19d3f752f9c3
- Parent:
- 35:484e384f9bf1
- Child:
- 37:00775e368a71
--- a/main.cpp Wed Apr 05 11:17:12 2017 +0000 +++ b/main.cpp Wed Apr 05 13:33:32 2017 +0000 @@ -1,12 +1,14 @@ + #include "mbed.h" #include "rtos.h" #include "string.h" #include <stdio.h> #include <ctype.h> - #include "hts221.h" +#include "hts221.h" #include "LPS25H.h" -#define SWITCH1_RELEASE 1 +#define SIGNAL_doMeasure 1 +#define SWITCH1_RELEASE 90 void thread1(); @@ -41,51 +43,45 @@ // Call this on precise intervals -void adcISR() { +void DoMeasure() { - while(1) - { - //Read sample - make a copy - float temperature = 0 , humidity = 0,pressure = 0; + 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(); - - // - // Initializing object with count variable and sending it through mail works perfectly fine, output si correct! - // But if I read the temperature / humidity / pressure using a ticker, I get 168 degrees :( . - // while I read from main thread, i get the correct values, which made me doubt the struct / mailbox, debugged it at my best practice but I couldn't get it sorted. + //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; + } - measure->temperature = temperature; // you can test the mailbox by assigning the count variable to measure->temperature, and see the expected value. - count++; - measure->humidity = humidity; - count++; - measure->pressure = pressure; - count++; - // printf("%4.2fC %3.1f%% %6.1f \r\n", measure->temperature, measure->humidity,measure->pressure); + //Fill in the data + measurer.ReadTempHumi(&temperature,&humidity); + barometer.get(); + pressure = barometer.pressure(); + - //Write to queue - osStatus stat = mail_box.put(measure); //Note we are sending the "pointer" + 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; - } - Thread::wait(5000); + //Check if succesful + if (stat == osErrorResource) { + printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); + mail_box.free(measure); + return; + } } } @@ -113,7 +109,10 @@ } //end while } - + void SendSignalDoMeasure() + { + t2->signal_set(SIGNAL_doMeasure); +} // Main thread int main() { @@ -124,24 +123,24 @@ printf("Welcome\n"); //Hook up timer interrupt -// Ticker timer; -// timer.attach(&adcISR, 5); + Ticker timer; + timer.attach(&SendSignalDoMeasure, 5.0); //Threads t1 = new Thread(); t1->start(thread1); t2 = new Thread(); - t2->start(adcISR); + t2->start(DoMeasure); printf("Main Thread\n"); while(1) { Thread::wait(3000); - printf("MainThreadActive \r\n"); float temp,humi; measurer.ReadTempHumi(&temp, &humi); barometer.get(); - printf("Main ThreaD: %fC %f %f \r\n", temp, humi,barometer.pressure()); + // t2->signal_set(SIGNAL_doMeasure); + printf("Main Thread Measures: %fC %f %f \r\n", temp, humi,barometer.pressure()); } }