Lab version

Committer:
noutram
Date:
Wed Mar 09 12:47:33 2016 +0000
Revision:
0:ce9972ee58a1
Lab version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:ce9972ee58a1 1 #include "mbed.h"
noutram 0:ce9972ee58a1 2
noutram 0:ce9972ee58a1 3 // There is a LOT of code repetition here.
noutram 0:ce9972ee58a1 4 // Following bad practise, you can almost copy and paste the code for the red LED to add the greedn
noutram 0:ce9972ee58a1 5 // Further task: Consider ways to reduce this to make it simpler to maintain.
noutram 0:ce9972ee58a1 6
noutram 0:ce9972ee58a1 7 void sw1TimeOutHandler();
noutram 0:ce9972ee58a1 8 void sw1RisingEdge();
noutram 0:ce9972ee58a1 9 void sw1FallingEdge();
noutram 0:ce9972ee58a1 10 void sw2TimeOutHandler();
noutram 0:ce9972ee58a1 11 void sw2RisingEdge();
noutram 0:ce9972ee58a1 12 void sw2FallingEdge();
noutram 0:ce9972ee58a1 13
noutram 0:ce9972ee58a1 14 #define EDGE_RISEN 1
noutram 0:ce9972ee58a1 15 #define EDGE_FALLEN 0
noutram 0:ce9972ee58a1 16
noutram 0:ce9972ee58a1 17 //Global Objects
noutram 0:ce9972ee58a1 18 DigitalOut red_led(D7);
noutram 0:ce9972ee58a1 19 DigitalOut green_led(D5);
noutram 0:ce9972ee58a1 20 InterruptIn sw1(D4);
noutram 0:ce9972ee58a1 21 InterruptIn sw2(D3);
noutram 0:ce9972ee58a1 22
noutram 0:ce9972ee58a1 23 Timeout sw1TimeOut; //Used to prevent switch bounce
noutram 0:ce9972ee58a1 24 Timeout sw2TimeOut;
noutram 0:ce9972ee58a1 25 int sw1State = EDGE_FALLEN; //Initial state for switch 1
noutram 0:ce9972ee58a1 26 int sw2State = EDGE_FALLEN;
noutram 0:ce9972ee58a1 27
noutram 0:ce9972ee58a1 28 //Interrupt service routine for handling the timeout
noutram 0:ce9972ee58a1 29 void sw1TimeOutHandler() {
noutram 0:ce9972ee58a1 30 sw1TimeOut.detach(); //Stop the timeout counter firing
noutram 0:ce9972ee58a1 31
noutram 0:ce9972ee58a1 32 //Which event does this follow?
noutram 0:ce9972ee58a1 33 switch (sw1State) {
noutram 0:ce9972ee58a1 34 case EDGE_RISEN:
noutram 0:ce9972ee58a1 35 sw1.fall(&sw1FallingEdge); //Now wait for a falling edge
noutram 0:ce9972ee58a1 36 break;
noutram 0:ce9972ee58a1 37 case EDGE_FALLEN:
noutram 0:ce9972ee58a1 38 sw1.rise(&sw1RisingEdge); //Now wait for a rising edge
noutram 0:ce9972ee58a1 39 break;
noutram 0:ce9972ee58a1 40 } //end switch
noutram 0:ce9972ee58a1 41 }
noutram 0:ce9972ee58a1 42
noutram 0:ce9972ee58a1 43 //Interrupt service routine for a rising edge (press)
noutram 0:ce9972ee58a1 44 void sw1RisingEdge() {
noutram 0:ce9972ee58a1 45 sw1.rise(NULL); //Disable detecting more rising edges
noutram 0:ce9972ee58a1 46 sw1State = EDGE_RISEN; //Flag state
noutram 0:ce9972ee58a1 47 sw1TimeOut.attach(&sw1TimeOutHandler, 0.2); //Start timeout timer
noutram 0:ce9972ee58a1 48 }
noutram 0:ce9972ee58a1 49
noutram 0:ce9972ee58a1 50 //Interrupt service routive for SW1 falling edge (release)
noutram 0:ce9972ee58a1 51 void sw1FallingEdge() {
noutram 0:ce9972ee58a1 52 sw1.fall(NULL); //Disable this interrupt
noutram 0:ce9972ee58a1 53 red_led = !red_led; //Toggle LED
noutram 0:ce9972ee58a1 54 sw1State = EDGE_FALLEN; //Flag state
noutram 0:ce9972ee58a1 55 sw1TimeOut.attach(&sw1TimeOutHandler, 0.2); //Start timeout counter
noutram 0:ce9972ee58a1 56 }
noutram 0:ce9972ee58a1 57
noutram 0:ce9972ee58a1 58 void sw2TimeOutHandler() {
noutram 0:ce9972ee58a1 59 sw2TimeOut.detach(); //Stop the timeout counter firing
noutram 0:ce9972ee58a1 60
noutram 0:ce9972ee58a1 61 //Which event does this follow?
noutram 0:ce9972ee58a1 62 switch (sw2State) {
noutram 0:ce9972ee58a1 63 case EDGE_RISEN:
noutram 0:ce9972ee58a1 64 sw2.fall(&sw2FallingEdge); //Now wait for a falling edge
noutram 0:ce9972ee58a1 65 break;
noutram 0:ce9972ee58a1 66 case EDGE_FALLEN:
noutram 0:ce9972ee58a1 67 sw2.rise(&sw2RisingEdge); //Now wait for a rising edge
noutram 0:ce9972ee58a1 68 break;
noutram 0:ce9972ee58a1 69 } //end switch
noutram 0:ce9972ee58a1 70 }
noutram 0:ce9972ee58a1 71
noutram 0:ce9972ee58a1 72 //Interrupt service routine for a rising edge (press)
noutram 0:ce9972ee58a1 73 void sw2RisingEdge() {
noutram 0:ce9972ee58a1 74 sw2.rise(NULL); //Disable detecting more rising edges
noutram 0:ce9972ee58a1 75 sw2State = EDGE_RISEN; //Flag state
noutram 0:ce9972ee58a1 76 sw2TimeOut.attach(&sw2TimeOutHandler, 0.2); //Start timeout timer
noutram 0:ce9972ee58a1 77 }
noutram 0:ce9972ee58a1 78
noutram 0:ce9972ee58a1 79 //Interrupt service routive for SW1 falling edge (release)
noutram 0:ce9972ee58a1 80 void sw2FallingEdge() {
noutram 0:ce9972ee58a1 81 sw2.fall(NULL); //Disable this interrupt
noutram 0:ce9972ee58a1 82 green_led = !green_led; //Toggle LED
noutram 0:ce9972ee58a1 83 sw2State = EDGE_FALLEN; //Flag state
noutram 0:ce9972ee58a1 84 sw2TimeOut.attach(&sw2TimeOutHandler, 0.2); //Start timeout counter
noutram 0:ce9972ee58a1 85 }
noutram 0:ce9972ee58a1 86
noutram 0:ce9972ee58a1 87 //Main - only has to initialise and sleep
noutram 0:ce9972ee58a1 88 int main() {
noutram 0:ce9972ee58a1 89 //Initial logging message
noutram 0:ce9972ee58a1 90 puts("START");
noutram 0:ce9972ee58a1 91
noutram 0:ce9972ee58a1 92 //Initial state
noutram 0:ce9972ee58a1 93 red_led = 0; //Set RED LED to OFF
noutram 0:ce9972ee58a1 94 green_led = 0;
noutram 0:ce9972ee58a1 95
noutram 0:ce9972ee58a1 96 //Configure interrupts, wait for first rising edge
noutram 0:ce9972ee58a1 97 sw1.rise(&sw1RisingEdge);
noutram 0:ce9972ee58a1 98 sw2.rise(&sw2RisingEdge);
noutram 0:ce9972ee58a1 99
noutram 0:ce9972ee58a1 100 //Main Polling Loop
noutram 0:ce9972ee58a1 101 while (true) {
noutram 0:ce9972ee58a1 102
noutram 0:ce9972ee58a1 103 //Put CPU back to sleep
noutram 0:ce9972ee58a1 104 sleep();
noutram 0:ce9972ee58a1 105
noutram 0:ce9972ee58a1 106 //You can ONLY reach this point if an ISR wakes the CPU
noutram 0:ce9972ee58a1 107 puts("ISR just woke the MPU");
noutram 0:ce9972ee58a1 108
noutram 0:ce9972ee58a1 109 } //end while
noutram 0:ce9972ee58a1 110
noutram 0:ce9972ee58a1 111 }