-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
141 lines (126 loc) · 4.22 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
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
137
138
139
140
141
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include "input.h"
#include "lcd.h"
#include "setting.h"
#include "shiftRegister.h"
extern LiquidCrystal_I2C lcd;
void handleInput(int inputPin);
struct LastPressed {
unsigned long up = 0;
unsigned long down = 0;
unsigned long left = 0;
unsigned long right = 0;
unsigned long reset1 = 0;
};
LastPressed lastPressed;
unsigned long lastPressedOverall = 0;
int lastAnalog;
void initializeInputs() {
pinMode(UP_PIN, INPUT_PULLUP);
pinMode(DOWN_PIN, INPUT_PULLUP);
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode(RESET_PIN, INPUT_PULLUP);
lastAnalog = (1023 - analogRead(VOLUME_PIN)) / 5;
}
void checkForInput() {
unsigned long ms = millis();
const int BUTTON_COOLDOWN = 200;
extern const bool DEBUG_MODE;
if (digitalRead(UP_PIN) == LOW && lastPressed.up + BUTTON_COOLDOWN <= ms)
handleInput(UP_PIN);
else if (digitalRead(DOWN_PIN) == LOW && lastPressed.down + BUTTON_COOLDOWN <= ms)
handleInput(DOWN_PIN);
else if (digitalRead(LEFT_PIN) == LOW && lastPressed.left + BUTTON_COOLDOWN <= ms)
handleInput(LEFT_PIN);
else if (digitalRead(RIGHT_PIN) == LOW && lastPressed.right + BUTTON_COOLDOWN <= ms)
handleInput(RIGHT_PIN);
else if (digitalRead(RESET_PIN) == LOW && lastPressed.reset1 + BUTTON_COOLDOWN <= ms)
handleInput(RESET_PIN);
else {
const int ANALOG_CHANGE_RATE = 5;
const int ANALOG_WITHIN_RANGE = 20;
const int ANALOG_CHANGE_TIME = BUTTON_COOLDOWN * 2;
// conform analog value to 0-200
int analogValue = (1023 - analogRead(VOLUME_PIN)) / 5;
// if (DEBUG_MODE) Serial.println(analogValue);
// if volume has changed enough,
// if volume hasn't changed too much for it to be a glitch,
// and if another button hasn't been pressed within a certain period for it to affect the reading
if ((analogValue >= (lastAnalog + ANALOG_CHANGE_RATE) || analogValue <= (lastAnalog - ANALOG_CHANGE_RATE)) &&
(analogValue < (lastAnalog + ANALOG_WITHIN_RANGE) && analogValue >(lastAnalog - ANALOG_WITHIN_RANGE)) &&
lastPressedOverall + ANALOG_CHANGE_TIME <= ms) {
//conform analog to certain values if they are within range
if (analogValue <= 105 && analogValue >= 95)
analogValue = 100;
else if (analogValue > 195)
analogValue = 200;
else if (analogValue < 5)
analogValue = 0;
lastAnalog = analogValue;
handleInput(VOLUME_PIN); //implied that lastAnalog has also been changed
}
}
}
void handleInput(int inputPin)
{
unsigned long ms = millis();
extern const bool DEBUG_MODE;
// if (DEBUG_MODE) Serial.printf("Input: %i\n", inputPin);
switch (inputPin) {
case UP_PIN:
case DOWN_PIN:
// prevent settings from changing while not showing
if (menuState == MS_SETTINGS) {
if (inputPin == UP_PIN) {
lastPressed.up = ms;
} else {
lastPressed.down = ms;
}
lastPressedOverall = ms;
if (currentMenu == S_TEST_NOTES) {
lcd.setCursor(0, 1);
lcd.print("Testing... ");
testNotes();
lcd.setCursor(0, 1);
lcd.print("Up/Down to run ");
} else {
changeSetting(inputPin == UP_PIN ? 1 : -1);
}
updateDisplay();
}
break;
case LEFT_PIN:
lastPressed.left = ms;
lastPressedOverall = ms;
currentMenu--;
if (currentMenu < 0)
currentMenu = NUM_OF_MENUS - 1;
menuState = MS_SETTINGS;
updateDisplay();
break;
case RIGHT_PIN:
lastPressed.right = ms;
lastPressedOverall = ms;
currentMenu++;
if (currentMenu >= NUM_OF_MENUS)
currentMenu = 0;
menuState = MS_SETTINGS;
updateDisplay();
break;
case RESET_PIN:
lastPressed.reset1 = ms;
exitScreen = ms + SPECIAL_MENU_TIMEOUT;
resetNotes();
menuState = MS_RESET;
updateDisplay();
break;
case VOLUME_PIN:
exitScreen = ms + SPECIAL_MENU_TIMEOUT;
menuState = MS_VOLUME;
setVolume(lastAnalog);
updateDisplay();
break;
}
}