Skip to content

Commit 37dbbe5

Browse files
author
Cruz Monrreal
authored
Merge pull request #2 from aashishc1988/IdleLoop
Idle loop
2 parents f67cf64 + 94063ea commit 37dbbe5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

IdleLoop/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# IdleLoop example #
2+
3+
IdleLoop usage example for Mbed OS
4+
5+
This is an example showing the purpose of IdleLoop thread that is scheduled when no other threads are ready to run. In this example, we have a regular thread that is scheduled to run every 5 seconds. Because no other thread is scheduled to run, IdleLoop runs and toggles the LED.

IdleLoop/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "mbed.h"
2+
#include "rtos_idle.h"
3+
4+
// LED for main thread
5+
DigitalOut led2(LED2);
6+
// LED for idle thread
7+
DigitalOut led1(LED1);
8+
LowPowerTicker watchdogTicker;
9+
Thread watchdogThread;
10+
EventQueue watchdogQueue;
11+
12+
void watchdogRefreshHandler() {
13+
led2 = !led2;
14+
};
15+
16+
void watchdogRefreshIsr() {
17+
watchdogQueue.call(callback(&watchdogRefreshHandler));
18+
};
19+
20+
void new_idle_loop()
21+
{
22+
// Executes when no other thread is running
23+
led1 = !led1;
24+
}
25+
26+
int main() {
27+
28+
watchdogThread.start(callback(&watchdogQueue, &EventQueue::dispatch_forever));
29+
watchdogTicker.attach(callback(&watchdogRefreshIsr), 5);
30+
rtos_attach_idle_hook(&new_idle_loop);
31+
}

0 commit comments

Comments
 (0)