Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
// An EventQueue is a very useful construct in Mbed OS, it allows you to schedule events
// and to defer from one context to another (e.g. from ISR to normal thread) without
// writing your own state machines, and while maintaining context.
// https://os.mbed.com/docs/v5.6/tutorials/the-eventqueue-api.html
EventQueue queue;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led4(LED4);
InterruptIn btn(BUTTON1);
//Setup the Sensor
Sht31 sht31(I2C_SDA, I2C_SCL);
float temp;
float humidity;
int eventId = 0;
void blink_led() {
led1 = !led1;
temp = sht31.readTemperature ();
humidity = sht31.readHumidity();
printf("Temp %.2f Humidity %.2f\n", temp, humidity);
}
void blinkWarning(){
led4 = !led4;
}
void checkTemps(){
printf("Checking Temperatures\n");
if (temp > 30.0){
led2 = 1;
printf("Too Hot!!!");
}
else{
led2 = 0;
}
if (temp < 5.0){
printf ("Too Cold!!\n");
if (eventId == 0){
eventId = queue.call_every(500, &blinkWarning);
printf("Sub Event Triggered Id is %d\n", eventId);
}
}
else{
if (eventId > 0){
queue.cancel(eventId);
led4 = 0;
eventId = 0;
}
}
}
// This does not run in an ISR, so it's safe to use `printf` or other blocking calls
void btn_handler() {
printf("Button Pressed\n");
led2 = !led2;
}
int main() {
// Schedule an event to run every second
queue.call_every(1000, &blink_led);
queue.call_every(1000, &checkTemps);
// Normally code in the `fall` handler runs in an ISR,
// but you can directly defer it to the thread that runs the queue
btn.rise(queue.event(&btn_handler));
// Because the simulator does not support multiple threads,
// we have to call dispatch_forever from the main thread.
// Typically you'd run this on a separate thread within Mbed's RTOS.
queue.dispatch_forever();
}