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