Fixed probleb with copy assignment of settings in LEDRunner and DisplayHelper classes. Cleaned code up a bit.
This commit is contained in:
committed by
s.aydarov
parent
031a19b107
commit
11b4909f39
@@ -21,4 +21,8 @@
|
||||
|
||||
#define LONG_CLICK_TIME_MS 500
|
||||
|
||||
#define BTN_UP_SHORT_CLICKED 1
|
||||
#define BTN_DOWN_SHORT_CLICKED -1
|
||||
#define BTN_DOWN_UP_NO_SHORT_CLICKED 0
|
||||
|
||||
#endif
|
||||
50
DisplayDefines.h
Normal file
50
DisplayDefines.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef EYE_TRAINER_DISPLAYDEFINES
|
||||
#define EYE_TRAINER_DISPLAYDEFINES 0
|
||||
|
||||
#include <TM1637Display.h>
|
||||
|
||||
//SEG_A - Up
|
||||
//SEG_B..SEG_F - Clockwise
|
||||
//SEG_G - middle
|
||||
|
||||
#define NONE 0
|
||||
#define A SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G
|
||||
#define B SEG_F | SEG_C | SEG_D | SEG_E | SEG_G
|
||||
#define C SEG_A | SEG_F | SEG_E | SEG_D
|
||||
#define D SEG_B | SEG_C | SEG_D | SEG_E | SEG_G
|
||||
#define E SEG_A | SEG_D | SEG_E | SEG_F | SEG_G
|
||||
#define F SEG_A | SEG_F | SEG_G | SEG_E
|
||||
#define G SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G
|
||||
#define H SEG_F | SEG_C | SEG_E | SEG_G
|
||||
#define I SEG_B | SEG_C
|
||||
#define J SEG_B | SEG_C | SEG_D
|
||||
#define K SEG_A
|
||||
#define L SEG_F | SEG_E | SEG_D
|
||||
#define M SEG_C | SEG_G | SEG_E
|
||||
#define N SEG_C | SEG_G | SEG_E
|
||||
#define O SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F
|
||||
#define P SEG_A | SEG_B | SEG_G | SEG_F | SEG_E
|
||||
#define Q SEG_A | SEG_B | SEG_C | SEG_G | SEG_F
|
||||
#define R SEG_E | SEG_G
|
||||
#define S SEG_A | SEG_F | SEG_G | SEG_C | SEG_D
|
||||
#define T SEG_F | SEG_E | SEG_D | SEG_G
|
||||
#define U SEG_C | SEG_D | SEG_E
|
||||
#define V SEG_C | SEG_D | SEG_E | SEG_F | SEG_B
|
||||
#define W SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G
|
||||
#define X SEG_B | SEG_C | SEG_E | SEG_F | SEG_G
|
||||
#define Y SEG_F | SEG_G | SEG_B | SEG_C
|
||||
#define Z SEG_A | SEG_B | SEG_G | SEG_E | SEG_D
|
||||
|
||||
#define WORD(a, b, c, d) (const uint8_t[]) {a, b, c, d}
|
||||
|
||||
#define WORD_DONE WORD( D, O, N, E )
|
||||
#define WORD_RUN WORD( R, U, N, NONE )
|
||||
#define WORD_MODE WORD( R, M, O, D )
|
||||
#define WORD_TIME_SWITCH WORD( T, I, M, E )
|
||||
#define WORD_TIME_MODE WORD( T, M, O, D )
|
||||
#define WORD_BRIGHTNESS WORD( B, R, G, H )
|
||||
#define WORD_TEST WORD( T, E, S, T )
|
||||
#define WORD_ WORD( SEG_G, SEG_G, SEG_G, SEG_G )
|
||||
|
||||
|
||||
#endif
|
||||
124
DisplayHelper.h
124
DisplayHelper.h
@@ -5,6 +5,7 @@
|
||||
#include "Defines.h"
|
||||
#include "Settings.h"
|
||||
#include "Enums.h"
|
||||
#include "DisplayDefines.h"
|
||||
|
||||
class DisplayHelper
|
||||
{
|
||||
@@ -14,73 +15,12 @@ class DisplayHelper
|
||||
public:
|
||||
DisplayHelper() { }
|
||||
|
||||
DisplayHelper(Settings* _Settings)
|
||||
{
|
||||
m_Settings = _Settings;
|
||||
}
|
||||
|
||||
static const uint8_t NONE = 0;
|
||||
static const uint8_t A = SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G;
|
||||
static const uint8_t B = SEG_F | SEG_C | SEG_D | SEG_E | SEG_G;
|
||||
static const uint8_t C = SEG_A | SEG_F | SEG_E | SEG_D;
|
||||
static const uint8_t D = SEG_B | SEG_C | SEG_D | SEG_E | SEG_G;
|
||||
static const uint8_t E = SEG_A | SEG_D | SEG_E | SEG_F | SEG_G;
|
||||
static const uint8_t F = SEG_A | SEG_F | SEG_G | SEG_E;
|
||||
static const uint8_t G = SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G;
|
||||
static const uint8_t H = SEG_F | SEG_C | SEG_E | SEG_G;
|
||||
static const uint8_t I = SEG_B | SEG_C;
|
||||
static const uint8_t J = SEG_B | SEG_C | SEG_D;
|
||||
static const uint8_t K = SEG_A;
|
||||
static const uint8_t L = SEG_F | SEG_E | SEG_D;
|
||||
static const uint8_t M = SEG_C | SEG_G | SEG_E;
|
||||
static const uint8_t N = SEG_C | SEG_G | SEG_E;
|
||||
static const uint8_t O = SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F;
|
||||
static const uint8_t P = SEG_A | SEG_B | SEG_G | SEG_F | SEG_E;
|
||||
static const uint8_t Q = SEG_G;
|
||||
static const uint8_t R = SEG_E | SEG_G;
|
||||
static const uint8_t S = SEG_A | SEG_F | SEG_G | SEG_C | SEG_D;
|
||||
static const uint8_t T = SEG_F | SEG_E | SEG_D | SEG_G;
|
||||
static const uint8_t U = SEG_C | SEG_D | SEG_E;
|
||||
static const uint8_t V = SEG_C | SEG_D | SEG_E | SEG_F | SEG_B;
|
||||
static const uint8_t W = SEG_D;
|
||||
static const uint8_t X = SEG_B | SEG_C | SEG_E | SEG_F | SEG_G;
|
||||
static const uint8_t Y = SEG_F | SEG_G | SEG_B | SEG_C;
|
||||
static const uint8_t Z = SEG_A | SEG_B | SEG_G | SEG_E | SEG_D;
|
||||
|
||||
void setup()
|
||||
{
|
||||
display.setBrightness(0x0f);
|
||||
m_DrawTitle = true;
|
||||
}
|
||||
|
||||
void buttonsTest()
|
||||
{
|
||||
bool btnMainState = digitalRead(PIN_BTN_MAIN);
|
||||
bool btnUpState = digitalRead(PIN_BTN_UP);
|
||||
bool btnDownState = digitalRead(PIN_BTN_DOWN);
|
||||
int numChanged = currentTestNum;
|
||||
|
||||
if(btnMainState == HIGH)
|
||||
{
|
||||
numChanged = 0;
|
||||
}
|
||||
else if (btnUpState == HIGH)
|
||||
{
|
||||
numChanged++;
|
||||
}
|
||||
else if (btnDownState == HIGH)
|
||||
{
|
||||
numChanged--;
|
||||
}
|
||||
|
||||
if(numChanged != currentTestNum)
|
||||
{
|
||||
currentTestNum = numChanged;
|
||||
display.clear();
|
||||
display.showNumberDec(currentTestNum, false);
|
||||
}
|
||||
}
|
||||
|
||||
void runTest()
|
||||
{
|
||||
int k;
|
||||
@@ -188,6 +128,19 @@ public:
|
||||
display.setSegments(WORD_DONE);
|
||||
}
|
||||
|
||||
void UpdateModeDisplay(bool _RunnerActive, DeviceMode _DeviceMode)
|
||||
{
|
||||
switch(_DeviceMode)
|
||||
{
|
||||
case DeviceMode::RUN: DrawRunInfo(_RunnerActive); break;
|
||||
case DeviceMode::SETTINGS_MODE: DrawSettingsRunModeInfo(); break;
|
||||
case DeviceMode::SETTINGS_BRIGHTNESS: DrawSettingsBrightnessInfo(); break;
|
||||
case DeviceMode::SETTINGS_TIME_SWITCH: DrawSettingsTimeSwitchInfo(); break;
|
||||
case DeviceMode::SETTINGS_TIME_MODE: DrawSettingsTimeModeInfo(); break;
|
||||
case DeviceMode::TEST: DrawTestInfo(); break;
|
||||
}
|
||||
}
|
||||
|
||||
void DrawRunInfo(bool _Run)
|
||||
{
|
||||
if(m_DrawTitle)
|
||||
@@ -196,15 +149,15 @@ public:
|
||||
}
|
||||
else if(_Run)
|
||||
{
|
||||
display.setSegments((const uint8_t[]) {P, L, A, Y});
|
||||
display.setSegments(WORD(P, L, A, Y));
|
||||
}
|
||||
else
|
||||
{
|
||||
display.setSegments((const uint8_t[]) {P, A, U, S});
|
||||
display.setSegments(WORD(P, A, U, S));
|
||||
}
|
||||
}
|
||||
|
||||
void DrawSettingsRunModeInfo(RunMode _RunMode)
|
||||
void DrawSettingsRunModeInfo()
|
||||
{
|
||||
if(m_DrawTitle)
|
||||
{
|
||||
@@ -212,17 +165,17 @@ public:
|
||||
}
|
||||
else
|
||||
{
|
||||
switch(_RunMode)
|
||||
switch(Settings::getInstance().RunModeState)
|
||||
{
|
||||
case RunMode::FORWARD: display.setSegments((const uint8_t[]) { F, W, R, D }); break;
|
||||
case RunMode::BACKWARD: display.setSegments((const uint8_t[]) { B, A, C, K }); break;
|
||||
case RunMode::BOTH: display.setSegments((const uint8_t[]) { B, O, T, H }); break;
|
||||
case RunMode::RANDOM: display.setSegments((const uint8_t[]) { R, N, D, NONE }); break;
|
||||
case RunMode::FORWARD: display.setSegments(WORD(F, W, R, D )); break;
|
||||
case RunMode::BACKWARD: display.setSegments(WORD(B, A, C, K )); break;
|
||||
case RunMode::BOTH: display.setSegments(WORD(B, O, T, H )); break;
|
||||
case RunMode::RANDOM: display.setSegments(WORD(R, N, D, NONE)); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DrawSettingsTimeSwitchInfo(int _SwitchTime)
|
||||
void DrawSettingsTimeSwitchInfo()
|
||||
{
|
||||
if(m_DrawTitle)
|
||||
{
|
||||
@@ -230,11 +183,11 @@ public:
|
||||
}
|
||||
else
|
||||
{
|
||||
display.showNumberDec(_SwitchTime, false);
|
||||
display.showNumberDec(Settings::getInstance().getDisplaySwitchTime(), false);
|
||||
}
|
||||
}
|
||||
|
||||
void DrawSettingsTimeModeInfo(TimeMode _TimeMode)
|
||||
void DrawSettingsTimeModeInfo()
|
||||
{
|
||||
if(m_DrawTitle)
|
||||
{
|
||||
@@ -242,16 +195,16 @@ public:
|
||||
}
|
||||
else
|
||||
{
|
||||
switch(_TimeMode)
|
||||
switch(Settings::getInstance().TimeModeState)
|
||||
{
|
||||
case TimeMode::CONST: display.setSegments((const uint8_t[]) { C, N, S, T }); break;
|
||||
case TimeMode::MANUAL: display.setSegments((const uint8_t[]) { M, A, N, L }); break;
|
||||
case TimeMode::DECREASING: display.setSegments((const uint8_t[]) { D, E, C, R }); break;
|
||||
case TimeMode::CONST: display.setSegments(WORD(C, N, S, T )); break;
|
||||
case TimeMode::MANUAL: display.setSegments(WORD(M, A, N, L )); break;
|
||||
case TimeMode::DECREASING: display.setSegments(WORD(D, E, C, R )); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DrawSettingsBrightnessInfo(int _Brightness)
|
||||
void DrawSettingsBrightnessInfo()
|
||||
{
|
||||
if(m_DrawTitle)
|
||||
{
|
||||
@@ -259,7 +212,7 @@ public:
|
||||
}
|
||||
else
|
||||
{
|
||||
display.showNumberDec(_Brightness, false);
|
||||
display.showNumberDec(Settings::getInstance().Brightness, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,22 +233,7 @@ public:
|
||||
|
||||
private:
|
||||
TM1637Display display = TM1637Display::TM1637Display(PIN_DISPLAY_CLK, PIN_DISPLAY_DIO);
|
||||
Settings* m_Settings;
|
||||
int currentTestNum = 0;
|
||||
|
||||
//seg A = upper ground
|
||||
//seg B = upper right
|
||||
//seg C = lower right
|
||||
|
||||
uint8_t WORD_DONE[4] = { D, O, N, E };
|
||||
uint8_t WORD_RUN[4] = { R, U, N, NONE };
|
||||
uint8_t WORD_MODE[4] = { R, M, O, D };
|
||||
uint8_t WORD_TIME_SWITCH[4] = { T, I, M, E };
|
||||
uint8_t WORD_TIME_MODE[4] = { T, M, O, D };
|
||||
uint8_t WORD_BRIGHTNESS[4] = { B, R, G, H };
|
||||
uint8_t WORD_TEST[4] = { T, E, S, T };
|
||||
uint8_t WORD_[4] = { SEG_G, SEG_G, SEG_G, SEG_G };
|
||||
|
||||
bool m_DrawTitle = true;
|
||||
};
|
||||
|
||||
|
||||
145
EyeTrainerMain.h
145
EyeTrainerMain.h
@@ -4,6 +4,7 @@
|
||||
#include "LEDRunner.h"
|
||||
#include "InputHelper.h"
|
||||
#include "Enums.h"
|
||||
#include "WrapHelper.h"
|
||||
|
||||
class EyeTrainerMain
|
||||
{
|
||||
@@ -25,21 +26,23 @@ public:
|
||||
|
||||
if(inputEvent)
|
||||
{
|
||||
ProcessChangeMode();
|
||||
int upDownButtonsShortClickState = m_InputHelper.GetUpDownButtonsShortClicked();
|
||||
int upDownButtonsHoldState = m_InputHelper.GetUpDownButtonsHold();
|
||||
switch(m_deviceMode)
|
||||
{
|
||||
case DeviceMode::RUN: ProcessRun(); break;
|
||||
case DeviceMode::SETTINGS_MODE: ProcessSettingsRunMode(); break;
|
||||
case DeviceMode::SETTINGS_TIME_SWITCH: ProcessSwitchTime(); break;
|
||||
case DeviceMode::SETTINGS_BRIGHTNESS: ProcessBrightness(); break;
|
||||
case DeviceMode::SETTINGS_TIME_MODE: ProcessTimeMode(); break;
|
||||
case DeviceMode::TEST: ProcessTest(); break;
|
||||
case DeviceMode::RUN: ProcessRun(upDownButtonsShortClickState, upDownButtonsHoldState); break;
|
||||
case DeviceMode::SETTINGS_MODE: ProcessSettingsRunMode(upDownButtonsShortClickState, upDownButtonsHoldState); break;
|
||||
case DeviceMode::SETTINGS_TIME_SWITCH: ProcessSwitchTime(upDownButtonsShortClickState, upDownButtonsHoldState); break;
|
||||
case DeviceMode::SETTINGS_BRIGHTNESS: ProcessBrightness(upDownButtonsShortClickState, upDownButtonsHoldState); break;
|
||||
case DeviceMode::SETTINGS_TIME_MODE: ProcessTimeMode(upDownButtonsShortClickState, upDownButtonsHoldState); break;
|
||||
case DeviceMode::TEST: ProcessTest(upDownButtonsShortClickState, upDownButtonsHoldState); break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long deltaTime = millis() - m_PrevTime;
|
||||
m_PrevTime = millis();
|
||||
|
||||
m_LEDRunner.update(deltaTime, m_Settings.SwitchTime, m_Settings.RunModeState, m_Settings.TimeModeState);
|
||||
unsigned long currentTime = millis();
|
||||
unsigned long deltaTime = currentTime - m_PrevTime;
|
||||
m_PrevTime = currentTime;
|
||||
m_LEDRunner.update(deltaTime);
|
||||
}
|
||||
|
||||
void runTest()
|
||||
@@ -49,45 +52,16 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
static const uint8_t BTN_UP_SHORT_CLICKED = 1;
|
||||
static const uint8_t BTN_DOWN_SHORT_CLICKED = -1;
|
||||
static const uint8_t BTN_DOWN_UP_NO_SHORT_CLICKED = 0;
|
||||
|
||||
//==============================================================
|
||||
//state
|
||||
//==============================================================
|
||||
|
||||
enum DeviceMode m_deviceMode = DeviceMode::RUN;
|
||||
|
||||
Settings m_Settings = Settings();
|
||||
LEDRunner m_LEDRunner = LEDRunner(&m_Settings);
|
||||
DisplayHelper m_DisplayHelper = DisplayHelper(&m_Settings);
|
||||
LEDRunner m_LEDRunner = LEDRunner();
|
||||
DisplayHelper m_DisplayHelper = DisplayHelper();
|
||||
InputHelper m_InputHelper = InputHelper();
|
||||
unsigned long m_PrevTime = 0;
|
||||
|
||||
//==============================================================
|
||||
//end of state
|
||||
//==============================================================
|
||||
|
||||
int wrapInt(int _Num, int _Max)
|
||||
void UpdateModeDisplay()
|
||||
{
|
||||
if(_Num < 0)
|
||||
{
|
||||
return (_Max + _Num % _Max);
|
||||
}
|
||||
if(_Num >= _Max)
|
||||
{
|
||||
return _Num % _Max;
|
||||
}
|
||||
|
||||
return _Num;
|
||||
}
|
||||
|
||||
int wrapInt(int _Num, int _Min, int _Max)
|
||||
{
|
||||
int num = _Num - _Min;
|
||||
int max = _Max - _Min;
|
||||
return wrapInt(num, max) + _Min;
|
||||
m_DisplayHelper.UpdateModeDisplay(m_LEDRunner.getRun(), m_deviceMode);
|
||||
}
|
||||
|
||||
void ProcessChangeMode()
|
||||
@@ -105,44 +79,11 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateModeDisplay()
|
||||
void ProcessRun(int upDownButtons, int upDownButtonsHold)
|
||||
{
|
||||
switch(m_deviceMode)
|
||||
if(Settings::getInstance().TimeModeState == TimeMode::MANUAL && upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED)
|
||||
{
|
||||
case DeviceMode::RUN: m_DisplayHelper.DrawRunInfo(m_LEDRunner.getRun()); break;
|
||||
case DeviceMode::SETTINGS_MODE: m_DisplayHelper.DrawSettingsRunModeInfo(m_Settings.RunModeState); break;
|
||||
case DeviceMode::SETTINGS_BRIGHTNESS: m_DisplayHelper.DrawSettingsBrightnessInfo(m_Settings.Brightness); break;
|
||||
case DeviceMode::SETTINGS_TIME_SWITCH: m_DisplayHelper.DrawSettingsTimeSwitchInfo(m_Settings.SwitchTime / 10); break;
|
||||
case DeviceMode::SETTINGS_TIME_MODE: m_DisplayHelper.DrawSettingsTimeModeInfo(m_Settings.TimeModeState); break;
|
||||
case DeviceMode::TEST: m_DisplayHelper.DrawTestInfo(); break;
|
||||
}
|
||||
}
|
||||
|
||||
int GetUpDownButtonsShortClicked()
|
||||
{
|
||||
bool upButtonShortClicked = m_InputHelper.getButtonUp().IsNowShortClicked();
|
||||
bool downButtonShortClicked = m_InputHelper.getButtonDown().IsNowShortClicked();
|
||||
if(upButtonShortClicked)
|
||||
return BTN_UP_SHORT_CLICKED;
|
||||
else if(downButtonShortClicked)
|
||||
return BTN_DOWN_SHORT_CLICKED;
|
||||
return BTN_DOWN_UP_NO_SHORT_CLICKED;
|
||||
}
|
||||
|
||||
int GetUpDownButtonsHold()
|
||||
{
|
||||
bool upButtonHold = m_InputHelper.getButtonUp().IsHolded();
|
||||
bool downButtonHold = m_InputHelper.getButtonDown().IsHolded();
|
||||
return (upButtonHold - downButtonHold);
|
||||
}
|
||||
|
||||
void ProcessRun()
|
||||
{
|
||||
ProcessChangeMode();
|
||||
int upDownButtons = GetUpDownButtonsShortClicked();
|
||||
if(m_Settings.TimeModeState == TimeMode::MANUAL && upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED)
|
||||
{
|
||||
m_LEDRunner.manualUpdate(m_Settings.RunModeState, upDownButtons);
|
||||
m_LEDRunner.manualUpdate(Settings::getInstance().RunModeState, upDownButtons);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -161,79 +102,65 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessSettingsRunMode()
|
||||
void ProcessSettingsRunMode(int upDownButtons, int upDownButtonsHold)
|
||||
{
|
||||
ProcessChangeMode();
|
||||
int upDownButtons = GetUpDownButtonsShortClicked();
|
||||
if(upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED)
|
||||
{
|
||||
m_DisplayHelper.setDrawTitle(false);
|
||||
m_Settings.switchRunMode(upDownButtons);
|
||||
Settings::getInstance().switchRunMode(upDownButtons);
|
||||
UpdateModeDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessSwitchTime()
|
||||
void ProcessSwitchTime(int upDownButtons, int upDownButtonsHold)
|
||||
{
|
||||
ProcessChangeMode();
|
||||
|
||||
int upDownButtons = GetUpDownButtonsShortClicked();
|
||||
int upDownHoldButtons = GetUpDownButtonsHold();
|
||||
if(upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED)
|
||||
{
|
||||
m_DisplayHelper.setDrawTitle(false);
|
||||
m_Settings.setSwitchTime(m_Settings.SwitchTime + upDownButtons * 50);
|
||||
Settings::getInstance().setSwitchTime(Settings::getInstance().SwitchTime + upDownButtons * 50);
|
||||
UpdateModeDisplay();
|
||||
}
|
||||
if(upDownHoldButtons != 0)
|
||||
if(upDownButtonsHold != 0)
|
||||
{
|
||||
m_DisplayHelper.setDrawTitle(false);
|
||||
m_Settings.setSwitchTime(m_Settings.SwitchTime + upDownHoldButtons * 50);
|
||||
Settings::getInstance().setSwitchTime(Settings::getInstance().SwitchTime + upDownButtonsHold * 50);
|
||||
UpdateModeDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessBrightness()
|
||||
void ProcessBrightness(int upDownButtons, int upDownButtonsHold)
|
||||
{
|
||||
ProcessChangeMode();
|
||||
|
||||
int upDownButtons = GetUpDownButtonsShortClicked();
|
||||
int upDownHoldButtons = GetUpDownButtonsHold();
|
||||
if(upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED)
|
||||
{
|
||||
m_LEDRunner.setAllHigh();
|
||||
m_DisplayHelper.setDrawTitle(false);
|
||||
m_Settings.setBrightness(m_Settings.Brightness + upDownButtons);
|
||||
m_LEDRunner.updateBrightness(m_Settings.Brightness);
|
||||
Settings::getInstance().setBrightness(Settings::getInstance().Brightness + upDownButtons);
|
||||
m_LEDRunner.updateBrightness(Settings::getInstance().Brightness);
|
||||
UpdateModeDisplay();
|
||||
}
|
||||
|
||||
if(upDownHoldButtons != 0)
|
||||
if(upDownButtonsHold != 0)
|
||||
{
|
||||
m_LEDRunner.setAllHigh();
|
||||
m_DisplayHelper.setDrawTitle(false);
|
||||
m_Settings.setBrightness(m_Settings.Brightness + upDownHoldButtons);
|
||||
m_LEDRunner.updateBrightness(m_Settings.Brightness);
|
||||
Settings::getInstance().setBrightness(Settings::getInstance().Brightness + upDownButtonsHold);
|
||||
m_LEDRunner.updateBrightness(Settings::getInstance().Brightness);
|
||||
UpdateModeDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessTimeMode()
|
||||
void ProcessTimeMode(int upDownButtons, int upDownButtonsHold)
|
||||
{
|
||||
ProcessChangeMode();
|
||||
int upDownButtons = GetUpDownButtonsShortClicked();
|
||||
if(upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED)
|
||||
{
|
||||
m_DisplayHelper.setDrawTitle(false);
|
||||
m_Settings.switchTimeMode(upDownButtons);
|
||||
Settings::getInstance().switchTimeMode(upDownButtons);
|
||||
UpdateModeDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessTest()
|
||||
void ProcessTest(int upDownButtons, int upDownButtonsHold)
|
||||
{
|
||||
ProcessChangeMode();
|
||||
int upDownButtons = GetUpDownButtonsShortClicked();
|
||||
if(upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED)
|
||||
{
|
||||
runTest();
|
||||
|
||||
@@ -42,11 +42,29 @@ public:
|
||||
return m_DownButton;
|
||||
}
|
||||
|
||||
int GetUpDownButtonsShortClicked()
|
||||
{
|
||||
bool upButtonShortClicked = m_UpButton.IsNowShortClicked();
|
||||
bool downButtonShortClicked = m_DownButton.IsNowShortClicked();
|
||||
if(upButtonShortClicked)
|
||||
return BTN_UP_SHORT_CLICKED;
|
||||
else if(downButtonShortClicked)
|
||||
return BTN_DOWN_SHORT_CLICKED;
|
||||
return BTN_DOWN_UP_NO_SHORT_CLICKED;
|
||||
}
|
||||
|
||||
int GetUpDownButtonsHold()
|
||||
{
|
||||
bool upButtonHold = m_UpButton.IsHolded();
|
||||
bool downButtonHold = m_DownButton.IsHolded();
|
||||
return (upButtonHold - downButtonHold);
|
||||
}
|
||||
|
||||
private:
|
||||
bool m_InputEvent;
|
||||
|
||||
SimpleButton m_MainButton = SimpleButton(PIN_BTN_MAIN, LONG_CLICK_TIME_MS);
|
||||
SimpleButton m_UpButton = SimpleButton(PIN_BTN_UP, LONG_CLICK_TIME_MS);
|
||||
SimpleButton m_UpButton = SimpleButton(PIN_BTN_UP, LONG_CLICK_TIME_MS);
|
||||
SimpleButton m_DownButton = SimpleButton(PIN_BTN_DOWN, LONG_CLICK_TIME_MS);
|
||||
};
|
||||
|
||||
|
||||
132
LEDRunner.h
132
LEDRunner.h
@@ -10,14 +10,12 @@
|
||||
#include "Defines.h"
|
||||
#include <ShiftRegister74HC595.h>
|
||||
#include "Settings.h"
|
||||
#include "WrapHelper.h"
|
||||
|
||||
class LEDRunner
|
||||
{
|
||||
public:
|
||||
LEDRunner(Settings* _Settings)
|
||||
{
|
||||
this->m_Settings = _Settings;
|
||||
}
|
||||
LEDRunner() { }
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -44,23 +42,23 @@ public:
|
||||
void manualUpdate(RunMode _RunMode, int _NextLEDIndexIncrement)
|
||||
{
|
||||
nextLED(_RunMode, _NextLEDIndexIncrement);
|
||||
sr.setAllLow();
|
||||
sr.set(m_CurrentLED, HIGH);
|
||||
m_LEDs.setAllLow();
|
||||
m_LEDs.set(m_CurrentLED, HIGH);
|
||||
}
|
||||
|
||||
void update(unsigned long _DeltaTime, int _SwitchTime, RunMode _RunMode, TimeMode _TimeMode)
|
||||
void update(unsigned long _DeltaTime)
|
||||
{
|
||||
if(!m_Run || _TimeMode == TimeMode::MANUAL)
|
||||
if(!m_Run || Settings::getInstance().TimeModeState == TimeMode::MANUAL)
|
||||
return;
|
||||
|
||||
m_CurrentTime += _DeltaTime;
|
||||
|
||||
if(m_CurrentTime > _SwitchTime)
|
||||
if(m_CurrentTime > Settings::getInstance().SwitchTime)
|
||||
{
|
||||
m_CurrentTime = 0;
|
||||
nextLED(_RunMode, 1);
|
||||
sr.setAllLow();
|
||||
sr.set(m_CurrentLED, HIGH);
|
||||
nextLED(Settings::getInstance().RunModeState, 1);
|
||||
m_LEDs.setAllLow();
|
||||
m_LEDs.set(m_CurrentLED, HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,10 +79,6 @@ public:
|
||||
|
||||
void runTest()
|
||||
{
|
||||
// // setting all pins at the same time to either HIGH or LOW
|
||||
// sr.setAllHigh(); // set all pins HIGH
|
||||
// delay(500);
|
||||
|
||||
analogWrite(PIN_LED_GND_BUS, 120);
|
||||
|
||||
for(int i = 0; i < 4; i++)
|
||||
@@ -95,13 +89,13 @@ public:
|
||||
delay(250); // wait for a second
|
||||
}
|
||||
|
||||
sr.setAllHigh();
|
||||
m_LEDs.setAllHigh();
|
||||
delay(500);
|
||||
sr.setAllLow();
|
||||
m_LEDs.setAllLow();
|
||||
delay(500);
|
||||
sr.setAllHigh();
|
||||
m_LEDs.setAllHigh();
|
||||
delay(500);
|
||||
sr.setAllLow();
|
||||
m_LEDs.setAllLow();
|
||||
delay(500);
|
||||
|
||||
for(int i = 0; i < 4; i++)
|
||||
@@ -117,29 +111,29 @@ public:
|
||||
// setting single pins
|
||||
for (int i = 0; i < MAX_LED_INDEX; i++)
|
||||
{
|
||||
sr.set(i, HIGH);
|
||||
m_LEDs.set(i, HIGH);
|
||||
delay(25);
|
||||
sr.set(i, LOW);
|
||||
m_LEDs.set(i, LOW);
|
||||
}
|
||||
// setting single pins
|
||||
// setting single pins
|
||||
for (int i = MAX_LED_INDEX; i > 0; i--)
|
||||
{
|
||||
sr.set(i, HIGH);
|
||||
m_LEDs.set(i, HIGH);
|
||||
delay(25);
|
||||
sr.set(i, LOW);
|
||||
m_LEDs.set(i, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
sr.set(0, HIGH);
|
||||
m_LEDs.set(0, HIGH);
|
||||
delay(50);
|
||||
|
||||
for (int i = 0; i < MAX_LED_INDEX / 2; i++)
|
||||
{
|
||||
sr.set(i, HIGH);
|
||||
sr.set(MAX_LED_INDEX - i, HIGH);
|
||||
m_LEDs.set(i, HIGH);
|
||||
m_LEDs.set(MAX_LED_INDEX - i, HIGH);
|
||||
delay(150);
|
||||
sr.set(i, LOW);
|
||||
sr.set(MAX_LED_INDEX - i, LOW);
|
||||
m_LEDs.set(i, LOW);
|
||||
m_LEDs.set(MAX_LED_INDEX - i, LOW);
|
||||
}
|
||||
|
||||
|
||||
@@ -147,15 +141,15 @@ public:
|
||||
{
|
||||
for (int i = MAX_LED_INDEX / 2; i >= 0; i--)
|
||||
{
|
||||
sr.set(i, HIGH);
|
||||
sr.set(MAX_LED_INDEX - i, HIGH);
|
||||
m_LEDs.set(i, HIGH);
|
||||
m_LEDs.set(MAX_LED_INDEX - i, HIGH);
|
||||
delay(50);
|
||||
sr.set(i, LOW);
|
||||
sr.set(MAX_LED_INDEX - i, LOW);
|
||||
m_LEDs.set(i, LOW);
|
||||
m_LEDs.set(MAX_LED_INDEX - i, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
sr.setAllHigh(); // set all pins HIGH
|
||||
m_LEDs.setAllHigh(); // set all pins HIGH
|
||||
|
||||
for(int i = 10; i > 0; i--)
|
||||
{
|
||||
@@ -168,81 +162,39 @@ public:
|
||||
|
||||
void blink(const uint8_t _LedIndex, unsigned long _Time)
|
||||
{
|
||||
sr.setAllLow();
|
||||
sr.set(_LedIndex, HIGH);
|
||||
m_LEDs.setAllLow();
|
||||
m_LEDs.set(_LedIndex, HIGH);
|
||||
delay(_Time);
|
||||
sr.setAllLow();
|
||||
m_LEDs.setAllLow();
|
||||
}
|
||||
|
||||
void setAndClearOthers(const uint8_t _LedIndex, const uint8_t _Value)
|
||||
{
|
||||
sr.setAllLow();
|
||||
sr.set(_LedIndex, _Value);
|
||||
m_LEDs.setAllLow();
|
||||
m_LEDs.set(_LedIndex, _Value);
|
||||
}
|
||||
|
||||
void set(const uint8_t _LedIndex, const uint8_t _Value)
|
||||
{
|
||||
sr.set(_LedIndex, _Value);
|
||||
m_LEDs.set(_LedIndex, _Value);
|
||||
}
|
||||
|
||||
void setAllHigh()
|
||||
{
|
||||
sr.setAllHigh();
|
||||
m_LEDs.setAllHigh();
|
||||
}
|
||||
|
||||
void setAllLow()
|
||||
{
|
||||
sr.setAllLow();
|
||||
m_LEDs.setAllLow();
|
||||
}
|
||||
|
||||
private:
|
||||
Settings* m_Settings;
|
||||
bool m_Run = false;
|
||||
unsigned long m_CurrentTime = 0;
|
||||
int m_CurrentLED = 0;
|
||||
int m_CurrentLEDPingPong = 0;
|
||||
ShiftRegister74HC595<REGISTER_SIZE> sr = ShiftRegister74HC595<REGISTER_SIZE>::ShiftRegister74HC595(PIN_LED_DATA, PIN_LED_CLOCK, PIN_LED_LATCH);
|
||||
|
||||
int wrapInt(int _Num, int _Max)
|
||||
{
|
||||
if(_Num < 0)
|
||||
{
|
||||
return (_Max + _Num % _Max);
|
||||
}
|
||||
if(_Num >= _Max)
|
||||
{
|
||||
return _Num % _Max;
|
||||
}
|
||||
|
||||
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;
|
||||
int max = _Max - _Min;
|
||||
return wrapInt(num, max) + _Min;
|
||||
}
|
||||
bool m_Run = false;
|
||||
unsigned long m_CurrentTime = 0;
|
||||
int m_CurrentLED = 0;
|
||||
int m_CurrentLEDPingPong = 0;
|
||||
ShiftRegister74HC595<REGISTER_SIZE> m_LEDs = ShiftRegister74HC595<REGISTER_SIZE>::ShiftRegister74HC595(PIN_LED_DATA, PIN_LED_CLOCK, PIN_LED_LATCH);
|
||||
};
|
||||
|
||||
#endif
|
||||
21
Settings.h
21
Settings.h
@@ -12,10 +12,24 @@ public:
|
||||
int SwitchTime = 3000; //In milliseconds
|
||||
float Brightness = MAX_BRIGHTNESS / 2;
|
||||
|
||||
Settings()
|
||||
//=======================================
|
||||
//Singleton. AVOID COPY ASSIGNMENTS
|
||||
//=======================================
|
||||
|
||||
static Settings& getInstance()
|
||||
{
|
||||
static Settings instance; // Guaranteed to be destroyed. Instantiated on first use.
|
||||
return instance;
|
||||
}
|
||||
|
||||
Settings() {}
|
||||
Settings(Settings const&) = delete;
|
||||
void operator = (Settings const&) = delete;
|
||||
|
||||
//=======================================
|
||||
//Singleton. AVOID COPY ASSIGNMENTS
|
||||
//=======================================
|
||||
|
||||
void setSwitchTime(int _SwitchTime)
|
||||
{
|
||||
if(_SwitchTime > 15000)
|
||||
@@ -67,6 +81,11 @@ public:
|
||||
TimeModeState = static_cast<TimeMode>(wrapInt(static_cast<int>(TimeModeState) + _ModeIncrement, 3));
|
||||
}
|
||||
|
||||
int getDisplaySwitchTime()
|
||||
{
|
||||
return SwitchTime / 10;
|
||||
}
|
||||
|
||||
private:
|
||||
int wrapInt(int _Num, int _Max)
|
||||
{
|
||||
|
||||
44
WrapHelper.h
Normal file
44
WrapHelper.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef EYE_TRAINER_WRAPHELPER
|
||||
#define EYE_TRAINER_WRAPHELPER 0
|
||||
|
||||
int wrapInt(int _Num, int _Max)
|
||||
{
|
||||
if(_Num < 0)
|
||||
{
|
||||
return (_Max + _Num % _Max);
|
||||
}
|
||||
if(_Num >= _Max)
|
||||
{
|
||||
return _Num % _Max;
|
||||
}
|
||||
|
||||
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;
|
||||
int max = _Max - _Min;
|
||||
return wrapInt(num, max) + _Min;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user