blob: 381f2f246e6930bfe559809f2e6d005651a86e16 [file] [log] [blame]
Neil Balch6040a352018-03-04 16:02:56 -08001#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_DEBOUNCER_H_
2#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_DEBOUNCER_H_
3
Stephan Pleinesf63bde82024-01-13 15:59:33 -08004namespace y2018::control_loops::superstructure {
Neil Balch6040a352018-03-04 16:02:56 -08005
Stephan Pleinesf63bde82024-01-13 15:59:33 -08006// Ensures that a certain number of states of a certain type are recieved
7// before the actual state is changed.
Neil Balch6040a352018-03-04 16:02:56 -08008class 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 Pleinesf63bde82024-01-13 15:59:33 -080038} // namespace y2018::control_loops::superstructure
Neil Balch6040a352018-03-04 16:02:56 -080039
40#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_DEBOUNCER_H_