Lab version
Fork of Task327 by
main.cpp@3:384a8f9726ac, 2017-10-12 (annotated)
- Committer:
- noutram
- Date:
- Thu Oct 12 14:35:26 2017 +0000
- Revision:
- 3:384a8f9726ac
- Parent:
- 2:23bd19542152
- Child:
- 4:c85556b07606
Tidy up
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 | 0:bf0e4c6e1f4d | 26 | sw1.fall(&sw1FallingEdge); //Now wait for a falling edge |
noutram | 0:bf0e4c6e1f4d | 27 | break; |
noutram | 0:bf0e4c6e1f4d | 28 | case EDGE_FALLEN: |
noutram | 0:bf0e4c6e1f4d | 29 | sw1.rise(&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 | //Initial logging message |
noutram | 0:bf0e4c6e1f4d | 49 | puts("START"); |
noutram | 0:bf0e4c6e1f4d | 50 | |
noutram | 0:bf0e4c6e1f4d | 51 | //Configure interrupts, wait for first rising edge |
noutram | 0:bf0e4c6e1f4d | 52 | sw1.rise(&sw1RisingEdge); |
noutram | 0:bf0e4c6e1f4d | 53 | |
noutram | 0:bf0e4c6e1f4d | 54 | //Main Polling Loop |
noutram | 0:bf0e4c6e1f4d | 55 | while (true) { |
noutram | 2:23bd19542152 | 56 | //Set LED to OFF |
noutram | 2:23bd19542152 | 57 | led = 0; |
noutram | 0:bf0e4c6e1f4d | 58 | |
noutram | 0:bf0e4c6e1f4d | 59 | //Put CPU back to sleep |
noutram | 3:384a8f9726ac | 60 | sleep(); //woken by rising edge |
noutram | 3:384a8f9726ac | 61 | puts("Rising"); |
noutram | 3:384a8f9726ac | 62 | updateState(); |
noutram | 3:384a8f9726ac | 63 | |
noutram | 2:23bd19542152 | 64 | sleep(); //Falling edge |
noutram | 2:23bd19542152 | 65 | puts("Falling"); |
noutram | 3:384a8f9726ac | 66 | updateState(); |
noutram | 0:bf0e4c6e1f4d | 67 | |
noutram | 3:384a8f9726ac | 68 | //You can ONLY reach this point if an ISR wakes the CPU twice |
noutram | 2:23bd19542152 | 69 | for (int n=0; n<10; n++) { |
noutram | 2:23bd19542152 | 70 | led = !led; |
noutram | 2:23bd19542152 | 71 | wait(0.1); |
noutram | 2:23bd19542152 | 72 | } |
noutram | 0:bf0e4c6e1f4d | 73 | |
noutram | 0:bf0e4c6e1f4d | 74 | } //end while |
noutram | 0:bf0e4c6e1f4d | 75 | |
noutram | 0:bf0e4c6e1f4d | 76 | } |