-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
89 lines (71 loc) · 2.21 KB
/
input.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
#include "input.h"
void InputHandler::setupInputs() {
pinMode(MAIN_BUTTON_PIN, INPUT_PULLUP);
pinMode(PING_BUTTON_PIN, INPUT_PULLUP);
if (digitalRead(MAIN_BUTTON_PIN) == LOW) {
onStartupMainDown();
}
}
void InputHandler::checkInputs() {
// Check for Main long push
if (wasLongMainPress != true && oldMainButtonState == LOW && millis() - lastMainButtonUpdate > LONG_PRESS_MILLIS) {
wasLongMainPress = true;
onMainLongDown();
}
// Main button check
int mainButtonState;
if ((mainButtonState = digitalRead(MAIN_BUTTON_PIN)) != oldMainButtonState) {
if (millis() - lastMainButtonUpdate > DEBOUNCE_MILLIS) {
if (mainButtonState == HIGH ) {
if (!wasLongMainPress) {
onMainDown();
} else {
wasLongMainPress = false;
}
}
oldMainButtonState = mainButtonState;
lastMainButtonUpdate = millis();
}
}
// Ping button check
int pingButtonState;
if ((pingButtonState = digitalRead(PING_BUTTON_PIN)) != oldPingButtonState) {
if (millis() - lastPingButtonUpdate > DEBOUNCE_MILLIS) {
if (pingButtonState == LOW) {
onPingDown();
}
oldPingButtonState = pingButtonState;
lastPingButtonUpdate = millis();
}
}
// Rotary encoder check
long encoderPos = encoder.read();
if (encoderPos != oldEncoderPos) {
if (oldMainButtonState == LOW) {
wasLongMainPress = true;
onPushedRotation(encoderPos);
} else {
onRotation(encoderPos);
}
oldEncoderPos = encoderPos;
}
}
// Must set this before setupInputs() to work!
void InputHandler::setOnStartupMainDown(void(*functionPointer)()) {
onStartupMainDown = functionPointer;
}
void InputHandler::setOnRotation(void(*functionPointer)(long)) {
onRotation = functionPointer;
}
void InputHandler::setOnPushedRotation(void(*functionPointer)(long)) {
onPushedRotation = functionPointer;
}
void InputHandler::setOnMainDown(void(*functionPointer)()) {
onMainDown = functionPointer;
}
void InputHandler::setOnMainLongDown(void(*functionPointer)()) {
onMainLongDown = functionPointer;
}
void InputHandler::setOnPingDown(void(*functionPointer)()) {
onPingDown = functionPointer;
}