Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
Loading...
Searching...
No Matches
Debounce.h
1#pragma once
2
9
10class Debounce
11{
12public:
19 struct Settings
20 {
21 unsigned int intervalPositiveEdge;
22 unsigned int intervalNegativeEdge;
23 };
24 typedef enum {
25 FALLING = -1,
26 NONE = 0,
27 RISING = 1,
28 } Edge;
29 Debounce();
33 Debounce(unsigned int interval);
37 Debounce(const Settings& settings);
45 void setup(unsigned int interval);
53 void setup(const Settings& settings);
57 bool process(bool input)
58 {
59 oldState = state;
60 if(debouncing) {
61 --debouncing;
62 } else {
63 if(input > state) {
64 debouncing = intervalPositiveEdge;
65 }
66 else if(input < state) {
67 debouncing = intervalNegativeEdge;
68 }
69 state = input;
70 }
71 return state;
72 }
73
76 bool get() { return state; }
80 Edge edgeDetected() { return detectEdge(oldState, state); };
84 static Edge detectEdge(bool oldState, bool newState) {
85 Edge edge = NONE;
86 if(oldState != newState)
87 edge = newState > oldState ? RISING : FALLING;
88 return edge;
89 }
90private:
91 unsigned int debouncing;
92 unsigned int intervalPositiveEdge;
93 unsigned int intervalNegativeEdge;
94protected:
95 bool state;
96 bool oldState;
97};
Debounce a boolean reading.
Definition Debounce.h:11
void setup(unsigned int interval)
Definition Debounce.cpp:15
bool process(bool input)
Definition Debounce.h:57
static Edge detectEdge(bool oldState, bool newState)
Definition Debounce.h:84
Edge edgeDetected()
Definition Debounce.h:80
bool get()
Definition Debounce.h:76
Edge
Definition Debounce.h:24
@ RISING
The state has transitioned from low to high.
Definition Debounce.h:27
@ NONE
The state has remained the same.
Definition Debounce.h:26
@ FALLING
The state has transitioned from high to low.
Definition Debounce.h:25
Definition Debounce.h:20
unsigned int intervalPositiveEdge
Debouncing interval when encountering a positive edge (false to true transition).
Definition Debounce.h:21
unsigned int intervalNegativeEdge
Debouncing interval when encountering a negative edge ('true' to 'false' transition).
Definition Debounce.h:22