From 7861a6d207efb1bf410f620687e5990264171832 Mon Sep 17 00:00:00 2001 From: Author glitchrain Date: Sat, 14 Dec 2024 07:46:47 -0800 Subject: [PATCH] Added ping pong mode (with minor errors). --- DisplayHelper.h | 6 ++---- EyeTrainerMain.h | 3 ++- LEDRunner.h | 28 ++++++++++++++++++++++++++-- Settings.h | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/DisplayHelper.h b/DisplayHelper.h index 5bf5615..14162bb 100644 --- a/DisplayHelper.h +++ b/DisplayHelper.h @@ -195,13 +195,11 @@ public: } else if(_Run) { - uint8_t forward[4] = {P, L, A, NONE}; - display.setSegments(forward); + display.setSegments((const uint8_t[]) {P, L, A, NONE}); } else { - uint8_t forward[4] = {P, A, U, S}; - display.setSegments(forward); + display.setSegments((const uint8_t[]) {P, A, U, S}); } } diff --git a/EyeTrainerMain.h b/EyeTrainerMain.h index e15f53b..e2710b7 100644 --- a/EyeTrainerMain.h +++ b/EyeTrainerMain.h @@ -71,7 +71,7 @@ private: { if(_Num < 0) { - return (_Max - _Num) % _Max; + return (_Max + _Num % _Max); } if(_Num >= _Max) { @@ -133,6 +133,7 @@ private: return (upButtonHold - downButtonHold); } + //TODO: FIX BUG HERE: display is not updated. void ProcessRun() { ProcessChangeMode(); diff --git a/LEDRunner.h b/LEDRunner.h index 7deff56..fbcefb1 100644 --- a/LEDRunner.h +++ b/LEDRunner.h @@ -35,7 +35,10 @@ public: case RunMode::FORWARD: m_CurrentLED = wrapInt(m_CurrentLED + 1, MAX_LED_INDEX + 1); break; case RunMode::BACKWARD: m_CurrentLED = wrapInt(m_CurrentLED - 1, MAX_LED_INDEX + 1); break; //TODO: ADD PING PONG - case RunMode::BOTH: m_CurrentLED = wrapInt(m_CurrentLED - 1, MAX_LED_INDEX + 1); break; + case RunMode::BOTH: + m_CurrentLEDPingPong = wrapInt(m_CurrentLEDPingPong + 1, (MAX_LED_INDEX + 1) * 2); + m_CurrentLED = pingPong(m_CurrentLEDPingPong, MAX_LED_INDEX + 1); + break; case RunMode::RANDOM: m_CurrentLED = random(0, MAX_LED_INDEX + 1); break; } } @@ -192,13 +195,14 @@ public: bool m_Run = false; unsigned long m_CurrentTime = 0; int m_CurrentLED = 0; + int m_CurrentLEDPingPong = 0; ShiftRegister74HC595 sr = ShiftRegister74HC595::ShiftRegister74HC595(PIN_LED_DATA, PIN_LED_CLOCK, PIN_LED_LATCH); int wrapInt(int _Num, int _Max) { if(_Num < 0) { - return (_Max - _Num) % _Max; + return (_Max + _Num % _Max); } if(_Num >= _Max) { @@ -208,6 +212,26 @@ public: return _Num; } + //Fix upper bounce (all LEDs are off) + int pingPong(int _Num, int _Max) + { + if(_Num < 0) + { + return (_Max + _Num % _Max); + } + if(_Num > _Max * 2) + { + return _Num % _Max; + } + if(_Num > _Max) + { + return _Max - _Num % _Max; + } + + + return _Num; + } + int wrapInt(int _Num, int _Min, int _Max) { int num = _Num - _Min; diff --git a/Settings.h b/Settings.h index 21eddb3..1482adf 100644 --- a/Settings.h +++ b/Settings.h @@ -88,7 +88,7 @@ private: { if(_Num < 0) { - return (_Max - _Num) % _Max; + return (_Max + _Num % _Max); } if(_Num >= _Max) {