Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Debouncer.hxx
Go to the documentation of this file.
1
61#ifndef _UTILS_DEBOUNCER_HXX_
62#define _UTILS_DEBOUNCER_HXX_
63
67{
68public:
70 typedef uint8_t Options;
71
75 QuiesceDebouncer(const Options &wait_count)
76 : count_(0)
77 , waitCount_(wait_count)
78 , currentState_(0)
79 {
80 }
81
84 void reset_options(const Options &opts)
85 {
86 waitCount_ = opts;
88 }
89
91 void initialize(bool state)
92 {
93 currentState_ = state ? 1 : 0;
94 count_ = 0;
95 }
96
98 void override(bool new_state)
99 {
100 initialize(new_state);
101 }
102
105 {
106 return currentState_;
107 }
108
113 bool update_state(bool state)
114 {
115 unsigned new_state = state ? 1 : 0;
116 if (new_state == currentState_)
117 {
118 count_ = 0;
119 return false;
120 }
121 if (++count_ == waitCount_)
122 {
123 currentState_ = new_state;
124 count_ = 0;
125 return true;
126 }
127 return false;
128 }
129
130private:
132 unsigned count_ : 8;
134 unsigned waitCount_ : 8;
136 unsigned currentState_ : 1;
137};
138
143{
144public:
147 typedef struct
148 {
150 uint8_t window_size;
153 uint8_t min_count;
154 } Options;
155
158 : opts_(opts)
159 , lastCount_(0)
160 , currentState_(0)
161 {
162 trim_options();
163 }
164
167 void reset_options(const Options &opts)
168 {
169 opts_ = opts;
170 trim_options();
172 }
173
176 {
177 return opts_;
178 }
179
183 void initialize(bool state)
184 {
185 if (state) {
186 currentState_ = 1;
187 window_ = 0xFFFFFFFFu;
189 } else {
190 currentState_ = 0;
191 window_ = 0;
192 lastCount_ = 0;
193 }
194 }
195
198 void override(bool new_state)
199 {
200 initialize(new_state);
201 }
202
205 {
206 return currentState_;
207 }
208
214 bool update_state(bool state)
215 {
216 if (window_ & (1<<(opts_.window_size - 1))) {
217 --lastCount_;
218 }
219 window_ <<= 1;
220 if (state) {
221 lastCount_++;
222 window_ |= 1;
223 }
224 unsigned new_state = lastCount_ >= opts_.min_count ? 1 : 0;
225 if (new_state != currentState_) {
226 currentState_ = new_state;
227 return true;
228 } else {
229 return false;
230 }
231 }
232
233private:
234 void trim_options()
235 {
236 if (opts_.window_size > 32)
237 {
238 opts_.window_size = 32;
239 }
240 if (opts_.window_size == 0)
241 {
242 opts_.window_size = 1;
243 }
244 if (opts_.min_count == 0)
245 {
246 opts_.min_count = 1;
247 }
249 {
251 }
252 }
253
254 uint32_t window_{0};
256 unsigned lastCount_ : 6;
257 unsigned currentState_ : 1;
258};
259
263template <class Debouncer> class ToggleDebouncer
264{
265public:
267 typedef typename Debouncer::Options Options;
268
271 ToggleDebouncer(const Options &options)
272 : impl_(options)
273 , eventState_(0)
274 {
275 }
276
278 void initialize(bool state)
279 {
280 // comes from the hardware
281 impl_.initialize(state);
282 }
283
286 void override(bool state)
287 {
288 eventState_ = state ? 1 : 0;
289 }
290
293 {
294 // We ignore the local state and just publish the network state.
295 return eventState_;
296 }
297
300 bool update_state(bool state)
301 {
302 bool press_changed = impl_.update_state(state);
303 // Look for rising edge
304 if (press_changed && impl_.current_state())
305 {
306 // flip desired state and report update
307 eventState_ ^= 1;
308 return true;
309 }
310 return false;
311 }
312
313private:
315 Debouncer impl_;
317 unsigned eventState_ : 1;
318};
319
320#endif // _UTILS_DEBOUNCER_HXX_
This debouncer will count the ON values and if the number of ON values in a given window exceeds a th...
void reset_options(const Options &opts)
Re-creates the debouncer with new options.
uint32_t window_
bit-map of last observations. bit 0 is latest.
Options opts_
options
const Options & options()
Retrieves the currently set options.
unsigned currentState_
last known state
bool update_state(bool state)
Callback from the polling loop checking the input state.
void initialize(bool state)
Initilalize the debouncer.
CountingDebouncer(const Options &opts)
Constructor.
unsigned lastCount_
how many 1 bits we have in the window
This debouncer will update state if for N consecutive attempts the input value is the same.
Definition Debouncer.hxx:67
unsigned count_
How many times we've seen the proposed new state.
bool update_state(bool state)
Iteration function of the debouncer.
unsigned currentState_
Current visible state.
unsigned waitCount_
Configuration: what is the quiesce count we have to reach.
uint8_t Options
Type declaring what opeions we can supply to this class.
Definition Debouncer.hxx:70
void initialize(bool state)
Initializes the debouncer.
Definition Debouncer.hxx:91
void reset_options(const Options &opts)
Re-creates the debouncer with new options.
Definition Debouncer.hxx:84
QuiesceDebouncer(const Options &wait_count)
Constructor.
Definition Debouncer.hxx:75
This class acts as a debouncer that uses one momentary input button, and switches the event state at ...
Debouncer impl_
Implementation of the debounce logic.
unsigned eventState_
what the world thinks about the event
bool update_state(bool state)
Inserts a single poll measurement.
ToggleDebouncer(const Options &options)
Constructor.
void initialize(bool state)
Sets the initial state.
Debouncer::Options Options
Options type.
Static parameters for the CountingDebouncer.
uint8_t min_count
Number of samples that have to be 1 within the window to consider the debounce value 1.
uint8_t window_size
Number of samples to keep. Must be between 1 and 32.