rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
signaldebounce.h
Go to the documentation of this file.
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
9public:
10 SignalDebounce(float debounceSeconds, bool initialState) :
11 period(debounceSeconds),
12 state(initialState)
13 { }
14 SignalDebounce(float debounceSeconds) :
15 period(debounceSeconds),
16 state(false)
17 { }
18
19 // feed it with raw value periodically
20 void set(bool newState)
21 {
22 if (state == newState) {
23 timer.reset();
24 }
25 else if (timer.hasElapsedSec(period)) {
26 state = newState;
27 timer.reset();
28 }
29 }
30
31 // get the 'smooth' value
32 bool get() const {
33 return state;
34 }
35
36private:
37 Timer timer;
38 const float period;
39 bool state;
40};
SignalDebounce(float debounceSeconds)
SignalDebounce(float debounceSeconds, bool initialState)
void set(bool newState)
bool get() const
const float period