-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathddjoystick.h
More file actions
975 lines (929 loc) · 24.7 KB
/
ddjoystick.h
File metadata and controls
975 lines (929 loc) · 24.7 KB
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
#ifndef ddjoystick_h
#define ddjoystick_h
/// Struct used by the helper class like JoystickInterface
struct JoystickPress
{
int xPressed; // -1, 0 or 1
int yPressed; // -1, 0 or 1
};
/// Struct used by the helper class like JoystickInterface
struct ABCDPressed
{
bool aPressed;
bool bPressed;
bool cPressed;
bool dPressed;
};
/// Struct used by the helper class like JoystickInterface
struct JoystickPressCode
{
int xPressed;
int yPressed;
bool swPressed;
};
/// Base Helper class for joystick input tracking. For an example use, you may want to refer to [Wireless Joystick Module Experiment With ESP8266 on ESP-Now](https://www.instructables.com/Wireless-Joystick-Module-Experiment-With-ESP8266-o/)
class JoystickInterface
{
public:
static const long BlackOutMillis = 50;
protected:
JoystickInterface(bool buttonsOnly)
{
this->buttonsOnly = buttonsOnly;
// this->lastCheckJoystickPress.xPressed = 0;
// this->lastCheckJoystickPress.yPressed = 0;
this->lastCheckMillis = 0;
// this->lastCheckABCDPressed.aPressed = false;
// this->lastCheckABCDPressed.bPressed = false;
// this->lastCheckABCDPressed.dPressed = false;
// this->lastCheckABCDPressed.cPressed = false;
}
public:
const JoystickPress *checkJoystickPress(int repeat = 0)
{
int xPressed = _checkPressedX(repeat, false);
int yPressed = _checkPressedY(repeat, false);
if (xPressed != 0 || yPressed != 0)
{
// Serial.print(xPressed);
// Serial.print("/");
// Serial.println(yPressed);
long nowMillis = millis();
long diffMillis = nowMillis - this->lastCheckMillis;
// Serial.println(diffMillis);
// delay(200);
if (diffMillis >= BlackOutMillis)
{
lastCheckJoystickPress.xPressed = 0;
lastCheckJoystickPress.yPressed = 0;
bool pressedA = _checkPressedBypass('A') || yPressed == -1;
bool pressedB = _checkPressedBypass('B') || xPressed == 1;
bool pressedC = _checkPressedBypass('C') || yPressed == 1;
bool pressedD = _checkPressedBypass('D') || xPressed == -1;
if (pressedB)
{
// Serial.println("B");
lastCheckJoystickPress.xPressed = 1;
}
else if (pressedD)
{
// Serial.println("D");
lastCheckJoystickPress.xPressed = -1;
}
if (pressedA)
{
// Serial.println("A");
lastCheckJoystickPress.yPressed = -1;
}
else if (pressedC)
{
// Serial.println("C");
lastCheckJoystickPress.yPressed = 1;
}
lastCheckMillis = nowMillis;
return &lastCheckJoystickPress;
}
}
return NULL;
}
inline bool checkSWPressed(int repeat = 0)
{
return _checkPressed('E', repeat);
}
inline bool forButtonsOnly() const {
return this->buttonsOnly;
}
const ABCDPressed *checkABCDPressed(int repeat = 0)
{
int aPressed = _checkPressed('A', repeat);
int bPressed = _checkPressed('B', repeat);
int cPressed = _checkPressed('C', repeat);
int dPressed = _checkPressed('D', repeat);
if (aPressed || bPressed || cPressed || dPressed)
{
long nowMillis = millis();
long diffMillis = nowMillis - this->lastCheckMillis;
if (diffMillis >= BlackOutMillis)
{
//delay(200); // delay a bit
this->lastCheckABCDPressed.aPressed = _checkPressedBypass('A') || aPressed;
this->lastCheckABCDPressed.bPressed = _checkPressedBypass('B') || bPressed;
this->lastCheckABCDPressed.cPressed = _checkPressedBypass('C') || cPressed;
this->lastCheckABCDPressed.dPressed = _checkPressedBypass('D') || dPressed;
lastCheckMillis = nowMillis;
return &lastCheckABCDPressed;
}
}
return NULL;
}
inline bool checkAPressed(int repeat = 0)
{
return _checkPressed('A', repeat);
}
inline bool checkBPressed(int repeat = 0)
{
return _checkPressed('B', repeat);
}
inline bool checkCPressed(int repeat = 0)
{
return _checkPressed('C', repeat);
}
inline bool checkDPressed(int repeat = 0)
{
return _checkPressed('D', repeat);
}
public:
bool checkJoystickPressCode(JoystickPressCode &joystickPressCode, int repeat = 0)
{
joystickPressCode.xPressed = _checkPressedX(repeat, true);
joystickPressCode.yPressed = _checkPressedY(repeat, true);
joystickPressCode.swPressed = _checkPressed('E', repeat);
return joystickPressCode.xPressed != 0 || joystickPressCode.yPressed != 0 || joystickPressCode.swPressed;
// joystickPressCode.xPressed = 0;
// joystickPressCode.yPressed = 0;
// joystickPressCode.swPressed = false;
// const JoystickPress *joystickPress = checkJoystickPress(repeat);
// bool swPressed = checkSWPressed(repeat);
// if (joystickPress != NULL || swPressed)
// {
// if (joystickPress != NULL)
// {
// joystickPressCode.xPressed = joystickPress->xPressed;
// joystickPressCode.yPressed = joystickPress->yPressed;
// }
// joystickPressCode.swPressed = swPressed;
// return true;
// }
// else
// {
// return false;
// }
}
protected:
///
/// @param button can be 'A' to 'E'
///
inline bool _checkPressed(char button, int repeat)
{
return _checkPressed(button, repeat, false);
}
inline bool _checkPressedBypass(char button) {
return _checkPressed(button, 0, true);
}
protected:
virtual int _checkPressedX(int repeat, bool raw);
virtual int _checkPressedY(int repeat, bool raw);
virtual bool _checkPressed(char button, int repeat, bool bypass);
private:
bool buttonsOnly;
JoystickPress lastCheckJoystickPress;
ABCDPressed lastCheckABCDPressed;
long lastCheckMillis;
};
/// Helper class for joystick input tracking. See JoystickInterface.
class ButtonPressTracker
{
public:
ButtonPressTracker(uint8_t pin)
{
this->pin = pin;
this->pressed = false;
this->blackOutMillis = 0;
this->nextRepeatMillis = 0;
}
bool checkPressed(int repeat = 0)
{
int reading = digitalRead(this->pin);
return setPressed(reading == 0, repeat);
}
bool checkPressedBypass()
{
int reading = digitalRead(this->pin);
if (true) {
} else {
setPressed(reading == 0);
}
return pressed;
}
inline bool checkPressed(int repeat, bool bypass) {
if (bypass) {
return checkPressedBypass();
} else {
return checkPressed(repeat);
}
}
// inline bool debug_checkPressed(int repeat, bool bypass, char button) {
// if (bypass) {
// //Serial.println("BYPASS");
// return checkPressedBypass();
// } else {
// // Serial.print(button);
// // Serial.print(">");
// // Serial.println(repeat);
// return checkPressed(repeat);
// }
// //delay(50);
// }
private:
bool setPressed(bool pressed, int repeat = 0)
{
if (repeat == 0)
{
this->nextRepeatMillis = 0;
}
long nowMillis = millis();
if (this->blackOutMillis != 0)
{
long diff = this->blackOutMillis - nowMillis;
if (diff < 0)
{
this->blackOutMillis = 0;
}
}
if (this->blackOutMillis == 0)
{
if (pressed != this->pressed)
{
this->pressed = pressed;
this->blackOutMillis = nowMillis + JoystickInterface::BlackOutMillis /*50*/;
if (repeat != 0 && this->pressed)
{
this->nextRepeatMillis = nowMillis + repeat;
}
else
{
this->nextRepeatMillis = 0;
}
return this->pressed;
}
}
if (this->nextRepeatMillis != 0)
{
long diff = this->nextRepeatMillis - nowMillis;
if (diff < 0)
{
this->nextRepeatMillis = nowMillis + repeat;
return true;
}
}
return false;
}
private:
uint8_t pin;
bool pressed;
long blackOutMillis;
long nextRepeatMillis;
};
/// Helper class for joystick input tracking. See JoystickInterface.
class JoystickPressTracker
{
public:
static const int DefAutoTuneThreshold = 50;
static const int DefMinReading = DefAutoTuneThreshold;
static const int DefMaxReading = 1023 - DefAutoTuneThreshold;
public:
JoystickPressTracker(uint8_t pin, int minReading = DefMinReading, int maxReading = DefMaxReading)
{
this->pin = pin;
this->autoTuneThreshold = -1;
resetMinMax(minReading, maxReading, true);
}
///
/// @param autoTuneThreshold -1 if no auto tune
///
JoystickPressTracker(uint8_t pin, bool reverseDir, int autoTuneThreshold = DefAutoTuneThreshold)
{
// int autoThreshold = autoTune ? 200 : -1;
// int autoThreshold = autoTune ? 50 : -1;
this->pin = pin;
this->autoTuneThreshold = autoTuneThreshold;
this->autoMin = 10000;
this->autoMax = -1;
int minReading = reverseDir ? DefMaxReading : DefMinReading;
int maxReading = reverseDir ? DefMinReading : DefMaxReading;
resetMinMax(minReading, maxReading, true);
}
public:
int8_t checkPressed(int repeat = 0)
{
int reading = analogRead(this->pin);
if (this->autoTuneThreshold != -1)
{
if (reading < this->autoMin)
{
this->autoMin = reading;
}
if (reading > this->autoMax)
{
this->autoMax = reading;
}
if ((this->autoMax - this->autoMin) >= 800)
{
resetMinMax(this->autoMin + this->autoTuneThreshold, this->autoMax - this->autoTuneThreshold, false);
}
}
return setReading(reading, repeat);
}
int checkPressedBypass()
{
int reading = analogRead(this->pin);
if (true) {
} else {
setReading(reading);
}
int pressed = readingToPressedDir(reading);
// Serial.print(reading);
// Serial.print(">>");
// Serial.println(pressed);
return pressed;
}
int8_t checkPressed(int repeat, bool bypass) {
if (bypass) {
return checkPressedBypass();
} else {
return checkPressed(repeat);
}
}
inline int one() {
return this->reverseDir ? -1 : 1;
}
inline int minusOne() {
return this->reverseDir ? 1 : -1;
}
int readBypass()
{
int reading = analogRead(this->pin);
setReading(reading);
return reading;
}
private:
int readingToPressedDir(int reading)
{
if (reading <= this->minReading)
{
if (this->reverseDir)
{
return 1;
}
else
{
return -1;
}
}
if (reading >= this->maxReading)
{
if (this->reverseDir)
{
return -1;
}
else
{
return 1;
}
}
return 0;
}
int8_t setReading(int reading, int repeat = 0)
{
if (repeat == 0)
{
this->nextRepeatMillis = 0;
this->autoRepeatDir = 0;
}
long nowMillis = millis();
int8_t oriPressedDir = this->pressedDir;
int pressedDir = readingToPressedDir(reading);
if (pressedDir != 0)
{
this->pressedDir = pressedDir;
}
else
{
this->pressedDir = 0;
this->nextRepeatMillis = 0;
this->autoRepeatDir = 0;
}
if (!this->needReset && this->pressedMillis != 0 && (this->pressedDir == oriPressedDir))
{
// Serial.println(reading);
// delay(100);
long diffMillis = nowMillis - this->pressedMillis;
if (diffMillis >= JoystickInterface::BlackOutMillis)
{
this->pressedDir = 0;
this->pressedMillis = 0;
this->needReset = true;
if (repeat != 0 && oriPressedDir != 0)
{
this->nextRepeatMillis = nowMillis + repeat;
this->autoRepeatDir = oriPressedDir;
}
else
{
this->nextRepeatMillis = 0;
this->autoRepeatDir = 0;
}
// Serial.print(repeat);
// Serial.print(":");
// Serial.print(reading);
// Serial.print("=>");
// Serial.println(oriPressedDir);
return oriPressedDir;
}
}
else
{
if (this->pressedDir != 0)
{
if (this->pressedMillis == 0)
{
this->pressedMillis = millis();
}
}
else
{
this->pressedMillis = 0;
this->needReset = false;
}
}
if (this->nextRepeatMillis != 0)
{
long diff = this->nextRepeatMillis - nowMillis;
if (diff < 0)
{
this->nextRepeatMillis = nowMillis + repeat;
return this->autoRepeatDir;
}
}
return 0;
}
void resetMinMax(int minReading, int maxReading, bool forceReset)
{
if (forceReset || this->minReading != minReading || this->maxReading != maxReading)
{
if (forceReset)
{
if (maxReading < minReading)
{
int temp = maxReading;
maxReading = minReading;
minReading = temp;
this->reverseDir = true;
}
else
{
this->reverseDir = false;
}
}
this->maxReading = maxReading;
this->minReading = minReading;
this->pressedDir = 0;
this->pressedMillis = 0;
this->needReset = false;
this->nextRepeatMillis = 0;
this->autoRepeatDir = 0;
// Serial.print(this->minReading);
// Serial.print("-");
// Serial.println(this->maxReading);
// delay(100);
}
}
private:
int maxReading;
int minReading;
bool reverseDir;
int autoTuneThreshold; // -1 if not auto tune
int autoMin;
int autoMax;
private:
uint8_t pin;
int pressedDir;
long pressedMillis;
bool needReset;
long nextRepeatMillis;
int autoRepeatDir;
};
/// Helper class for joystick input tracking. See JoystickInterface.
class JoystickJoystick : public JoystickInterface
{
public:
JoystickJoystick(JoystickPressTracker *xTracker, JoystickPressTracker *yTracker, ButtonPressTracker *swTracker) : JoystickInterface(false)
{
this->xTracker = xTracker;
this->yTracker = yTracker;
this->swTracker = swTracker;
}
protected:
virtual int _checkPressedX(int repeat, bool raw)
{
// Serial.println(repeat);
// delay(100);
int pressed = xTracker != NULL ? xTracker->checkPressed(repeat) : 0;
if (pressed != 0) {
// Serial.print("%X=");
// Serial.println(pressed);
}
return pressed;
}
virtual int _checkPressedY(int repeat, bool raw)
{
return yTracker != NULL ? yTracker->checkPressed(repeat) : 0;
}
virtual bool _checkPressed(char button, int repeat, bool bypass)
{
if (button == 'A')
{
return yTracker != NULL && (yTracker->checkPressed(repeat, bypass) == -1);
}
else if (button == 'B')
{
return xTracker != NULL && (xTracker->checkPressed(repeat, bypass) == 1);
}
else if (button == 'C')
{
return yTracker != NULL && (yTracker->checkPressed(repeat, bypass) == 1);
}
else if (button == 'D')
{
return xTracker != NULL && (xTracker->checkPressed(repeat, bypass) == -1);
}
else if (button == 'E')
{
bool pressed = swTracker != NULL && swTracker->checkPressed(repeat, bypass);
//if (pressed && repeat != 0 && !bypass) { Serial.print(repeat); Serial.println(" <SW>"); }
return pressed;
}
else
{
return false;
}
}
// virtual bool _checkPressedBypass(char button)
// {
// if (button == 'A')
// {
// // Serial.println(yTracker->checkPressedBypass());
// // delay(200);
// return yTracker != NULL && (yTracker->checkPressedBypass() == -1);
// }
// else if (button == 'B')
// {
// return xTracker != NULL && (xTracker->checkPressedBypass() == 1);
// }
// else if (button == 'C')
// {
// return yTracker != NULL && (yTracker->checkPressedBypass() == 1);
// }
// else if (button == 'D')
// {
// return xTracker != NULL && (xTracker->checkPressedBypass() == -1);
// }
// else if (button == 'E')
// {
// return swTracker != NULL && swTracker->checkPressedBypass();
// }
// else
// {
// return false;
// }
// }
private:
JoystickPressTracker *xTracker;
JoystickPressTracker *yTracker;
ButtonPressTracker *swTracker;
};
/// Helper class for joystick input tracking. See JoystickInterface.
class ButtonJoystickBasic : public JoystickInterface
{
protected:
ButtonJoystickBasic(bool buttonsOnly) : JoystickInterface(buttonsOnly)
{
}
protected:
virtual int _checkPressedX(int repeat, bool raw)
{
//if (!raw) { Serial.print(repeat); Serial.println(); delay(100); }
bool pressedB = _checkPressed('B', repeat);
bool pressedD = _checkPressed('D', repeat);
if (pressedB)
{
//Serial.println("*B");
return raw && pressedD ? 2 : 1;
}
else if (pressedD)
{
//Serial.println("*D");
return -1;
}
else
{
return 0;
}
}
virtual int _checkPressedY(int repeat, bool raw)
{
bool pressedA = _checkPressed('A', repeat);
bool pressedC = _checkPressed('C', repeat);
if (pressedA)
{
//Serial.println("*A");
return raw && pressedC ? 2 : -1;
}
else if (pressedC)
{
// Serial.println("*C");
return 1;
}
else
{
return 0;
}
}
// virtual bool _checkPressed(char button, int repeat)
// {
// if (button == 'A')
// {
// return upTracker != NULL && upTracker->checkPressed(repeat);
// }
// else if (button == 'B')
// {
// return rightTracker != NULL && rightTracker->checkPressed(repeat);
// }
// else if (button == 'C')
// {
// return downTracker != NULL && downTracker->checkPressed(repeat);
// }
// else if (button == 'D')
// {
// return leftTracker != NULL && leftTracker->checkPressed(repeat);
// }
// else if (button == 'E')
// {
// return midTracker != NULL && midTracker->checkPressed(repeat);
// }
// else
// {
// return false;
// }
// }
// virtual bool _checkPressedBypass(char button)
// {
// if (button == 'A')
// {
// return upTracker != NULL && upTracker->checkPressedBypass();
// }
// else if (button == 'B')
// {
// return rightTracker != NULL && rightTracker->checkPressedBypass();
// }
// else if (button == 'C')
// {
// return downTracker != NULL && downTracker->checkPressedBypass();
// }
// else if (button == 'D')
// {
// return leftTracker != NULL && leftTracker->checkPressedBypass();
// }
// else if (button == 'E')
// {
// return midTracker != NULL && midTracker->checkPressedBypass();
// }
// else
// {
// return false;
// }
// }
};
/// Helper class for joystick input tracking. See JoystickInterface.
class DecodedJoystick : public ButtonJoystickBasic
{
public:
DecodedJoystick(bool buttonsOnly) : ButtonJoystickBasic(buttonsOnly)
{
aValid = false;
bValid = false;
cValid = false;
dValid = false;
eValid = false;
}
public:
void decode(JoystickPressCode &joystickPressCode)
{
this->joystickPressCode.xPressed = joystickPressCode.xPressed;
this->joystickPressCode.yPressed = joystickPressCode.yPressed;
this->joystickPressCode.swPressed = joystickPressCode.swPressed;
this->aValid = true;
this->bValid = true;
this->cValid = true;
this->dValid = true;
this->eValid = true;
}
protected:
virtual bool _checkPressed(char button, int repeat, bool bypass)
{
bool res = false;
if (button == 'A')
{
if (aValid || bypass)
{
res = joystickPressCode.yPressed == -1 || joystickPressCode.yPressed == 2;
aValid = false;
}
}
else if (button == 'B')
{
if (bValid || bypass)
{
res = joystickPressCode.xPressed == 1 || joystickPressCode.xPressed == 2;
bValid = false;
}
}
else if (button == 'C')
{
if (cValid || bypass)
{
res = joystickPressCode.yPressed == 1 || joystickPressCode.yPressed == 2;
cValid = false;
}
}
else if (button == 'D')
{
if (dValid || bypass)
{
res = joystickPressCode.xPressed == -1 || joystickPressCode.xPressed == 2;
dValid = false;
}
}
else if (button == 'E')
{
if (eValid || bypass)
{
res = joystickPressCode.swPressed;
eValid = false;
}
}
return res;
}
private:
JoystickPressCode joystickPressCode;
bool aValid;
bool bValid;
bool cValid;
bool dValid;
bool eValid;
};
/// Helper class for joystick input tracking. See JoystickInterface.
class ButtonJoystick : public ButtonJoystickBasic
{
public:
ButtonJoystick(ButtonPressTracker *upTracker, ButtonPressTracker *rightTracker, ButtonPressTracker *downTracker, ButtonPressTracker *leftTracker, ButtonPressTracker *midTracker) : ButtonJoystick(upTracker, rightTracker, downTracker, leftTracker, midTracker, false)
{
}
protected:
ButtonJoystick(ButtonPressTracker *upTracker, ButtonPressTracker *rightTracker, ButtonPressTracker *downTracker, ButtonPressTracker *leftTracker, ButtonPressTracker *midTracker, bool buttonsOnly) : ButtonJoystickBasic(buttonsOnly)
{
this->leftTracker = leftTracker;
this->rightTracker = rightTracker;
this->upTracker = upTracker;
this->downTracker = downTracker;
this->midTracker = midTracker;
//this->buttonsOnly = buttonsOnly;
}
protected:
// virtual int _checkPressedX(int repeat, bool raw)
// {
// // Serial.print("?");
// bool pressedB = _checkPressed('B', repeat);
// bool pressedD = _checkPressed('D', repeat);
// if (pressedB)
// {
// // Serial.println("*B");
// return raw && pressedD ? 2 : -1;
// }
// else if (pressedD)
// {
// // Serial.println("*D");
// return 1;
// }
// else
// {
// return 0;
// }
// }
// virtual int _checkPressedY(int repeat, bool raw)
// {
// bool pressedA = _checkPressed('A', repeat);
// bool pressedC = _checkPressed('C', repeat);
// if (pressedA)
// {
// // Serial.println("*A");
// return raw && pressedC ? 2 : -1;
// }
// else if (pressedC)
// {
// // Serial.println("*C");
// return 1;
// }
// else
// {
// return 0;
// }
// }
virtual bool _checkPressed(char button, int repeat, bool bypass)
{
if (button == 'A')
{
bool pressed = upTracker != NULL && upTracker->checkPressed(repeat, bypass);
//if (pressed && repeat != 0 && !bypass) { Serial.print(repeat); Serial.println(" <A>"); }
return pressed;
}
else if (button == 'B')
{
return rightTracker != NULL && rightTracker->checkPressed(repeat, bypass);
}
else if (button == 'C')
{
return downTracker != NULL && downTracker->checkPressed(repeat, bypass);
}
else if (button == 'D')
{
return leftTracker != NULL && leftTracker->checkPressed(repeat, bypass);
}
else if (button == 'E')
{
bool pressed = midTracker != NULL && midTracker->checkPressed(repeat, bypass);
//if (pressed && !bypass) { Serial.print(repeat); Serial.println(" <E>"); }
return pressed;
}
else
{
return false;
}
}
// virtual bool _checkPressedBypass(char button)
// {
// if (button == 'A')
// {
// return upTracker != NULL && upTracker->checkPressedBypass();
// }
// else if (button == 'B')
// {
// return rightTracker != NULL && rightTracker->checkPressedBypass();
// }
// else if (button == 'C')
// {
// return downTracker != NULL && downTracker->checkPressedBypass();
// }
// else if (button == 'D')
// {
// return leftTracker != NULL && leftTracker->checkPressedBypass();
// }
// else if (button == 'E')
// {
// return midTracker != NULL && midTracker->checkPressedBypass();
// }
// else
// {
// return false;
// }
// }
private:
ButtonPressTracker *leftTracker;
ButtonPressTracker *rightTracker;
ButtonPressTracker *upTracker;
ButtonPressTracker *downTracker;
ButtonPressTracker *midTracker;
//bool buttonsOnly;
};
/// Helper class for joystick input tracking. See JoystickInterface.
class ButtonsOnly : public ButtonJoystick
{
public:
ButtonsOnly(ButtonPressTracker *aTracker, ButtonPressTracker *bTracker, ButtonPressTracker *cTracker, ButtonPressTracker *dTracker) : ButtonJoystick(aTracker, bTracker, cTracker, dTracker, NULL, true)
{
}
// public:
// inline bool checkButtonAPressed(int repeat) {
// return checkButtonPressed('A', repeat);
// }
// inline bool checkButtonBPressed(int repeat) {
// return checkButtonPressed('B', repeat);
// }
// inline bool checkButtonCPressed(int repeat) {
// return checkButtonPressed('C', repeat);
// }
// inline bool checkButtonDPressed(int repeat) {
// return checkButtonPressed('D', repeat);
// }
};
JoystickPressTracker *SetupNewJoystickPressTracker(uint8_t pin, bool reverseDir, int autoTuneThreshold = JoystickPressTracker::DefAutoTuneThreshold)
{
pinMode(pin, INPUT);
return new JoystickPressTracker(pin, reverseDir, autoTuneThreshold);
}
ButtonPressTracker *SetupNewButtonPressTracker(uint8_t pin)
{
pinMode(pin, INPUT_PULLUP);
return new ButtonPressTracker(pin);
}
#endif