Version 5
Dependencies: BMP280 TextLCD BME280
SWPoll.hpp@9:f5eae5211225, 2018-12-07 (annotated)
- Committer:
- Alix955
- Date:
- Fri Dec 07 13:24:50 2018 +0000
- Revision:
- 9:f5eae5211225
- Parent:
- 0:f9a18207d99c
Version 5, mine & sams versions merged
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
O_Thom | 0:f9a18207d99c | 1 | #include "mbed.h" |
O_Thom | 0:f9a18207d99c | 2 | class SWPoll { |
O_Thom | 0:f9a18207d99c | 3 | private: |
O_Thom | 0:f9a18207d99c | 4 | enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE}; |
O_Thom | 0:f9a18207d99c | 5 | State state; // Internal state |
O_Thom | 0:f9a18207d99c | 6 | DigitalIn& sw; // These are references (aliases) and MUST be initialised |
O_Thom | 0:f9a18207d99c | 7 | int status; // "" |
O_Thom | 0:f9a18207d99c | 8 | Timer t; // Each instance has it's own timer, so this is is finite |
O_Thom | 0:f9a18207d99c | 9 | |
O_Thom | 0:f9a18207d99c | 10 | public: |
O_Thom | 0:f9a18207d99c | 11 | //Constructor - MUST be given two parameters (for the switch and led) BY REFERENCE |
O_Thom | 0:f9a18207d99c | 12 | SWPoll(DigitalIn& gpioIn, int flag) : sw(gpioIn), status(flag) { |
O_Thom | 0:f9a18207d99c | 13 | state = LOW; |
O_Thom | 0:f9a18207d99c | 14 | t.reset(); |
O_Thom | 0:f9a18207d99c | 15 | flag = 0; |
O_Thom | 0:f9a18207d99c | 16 | } |
O_Thom | 0:f9a18207d99c | 17 | //Destructor - should the instance go out of scope, this is called |
O_Thom | 0:f9a18207d99c | 18 | ~SWPoll() { |
O_Thom | 0:f9a18207d99c | 19 | //Shut down |
O_Thom | 0:f9a18207d99c | 20 | t.stop(); |
O_Thom | 0:f9a18207d99c | 21 | t.reset(); |
O_Thom | 0:f9a18207d99c | 22 | flag = 0; |
O_Thom | 0:f9a18207d99c | 23 | } |
O_Thom | 0:f9a18207d99c | 24 | //The public API - poll the switches |
O_Thom | 0:f9a18207d99c | 25 | //Bascially, a mealy machine - uses a timer to manage switch bounce |
O_Thom | 0:f9a18207d99c | 26 | void poll() { |
O_Thom | 0:f9a18207d99c | 27 | switch (state) |
O_Thom | 0:f9a18207d99c | 28 | { |
O_Thom | 0:f9a18207d99c | 29 | //Waiting for switch to rise: |
O_Thom | 0:f9a18207d99c | 30 | case LOW: |
O_Thom | 0:f9a18207d99c | 31 | if (sw == 1) { |
O_Thom | 0:f9a18207d99c | 32 | state = LOW_DEBOUNCE; |
O_Thom | 0:f9a18207d99c | 33 | t.reset(); |
O_Thom | 0:f9a18207d99c | 34 | t.start(); |
O_Thom | 0:f9a18207d99c | 35 | } |
O_Thom | 0:f9a18207d99c | 36 | break; |
O_Thom | 0:f9a18207d99c | 37 | |
O_Thom | 0:f9a18207d99c | 38 | case LOW_DEBOUNCE: |
O_Thom | 0:f9a18207d99c | 39 | if (t.read_ms() >= 200) { |
O_Thom | 0:f9a18207d99c | 40 | state = HIGH; |
O_Thom | 0:f9a18207d99c | 41 | t.stop(); |
O_Thom | 0:f9a18207d99c | 42 | t.reset(); |
O_Thom | 0:f9a18207d99c | 43 | } |
O_Thom | 0:f9a18207d99c | 44 | break; |
O_Thom | 0:f9a18207d99c | 45 | |
O_Thom | 0:f9a18207d99c | 46 | case HIGH: |
O_Thom | 0:f9a18207d99c | 47 | if (sw == 0) { |
O_Thom | 0:f9a18207d99c | 48 | flag = !flag; //Toggle output on state transition |
O_Thom | 0:f9a18207d99c | 49 | state = HIGH_DEBOUNCE; |
O_Thom | 0:f9a18207d99c | 50 | t.reset(); //(purely defensive) |
O_Thom | 0:f9a18207d99c | 51 | t.start(); |
O_Thom | 0:f9a18207d99c | 52 | } |
O_Thom | 0:f9a18207d99c | 53 | break; |
O_Thom | 0:f9a18207d99c | 54 | case HIGH_DEBOUNCE: |
O_Thom | 0:f9a18207d99c | 55 | if (t.read_ms() >= 200) { |
O_Thom | 0:f9a18207d99c | 56 | state = LOW; |
O_Thom | 0:f9a18207d99c | 57 | t.stop(); |
O_Thom | 0:f9a18207d99c | 58 | t.reset(); |
O_Thom | 0:f9a18207d99c | 59 | } |
O_Thom | 0:f9a18207d99c | 60 | break; |
O_Thom | 0:f9a18207d99c | 61 | default: |
O_Thom | 0:f9a18207d99c | 62 | t.stop(); |
O_Thom | 0:f9a18207d99c | 63 | t.reset(); |
O_Thom | 0:f9a18207d99c | 64 | state = LOW; |
O_Thom | 0:f9a18207d99c | 65 | break; |
O_Thom | 0:f9a18207d99c | 66 | } //end switch |
O_Thom | 0:f9a18207d99c | 67 | |
O_Thom | 0:f9a18207d99c | 68 | //This is a Mealy Machine - so no output logic follows |
O_Thom | 0:f9a18207d99c | 69 | } |
O_Thom | 0:f9a18207d99c | 70 | }; |