-
Notifications
You must be signed in to change notification settings - Fork 0
/
LOOPButton.cpp
50 lines (44 loc) · 1.04 KB
/
LOOPButton.cpp
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
#include "LOOPButton.h"
//****************************************************************************************
//LOOPButton (Pin Number, Command, Note Number, Channel, Debounce Time)
LOOPButton::LOOPButton(byte pin, byte command, byte value, byte channel, byte debounce)
{
_pin = pin;
pinMode(_pin, INPUT_PULLUP);
_debounce = debounce;
_time = 0;
_new = true;
_busy = false;
_last = 1;
Bcommand = command;
Bvalue = value;
Bchannel = channel;
}
byte LOOPButton::getValue()
{
// Se busy não estivert setado - read MIDIButton
if (_busy == false) {
if (digitalRead(_pin) == _last) return 2;
}
// Se novo bit setado, checa tempo
if (_new == true) {
_busy = true;
_new = false;
_time = millis();
return 255;
}
// Checar debounce
if (millis() - _time < _debounce) return 255;
// Checar estado do pino
if (digitalRead(_pin) == _last) {
_busy = false;
_new = true;
return 255;
}
else {
_busy = false;
_new = true;
_last = ((~_last) & 0b00000001);
return _last;
}
}