31#ifndef ETL_DEBOUNCE_INCLUDED
32#define ETL_DEBOUNCE_INCLUDED
35#include "static_assert.h"
41 namespace private_debounce
47 typedef uint_least8_t flags_t;
48 typedef uint16_t count_t;
58 if (sample !=
bool((
flags & Sample) != 0))
61 flags = (
flags & ~Sample) | (sample ? Sample : 0);
73 return (
flags & Change) != 0;
82 return ((
flags & State) > Off);
91 return (
flags & State) > On;
100 return ((
flags & State) == Repeating);
120 :
flags(initial_state ? On : Off),
135 void get_next(
bool sample,
bool condition_set,
bool condition_clear,
const uint_least8_t state_table[][2])
137 int index1 = ((
flags & State) * 2) + (sample ? 1 : 0);
138 int index2 = (sample ? (condition_set ? 0 : 1) : (condition_clear ? 0 : 1));
140 flags_t next =
flags;
143 next |= state_table[index1][index2];
183 void set_state(
bool sample,
bool condition_set,
bool condition_clear)
185 static ETL_CONSTANT uint_least8_t state_table[4][2] =
187 { debounce_base::Off, debounce_base::Off },
188 { debounce_base::On, debounce_base::Off },
189 { debounce_base::Off, debounce_base::On },
190 { debounce_base::On, debounce_base::On },
193 get_next(sample, condition_set, condition_clear, state_table);
199 bool process(
bool sample, count_t valid_count)
203 if (count < UINT16_MAX)
207 bool valid = (count == valid_count);
209 switch (flags & State)
213 set_state(sample, valid, valid);
219 set_state(sample, valid, valid);
235 return (flags & Change);
261 void set_state(
bool sample,
bool condition_set,
bool condition_clear)
263 static ETL_CONSTANT uint_least8_t state_table[6][2] =
265 { debounce_base::Off, debounce_base::Off },
266 { debounce_base::On, debounce_base::Off },
267 { debounce_base::Off, debounce_base::On },
268 { debounce_base::Held, debounce_base::On },
269 { debounce_base::Off, debounce_base::Held },
270 { debounce_base::Held, debounce_base::Held }
273 get_next(sample, condition_set, condition_clear, state_table);
279 bool process(
bool sample, count_t valid_count, count_t hold_count)
283 if (count < UINT16_MAX)
287 bool valid = (count == valid_count);
288 bool hold = (count == hold_count);
290 switch (flags & State)
294 set_state(sample, valid, valid);
300 set_state(sample, hold, valid);
306 set_state(sample, hold, valid);
322 return (flags & Change);
348 void set_state(
bool sample,
bool condition_set,
bool condition_clear)
350 static ETL_CONSTANT uint_least8_t state_table[8][2] =
352 { debounce_base::Off, debounce_base::Off },
353 { debounce_base::On, debounce_base::Off },
354 { debounce_base::Off, debounce_base::On },
355 { debounce_base::Held, debounce_base::On },
356 { debounce_base::Off, debounce_base::Held },
357 { debounce_base::Repeating, debounce_base::Held },
358 { debounce_base::Off, debounce_base::Repeating },
359 { debounce_base::Repeating, debounce_base::Repeating }
362 get_next(sample, condition_set, condition_clear, state_table);
368 bool process(
bool sample, count_t valid_count, count_t hold_count, count_t repeat_count)
372 if (count < UINT16_MAX)
376 bool valid = (count == valid_count);
377 bool hold = (count == hold_count);
378 bool repeat = (count == repeat_count);
380 switch (flags & State)
384 set_state(sample, valid, valid);
390 set_state(sample, hold, valid);
396 set_state(sample, repeat, valid);
402 set_state(sample, repeat, valid);
404 if (sample && repeat)
424 return (flags & Change);
433 template <const u
int16_t VALID_COUNT = 0, const u
int16_t HOLD_COUNT = 0, const u
int16_t REPEAT_COUNT = 0>
442 : debounce4(initial_state)
454 return process(sample, VALID_COUNT, HOLD_COUNT, REPEAT_COUNT);
462 template <const u
int16_t VALID_COUNT, const u
int16_t HOLD_COUNT>
471 : debounce3(initial_state)
486 return process(sample, VALID_COUNT, HOLD_COUNT);
494 template <const u
int16_t VALID_COUNT>
503 : debounce2(initial_state)
517 return process(sample, VALID_COUNT);
535 : debounce4(initial_state),
548 debounce(count_t valid, count_t hold = 0, count_t repeat = 0)
551 set(valid, hold, repeat);
557 void set(count_t valid, count_t hold = 0, count_t repeat = 0)
561 repeat_count = repeat;
576 return process(sample, valid_count, hold_count, repeat_count);
583 count_t repeat_count;
bool add(bool sample)
Definition: debounce.h:574
void set(count_t valid, count_t hold=0, count_t repeat=0)
Constructor.
Definition: debounce.h:557
debounce(count_t valid, count_t hold=0, count_t repeat=0)
Definition: debounce.h:548
debounce(bool initial_state=false)
Definition: debounce.h:534
bool add(bool sample)
Definition: debounce.h:515
debounce(bool initial_state=false)
Constructor.
Definition: debounce.h:502
bool add(bool sample)
Definition: debounce.h:484
debounce(bool initial_state=false)
Constructor.
Definition: debounce.h:470
Definition: debounce.h:435
bool add(bool sample)
Definition: debounce.h:452
debounce(bool initial_state=false)
Constructor.
Definition: debounce.h:441
State change logic for 2 state debounce.
Definition: debounce.h:165
~debounce2()
Destructor.
Definition: debounce.h:176
State change logic for 3 state debounce.
Definition: debounce.h:243
~debounce3()
Destructor.
Definition: debounce.h:254
State change logic for 4 state debounce.
Definition: debounce.h:330
~debounce4()
Destructor.
Definition: debounce.h:341
Definition: debounce.h:44
bool has_changed() const
Definition: debounce.h:71
void add_sample(bool sample)
Definition: debounce.h:55
bool is_repeating() const
Definition: debounce.h:98
debounce_base(bool initial_state)
Constructor.
Definition: debounce.h:119
bool is_set() const
Definition: debounce.h:80
bool is_held() const
Definition: debounce.h:89
void get_next(bool sample, bool condition_set, bool condition_clear, const uint_least8_t state_table[][2])
Gets the next state based on the inputs.
Definition: debounce.h:135
~debounce_base()
Destructor.
Definition: debounce.h:128
A templated set implementation that uses a fixed size buffer.
Definition: set.h:2502
bitset_ext
Definition: absolute.h:38