updated for mbed-os 5.5

Committer:
noutram
Date:
Thu Jul 13 14:48:11 2017 +0000
Revision:
0:3590cc227050
updated for mbed-os 5.5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:3590cc227050 1 #include "mbed.h"
noutram 0:3590cc227050 2
noutram 0:3590cc227050 3 void updateState(int &sw, int &swState, DigitalOut &led, Timer &tmr) ;
noutram 0:3590cc227050 4
noutram 0:3590cc227050 5 DigitalOut red_led(D7);
noutram 0:3590cc227050 6 DigitalOut green_led(D5);
noutram 0:3590cc227050 7 DigitalIn SW1(D4);
noutram 0:3590cc227050 8 DigitalIn SW2(D3);
noutram 0:3590cc227050 9 //This is the solution based on the proposed flowchart.
noutram 0:3590cc227050 10 //The precise delay required may need adjusting
noutram 0:3590cc227050 11
noutram 0:3590cc227050 12 Timer tmr1;
noutram 0:3590cc227050 13 Timer tmr2;
noutram 0:3590cc227050 14
noutram 0:3590cc227050 15 #define WAITING4PRESS 0
noutram 0:3590cc227050 16 #define WAITING4BOUNCE_RISING 1
noutram 0:3590cc227050 17 #define WAITING4RELEASE 2
noutram 0:3590cc227050 18 #define WAITING4BOUNCE_FALLING 4
noutram 0:3590cc227050 19
noutram 0:3590cc227050 20 int main() {
noutram 0:3590cc227050 21 //Initial logging message
noutram 0:3590cc227050 22 puts("START");
noutram 0:3590cc227050 23
noutram 0:3590cc227050 24 //Initial state
noutram 0:3590cc227050 25 red_led = 0; //Set RED LED to OFF
noutram 0:3590cc227050 26 green_led = 0;
noutram 0:3590cc227050 27
noutram 0:3590cc227050 28 //Switch state
noutram 0:3590cc227050 29 int sw1State = 0;
noutram 0:3590cc227050 30 int sw2State = 0;
noutram 0:3590cc227050 31
noutram 0:3590cc227050 32 //Timers
noutram 0:3590cc227050 33 tmr1.stop();
noutram 0:3590cc227050 34 tmr1.reset();
noutram 0:3590cc227050 35 tmr2.stop();
noutram 0:3590cc227050 36 tmr2.reset();
noutram 0:3590cc227050 37
noutram 0:3590cc227050 38 //Initial logging message
noutram 0:3590cc227050 39 puts("Entering state WAITING4PRESS");
noutram 0:3590cc227050 40
noutram 0:3590cc227050 41 //Main Polling Loop
noutram 0:3590cc227050 42 while (true) {
noutram 0:3590cc227050 43
noutram 0:3590cc227050 44 //Poll inputs (without blocking)
noutram 0:3590cc227050 45 int sw1 = SW1;
noutram 0:3590cc227050 46 int sw2 = SW2;
noutram 0:3590cc227050 47
noutram 0:3590cc227050 48 updateState(sw1, sw1State, red_led, tmr1);
noutram 0:3590cc227050 49 updateState(sw2, sw2State, green_led, tmr2);
noutram 0:3590cc227050 50
noutram 0:3590cc227050 51 } //end while
noutram 0:3590cc227050 52
noutram 0:3590cc227050 53 }
noutram 0:3590cc227050 54
noutram 0:3590cc227050 55 // Update the state for a given switch and LED pair
noutram 0:3590cc227050 56 // Parameters:
noutram 0:3590cc227050 57 // sw is the switch input, 0 for released and 1 for pressed, passed by value
noutram 0:3590cc227050 58 // swState is an integer that keeps a record of state, passed by reference (updated by the function)
noutram 0:3590cc227050 59 // led is of type DigitalOut, connected to a LED
noutram 0:3590cc227050 60 // tmr is of type Timer, a reference to unique instance of a timer
noutram 0:3590cc227050 61 // This function cannot be called by an ISR
noutram 0:3590cc227050 62 void updateState(int sw, int &swState, DigitalOut &led, Timer &tmr)
noutram 0:3590cc227050 63 {
noutram 0:3590cc227050 64 //LED
noutram 0:3590cc227050 65 switch (swState) {
noutram 0:3590cc227050 66
noutram 0:3590cc227050 67 //Waiting for switch to be pressed
noutram 0:3590cc227050 68 case WAITING4PRESS:
noutram 0:3590cc227050 69
noutram 0:3590cc227050 70 if (sw == 1) {
noutram 0:3590cc227050 71 //Output: start timer
noutram 0:3590cc227050 72 tmr.reset();
noutram 0:3590cc227050 73 tmr.start();
noutram 0:3590cc227050 74
noutram 0:3590cc227050 75 //Next state
noutram 0:3590cc227050 76 swState = WAITING4BOUNCE_RISING;
noutram 0:3590cc227050 77 puts("Entering state: WAITING4BOUNCE_RISING");
noutram 0:3590cc227050 78 }
noutram 0:3590cc227050 79 break;
noutram 0:3590cc227050 80
noutram 0:3590cc227050 81 //Waiting for 50ms to elapse
noutram 0:3590cc227050 82 case WAITING4BOUNCE_RISING:
noutram 0:3590cc227050 83 if (tmr.read_ms() > 50) {
noutram 0:3590cc227050 84 //Outputs: Stop timer
noutram 0:3590cc227050 85 tmr.stop();
noutram 0:3590cc227050 86 //Next state
noutram 0:3590cc227050 87 swState = WAITING4RELEASE;
noutram 0:3590cc227050 88 puts("Entering state: WAITING4RELEASE");
noutram 0:3590cc227050 89 }
noutram 0:3590cc227050 90 break;
noutram 0:3590cc227050 91
noutram 0:3590cc227050 92 //Waiting for switch to be released
noutram 0:3590cc227050 93 case WAITING4RELEASE:
noutram 0:3590cc227050 94 if (sw == 0) {
noutram 0:3590cc227050 95 //Outputs: Toggle the LED and start timer
noutram 0:3590cc227050 96 led = !led;
noutram 0:3590cc227050 97 tmr.reset();
noutram 0:3590cc227050 98 tmr.start();
noutram 0:3590cc227050 99 //Next state
noutram 0:3590cc227050 100 swState = WAITING4BOUNCE_FALLING;
noutram 0:3590cc227050 101 puts("Entering state: WAITING4BOUNCE_FALLING");
noutram 0:3590cc227050 102 }
noutram 0:3590cc227050 103 break;
noutram 0:3590cc227050 104
noutram 0:3590cc227050 105 //Waiting 50ms for switch bounce
noutram 0:3590cc227050 106 case WAITING4BOUNCE_FALLING:
noutram 0:3590cc227050 107 if (tmr.read_ms() > 50) {
noutram 0:3590cc227050 108 //Outputs: Reset timer 1
noutram 0:3590cc227050 109 tmr.stop();
noutram 0:3590cc227050 110 tmr.reset();
noutram 0:3590cc227050 111
noutram 0:3590cc227050 112 //Next state:
noutram 0:3590cc227050 113 swState = WAITING4PRESS;
noutram 0:3590cc227050 114 puts("Entering state: WAITING4PRESS");
noutram 0:3590cc227050 115 }
noutram 0:3590cc227050 116 break;
noutram 0:3590cc227050 117
noutram 0:3590cc227050 118 default:
noutram 0:3590cc227050 119 //Something has gone very wrong
noutram 0:3590cc227050 120 tmr.stop();
noutram 0:3590cc227050 121 tmr.reset();
noutram 0:3590cc227050 122 led = 0;
noutram 0:3590cc227050 123 swState = WAITING4PRESS;
noutram 0:3590cc227050 124 puts("ERROR");
noutram 0:3590cc227050 125 break;
noutram 0:3590cc227050 126
noutram 0:3590cc227050 127 } //end switch
noutram 0:3590cc227050 128 } //end function