Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:397b84c74d17, 2017-10-23 (annotated)
- Committer:
- noutram
- Date:
- Mon Oct 23 09:37:46 2017 +0000
- Revision:
- 0:397b84c74d17
- Child:
- 1:e84a51c98d75
Managing 2 switches with rapid polling
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 | //Function prototypes |
| noutram | 0:397b84c74d17 | 9 | void task1(); |
| noutram | 0:397b84c74d17 | 10 | void task2(); |
| noutram | 0:397b84c74d17 | 11 | |
| noutram | 0:397b84c74d17 | 12 | //Hardware objects |
| noutram | 0:397b84c74d17 | 13 | DigitalOut red_led(PE_15); //CountUp is in its critical section |
| noutram | 0:397b84c74d17 | 14 | DigitalOut yellow_led(PB_10); //CountDown is in its critical section |
| noutram | 0:397b84c74d17 | 15 | DigitalOut green_led(PB_11); //counter != 0 |
| noutram | 0:397b84c74d17 | 16 | DigitalOut onboardLED(LED1); |
| noutram | 0:397b84c74d17 | 17 | |
| noutram | 0:397b84c74d17 | 18 | DigitalIn button(USER_BUTTON); |
| noutram | 0:397b84c74d17 | 19 | DigitalIn sw1(PE_12); |
| noutram | 0:397b84c74d17 | 20 | DigitalIn sw2(PE_14); |
| noutram | 0:397b84c74d17 | 21 | |
| noutram | 0:397b84c74d17 | 22 | SWPoll switch1(sw1, red_led); |
| noutram | 0:397b84c74d17 | 23 | SWPoll switch2(sw2, green_led); |
| noutram | 0:397b84c74d17 | 24 | |
| noutram | 0:397b84c74d17 | 25 | |
| noutram | 0:397b84c74d17 | 26 | |
| noutram | 0:397b84c74d17 | 27 | int main() { |
| noutram | 0:397b84c74d17 | 28 | |
| noutram | 0:397b84c74d17 | 29 | //Main uses a Timer |
| noutram | 0:397b84c74d17 | 30 | yellow_led = 1; |
| noutram | 0:397b84c74d17 | 31 | Timer t; |
| noutram | 0:397b84c74d17 | 32 | |
| noutram | 0:397b84c74d17 | 33 | //Now loop forever |
| noutram | 0:397b84c74d17 | 34 | t.start(); |
| noutram | 0:397b84c74d17 | 35 | while(1) { |
| noutram | 0:397b84c74d17 | 36 | //Flash the yellow on the "main thread" |
| noutram | 0:397b84c74d17 | 37 | if (t.read_ms() >= 500) { |
| noutram | 0:397b84c74d17 | 38 | yellow_led = !yellow_led; |
| noutram | 0:397b84c74d17 | 39 | t.reset(); |
| noutram | 0:397b84c74d17 | 40 | } |
| noutram | 0:397b84c74d17 | 41 | switch1.poll(); |
| noutram | 0:397b84c74d17 | 42 | switch2.poll(); |
| noutram | 0:397b84c74d17 | 43 | |
| noutram | 0:397b84c74d17 | 44 | }; |
| noutram | 0:397b84c74d17 | 45 | } |
| noutram | 0:397b84c74d17 | 46 | |
| noutram | 0:397b84c74d17 | 47 | |
| noutram | 0:397b84c74d17 | 48 | //Thread 1 - polling sw1 and controlling the red LED |
| noutram | 0:397b84c74d17 | 49 | void task1() |
| noutram | 0:397b84c74d17 | 50 | { |
| noutram | 0:397b84c74d17 | 51 | //Loop forever |
| noutram | 0:397b84c74d17 | 52 | while(1) { |
| noutram | 0:397b84c74d17 | 53 | //Spin on sw1 |
| noutram | 0:397b84c74d17 | 54 | while (sw1 == RELEASED) {}; |
| noutram | 0:397b84c74d17 | 55 | //Allow short delay for switch bounce |
| noutram | 0:397b84c74d17 | 56 | Thread::wait(200); |
| noutram | 0:397b84c74d17 | 57 | //Spin again on sw1 |
| noutram | 0:397b84c74d17 | 58 | while (sw1 == PRESSED) {}; |
| noutram | 0:397b84c74d17 | 59 | //Toggle LED |
| noutram | 0:397b84c74d17 | 60 | red_led = !red_led; |
| noutram | 0:397b84c74d17 | 61 | //Again, wait for switch bounce |
| noutram | 0:397b84c74d17 | 62 | Thread::wait(200); |
| noutram | 0:397b84c74d17 | 63 | } |
| noutram | 0:397b84c74d17 | 64 | } |
| noutram | 0:397b84c74d17 | 65 | |
| noutram | 0:397b84c74d17 | 66 | //Thread 2 - polling sw2 and controlling the green LED |
| noutram | 0:397b84c74d17 | 67 | void task2() |
| noutram | 0:397b84c74d17 | 68 | { |
| noutram | 0:397b84c74d17 | 69 | //Loop forever |
| noutram | 0:397b84c74d17 | 70 | while(1) { |
| noutram | 0:397b84c74d17 | 71 | //Spin on sw2 |
| noutram | 0:397b84c74d17 | 72 | while (sw2 == RELEASED) {}; |
| noutram | 0:397b84c74d17 | 73 | //Allow short delay for switch bounce |
| noutram | 0:397b84c74d17 | 74 | Thread::wait(200); |
| noutram | 0:397b84c74d17 | 75 | //Spin again on sw2 |
| noutram | 0:397b84c74d17 | 76 | while (sw2 == PRESSED) {}; |
| noutram | 0:397b84c74d17 | 77 | //Toggle LED |
| noutram | 0:397b84c74d17 | 78 | green_led = !green_led; |
| noutram | 0:397b84c74d17 | 79 | //Again, wait for switch bounce |
| noutram | 0:397b84c74d17 | 80 | Thread::wait(200); |
| noutram | 0:397b84c74d17 | 81 | } |
| noutram | 0:397b84c74d17 | 82 | } |