forked from dmadison/McCree-Hammershot
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Arduino_arcade_light_gun.ino
718 lines (682 loc) · 21.4 KB
/
Arduino_arcade_light_gun.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
* Project Arduino Arcade PC Light Gun
* @author Edward Webber
* @link https://github.com/Domush/Arduino-Arcade-PC-light-gun
* @license GPLv3 non-commercial - Copyright (c) 2021 Edward Webber
*/
// ===============
// User Settings
// ===============
// Tuning Options
const long mpuUpdateRate = 5; // In milliseconds
// Default multiplier (if EEPROM is empty) -- Converts gun movement into appropriate mouse movement
int mpuMovementMultiplierX = 64;
int mpuMovementMultiplierY = 53;
// Debug Flags (uncomment to display comments via serial connection)
// #define DEBUG // Enabling will wait for serial connection before activating
#ifdef DEBUG
#define DEBUG_BUTTONS // Show button presses (and what they do)
// #define DEBUG_GYRO // Show mouse movements (very spammy, use with caution)
#define DEBUG_ENCODER // Show encoder movements
#define DEBUG_JOYSTICK // Show joystick movements
#endif
// Pin Definitions (change with care)
const uint8_t buttonTriggerPin = 10;
const uint8_t buttonAltPin = 16;
const uint8_t buttonReloadPin = 14;
const uint8_t encoderDTPin = 6;
const uint8_t encoderCLKPin = 5;
const uint8_t buttonEncoderPin = 7;
const uint8_t joystickXPin = A0;
const uint8_t joystickYPin = A1;
const uint8_t buttonJoystickPin = 15;
const uint8_t switchGunEnablePin = 9;
const uint8_t switchScrollEnablePin = 8;
// ========================
// DO NOT EDIT BELOW HERE
// ========================
#include <SPI.h>
#include <Wire.h>
#include <Mouse.h>
#include <Keyboard.h>
#include <Adafruit_GFX.h>
#include <MPU6050_light.h>
#include <MD_REncoder.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <EEPROM.h>
// Global Variables
long timestamp;
long mpuLastUpdate;
long debugLastUpdate;
long eepromLastUpdate;
long displayLastUpdate;
char currentAdjustmentAxis = 'X';
boolean mpuMultiplierXHasChanged = false;
boolean mpuMultiplierYHasChanged = false;
boolean displayDimmed = false;
boolean activeScroll = false;
boolean activeGun = false;
boolean activeTrigger = false;
boolean activeAlt = false;
boolean activeReload = false;
boolean activeEncoder = false;
boolean activeJoystick = false;
boolean activeJoystickXplus = false;
boolean activeJoystickXminus = false;
boolean activeJoystickYplus = false;
boolean activeJoystickYminus = false;
#ifdef DEBUG
#define DEBUG_PRINT(x) \
do { \
Serial.print(x); \
} while (0)
#define DEBUG_PRINTLN(x) \
do { \
Serial.println(x); \
} while (0)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
// Instantiate Objects
MD_REncoder encoder = MD_REncoder(encoderCLKPin, encoderDTPin); // Encoder
MPU6050 mpu = MPU6050(Wire); // MPU6050 gryo
// OakOLED oled;
// Adafruit_SSD1306 oled;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharinrg Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Configure display options
void displayConfig(bool displayClear = false, int displayTextSize = 1) {
display.setTextSize(displayTextSize);
display.setTextColor(1);
display.setRotation(0);
if (displayClear) {
display.clearDisplay();
display.setCursor(0, 0);
}
if (displayDimmed) {
display.dim(false);
displayDimmed = false;
}
displayLastUpdate = timestamp;
}
void setup() {
#ifdef DEBUG
Serial.begin(115200);
while (!Serial)
; // Wait for connection
#endif
pinMode(switchGunEnablePin, INPUT_PULLUP);
pinMode(switchScrollEnablePin, INPUT_PULLUP);
pinMode(buttonTriggerPin, INPUT_PULLUP);
pinMode(buttonAltPin, INPUT_PULLUP);
pinMode(buttonReloadPin, INPUT_PULLUP);
pinMode(buttonEncoderPin, INPUT_PULLUP);
pinMode(buttonJoystickPin, INPUT_PULLUP);
pinMode(joystickXPin, INPUT);
pinMode(joystickYPin, INPUT);
if (digitalRead(buttonTriggerPin) == 0) {
failsafe(); // In case something goes wrong, stop everything and display serial message
}
Mouse.begin();
Keyboard.begin();
encoder.begin();
Wire.begin();
mpu.begin();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { // Address 0x3C for 128x64
DEBUG_PRINTLN("SSD1306 initialization failed");
failsafe();
}
EEPROM.begin();
int eepromValue = 0;
EEPROM.get(0, eepromValue); // 0 = address of mpuMovementMultiplierX value in EEPROM
if (eepromValue > 0 && eepromValue < 255) {
mpuMovementMultiplierX = eepromValue;
}
eepromValue = 0;
EEPROM.get(1, eepromValue); // 1 = address of mpuMovementMultiplierY value in EEPROM
if (eepromValue > 0 && eepromValue < 255) {
mpuMovementMultiplierY = eepromValue;
}
// mpu.calcGyroOffsets(true);
mpu.calcOffsets(); // Calibrate gyro and accelerometer
}
void loop() {
timestamp = millis();
// Move mouse cursor
if (timestamp >= mpuLastUpdate + mpuUpdateRate) {
mpuLastUpdate = timestamp;
ProcessMPU();
}
// Save updated values to EEPROM
if (timestamp >= eepromLastUpdate + 60000) { // Delay EEPROM saves by 60 seconds to increase EEPROM lifespan
eepromLastUpdate = timestamp;
SaveToEEPROM();
}
// Dim the OLED display if left unused
if (!displayDimmed) {
if (timestamp >= displayLastUpdate + 5000) { // Delay EEPROM saves by 60 seconds to increase EEPROM lifespan
if (!activeGun) {
// Display if gun is disabled
displayConfig(true, 2);
display.setCursor(15, 0);
display.println(" Gun\n disabled");
display.display();
// End display
}
display.dim(true);
displayDimmed = true;
}
}
ProcessSwitches(); // Check all toggle switches
ProcessEncoder(); // Check if encoder has been turned
ProcessJoystick(); // Check if joystick is being used
ProcessButtons(); // Check all buttons
}
// Handle toggle switches from all sources
void ProcessSwitches() {
boolean gunEnabled = !digitalRead(switchGunEnablePin);
boolean scrollEnabled = !digitalRead(switchScrollEnablePin);
// Process scroll/XY adjust toggle switch
if (scrollEnabled) {
if (!activeScroll) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Rotary scrolling mode");
#endif
// Display what's happening
displayConfig(true, 2);
display.setCursor(10, 0);
display.println("Scrolling\n mode");
display.display();
// End display
activeScroll = true;
}
} else if (!scrollEnabled && activeScroll) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Axis sensitivity adjustment mode");
#endif
// Display what's happening
displayConfig(true, 2);
display.setCursor(0, 0);
display.println("X/Y adjust\n mode");
display.display();
// End display
activeScroll = false;
}
// Process gun enable/disable toggle switch
if (gunEnabled) {
if (!activeGun) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Enabling Gun Controls");
#endif
// Display what's happening
displayConfig(true, 2);
display.setCursor(15, 0);
display.println("Powering\n up!");
display.display();
// End display
activeGun = true;
}
} else if (!gunEnabled && activeGun) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Disabling Gun Controls");
#endif
// Display what's happening
displayConfig(true, 2);
display.setCursor(15, 0);
display.println(" Gun\n disabled");
display.display();
// End display
activeGun = false;
}
}
// Handle button presses from all sources
void ProcessButtons() {
boolean pressedTrigger = !digitalRead(buttonTriggerPin);
boolean pressedAlt = !digitalRead(buttonAltPin);
boolean pressedReload = !digitalRead(buttonReloadPin);
boolean pressedJoystick = !digitalRead(buttonJoystickPin);
boolean pressedEncoder = !digitalRead(buttonEncoderPin);
// Process trigger button
if (pressedTrigger && activeGun) {
if (!activeTrigger) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Pressing left mouse button");
#endif
// Display what's happening
displayConfig(true, 3);
display.setCursor(random(0, 40), random(0, 10));
const char* buttonTriggerWords[] = {"BANG!", " POW!", "BOOM!", "BLAM!", "WHIZ!"};
display.println(buttonTriggerWords[random(0, 4)]);
display.display();
// End display
Mouse.press();
activeTrigger = true;
}
} else if (!pressedTrigger && activeTrigger) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Releasing left mouse button");
#endif
displayConfig(true);
display.display();
Mouse.release();
activeTrigger = false;
}
// Process encoder button
if (pressedEncoder && activeGun) {
if (!activeEncoder) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Encoder button pressed (Hold for aim adjust)");
#endif
// Display what's happening
displayConfig(true, 2);
display.setCursor(5, 0);
display.println("Adjusting\n aim");
display.display();
// End display
activeEncoder = true;
}
} else if (!pressedEncoder && activeEncoder && !activeScroll) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINT("Encoder button released");
DEBUG_PRINT(" - Switching sensitivity adjustment to ");
DEBUG_PRINTLN(currentAdjustmentAxis == 'X' ? "Y axis" : "X axis");
#endif
if (currentAdjustmentAxis == 'X') {
currentAdjustmentAxis = 'Y';
if (mpuMultiplierXHasChanged) {
EEPROM.put(0, mpuMovementMultiplierX);
mpuMultiplierXHasChanged = false;
}
} else {
currentAdjustmentAxis = 'X';
if (mpuMultiplierYHasChanged) {
EEPROM.put(1, mpuMovementMultiplierY);
mpuMultiplierYHasChanged = false;
}
}
// Display what's happening
displayConfig(true);
display.println(currentAdjustmentAxis == 'X' ? " Adjusting X axis" : " Adjusting Y axis");
display.display();
// End display
activeEncoder = false;
} else if (!pressedEncoder && activeEncoder && activeScroll) {
display.clearDisplay(); // Nothing to display
display.display();
activeEncoder = false;
}
// Process Alt button (forward trigger)
if (pressedAlt && activeGun) {
if (!activeAlt && activeEncoder) {
// If *both* the encode and alt buttons are pressed simultaneously, re-calibrate the gyro
// mpu.calcGyroOffsets(true);
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Re-calculating gyro offsets");
#endif
// Display what's happening
displayConfig(true, 2);
display.setCursor(5, 0);
display.println("Initialize\n gyro");
display.display();
// End display
mpu.calcOffsets(); // gyro and accelerometer calibration
} else if (!activeAlt) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Pressing middle mouse button");
#endif
// Display what's happening
displayConfig(true, 2);
display.setCursor(15, 0);
display.println("Middle\n click");
display.display();
// End display
Mouse.press(MOUSE_MIDDLE);
activeAlt = true;
}
} else if (!pressedAlt && activeAlt) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Releasing middle mouse button");
#endif
displayConfig(true);
display.display();
Mouse.release(MOUSE_MIDDLE);
activeAlt = false;
}
// Process reload button
if (pressedReload && activeGun) {
if (!activeReload) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Pressing R");
#endif
// Display what's happening
displayConfig(true, 2);
display.setCursor(10, 5);
display.println("Reloading");
display.display();
// End display
Keyboard.press('r');
activeReload = true;
}
} else if (!pressedReload && activeReload) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Releasing R");
#endif
displayConfig(true);
display.display();
Keyboard.release('r');
activeReload = false;
}
// Process joystick button
if (pressedJoystick && activeGun) {
if (!activeJoystick) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Pressing right mouse button");
#endif
// Display what's happening
displayConfig(true, 2);
display.setCursor(15, 0);
display.println("Right\n click");
display.display();
// End display
Mouse.click(MOUSE_RIGHT);
activeJoystick = true;
}
} else if (!pressedJoystick && activeJoystick) {
#ifdef DEBUG_BUTTONS
DEBUG_PRINTLN("Releasing right mouse button");
#endif
displayConfig(true);
display.display();
Mouse.release(MOUSE_RIGHT);
activeJoystick = false;
}
}
// Handle joystick movements
void ProcessJoystick() {
int joystickXValue = analogRead(joystickXPin);
int joystickYValue = analogRead(joystickYPin);
// Process X values (strafing movement)
if (joystickXValue > 700 && activeGun) {
if (!activeJoystickXplus) {
#ifdef DEBUG_JOYSTICK
DEBUG_PRINTLN("Moving right");
#else
// Display what's happening
displayConfig(true, 2);
display.setCursor(15, 0);
display.println("Moving\n right");
display.display();
// End display
Keyboard.release('a');
Keyboard.press('d');
#endif
activeJoystickXminus = false;
activeJoystickXplus = true;
}
} else if (joystickXValue < 300 && activeGun) {
if (!activeJoystickXminus) {
#ifdef DEBUG_JOYSTICK
DEBUG_PRINTLN("Moving left");
#else
// Display what's happening
displayConfig(true, 2);
display.setCursor(15, 0);
display.println("Moving\n left");
display.display();
// End display
Keyboard.release('d');
Keyboard.press('a');
#endif
activeJoystickXplus = false;
activeJoystickXminus = true;
}
} else if (joystickXValue < 600 && joystickXValue > 400) {
if (activeJoystickXminus || activeJoystickXplus) {
#ifdef DEBUG_JOYSTICK
DEBUG_PRINTLN("Stopping X movement");
#else
displayConfig(true);
display.display();
Keyboard.release('d');
Keyboard.release('a');
#endif
activeJoystickXplus = false;
activeJoystickXminus = false;
}
}
// Process Y values (forward/reverse movement)
if (joystickYValue > 700 && activeGun) {
if (!activeJoystickYplus) {
#ifdef DEBUG_JOYSTICK
DEBUG_PRINTLN("Moving forward");
#else
// Display what's happening
displayConfig(true, 2);
display.setCursor(20, 5);
display.println("Onward!");
display.display();
// End display
Keyboard.release('s');
Keyboard.press('w');
#endif
activeJoystickYminus = false;
activeJoystickYplus = true;
}
} else if (joystickYValue < 300 && activeGun) {
if (!activeJoystickYminus) {
#ifdef DEBUG_JOYSTICK
DEBUG_PRINTLN("Moving backwards");
#else
// Display what's happening
displayConfig(true, 2);
display.setCursor(15, 5);
display.println("Retreat!");
display.display();
// End display
Keyboard.release('w');
Keyboard.press('s');
#endif
activeJoystickYplus = false;
activeJoystickYminus = true;
}
} else if (joystickYValue < 600 && joystickYValue > 400) {
if (activeJoystickYminus || activeJoystickYplus) {
#ifdef DEBUG_JOYSTICK
DEBUG_PRINTLN("Stopping Y movement");
#else
displayConfig(true);
display.display();
Keyboard.release('w');
Keyboard.release('s');
#endif
activeJoystickYplus = false;
activeJoystickYminus = false;
}
}
}
// Handle encoder turns
void ProcessEncoder() {
uint8_t encoderPos = encoder.read();
if (encoderPos && activeGun) {
if (encoderPos == DIR_CCW) {
#ifdef DEBUG_ENCODER
if (activeScroll) {
DEBUG_PRINTLN("Scrolling down");
} else {
DEBUG_PRINT("Reducing ");
DEBUG_PRINT(currentAdjustmentAxis);
DEBUG_PRINT("-axis sensitivity to ");
DEBUG_PRINTLN(currentAdjustmentAxis == 'X' ? mpuMovementMultiplierX - 1 : mpuMovementMultiplierY - 1);
}
#endif
if (activeScroll) {
// Display what's happening
displayConfig(true, 2);
display.println("Scrolling\n down");
display.display();
// End display
Mouse.move(0, 0, -1);
} else {
// Display what's happening
displayConfig(true);
display.println(currentAdjustmentAxis == 'X' ? " Adjusting X axis" : " Adjusting Y axis");
display.println("--------------------");
display.print(currentAdjustmentAxis == 'X' ? "X axis multiplier: " : "Y axis multiplier: ");
display.println(currentAdjustmentAxis == 'X' ? mpuMovementMultiplierX - 1 : mpuMovementMultiplierY - 1);
display.display();
// End display
if (currentAdjustmentAxis == 'X') {
mpuMovementMultiplierX -= 1;
mpuMultiplierXHasChanged = true; // mark changed so it's saved to EEPROM later
} else {
mpuMovementMultiplierY -= 1;
mpuMultiplierYHasChanged = true; // mark changed so it's saved to EEPROM later
}
}
} else if (encoderPos == DIR_CW) {
#ifdef DEBUG_ENCODER
if (activeScroll) {
DEBUG_PRINTLN("Scrolling down");
} else {
DEBUG_PRINT("Increasing ");
DEBUG_PRINT(currentAdjustmentAxis);
DEBUG_PRINT("-axis sensitivity to ");
DEBUG_PRINTLN(currentAdjustmentAxis == 'X' ? mpuMovementMultiplierX + 1 : mpuMovementMultiplierY + 1);
}
#endif
if (activeScroll) {
// Display what's happening
displayConfig(true, 2);
display.println("Scrolling\n up");
display.display();
// End display
Mouse.move(0, 0, 1);
} else {
// Display what's happening
displayConfig(true);
display.println(currentAdjustmentAxis == 'X' ? " Adjusting X axis" : " Adjusting Y axis");
display.println("--------------------");
display.print(currentAdjustmentAxis == 'X' ? "X axis multiplier: " : "Y axis multiplier: ");
display.println(currentAdjustmentAxis == 'X' ? mpuMovementMultiplierX + 1 : mpuMovementMultiplierY + 1);
display.display();
// End display
if (currentAdjustmentAxis == 'X') {
mpuMovementMultiplierX += 1;
} else {
mpuMovementMultiplierY += 1;
}
}
}
}
}
// Handle gyro movements (gun aiming)
void ProcessMPU() {
static float mpuLastAngleX = 0, mpuLastAngleY = 0 /*, mpuLastAngleZ = 0*/;
static float mpuAngleX = 0, mpuAngleY = 0, mpuAngleZ = 0;
mpu.update();
// fetch axis and convert to gun-specific axes
mpuAngleX = -mpu.getAngleZ(); // horizontal axis
mpuAngleY = mpu.getAngleY(); // vertical axis
mpuAngleZ = mpu.getAngleX(); // lean
#ifdef DEBUG_GYRO
if (timestamp >= debugLastUpdate + 2000) { // only show debug info every 2 secs to avoid serial spam
DEBUG_PRINT("Gryo Hori:");
DEBUG_PRINT(mpuAngleX);
DEBUG_PRINT(" Vert:");
DEBUG_PRINT(mpuAngleY);
DEBUG_PRINT(" Lean:");
DEBUG_PRINTLN(mpuAngleZ);
}
#endif
bool mpuValidX = true, mpuValidY = true, mpuValidZ = true;
// ignore if lean angle too shallow (ignore twitching) or too steep (assume gun laying down)
if ((mpuAngleZ < 40 && mpuAngleZ > -40) || mpuAngleZ > 75 || mpuAngleZ < -75) {
mpuValidZ = false;
}
int mouseMoveX = 0;
if (mpuValidX) {
if (!activeEncoder) mouseMoveX = (mpuAngleX - mpuLastAngleX) * mpuMovementMultiplierX;
mpuLastAngleX = mpuAngleX;
}
int mouseMoveY = 0;
if (mpuValidY) {
if (!activeEncoder) mouseMoveY = (mpuAngleY - mpuLastAngleY) * mpuMovementMultiplierY;
mpuLastAngleY = mpuAngleY;
}
#ifdef DEBUG_GYRO
if (timestamp >= debugLastUpdate + 2000) { // only show debug info every 2 secs to avoid serial spam
DEBUG_PRINT("Moving mouse X:");
DEBUG_PRINT(mouseMoveX);
DEBUG_PRINT(" and Y:");
DEBUG_PRINT(mouseMoveY);
DEBUG_PRINT(" -- Leaning:");
// Display what's happening
displayConfig(true);
display.print("Moving mouse \nX:");
display.print(mouseMoveX);
display.print(" and Y:");
display.print(mouseMoveY);
display.println("\nLeaning:");
// End display
if (mpuValidZ) {
if (mpuAngleZ > 0) {
DEBUG_PRINTLN("right");
display.println("right");
} else {
DEBUG_PRINTLN("left");
display.println("left");
}
} else {
DEBUG_PRINTLN("none");
display.println("none");
}
display.display();
debugLastUpdate = timestamp;
}
#else
if ((mouseMoveX != 0 || mouseMoveY != 0) && activeGun) {
Mouse.move(mouseMoveX, mouseMoveY, 0);
}
if (mpuValidZ && activeGun) {
if (mpuAngleZ > 0) {
Keyboard.press(KEY_PAGE_DOWN);
} else {
Keyboard.press(KEY_PAGE_UP);
}
} else {
Keyboard.release(KEY_PAGE_DOWN);
Keyboard.release(KEY_PAGE_UP);
}
#endif
}
// Save values to EEPROM after a delay (saves on EEPROM lifespan)
void SaveToEEPROM() {
if (mpuMultiplierXHasChanged) {
#ifdef DEBUG_ENCODER
DEBUG_PRINT("Saving X movement multiplier (");
DEBUG_PRINT(mpuMovementMultiplierX);
DEBUG_PRINTLN(") to EEPROM");
#endif
EEPROM.put(0, mpuMovementMultiplierX);
mpuMultiplierXHasChanged = false;
}
if (mpuMultiplierYHasChanged) {
#ifdef DEBUG_ENCODER
DEBUG_PRINT("Saving Y movement multiplier (");
DEBUG_PRINT(mpuMovementMultiplierY);
DEBUG_PRINTLN(") to EEPROM");
#endif
EEPROM.put(1, mpuMovementMultiplierY);
mpuMultiplierYHasChanged = false;
}
}
// Abort message upon initialization failure
void failsafe() {
DEBUG_PRINTLN("Failsafe triggered. Code will not execute");
}