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.
Fork of Task327 by
main.cpp@4:c85556b07606, 2017-10-12 (annotated)
- Committer:
- noutram
- Date:
- Thu Oct 12 14:57:29 2017 +0000
- Revision:
- 4:c85556b07606
- Parent:
- 3:384a8f9726ac
- Child:
- 6:fbc5b7916329
Now even simpler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:bf0e4c6e1f4d | 1 | #include "mbed.h" |
noutram | 0:bf0e4c6e1f4d | 2 | |
noutram | 3:384a8f9726ac | 3 | void updateState(); |
noutram | 0:bf0e4c6e1f4d | 4 | void sw1RisingEdge(); |
noutram | 0:bf0e4c6e1f4d | 5 | void sw1FallingEdge(); |
noutram | 0:bf0e4c6e1f4d | 6 | |
noutram | 0:bf0e4c6e1f4d | 7 | #define EDGE_RISEN 1 |
noutram | 0:bf0e4c6e1f4d | 8 | #define EDGE_FALLEN 0 |
noutram | 0:bf0e4c6e1f4d | 9 | |
noutram | 0:bf0e4c6e1f4d | 10 | //Global Objects |
noutram | 3:384a8f9726ac | 11 | DigitalOut led(LED1); |
noutram | 2:23bd19542152 | 12 | InterruptIn sw1(USER_BUTTON); |
noutram | 3:384a8f9726ac | 13 | |
noutram | 3:384a8f9726ac | 14 | //State |
noutram | 3:384a8f9726ac | 15 | static int sw1State = EDGE_FALLEN; //Initial state for switch 1 |
noutram | 0:bf0e4c6e1f4d | 16 | |
noutram | 0:bf0e4c6e1f4d | 17 | //Interrupt service routine for handling the timeout |
noutram | 3:384a8f9726ac | 18 | void updateState() { |
noutram | 3:384a8f9726ac | 19 | |
noutram | 3:384a8f9726ac | 20 | //Allow for switch bounce |
noutram | 3:384a8f9726ac | 21 | wait(0.1); |
noutram | 3:384a8f9726ac | 22 | |
noutram | 0:bf0e4c6e1f4d | 23 | //Which event does this follow? |
noutram | 0:bf0e4c6e1f4d | 24 | switch (sw1State) { |
noutram | 0:bf0e4c6e1f4d | 25 | case EDGE_RISEN: |
noutram | 4:c85556b07606 | 26 | sw1.fall(callback(&sw1FallingEdge)); //Now wait for a falling edge |
noutram | 0:bf0e4c6e1f4d | 27 | break; |
noutram | 0:bf0e4c6e1f4d | 28 | case EDGE_FALLEN: |
noutram | 4:c85556b07606 | 29 | sw1.rise(callback(&sw1RisingEdge)); //Now wait for a rising edge |
noutram | 0:bf0e4c6e1f4d | 30 | break; |
noutram | 0:bf0e4c6e1f4d | 31 | } //end switch |
noutram | 0:bf0e4c6e1f4d | 32 | } |
noutram | 0:bf0e4c6e1f4d | 33 | |
noutram | 0:bf0e4c6e1f4d | 34 | //Interrupt service routine for a rising edge (press) |
noutram | 0:bf0e4c6e1f4d | 35 | void sw1RisingEdge() { |
noutram | 0:bf0e4c6e1f4d | 36 | sw1.rise(NULL); //Disable detecting more rising edges |
noutram | 0:bf0e4c6e1f4d | 37 | sw1State = EDGE_RISEN; //Flag state |
noutram | 0:bf0e4c6e1f4d | 38 | } |
noutram | 0:bf0e4c6e1f4d | 39 | |
noutram | 0:bf0e4c6e1f4d | 40 | //Interrupt service routive for SW1 falling edge (release) |
noutram | 0:bf0e4c6e1f4d | 41 | void sw1FallingEdge() { |
noutram | 3:384a8f9726ac | 42 | sw1.fall(NULL); //Disable this interrupt |
noutram | 0:bf0e4c6e1f4d | 43 | sw1State = EDGE_FALLEN; //Flag state |
noutram | 0:bf0e4c6e1f4d | 44 | } |
noutram | 0:bf0e4c6e1f4d | 45 | |
noutram | 0:bf0e4c6e1f4d | 46 | //Main - only has to initialise and sleep |
noutram | 0:bf0e4c6e1f4d | 47 | int main() { |
noutram | 0:bf0e4c6e1f4d | 48 | |
noutram | 0:bf0e4c6e1f4d | 49 | //Configure interrupts, wait for first rising edge |
noutram | 4:c85556b07606 | 50 | sw1.rise(callback(&sw1RisingEdge)); |
noutram | 0:bf0e4c6e1f4d | 51 | |
noutram | 4:c85556b07606 | 52 | //Set LED to ON |
noutram | 4:c85556b07606 | 53 | led = 1; |
noutram | 4:c85556b07606 | 54 | |
noutram | 0:bf0e4c6e1f4d | 55 | //Main Polling Loop |
noutram | 0:bf0e4c6e1f4d | 56 | while (true) { |
noutram | 4:c85556b07606 | 57 | |
noutram | 0:bf0e4c6e1f4d | 58 | //Put CPU back to sleep |
noutram | 3:384a8f9726ac | 59 | sleep(); //woken by rising edge |
noutram | 3:384a8f9726ac | 60 | puts("Rising"); |
noutram | 3:384a8f9726ac | 61 | updateState(); |
noutram | 3:384a8f9726ac | 62 | |
noutram | 2:23bd19542152 | 63 | sleep(); //Falling edge |
noutram | 2:23bd19542152 | 64 | puts("Falling"); |
noutram | 3:384a8f9726ac | 65 | updateState(); |
noutram | 0:bf0e4c6e1f4d | 66 | |
noutram | 3:384a8f9726ac | 67 | //You can ONLY reach this point if an ISR wakes the CPU twice |
noutram | 4:c85556b07606 | 68 | led = !led; |
noutram | 0:bf0e4c6e1f4d | 69 | |
noutram | 0:bf0e4c6e1f4d | 70 | } //end while |
noutram | 0:bf0e4c6e1f4d | 71 | |
noutram | 0:bf0e4c6e1f4d | 72 | } |