Added distance corrected switch time for LEDs.

This commit is contained in:
s.aydarov
2025-01-20 19:13:29 -08:00
parent 914b1d6588
commit 47cb78f6c6
6 changed files with 45 additions and 31 deletions

View File

@@ -29,4 +29,11 @@
#define BTN_DOWN_HOLDED -1 #define BTN_DOWN_HOLDED -1
#define BTN_DOWN_UP_NOT_HOLDED 0 #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 #endif

View File

@@ -167,10 +167,10 @@ public:
{ {
switch(Settings::getInstance().RunModeState) switch(Settings::getInstance().RunModeState)
{ {
case RunMode::FORWARD: display.setSegments(WORD(F, R, W, D )); 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::BACKWARD: display.setSegments(WORD(B, A, C, D )); break;
case RunMode::BOTH: display.setSegments(WORD(P, I, N, G )); break; case RunMode::PING_PONG: display.setSegments(WORD(P, I, N, G )); break;
case RunMode::RANDOM: display.setSegments(WORD(R, N, D, NONE)); break; case RunMode::RANDOM: display.setSegments(WORD(R, N, D, NONE)); break;
} }
} }
} }

View File

@@ -15,7 +15,7 @@ enum RunMode
{ {
FORWARD = 0, FORWARD = 0,
BACKWARD = 1, BACKWARD = 1,
BOTH = 2, PING_PONG = 2,
RANDOM = 3 RANDOM = 3
}; };

View File

@@ -39,7 +39,8 @@ public:
m_CurrentTime += _DeltaTime; m_CurrentTime += _DeltaTime;
if(m_CurrentTime > Settings::getInstance().SwitchTime) //Settings::getInstance().SwitchTime
if(m_CurrentTime > Settings::getInstance().getDistanceCorrectedSwitchTime(m_CurrentLED))
{ {
m_CurrentTime = 0; m_CurrentTime = 0;
nextLED(Settings::getInstance().RunModeState, 1); nextLED(Settings::getInstance().RunModeState, 1);
@@ -176,7 +177,7 @@ public:
} }
private: private:
bool m_Run = false; bool m_Run = true;
unsigned long m_CurrentTime = 0; unsigned long m_CurrentTime = 0;
int m_CurrentLED = 0; int m_CurrentLED = 0;
int m_CurrentLEDPingPong = 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::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::BACKWARD: m_CurrentLED = wrapInt(m_CurrentLED - _NextLEDIndexIncrement, MAX_LED_INDEX + 1); break;
case RunMode::BOTH: case RunMode::PING_PONG:
m_CurrentLEDPingPong = wrapInt(m_CurrentLEDPingPong + _NextLEDIndexIncrement, (MAX_LED_INDEX + 1) * 2); m_CurrentLEDPingPong = wrapInt(m_CurrentLEDPingPong + _NextLEDIndexIncrement, MAX_LED_INDEX * 2);
m_CurrentLED = pingPong(m_CurrentLEDPingPong, MAX_LED_INDEX + 1); m_CurrentLED = pingPong(m_CurrentLEDPingPong, MAX_LED_INDEX);
break; break;
case RunMode::RANDOM: m_CurrentLED = random(0, MAX_LED_INDEX + 1); break; case RunMode::RANDOM: m_CurrentLED = random(0, MAX_LED_INDEX + 1); break;
} }

View File

@@ -7,10 +7,11 @@
class Settings class Settings
{ {
public: public:
enum RunMode RunModeState = RunMode::RANDOM; enum RunMode RunModeState = RunMode::PING_PONG;
enum TimeMode TimeModeState = TimeMode::CONST; enum TimeMode TimeModeState = TimeMode::CONST;
int SwitchTime = 3000; //In milliseconds int SwitchTime = DEFAULT_SWITCH_TIME; //In milliseconds
float Brightness = MAX_BRIGHTNESS / 2; float Brightness = MAX_BRIGHTNESS / 2;
float LED_DISTANCE_ARRAY;
//======================================= //=======================================
//Singleton. AVOID COPY ASSIGNMENTS //Singleton. AVOID COPY ASSIGNMENTS
@@ -32,14 +33,14 @@ public:
void setSwitchTime(int _SwitchTime) void setSwitchTime(int _SwitchTime)
{ {
if(_SwitchTime > 15000) if(_SwitchTime > MAX_SWITCH_TIME)
{ {
SwitchTime = 15000; SwitchTime = MAX_SWITCH_TIME;
return; return;
} }
if(_SwitchTime < 250) if(_SwitchTime < MIN_SWITCH_TIME)
{ {
SwitchTime = 250; SwitchTime = MIN_SWITCH_TIME;
return; return;
} }
@@ -86,6 +87,11 @@ public:
return SwitchTime / 10; return SwitchTime / 10;
} }
int getDistanceCorrectedSwitchTime(int _LedIndex)
{
return (int) (min(max(SwitchTime * LedDistances[_LedIndex], MIN_SWITCH_TIME), MAX_SWITCH_TIME));
}
private: private:
int wrapInt(int _Num, int _Max) int wrapInt(int _Num, int _Max)
{ {

View File

@@ -15,23 +15,23 @@ int wrapInt(int _Num, int _Max)
return _Num; return _Num;
} }
//Fix upper bounce (all LEDs are off) int pingPong(int _Num, int _MaxHalfIndex)
int pingPong(int _Num, int _Max)
{ {
if(_Num < 0) const int maxHalfCount = _MaxHalfIndex + 1;
{ const int maxFullIndex = _MaxHalfIndex * 2;
return (_Max + _Num % _Max); const int maxFullCount = (_MaxHalfIndex + 1) * 2;
} int numInRange = _Num < 0
if(_Num > _Max * 2) ? _Num % maxFullCount == 0
{ ? 0
return _Num % _Max; : _Num % maxFullCount + maxFullCount
} : _Num % maxFullCount;
if(_Num > _Max)
{
return _Max - _Num % _Max;
}
return _Num; if(numInRange > _MaxHalfIndex)
{
return maxFullIndex - numInRange;
}
return numInRange;
} }
int wrapInt(int _Num, int _Min, int _Max) int wrapInt(int _Num, int _Min, int _Max)