Example solution of rapid polling (with C++)
Fork of Task330_polling by
main.cpp@2:4cf6f5cba257, 2019-09-18 (annotated)
- Committer:
- noutram
- Date:
- Wed Sep 18 10:54:46 2019 +0000
- Revision:
- 2:4cf6f5cba257
- Parent:
- 1:e84a51c98d75
2019
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:397b84c74d17 | 1 | #include "mbed.h" |
| noutram | 0:397b84c74d17 | 2 | #include "SWPoll.hpp" |
| noutram | 0:397b84c74d17 | 3 | |
| noutram | 0:397b84c74d17 | 4 | #define N 1000000 |
| noutram | 0:397b84c74d17 | 5 | #define RELEASED 0 |
| noutram | 0:397b84c74d17 | 6 | #define PRESSED 1 |
| noutram | 0:397b84c74d17 | 7 | |
| noutram | 0:397b84c74d17 | 8 | //Hardware objects |
| noutram | 0:397b84c74d17 | 9 | DigitalOut red_led(PE_15); //CountUp is in its critical section |
| noutram | 0:397b84c74d17 | 10 | DigitalOut yellow_led(PB_10); //CountDown is in its critical section |
| noutram | 0:397b84c74d17 | 11 | DigitalOut green_led(PB_11); //counter != 0 |
| noutram | 0:397b84c74d17 | 12 | DigitalOut onboardLED(LED1); |
| noutram | 0:397b84c74d17 | 13 | |
| noutram | 0:397b84c74d17 | 14 | DigitalIn button(USER_BUTTON); |
| noutram | 0:397b84c74d17 | 15 | DigitalIn sw1(PE_12); |
| noutram | 0:397b84c74d17 | 16 | DigitalIn sw2(PE_14); |
| noutram | 0:397b84c74d17 | 17 | |
| noutram | 0:397b84c74d17 | 18 | SWPoll switch1(sw1, red_led); |
| noutram | 0:397b84c74d17 | 19 | SWPoll switch2(sw2, green_led); |
| noutram | 0:397b84c74d17 | 20 | |
| noutram | 1:e84a51c98d75 | 21 | //LOOK AT SWPoll.hpp for the definition of the SWPoll class |
| noutram | 0:397b84c74d17 | 22 | |
| noutram | 0:397b84c74d17 | 23 | int main() { |
| noutram | 0:397b84c74d17 | 24 | |
| noutram | 0:397b84c74d17 | 25 | //Main uses a Timer |
| noutram | 0:397b84c74d17 | 26 | yellow_led = 1; |
| noutram | 0:397b84c74d17 | 27 | Timer t; |
| noutram | 0:397b84c74d17 | 28 | |
| noutram | 0:397b84c74d17 | 29 | //Now loop forever |
| noutram | 0:397b84c74d17 | 30 | t.start(); |
| noutram | 0:397b84c74d17 | 31 | while(1) { |
| noutram | 0:397b84c74d17 | 32 | //Flash the yellow on the "main thread" |
| noutram | 0:397b84c74d17 | 33 | if (t.read_ms() >= 500) { |
| noutram | 0:397b84c74d17 | 34 | yellow_led = !yellow_led; |
| noutram | 0:397b84c74d17 | 35 | t.reset(); |
| noutram | 0:397b84c74d17 | 36 | } |
| noutram | 0:397b84c74d17 | 37 | switch1.poll(); |
| noutram | 0:397b84c74d17 | 38 | switch2.poll(); |
| noutram | 0:397b84c74d17 | 39 | |
| noutram | 0:397b84c74d17 | 40 | }; |
| noutram | 0:397b84c74d17 | 41 | } |
| noutram | 0:397b84c74d17 | 42 | |
| noutram | 0:397b84c74d17 | 43 |
