Nicholas Outram
/
Task324Solution_OO
updated for mbed-os 5.5
main.cpp@0:7da792c21683, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:48:19 2017 +0000
- Revision:
- 0:7da792c21683
updated for mbed-os 5.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:7da792c21683 | 1 | #include "mbed.h" |
noutram | 0:7da792c21683 | 2 | |
noutram | 0:7da792c21683 | 3 | DigitalOut red_led(D7); |
noutram | 0:7da792c21683 | 4 | DigitalOut yellow_led(D6); |
noutram | 0:7da792c21683 | 5 | DigitalOut green_led(D5); |
noutram | 0:7da792c21683 | 6 | DigitalIn SW1(D4); |
noutram | 0:7da792c21683 | 7 | DigitalIn SW2(D3); |
noutram | 0:7da792c21683 | 8 | |
noutram | 0:7da792c21683 | 9 | //This is the solution based on the proposed flowchart. |
noutram | 0:7da792c21683 | 10 | |
noutram | 0:7da792c21683 | 11 | class SwitchState { |
noutram | 0:7da792c21683 | 12 | private: |
noutram | 0:7da792c21683 | 13 | //Enumerated type (integer) |
noutram | 0:7da792c21683 | 14 | enum STATE { WAITING4PRESS=1, WAITING4BOUNCE_RISING=2, WAITING4RELEASE=4, WAITING4BOUNCE_FALLING=8 }; |
noutram | 0:7da792c21683 | 15 | |
noutram | 0:7da792c21683 | 16 | STATE state; //Current state in the FSM |
noutram | 0:7da792c21683 | 17 | DigitalIn& sw; //Reference to a GPIO Input |
noutram | 0:7da792c21683 | 18 | DigitalOut& led; //Reference to a GPIO Output |
noutram | 0:7da792c21683 | 19 | Timer tmr; //Internal timer used for debounce |
noutram | 0:7da792c21683 | 20 | |
noutram | 0:7da792c21683 | 21 | //Toggle the LED state |
noutram | 0:7da792c21683 | 22 | void toggleLed() { |
noutram | 0:7da792c21683 | 23 | led = !led; |
noutram | 0:7da792c21683 | 24 | } |
noutram | 0:7da792c21683 | 25 | |
noutram | 0:7da792c21683 | 26 | //Next state (circular pattern) |
noutram | 0:7da792c21683 | 27 | void gotoNextState() { |
noutram | 0:7da792c21683 | 28 | state = (state >> 3) | ((state << 1) & 0b1111); |
noutram | 0:7da792c21683 | 29 | } |
noutram | 0:7da792c21683 | 30 | |
noutram | 0:7da792c21683 | 31 | //Constructor |
noutram | 0:7da792c21683 | 32 | public: |
noutram | 0:7da792c21683 | 33 | SwitchState(DigitalIn& gpioIn, DigitalOut& gpioOut) : sw(gpioIn), led(gpioOut) { |
noutram | 0:7da792c21683 | 34 | state = WAITING4PRESS; |
noutram | 0:7da792c21683 | 35 | tmr.stop(); |
noutram | 0:7da792c21683 | 36 | tmr.reset(); |
noutram | 0:7da792c21683 | 37 | } |
noutram | 0:7da792c21683 | 38 | |
noutram | 0:7da792c21683 | 39 | //Update the state using polling |
noutram | 0:7da792c21683 | 40 | void updateState() |
noutram | 0:7da792c21683 | 41 | { |
noutram | 0:7da792c21683 | 42 | switch (state) { |
noutram | 0:7da792c21683 | 43 | |
noutram | 0:7da792c21683 | 44 | //Waiting for switch to be pressed |
noutram | 0:7da792c21683 | 45 | case WAITING4PRESS: |
noutram | 0:7da792c21683 | 46 | |
noutram | 0:7da792c21683 | 47 | if (sw == 1) { |
noutram | 0:7da792c21683 | 48 | //Output: start timer |
noutram | 0:7da792c21683 | 49 | tmr.reset(); |
noutram | 0:7da792c21683 | 50 | tmr.start(); |
noutram | 0:7da792c21683 | 51 | |
noutram | 0:7da792c21683 | 52 | //Next state |
noutram | 0:7da792c21683 | 53 | gotoNextState(); |
noutram | 0:7da792c21683 | 54 | puts("Entering state: WAITING4BOUNCE_RISING"); |
noutram | 0:7da792c21683 | 55 | } |
noutram | 0:7da792c21683 | 56 | break; |
noutram | 0:7da792c21683 | 57 | |
noutram | 0:7da792c21683 | 58 | //Waiting for 50ms to elapse |
noutram | 0:7da792c21683 | 59 | case WAITING4BOUNCE_RISING: |
noutram | 0:7da792c21683 | 60 | if (tmr.read_ms() > 50) { |
noutram | 0:7da792c21683 | 61 | //Outputs: Stop timer |
noutram | 0:7da792c21683 | 62 | tmr.stop(); |
noutram | 0:7da792c21683 | 63 | //Next state |
noutram | 0:7da792c21683 | 64 | gotoNextState(); |
noutram | 0:7da792c21683 | 65 | puts("Entering state: WAITING4RELEASE"); |
noutram | 0:7da792c21683 | 66 | } |
noutram | 0:7da792c21683 | 67 | break; |
noutram | 0:7da792c21683 | 68 | |
noutram | 0:7da792c21683 | 69 | //Waiting for switch to be released |
noutram | 0:7da792c21683 | 70 | case WAITING4RELEASE: |
noutram | 0:7da792c21683 | 71 | if (sw == 0) { |
noutram | 0:7da792c21683 | 72 | //Outputs: Toggle the LED and start timer |
noutram | 0:7da792c21683 | 73 | toggleLed(); |
noutram | 0:7da792c21683 | 74 | tmr.reset(); |
noutram | 0:7da792c21683 | 75 | tmr.start(); |
noutram | 0:7da792c21683 | 76 | //Next state |
noutram | 0:7da792c21683 | 77 | gotoNextState(); |
noutram | 0:7da792c21683 | 78 | puts("Entering state: WAITING4BOUNCE_FALLING"); |
noutram | 0:7da792c21683 | 79 | } |
noutram | 0:7da792c21683 | 80 | break; |
noutram | 0:7da792c21683 | 81 | |
noutram | 0:7da792c21683 | 82 | //Waiting 50ms for switch bounce |
noutram | 0:7da792c21683 | 83 | case WAITING4BOUNCE_FALLING: |
noutram | 0:7da792c21683 | 84 | if (tmr.read_ms() > 50) { |
noutram | 0:7da792c21683 | 85 | //Outputs: Reset timer 1 |
noutram | 0:7da792c21683 | 86 | tmr.stop(); |
noutram | 0:7da792c21683 | 87 | tmr.reset(); |
noutram | 0:7da792c21683 | 88 | |
noutram | 0:7da792c21683 | 89 | //Next state: |
noutram | 0:7da792c21683 | 90 | gotoNextState(); |
noutram | 0:7da792c21683 | 91 | puts("Entering state: WAITING4PRESS"); |
noutram | 0:7da792c21683 | 92 | } |
noutram | 0:7da792c21683 | 93 | break; |
noutram | 0:7da792c21683 | 94 | |
noutram | 0:7da792c21683 | 95 | default: |
noutram | 0:7da792c21683 | 96 | //Something has gone very wrong |
noutram | 0:7da792c21683 | 97 | tmr.stop(); |
noutram | 0:7da792c21683 | 98 | tmr.reset(); |
noutram | 0:7da792c21683 | 99 | led = 0; |
noutram | 0:7da792c21683 | 100 | state = WAITING4PRESS; |
noutram | 0:7da792c21683 | 101 | puts("ERROR"); |
noutram | 0:7da792c21683 | 102 | break; |
noutram | 0:7da792c21683 | 103 | |
noutram | 0:7da792c21683 | 104 | } //end switch |
noutram | 0:7da792c21683 | 105 | |
noutram | 0:7da792c21683 | 106 | } |
noutram | 0:7da792c21683 | 107 | }; |
noutram | 0:7da792c21683 | 108 | |
noutram | 0:7da792c21683 | 109 | |
noutram | 0:7da792c21683 | 110 | int main() { |
noutram | 0:7da792c21683 | 111 | |
noutram | 0:7da792c21683 | 112 | //Initial logging message |
noutram | 0:7da792c21683 | 113 | puts("START"); |
noutram | 0:7da792c21683 | 114 | |
noutram | 0:7da792c21683 | 115 | //Create two objects using different inputs and outputs |
noutram | 0:7da792c21683 | 116 | SwitchState s1(SW1, red_led); |
noutram | 0:7da792c21683 | 117 | SwitchState s2(SW2, green_led); |
noutram | 0:7da792c21683 | 118 | |
noutram | 0:7da792c21683 | 119 | //Main Polling Loop |
noutram | 0:7da792c21683 | 120 | while (true) { |
noutram | 0:7da792c21683 | 121 | |
noutram | 0:7da792c21683 | 122 | s1.updateState(); |
noutram | 0:7da792c21683 | 123 | s2.updateState(); |
noutram | 0:7da792c21683 | 124 | |
noutram | 0:7da792c21683 | 125 | } //end while |
noutram | 0:7da792c21683 | 126 | |
noutram | 0:7da792c21683 | 127 | } |
noutram | 0:7da792c21683 | 128 | |
noutram | 0:7da792c21683 | 129 |