-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.h
51 lines (40 loc) · 892 Bytes
/
button.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef BUTTON_H_
#define BUTTON_H_
#include "midi.h"
namespace button {
class Debouncer {
public:
constexpr Debouncer() : interval(5) {}
bool filter(bool isDown) {
// debounce
if (isDown != wasDown) {
lastChangeTime = millis();
wasDown = isDown;
} else if (isDown != playing && ((millis() - lastChangeTime) > interval)) {
playing = isDown;
}
return playing;
}
private:
unsigned interval;
bool wasDown = false;
unsigned long lastChangeTime = 0;
bool playing = false;
};
class Button {
public:
Button(int inputPin, Chord toPlay) : pin(inputPin), ch(toPlay) {}
void setup() {
pinMode(pin, INPUT_PULLUP);
}
Chord poll() {
bool isDown = digitalRead(pin)==LOW;
return debouncer.filter(isDown) ? ch : Chord();
}
private:
int pin;
Chord ch;
Debouncer debouncer;
};
} // end namespace
#endif