diff --git a/Defines.h b/Defines.h index 9b6b722..5627d9f 100644 --- a/Defines.h +++ b/Defines.h @@ -29,4 +29,11 @@ #define BTN_DOWN_HOLDED -1 #define BTN_DOWN_UP_NOT_HOLDED 0 +#define LED_MAX_DISTANCE 1312 +#define LED_DISTANCE_ARRAY LedDistances [MAX_LED_INDEX + 1] = { 0.0, 0.013, 0.029, 0.049, 0.073, 0.101, 0.135, 0.176, 0.225, 0.285, 0.356, 0.442, 0.546, 0.670, 0.820, 1.0 } + +#define MAX_SWITCH_TIME 15000 +#define MIN_SWITCH_TIME 500 +#define DEFAULT_SWITCH_TIME 5000 + #endif \ No newline at end of file diff --git a/DisplayHelper.h b/DisplayHelper.h index a88a974..402d554 100644 --- a/DisplayHelper.h +++ b/DisplayHelper.h @@ -167,10 +167,10 @@ public: { switch(Settings::getInstance().RunModeState) { - case RunMode::FORWARD: display.setSegments(WORD(F, R, W, D )); break; - case RunMode::BACKWARD: display.setSegments(WORD(B, A, C, D )); break; - case RunMode::BOTH: display.setSegments(WORD(P, I, N, G )); break; - case RunMode::RANDOM: display.setSegments(WORD(R, N, D, NONE)); break; + case RunMode::FORWARD: display.setSegments(WORD(F, R, W, D )); break; + case RunMode::BACKWARD: display.setSegments(WORD(B, A, C, D )); break; + case RunMode::PING_PONG: display.setSegments(WORD(P, I, N, G )); break; + case RunMode::RANDOM: display.setSegments(WORD(R, N, D, NONE)); break; } } } diff --git a/Enums.h b/Enums.h index d84340f..57f783c 100644 --- a/Enums.h +++ b/Enums.h @@ -15,7 +15,7 @@ enum RunMode { FORWARD = 0, BACKWARD = 1, - BOTH = 2, + PING_PONG = 2, RANDOM = 3 }; diff --git a/LEDRunner.h b/LEDRunner.h index 86b2e80..4c651f4 100644 --- a/LEDRunner.h +++ b/LEDRunner.h @@ -39,7 +39,8 @@ public: m_CurrentTime += _DeltaTime; - if(m_CurrentTime > Settings::getInstance().SwitchTime) + //Settings::getInstance().SwitchTime + if(m_CurrentTime > Settings::getInstance().getDistanceCorrectedSwitchTime(m_CurrentLED)) { m_CurrentTime = 0; nextLED(Settings::getInstance().RunModeState, 1); @@ -176,7 +177,7 @@ public: } private: - bool m_Run = false; + bool m_Run = true; unsigned long m_CurrentTime = 0; int m_CurrentLED = 0; int m_CurrentLEDPingPong = 0; @@ -188,9 +189,9 @@ private: { case RunMode::FORWARD: m_CurrentLED = wrapInt(m_CurrentLED + _NextLEDIndexIncrement, MAX_LED_INDEX + 1); break; case RunMode::BACKWARD: m_CurrentLED = wrapInt(m_CurrentLED - _NextLEDIndexIncrement, MAX_LED_INDEX + 1); break; - case RunMode::BOTH: - m_CurrentLEDPingPong = wrapInt(m_CurrentLEDPingPong + _NextLEDIndexIncrement, (MAX_LED_INDEX + 1) * 2); - m_CurrentLED = pingPong(m_CurrentLEDPingPong, MAX_LED_INDEX + 1); + case RunMode::PING_PONG: + m_CurrentLEDPingPong = wrapInt(m_CurrentLEDPingPong + _NextLEDIndexIncrement, MAX_LED_INDEX * 2); + m_CurrentLED = pingPong(m_CurrentLEDPingPong, MAX_LED_INDEX); break; case RunMode::RANDOM: m_CurrentLED = random(0, MAX_LED_INDEX + 1); break; } diff --git a/Settings.h b/Settings.h index 1581a96..0029db8 100644 --- a/Settings.h +++ b/Settings.h @@ -7,10 +7,11 @@ class Settings { public: - enum RunMode RunModeState = RunMode::RANDOM; + enum RunMode RunModeState = RunMode::PING_PONG; enum TimeMode TimeModeState = TimeMode::CONST; - int SwitchTime = 3000; //In milliseconds + int SwitchTime = DEFAULT_SWITCH_TIME; //In milliseconds float Brightness = MAX_BRIGHTNESS / 2; + float LED_DISTANCE_ARRAY; //======================================= //Singleton. AVOID COPY ASSIGNMENTS @@ -32,14 +33,14 @@ public: void setSwitchTime(int _SwitchTime) { - if(_SwitchTime > 15000) + if(_SwitchTime > MAX_SWITCH_TIME) { - SwitchTime = 15000; + SwitchTime = MAX_SWITCH_TIME; return; } - if(_SwitchTime < 250) + if(_SwitchTime < MIN_SWITCH_TIME) { - SwitchTime = 250; + SwitchTime = MIN_SWITCH_TIME; return; } @@ -86,6 +87,11 @@ public: return SwitchTime / 10; } + int getDistanceCorrectedSwitchTime(int _LedIndex) + { + return (int) (min(max(SwitchTime * LedDistances[_LedIndex], MIN_SWITCH_TIME), MAX_SWITCH_TIME)); + } + private: int wrapInt(int _Num, int _Max) { diff --git a/WrapHelper.h b/WrapHelper.h index f02e776..3693d42 100644 --- a/WrapHelper.h +++ b/WrapHelper.h @@ -15,23 +15,23 @@ int wrapInt(int _Num, int _Max) return _Num; } -//Fix upper bounce (all LEDs are off) -int pingPong(int _Num, int _Max) +int pingPong(int _Num, int _MaxHalfIndex) { - if(_Num < 0) - { - return (_Max + _Num % _Max); - } - if(_Num > _Max * 2) - { - return _Num % _Max; - } - if(_Num > _Max) - { - return _Max - _Num % _Max; - } + const int maxHalfCount = _MaxHalfIndex + 1; + const int maxFullIndex = _MaxHalfIndex * 2; + const int maxFullCount = (_MaxHalfIndex + 1) * 2; + int numInRange = _Num < 0 + ? _Num % maxFullCount == 0 + ? 0 + : _Num % maxFullCount + maxFullCount + : _Num % maxFullCount; - return _Num; + if(numInRange > _MaxHalfIndex) + { + return maxFullIndex - numInRange; + } + + return numInRange; } int wrapInt(int _Num, int _Min, int _Max)