36 static const long BlackOutMillis = 50;
41 this->buttonsOnly = buttonsOnly;
44 this->lastCheckMillis = 0;
54 int xPressed = _checkPressedX(repeat,
false);
55 int yPressed = _checkPressedY(repeat,
false);
56 if (xPressed != 0 || yPressed != 0)
61 long nowMillis = millis();
62 long diffMillis = nowMillis - this->lastCheckMillis;
65 if (diffMillis >= BlackOutMillis)
67 lastCheckJoystickPress.xPressed = 0;
68 lastCheckJoystickPress.yPressed = 0;
69 bool pressedA = _checkPressedBypass(
'A') || yPressed == -1;
70 bool pressedB = _checkPressedBypass(
'B') || xPressed == 1;
71 bool pressedC = _checkPressedBypass(
'C') || yPressed == 1;
72 bool pressedD = _checkPressedBypass(
'D') || xPressed == -1;
76 lastCheckJoystickPress.xPressed = 1;
81 lastCheckJoystickPress.xPressed = -1;
86 lastCheckJoystickPress.yPressed = -1;
91 lastCheckJoystickPress.yPressed = 1;
93 lastCheckMillis = nowMillis;
94 return &lastCheckJoystickPress;
99 inline bool checkSWPressed(
int repeat = 0)
103 inline bool forButtonsOnly()
const {
104 return this->buttonsOnly;
106 const ABCDPressed *checkABCDPressed(
int repeat = 0)
112 if (aPressed || bPressed || cPressed || dPressed)
114 long nowMillis = millis();
115 long diffMillis = nowMillis - this->lastCheckMillis;
116 if (diffMillis >= BlackOutMillis)
119 this->lastCheckABCDPressed.aPressed = _checkPressedBypass(
'A') || aPressed;
120 this->lastCheckABCDPressed.bPressed = _checkPressedBypass(
'B') || bPressed;
121 this->lastCheckABCDPressed.cPressed = _checkPressedBypass(
'C') || cPressed;
122 this->lastCheckABCDPressed.dPressed = _checkPressedBypass(
'D') || dPressed;
123 lastCheckMillis = nowMillis;
124 return &lastCheckABCDPressed;
129 inline bool checkAPressed(
int repeat = 0)
133 inline bool checkBPressed(
int repeat = 0)
137 inline bool checkCPressed(
int repeat = 0)
141 inline bool checkDPressed(
int repeat = 0)
147 bool checkJoystickPressCode(
JoystickPressCode &joystickPressCode,
int repeat = 0)
149 joystickPressCode.xPressed = _checkPressedX(repeat,
true);
150 joystickPressCode.yPressed = _checkPressedY(repeat,
true);
152 return joystickPressCode.xPressed != 0 || joystickPressCode.yPressed != 0 || joystickPressCode.swPressed;
182 inline bool _checkPressedBypass(
char button) {
187 virtual int _checkPressedX(
int repeat,
bool raw);
188 virtual int _checkPressedY(
int repeat,
bool raw);
189 virtual bool _checkPressed(
char button,
int repeat,
bool bypass);
195 long lastCheckMillis;
205 this->pressed =
false;
206 this->blackOutMillis = 0;
207 this->nextRepeatMillis = 0;
209 bool checkPressed(
int repeat = 0)
211 int reading = digitalRead(this->pin);
212 return setPressed(reading == 0, repeat);
214 bool checkPressedBypass()
216 int reading = digitalRead(this->pin);
219 setPressed(reading == 0);
223 inline bool checkPressed(
int repeat,
bool bypass) {
225 return checkPressedBypass();
227 return checkPressed(repeat);
245 bool setPressed(
bool pressed,
int repeat = 0)
249 this->nextRepeatMillis = 0;
251 long nowMillis = millis();
252 if (this->blackOutMillis != 0)
254 long diff = this->blackOutMillis - nowMillis;
257 this->blackOutMillis = 0;
260 if (this->blackOutMillis == 0)
262 if (pressed != this->pressed)
264 this->pressed = pressed;
265 this->blackOutMillis = nowMillis + JoystickInterface::BlackOutMillis ;
266 if (repeat != 0 && this->pressed)
268 this->nextRepeatMillis = nowMillis + repeat;
272 this->nextRepeatMillis = 0;
274 return this->pressed;
277 if (this->nextRepeatMillis != 0)
279 long diff = this->nextRepeatMillis - nowMillis;
282 this->nextRepeatMillis = nowMillis + repeat;
293 long nextRepeatMillis;
301 static const int DefAutoTuneThreshold = 50;
302 static const int DefMinReading = DefAutoTuneThreshold;
303 static const int DefMaxReading = 1023 - DefAutoTuneThreshold;
306 JoystickPressTracker(uint8_t pin,
int minReading = DefMinReading,
int maxReading = DefMaxReading)
309 this->autoTuneThreshold = -1;
310 resetMinMax(minReading, maxReading,
true);
320 this->autoTuneThreshold = autoTuneThreshold;
321 this->autoMin = 10000;
323 int minReading = reverseDir ? DefMaxReading : DefMinReading;
324 int maxReading = reverseDir ? DefMinReading : DefMaxReading;
325 resetMinMax(minReading, maxReading,
true);
329 int8_t checkPressed(
int repeat = 0)
331 int reading = analogRead(this->pin);
332 if (this->autoTuneThreshold != -1)
334 if (reading < this->autoMin)
336 this->autoMin = reading;
338 if (reading > this->autoMax)
340 this->autoMax = reading;
342 if ((this->autoMax - this->autoMin) >= 800)
344 resetMinMax(this->autoMin + this->autoTuneThreshold, this->autoMax - this->autoTuneThreshold,
false);
347 return setReading(reading, repeat);
349 int checkPressedBypass()
351 int reading = analogRead(this->pin);
356 int pressed = readingToPressedDir(reading);
362 int8_t checkPressed(
int repeat,
bool bypass) {
364 return checkPressedBypass();
366 return checkPressed(repeat);
370 return this->reverseDir ? -1 : 1;
372 inline int minusOne() {
373 return this->reverseDir ? 1 : -1;
377 int reading = analogRead(this->pin);
383 int readingToPressedDir(
int reading)
385 if (reading <= this->minReading)
387 if (this->reverseDir)
396 if (reading >= this->maxReading)
398 if (this->reverseDir)
409 int8_t setReading(
int reading,
int repeat = 0)
413 this->nextRepeatMillis = 0;
414 this->autoRepeatDir = 0;
416 long nowMillis = millis();
417 int8_t oriPressedDir = this->pressedDir;
418 int pressedDir = readingToPressedDir(reading);
421 this->pressedDir = pressedDir;
425 this->pressedDir = 0;
426 this->nextRepeatMillis = 0;
427 this->autoRepeatDir = 0;
429 if (!this->needReset && this->pressedMillis != 0 && (this->pressedDir == oriPressedDir))
433 long diffMillis = nowMillis - this->pressedMillis;
434 if (diffMillis >= JoystickInterface::BlackOutMillis)
436 this->pressedDir = 0;
437 this->pressedMillis = 0;
438 this->needReset =
true;
439 if (repeat != 0 && oriPressedDir != 0)
441 this->nextRepeatMillis = nowMillis + repeat;
442 this->autoRepeatDir = oriPressedDir;
446 this->nextRepeatMillis = 0;
447 this->autoRepeatDir = 0;
454 return oriPressedDir;
459 if (this->pressedDir != 0)
461 if (this->pressedMillis == 0)
463 this->pressedMillis = millis();
468 this->pressedMillis = 0;
469 this->needReset =
false;
472 if (this->nextRepeatMillis != 0)
474 long diff = this->nextRepeatMillis - nowMillis;
477 this->nextRepeatMillis = nowMillis + repeat;
478 return this->autoRepeatDir;
483 void resetMinMax(
int minReading,
int maxReading,
bool forceReset)
485 if (forceReset || this->minReading != minReading || this->maxReading != maxReading)
489 if (maxReading < minReading)
491 int temp = maxReading;
492 maxReading = minReading;
494 this->reverseDir =
true;
498 this->reverseDir =
false;
501 this->maxReading = maxReading;
502 this->minReading = minReading;
503 this->pressedDir = 0;
504 this->pressedMillis = 0;
505 this->needReset =
false;
506 this->nextRepeatMillis = 0;
507 this->autoRepeatDir = 0;
519 int autoTuneThreshold;
528 long nextRepeatMillis;
538 this->xTracker = xTracker;
539 this->yTracker = yTracker;
540 this->swTracker = swTracker;
544 virtual int _checkPressedX(
int repeat,
bool raw)
548 int pressed = xTracker != NULL ? xTracker->checkPressed(repeat) : 0;
555 virtual int _checkPressedY(
int repeat,
bool raw)
557 return yTracker != NULL ? yTracker->checkPressed(repeat) : 0;
559 virtual bool _checkPressed(
char button,
int repeat,
bool bypass)
563 return yTracker != NULL && (yTracker->checkPressed(repeat, bypass) == -1);
565 else if (button ==
'B')
567 return xTracker != NULL && (xTracker->checkPressed(repeat, bypass) == 1);
569 else if (button ==
'C')
571 return yTracker != NULL && (yTracker->checkPressed(repeat, bypass) == 1);
573 else if (button ==
'D')
575 return xTracker != NULL && (xTracker->checkPressed(repeat, bypass) == -1);
577 else if (button ==
'E')
579 bool pressed = swTracker != NULL && swTracker->checkPressed(repeat, bypass);
633 virtual int _checkPressedX(
int repeat,
bool raw)
641 return raw && pressedD ? 2 : 1;
653 virtual int _checkPressedY(
int repeat,
bool raw)
660 return raw && pressedC ? 2 : -1;
746 this->joystickPressCode.xPressed = joystickPressCode.xPressed;
747 this->joystickPressCode.yPressed = joystickPressCode.yPressed;
748 this->joystickPressCode.swPressed = joystickPressCode.swPressed;
757 virtual bool _checkPressed(
char button,
int repeat,
bool bypass)
762 if (aValid || bypass)
764 res = joystickPressCode.yPressed == -1 || joystickPressCode.yPressed == 2;
768 else if (button ==
'B')
770 if (bValid || bypass)
772 res = joystickPressCode.xPressed == 1 || joystickPressCode.xPressed == 2;
776 else if (button ==
'C')
778 if (cValid || bypass)
780 res = joystickPressCode.yPressed == 1 || joystickPressCode.yPressed == 2;
784 else if (button ==
'D')
786 if (dValid || bypass)
788 res = joystickPressCode.xPressed == -1 || joystickPressCode.xPressed == 2;
792 else if (button ==
'E')
794 if (eValid || bypass)
796 res = joystickPressCode.swPressed;
824 this->leftTracker = leftTracker;
825 this->rightTracker = rightTracker;
826 this->upTracker = upTracker;
827 this->downTracker = downTracker;
828 this->midTracker = midTracker;
872 virtual bool _checkPressed(
char button,
int repeat,
bool bypass)
876 bool pressed = upTracker != NULL && upTracker->checkPressed(repeat, bypass);
880 else if (button ==
'B')
882 return rightTracker != NULL && rightTracker->checkPressed(repeat, bypass);
884 else if (button ==
'C')
886 return downTracker != NULL && downTracker->checkPressed(repeat, bypass);
888 else if (button ==
'D')
890 return leftTracker != NULL && leftTracker->checkPressed(repeat, bypass);
892 else if (button ==
'E')
894 bool pressed = midTracker != NULL && midTracker->checkPressed(repeat, bypass);
963JoystickPressTracker *SetupNewJoystickPressTracker(uint8_t pin,
bool reverseDir,
int autoTuneThreshold = JoystickPressTracker::DefAutoTuneThreshold)
970 pinMode(pin, INPUT_PULLUP);
Helper class for joystick input tracking. See JoystickInterface.
Definition: ddjoystick.h:732
Base Helper class for joystick input tracking. For an example use, you may want to refer to Wireless ...
Definition: ddjoystick.h:34
bool _checkPressed(char button, int repeat)
Definition: ddjoystick.h:178
Helper class for joystick input tracking. See JoystickInterface.
Definition: ddjoystick.h:534
Helper class for joystick input tracking. See JoystickInterface.
Definition: ddjoystick.h:299
JoystickPressTracker(uint8_t pin, bool reverseDir, int autoTuneThreshold=DefAutoTuneThreshold)
Definition: ddjoystick.h:315
Struct used by the helper class like JoystickInterface.
Definition: ddjoystick.h:16
Struct used by the helper class like JoystickInterface.
Definition: ddjoystick.h:26
Struct used by the helper class like JoystickInterface.
Definition: ddjoystick.h:9