-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buttons.ino
97 lines (76 loc) · 2.6 KB
/
Buttons.ino
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
void toggleDisplay(struct ClockState* s) {
Serial.println("Display toggle button pressed");
s->displayOn = !s->displayOn;
s->backLightStateChanged = true;
s->someStateChanged = true;
}
void buttonActionHander(struct Button* button,
struct ClockState* s, void (*f)(struct ClockState*) ) {
updateButtonState(button);
if (buttonPressed(button)) {
f(s);
resetButtonPressed(button);
}
}
void processBacklightButton(struct Button* button, struct ClockState* s) {
buttonActionHander(button, s, toggleDisplay);
}
void processHotButton(struct Button* button, struct ClockState* s) {
buttonActionHander(button, s, processHotButton);
}
void processSnoozeButton(struct Button* button, struct ClockState* s) {
buttonActionHander(button, s, processSnoozeButton);
}
boolean buttonPressed(struct Button* button) {
return button->buttonPressed;
}
void resetButtonPressed(struct Button* button) {
button->buttonPressed = false;
}
void initializeButton(struct Button* button, int pinNumber) {
button->buttonPin = pinNumber;
button->lastButtonState = 0;
button->buttonState = 0;
button->lastDebounceTime = 0;
button->buttonPressed = false;
pinMode(pinNumber, INPUT);
}
void processSnoozeButton(struct ClockState* s) {
Serial.println("Snooze button pressed");
s->ringing = false;
s->snoozed = true;
s->someStateChanged = true;
}
void processHotButton(struct ClockState* s) {
Serial.println("Hotness button pressed");
s->hot = !s->hot;
s->ringing = false;
s->someStateChanged = true;
}
void updateButtonState(struct Button* button) {
int reading = digitalRead(button->buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != button->lastButtonState) {
// reset the debouncing timer
button->lastDebounceTime = millis();
}
if ((millis() - button->lastDebounceTime) > DEBOUNCE_DELAY) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != button->buttonState) {
button->buttonState = reading;
// only toggle the state changed if the new button state is HIGH
if (button->buttonState == HIGH) {
Serial.println("Button pressed");
button->buttonPressed = true;
}
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
button->lastButtonState = reading;
}