Neil Balch | 6040a35 | 2018-03-04 16:02:56 -0800 | [diff] [blame] | 1 | #include "y2018/control_loops/superstructure/debouncer.h" |
| 2 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 3 | namespace y2018::control_loops::superstructure { |
Neil Balch | 6040a35 | 2018-03-04 16:02:56 -0800 | [diff] [blame] | 4 | |
| 5 | void Debouncer::Update(bool new_state) { |
| 6 | // If the incoming state is different from the one we have stored, increment |
| 7 | // the counter. |
| 8 | if (new_state != current_state_) { |
| 9 | consistent_count_++; |
| 10 | } else { |
| 11 | consistent_count_ = 0; |
| 12 | } |
| 13 | |
| 14 | // If we have reached the number required to change the state, change it. |
| 15 | if (consistent_count_ >= inputs_before_change_) { |
| 16 | current_state_ = new_state; |
| 17 | consistent_count_ = 0; |
| 18 | } |
| 19 | } |
| 20 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 21 | } // namespace y2018::control_loops::superstructure |