forked from johnspiegel/AveragingScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.cpp
136 lines (112 loc) · 2.89 KB
/
buttons.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "buttons.h"
#if defined(__AVR__)
#include <avr/interrupt.h>
#endif
namespace {
constexpr int debounceDelayMs = 50;
struct PinStatus {
uint8_t pin = 0;
volatile uint8_t state = HIGH;
volatile uint8_t nextState = HIGH;
volatile uint8_t newEvent = false;
volatile unsigned long nextStateMillis = 0;
PinStatus *nextPinStatus = nullptr;
};
//volatile uint16_t watched_digital_pins = 0;
PinStatus *pinStatuses = nullptr;
volatile int phantoms = 0;
volatile int bounces = 0;
} // namespace
#if defined(__AVR__)
ISR (PCINT0_vect)
#else
void handleButtonPress ()
#endif
{
const unsigned long isr_millis = millis();
int events = 0;
int myBounces = 0;
for (PinStatus *ps = pinStatuses; ps != nullptr; ps = ps->nextPinStatus) {
uint8_t state = digitalRead(ps->pin);
// Warning: overflow after 50 days.
if (isr_millis >= ps->nextStateMillis) {
if (state != ps->state) {
// Lock in a button press for 200ms.
ps->newEvent = true;
ps->nextStateMillis = isr_millis + debounceDelayMs;
}
if (state != ps->state || state != ps->nextState) {
events++;
}
ps->state = ps->nextState = state;
} else {
if (state != ps->state) {
myBounces++;
}
// Bounce!
ps->nextState = state;
}
}
if (events == 0 && myBounces == 0) {
phantoms++;
}
bounces += myBounces;
}
void watchDigitalPin(uint8_t pin) {
noInterrupts();
pinMode(pin, INPUT_PULLUP);
PinStatus *ps = new PinStatus();
ps->pin = pin;
ps->state = digitalRead(pin);
ps->nextState = ps->state;
ps->nextPinStatus = pinStatuses;
pinStatuses = ps;
// watched_digital_pins |= (1 << pin);
#if defined(__AVR__)
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
#else
attachInterrupt(digitalPinToInterrupt(pin), handleButtonPress, CHANGE);
#endif
interrupts();
}
bool wasPressed(uint8_t pin, uint8_t wantState) {
bool newEvent = false;
noInterrupts();
for (PinStatus *ps = pinStatuses; ps != nullptr; ps = ps->nextPinStatus) {
if (ps->pin != pin) {
continue;
}
if (ps->state == wantState) {
newEvent |= ps->newEvent;
ps->newEvent = false;
}
if (millis() >= ps->nextStateMillis) {
ps->newEvent = (ps->nextState != ps->state);
ps->state = ps->nextState;
ps->nextStateMillis = ps->nextStateMillis + debounceDelayMs;
}
if (ps->state == wantState) {
newEvent |= ps->newEvent;
ps->newEvent = false;
}
break;
}
interrupts();
return newEvent;
}
int getPhantoms() {
noInterrupts();
int r = phantoms;
phantoms = 0;
interrupts();
return r;
}
int getBounces() {
noInterrupts();
int r = bounces;
bounces = 0;
interrupts();
return r;
}