Steve Belvin
/
TimerCallHelloWorld
Uses the Timeout library to create a periodic call to a function that blinks the LEDs.
main.cpp@0:bbcd143cb457, 2010-11-29 (annotated)
- Committer:
- steve_b
- Date:
- Mon Nov 29 00:25:19 2010 +0000
- Revision:
- 0:bbcd143cb457
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
steve_b | 0:bbcd143cb457 | 1 | #include "mbed.h" |
steve_b | 0:bbcd143cb457 | 2 | |
steve_b | 0:bbcd143cb457 | 3 | int rtCount; |
steve_b | 0:bbcd143cb457 | 4 | Timeout timeTick; |
steve_b | 0:bbcd143cb457 | 5 | DigitalOut led1(LED1); |
steve_b | 0:bbcd143cb457 | 6 | DigitalOut led2(LED2); |
steve_b | 0:bbcd143cb457 | 7 | |
steve_b | 0:bbcd143cb457 | 8 | void flip_1() { |
steve_b | 0:bbcd143cb457 | 9 | led1 = !led1; |
steve_b | 0:bbcd143cb457 | 10 | } |
steve_b | 0:bbcd143cb457 | 11 | |
steve_b | 0:bbcd143cb457 | 12 | void flip_2() { |
steve_b | 0:bbcd143cb457 | 13 | led2 = !led2; |
steve_b | 0:bbcd143cb457 | 14 | } |
steve_b | 0:bbcd143cb457 | 15 | |
steve_b | 0:bbcd143cb457 | 16 | void action() { |
steve_b | 0:bbcd143cb457 | 17 | // Set timer to call back |
steve_b | 0:bbcd143cb457 | 18 | timeTick.attach(&action, 0.1); // setup flipper to call flip after 2 seconds |
steve_b | 0:bbcd143cb457 | 19 | rtCount++; // increment realtime counter |
steve_b | 0:bbcd143cb457 | 20 | if (rtCount == 5) { |
steve_b | 0:bbcd143cb457 | 21 | flip_1(); |
steve_b | 0:bbcd143cb457 | 22 | } else if (rtCount == 10) { |
steve_b | 0:bbcd143cb457 | 23 | flip_1(); |
steve_b | 0:bbcd143cb457 | 24 | flip_2(); |
steve_b | 0:bbcd143cb457 | 25 | rtCount = 0; |
steve_b | 0:bbcd143cb457 | 26 | } |
steve_b | 0:bbcd143cb457 | 27 | } |
steve_b | 0:bbcd143cb457 | 28 | |
steve_b | 0:bbcd143cb457 | 29 | int main() { |
steve_b | 0:bbcd143cb457 | 30 | rtCount = 0; |
steve_b | 0:bbcd143cb457 | 31 | led1 = 1; |
steve_b | 0:bbcd143cb457 | 32 | led2 = 1; |
steve_b | 0:bbcd143cb457 | 33 | timeTick.attach(&action, 0.1); // setup flipper to call flip after 2 seconds |
steve_b | 0:bbcd143cb457 | 34 | |
steve_b | 0:bbcd143cb457 | 35 | // spin in a main loop. flipper will interrupt it to call flip |
steve_b | 0:bbcd143cb457 | 36 | while (1) {} |
steve_b | 0:bbcd143cb457 | 37 | } |