Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Debounce.h
1 #pragma once
2 
10 class Debounce
11 {
12 public:
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  }
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  }
90 private:
91  unsigned int debouncing;
92  unsigned int intervalPositiveEdge;
93  unsigned int intervalNegativeEdge;
94 protected:
95  bool state;
96  bool oldState;
97 };
static Edge detectEdge(bool oldState, bool newState)
Definition: Debounce.h:84
unsigned int intervalPositiveEdge
Debouncing interval when encountering a positive edge (false to true transition)
Definition: Debounce.h:21
The state has remained the same.
Definition: Debounce.h:26
The state has transitioned from high to low.
Definition: Debounce.h:25
The state has transitioned from low to high.
Definition: Debounce.h:27
unsigned int intervalNegativeEdge
Debouncing interval when encountering a negative edge ('true' to 'false' transition) ...
Definition: Debounce.h:22
Debounce a boolean reading.
Definition: Debounce.h:10
Definition: Debounce.h:19
void setup(unsigned int interval)
Definition: Debounce.cpp:15
bool process(bool input)
Definition: Debounce.h:57
Edge edgeDetected()
Definition: Debounce.h:80
Edge
Definition: Debounce.h:24