Nicholas Outram
/
Task326
Lab version
main.cpp@1:a20f07949275, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:48:54 2017 +0000
- Revision:
- 1:a20f07949275
- Parent:
- 0:57de85b71119
updated for mbed-os 5.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:57de85b71119 | 1 | #include "mbed.h" |
noutram | 0:57de85b71119 | 2 | |
noutram | 0:57de85b71119 | 3 | //A slight improvement - however, the pressing of the switch still registers |
noutram | 0:57de85b71119 | 4 | //spurious falling edges! |
noutram | 0:57de85b71119 | 5 | |
noutram | 0:57de85b71119 | 6 | void sw1TimeOutHandler(); |
noutram | 0:57de85b71119 | 7 | void sw1FallingEdge(); |
noutram | 0:57de85b71119 | 8 | |
noutram | 0:57de85b71119 | 9 | |
noutram | 0:57de85b71119 | 10 | #define EDGE_RISEN 1 |
noutram | 0:57de85b71119 | 11 | #define EDGE_FALLEN 0 |
noutram | 0:57de85b71119 | 12 | |
noutram | 0:57de85b71119 | 13 | //Global Objects |
noutram | 0:57de85b71119 | 14 | DigitalOut red_led(D7); |
noutram | 0:57de85b71119 | 15 | DigitalOut green_led(D5); |
noutram | 0:57de85b71119 | 16 | InterruptIn sw1(D4); |
noutram | 0:57de85b71119 | 17 | InterruptIn sw2(D3); |
noutram | 0:57de85b71119 | 18 | |
noutram | 0:57de85b71119 | 19 | Timeout sw1TimeOut; //Used to prevent switch bounce |
noutram | 0:57de85b71119 | 20 | |
noutram | 0:57de85b71119 | 21 | //Interrupt service routine for handling the timeout |
noutram | 0:57de85b71119 | 22 | void sw1TimeOutHandler() { |
noutram | 0:57de85b71119 | 23 | sw1TimeOut.detach(); //Stop the timeout counter firing |
noutram | 0:57de85b71119 | 24 | sw1.fall(&sw1FallingEdge); //Now wait for a falling edge |
noutram | 0:57de85b71119 | 25 | } |
noutram | 0:57de85b71119 | 26 | |
noutram | 0:57de85b71119 | 27 | //Interrupt service routive for SW1 falling edge (release) |
noutram | 0:57de85b71119 | 28 | void sw1FallingEdge() { |
noutram | 0:57de85b71119 | 29 | sw1.fall(NULL); //Disable this interrupt |
noutram | 0:57de85b71119 | 30 | red_led = !red_led; //Toggle LED |
noutram | 0:57de85b71119 | 31 | sw1TimeOut.attach(&sw1TimeOutHandler, 0.2); //Start timeout counter |
noutram | 0:57de85b71119 | 32 | } |
noutram | 0:57de85b71119 | 33 | |
noutram | 0:57de85b71119 | 34 | //Main - only has to initialise and sleep |
noutram | 0:57de85b71119 | 35 | int main() { |
noutram | 0:57de85b71119 | 36 | //Initial logging message |
noutram | 0:57de85b71119 | 37 | puts("START"); |
noutram | 0:57de85b71119 | 38 | |
noutram | 0:57de85b71119 | 39 | //Initial state |
noutram | 0:57de85b71119 | 40 | red_led = 0; //Set RED LED to OFF |
noutram | 0:57de85b71119 | 41 | green_led = 1; |
noutram | 0:57de85b71119 | 42 | |
noutram | 0:57de85b71119 | 43 | //Configure interrupts, wait for first rising edge |
noutram | 0:57de85b71119 | 44 | sw1.fall(&sw1FallingEdge); |
noutram | 0:57de85b71119 | 45 | |
noutram | 0:57de85b71119 | 46 | //Main Polling Loop |
noutram | 0:57de85b71119 | 47 | while (true) { |
noutram | 0:57de85b71119 | 48 | |
noutram | 0:57de85b71119 | 49 | //Put CPU back to sleep |
noutram | 0:57de85b71119 | 50 | sleep(); |
noutram | 0:57de85b71119 | 51 | |
noutram | 0:57de85b71119 | 52 | //You can ONLY reach this point if an ISR wakes the CPU |
noutram | 0:57de85b71119 | 53 | puts("ISR just woke the MPU"); |
noutram | 0:57de85b71119 | 54 | |
noutram | 0:57de85b71119 | 55 | } //end while |
noutram | 0:57de85b71119 | 56 | |
noutram | 0:57de85b71119 | 57 | } |