Neil Balch | 6040a35 | 2018-03-04 16:02:56 -0800 | [diff] [blame^] | 1 | #ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_DEBOUNCER_H_ |
| 2 | #define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_DEBOUNCER_H_ |
| 3 | |
| 4 | namespace y2018 { |
| 5 | namespace control_loops { |
| 6 | namespace superstructure { |
| 7 | |
| 8 | // Ensures that a certain number of states of a certain type are recieved before |
| 9 | // the actual state is changed. |
| 10 | class Debouncer { |
| 11 | public: |
| 12 | // Parameters: |
| 13 | // - initial_state: the initial state of the debouncer. (assigned to |
| 14 | // current_state) |
| 15 | // - inputs_before_change: the number of inputs of the same type (true or |
| 16 | // false) required before the debouncer state is changed. |
| 17 | Debouncer(bool initial_state, int inputs_before_change) |
| 18 | : current_state_(initial_state), |
| 19 | inputs_before_change_(inputs_before_change) {} |
| 20 | |
| 21 | // Updates the debounder state with a new input value. |
| 22 | void Update(bool new_state); |
| 23 | |
| 24 | // Retrieves the current debouncer state. |
| 25 | bool current_state() const { return current_state_; } |
| 26 | |
| 27 | private: |
| 28 | // Stores the current debouncer state. |
| 29 | bool current_state_; |
| 30 | |
| 31 | // Stores the number of inputs of the same type (true or false) required |
| 32 | // before the debouncer state changes. |
| 33 | const int inputs_before_change_; |
| 34 | |
| 35 | // Stores the temporary count of inputs of the same type. When this number |
| 36 | // reaches inputs_before_change_, the debouncer state changes. |
| 37 | int consistent_count_ = 0; |
| 38 | }; |
| 39 | |
| 40 | } // namespace superstructure |
| 41 | } // namespace control_loops |
| 42 | } // namespace y2018 |
| 43 | |
| 44 | #endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_DEBOUNCER_H_ |