-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathButtons.cpp
260 lines (241 loc) · 6.5 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "Buttons.h"
#include <Arduino.h>
#include "ButtonReader.h"
#include "Enums.h"
#include "HID-Project.h"
#include "Globals.h"
ButtonReader buttonReader;
// Pin 18, A0 = UP
// Pin 19, A1 = RIGHT
// Pin 20, A2 = DOWN
// Pin 21, A3 = LEFT
Buttons::Buttons()
{
// Initialize Button Pins
// if (DEBUG) {Serial.print(F("DEBUG,buttons: About to initialize pins\r\n"));}
buttonReader.init();
// if (DEBUG) {Serial.print(F("DEBUG,buttons: pins initialized\r\n"));}
}
void Buttons::init()
{
// if (DEBUG) {Serial.print(F("DEBUG,buttons: initializing Gamepad1\r\n"));}
BootKeyboard.begin();
SingleConsumer.begin();
// if (DEBUG) {Serial.print(F("DEBUG,buttons: initialized Gamepad1\r\n"));}
}
bool Buttons::checkChanged()
{
if (config.buttonPressed)
{
return true;
}
if (buttonReader.update())
{
config.buttonPressed = true;
config.updateUSB = true;
return true;
}
else
{
return false;
}
}
void Buttons::readInputs()
{
unsigned char buttonPushed = 2; // this is set to notify light show that a button press has happened
// read shift register values
// if (DEBUG) {Serial.print(F("DEBUG"));}
if (config.buttonPressed || buttons.numberButtonsPressed > 0)
{
for (unsigned char i = 0; i < 24; i++)
{
bool currentButtonState = !buttonReader.state(i);
// if (DEBUG) {Serial.print(currentButtonState);}
if (currentButtonState != config.lastButtonState[i])
{
if (currentButtonState == 1)
{
buttonPushed = 1;
}
else if (currentButtonState == 0 && buttonPushed == 2)
{
buttonPushed = 0;
}
config.lastButtonState[i] = sendButtonPush(i, currentButtonState);
}
// if (DEBUG) {Serial.print("\r\n");}
}
}
if (buttonPushed == 1)
{
if (config.lightShowState == WAITING_INPUT || config.lightShowState == IN_RANDOM_MODE_WAITING_INPUT)
{
config.lightShowState = INPUT_RECEIVED_SET_LIGHTS_HIGH;
}
}
else if (buttonPushed == 0)
{
if (config.lightShowState == INPUT_RECEIVED_BUTTON_STILL_PRESSED)
{
config.lightShowState = INPUT_RECEIVED_SET_LIGHTS_LOW;
}
}
}
bool Buttons::sendButtonPush(unsigned char i, bool currentButtonState)
{
config.updateUSB = true;
// mod 24 so that the button number is always between 0 and 23
if (i == config.nightModeButton % 24)
{
if (config.nightModeButton < 24)
{
config.nightMode = currentButtonState;
}
else
{
if (currentButtonState == 1)
{
// if (DEBUG) {Serial.print("DEBUG,setting night mode to ");Serial.print(config.nightMode); Serial.print(F("\r\n"));}
config.nightMode = !config.nightMode;
}
}
}
if (config.buttonKeyDebounce[i] > 0 && currentButtonState == 0)
{
if (config.buttonKeyDebounce[i] < config.buttonDebounceCounter)
{
config.buttonKeyDebounce[i]++;
return 1;
}
else
{
config.buttonKeyDebounce[i] = 1;
numberButtonsPressed--;
// Serial.print(F("decrementing"));
// Serial.print(numberButtonsPressed);
// Serial.print(F("\r\n"));
}
}
else if (config.buttonKeyDebounce[i] > 0 && currentButtonState == 1)
{
numberButtonsPressed++;
// Serial.print(F("incrementing"));
// Serial.print(numberButtonsPressed);
// Serial.print(F("\r\n"));
}
if (i > 3 && i < 12 && currentButtonState == 0 && config.lastButtonState[i + 20] == 1 && config.lastButtonState[config.shiftButton] == 0)
{
sendActualButtonPress(i + 20, currentButtonState);
config.lastButtonState[i + 20] = currentButtonState;
}
if (i > 3 && i < 12 && config.lastButtonState[config.shiftButton] == 1)
{
buttonOffset = i + 20;
if (config.lastButtonState[config.shiftButton] == 1)
{
config.lastButtonState[buttonOffset] = currentButtonState;
sendActualButtonPress(buttonOffset, currentButtonState);
}
if (currentButtonState == 0)
{
sendActualButtonPress(i, currentButtonState);
sendActualButtonPress(buttonOffset, currentButtonState);
}
}
else
{
buttonOffset = i;
sendActualButtonPress(buttonOffset, currentButtonState);
}
// trigger solenoid if needed
for (int j = 0; j < 4; j++)
{
if (currentButtonState == 1 && config.solenoidButtonMap[j] > 0 && config.solenoidButtonMap[j] - 1 == i)
{
if (config.solenoidOutputMap[j] > 0)
{
outputs.updateOutput(config.solenoidOutputMap[j] - 1, 255);
}
}
else if (currentButtonState == 0 && config.solenoidButtonMap[j] > 0 && config.solenoidButtonMap[j] - 1 == i)
{
if (config.solenoidOutputMap[j] > 0)
{
outputs.updateOutput(config.solenoidOutputMap[j] - 1, 0);
}
}
}
return currentButtonState;
}
void Buttons::sendActualButtonPress(unsigned char buttonOffset, bool currentButtonState)
{
if (config.buttonKeyboard[buttonOffset] > 0)
{
if (currentButtonState == 1)
{
if (config.buttonKeyboard[buttonOffset] > 251)
{
if (config.buttonKeyboard[buttonOffset] == 252)
{
SingleConsumer.press(MEDIA_VOLUME_MUTE);
}
else if (config.buttonKeyboard[buttonOffset] == 253)
{
SingleConsumer.press(MEDIA_VOLUME_DOWN);
}
else if (config.buttonKeyboard[buttonOffset] == 254)
{
SingleConsumer.press(MEDIA_VOLUME_UP);
}
}
else
{
BootKeyboard.press(KeyboardKeycode(config.buttonKeyboard[buttonOffset]));
}
}
else
{
if (config.buttonKeyboard[buttonOffset] > 251)
{
if (config.buttonKeyboard[buttonOffset] == 252)
{
SingleConsumer.release(MEDIA_VOLUME_MUTE);
}
else if (config.buttonKeyboard[buttonOffset] == 253)
{
SingleConsumer.release(MEDIA_VOLUME_DOWN);
}
else if (config.buttonKeyboard[buttonOffset] == 254)
{
SingleConsumer.release(MEDIA_VOLUME_UP);
}
}
else
{
BootKeyboard.release(KeyboardKeycode(config.buttonKeyboard[buttonOffset]));
}
}
}
if (config.buttonKeyboard[buttonOffset] == 0 || config.disableButtonPressWhenKeyboardEnabled == 0)
{
if (currentButtonState == 1)
{
Gamepad1.press(buttonOffset + 1);
}
else
{
Gamepad1.release(buttonOffset + 1);
}
}
}
void Buttons::sendButtonState()
{
Serial.print(F("B,"));
for (int i = 0; i < 31; i++)
{
Serial.print(config.lastButtonState[i]);
Serial.print(F(","));
}
Serial.print(config.lastButtonState[31]);
Serial.print(F("\r\n"));
}