| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | #pragma once | |||
| 2 | ||||
| 3 | // Very generic signal debounce | |||
| 4 | // Unlike ButtonDebounce, works with any arbitrary signal (from ADC, CAN, etc) | |||
| 5 | ||||
| 6 | #include "rusefi/timer.h" | |||
| 7 | ||||
| 8 | class SignalDebounce { | |||
| 9 | public: | |||
| 10 | SignalDebounce(float debounceSeconds, bool initialState) : | |||
| 11 | period(debounceSeconds), | |||
| 12 | state(initialState) | |||
| 13 | { } | |||
| 14 | 1 | SignalDebounce(float debounceSeconds) : | ||
| 15 | 1 | period(debounceSeconds), | ||
| 16 | 1 | state(false) | ||
| 17 | 1 | { } | ||
| 18 | ||||
| 19 | // feed it with raw value periodically | |||
| 20 | 15 | void set(bool newState) | ||
| 21 | { | |||
| 22 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
|
2/2✓ Decision 'true' taken 6 times.
✓ Decision 'false' taken 9 times.
|
15 | if (state == newState) { |
| 23 | 6 | timer.reset(); | ||
| 24 | } | |||
| 25 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7 times.
|
2/2✓ Decision 'true' taken 2 times.
✓ Decision 'false' taken 7 times.
|
9 | else if (timer.hasElapsedSec(period)) { |
| 26 | 2 | state = newState; | ||
| 27 | 2 | timer.reset(); | ||
| 28 | } | |||
| 29 | 15 | } | ||
| 30 | ||||
| 31 | // get the 'smooth' value | |||
| 32 | 14 | bool get() const { | ||
| 33 | 14 | return state; | ||
| 34 | } | |||
| 35 | ||||
| 36 | private: | |||
| 37 | Timer timer; | |||
| 38 | const float period; | |||
| 39 | bool state; | |||
| 40 | }; | |||
| 41 |