updated for mbed-os 5.5

Committer:
noutram
Date:
Thu Jul 13 14:48:03 2017 +0000
Revision:
0:8ab606ae2e1d
updated for mbed-os 5.5

Who changed what in which revision?

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