blob: ad46b5162e48c30673e9cc9a914a7ea3057bdd67 [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
4namespace y2018 {
5namespace control_loops {
6namespace superstructure {
7
8// Ensures that a certain number of states of a certain type are recieved before
9// the actual state is changed.
10class 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_