From 4d7d67e7b6b49299e798730ef427456ffca6563b Mon Sep 17 00:00:00 2001 From: Author glitchrain Date: Sat, 14 Dec 2024 07:15:04 -0800 Subject: [PATCH] Setings are now controlled by buttons with displayed info. --- DisplayHelper.h | 139 ++++++++++++++++++++++++++++++---- Enums.h | 15 ++-- EyeTrainerMain.h | 189 ++++++++++++++++++++++++++++++++++++++++++++++- LEDRunner.h | 41 +++++++++- Settings.h | 94 ++++++++++++++++++++++- 5 files changed, 450 insertions(+), 28 deletions(-) diff --git a/DisplayHelper.h b/DisplayHelper.h index dc3ad59..5bf5615 100644 --- a/DisplayHelper.h +++ b/DisplayHelper.h @@ -4,6 +4,7 @@ #include #include "Defines.h" #include "Settings.h" +#include "Enums.h" class DisplayHelper { @@ -18,16 +19,39 @@ public: 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); } - loop() - { - - } - void buttonsTest() { bool btnMainState = digitalRead(PIN_BTN_MAIN); @@ -160,24 +184,109 @@ public: } // Done! - display.setSegments(SEG_DONE); + display.setSegments(WORD_DONE); } + void DrawRunInfo(bool _Title, bool _Run) + { + if(_Title) + { + display.setSegments(WORD_RUN); + } + else if(_Run) + { + uint8_t forward[4] = {P, L, A, NONE}; + display.setSegments(forward); + } + else + { + uint8_t forward[4] = {P, A, U, S}; + display.setSegments(forward); + } + } + + void DrawSettingsRunModeInfo(bool _Title, RunMode _RunMode) + { + if(_Title) + { + display.setSegments(WORD_MODE); + } + else + { + switch(_RunMode) + { + 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; + } + } + } + + void DrawSettingsTimeInfo(bool _Title, int _SwitchTime) + { + if(_Title) + { + display.setSegments(WORD_TIME); + } + else + { + display.showNumberDec(_SwitchTime, false); + } + } + + void DrawSettingsBrightnessInfo(bool _Title, int _Brightness) + { + if(_Title) + { + display.setSegments(WORD_BRIGHTNESS); + } + else + { + display.showNumberDec(_Brightness, false); + } + } + + void DrawTestInfo(bool _Title) + { + display.setSegments(WORD_TEST); + } + + //DOES NOT WORK! + // uint8_t* encodeNumber(uint8_t _Num) + // { + // uint8_t data[] = { 0xff, 0xff, 0xff, 0xff }; + // // data[3] = display.encodeDigit(_Num % 10); + // // _Num = _Num / 10; + // // data[2] = display.encodeDigit(_Num % 10); + // // _Num = _Num / 10; + // // data[1] = display.encodeDigit(_Num % 10); + // // _Num = _Num / 10; + // // data[0] = display.encodeDigit(_Num % 10); + + // data[3] = display.encodeDigit(1); + // data[2] = display.encodeDigit(2); + // data[1] = display.encodeDigit(3); + // data[0] = display.encodeDigit(4); + // return data; + // } + private: TM1637Display display = TM1637Display::TM1637Display(PIN_DISPLAY_CLK, PIN_DISPLAY_DIO); Settings* m_Settings; int currentTestNum = 0; - // const uint8_t SEG_DONE[] = { - uint8_t SEG_DONE[4] = { - SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d - SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O - SEG_C | SEG_E | SEG_G, // n - SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E - }; - - + //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] = { M, O, D, E }; + uint8_t WORD_TIME[4] = { T, I, M, E }; + 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 }; }; #endif \ No newline at end of file diff --git a/Enums.h b/Enums.h index 010aca1..efeca35 100644 --- a/Enums.h +++ b/Enums.h @@ -3,25 +3,24 @@ enum DeviceMode { - IDLE, + RUN = 0, SETTINGS_MODE, SETTINGS_BRIGHTNESS, SETTINGS_TIME, - RUN, + TEST }; enum RunMode { - FORWARD, - BACKWARD, - BOTH, - RANDOM, - TEST + FORWARD = 0, + BACKWARD = 1, + BOTH = 2, + RANDOM = 3 }; enum TimeMode { - CONST, + CONST = 0, MANUAL, INCREASING }; diff --git a/EyeTrainerMain.h b/EyeTrainerMain.h index 4b18d25..2f3b4e9 100644 --- a/EyeTrainerMain.h +++ b/EyeTrainerMain.h @@ -3,6 +3,7 @@ #include "Settings.h" #include "LEDRunner.h" #include "InputHelper.h" +#include "Enums.h" class EyeTrainerMain { @@ -11,12 +12,14 @@ public: //state //============================================================== - DeviceMode m_deviceMode; + enum DeviceMode m_deviceMode = DeviceMode::RUN; Settings m_Settings = Settings(); LEDRunner m_LEDRunner = LEDRunner(&m_Settings); DisplayHelper m_DisplayHelper = DisplayHelper(&m_Settings); InputHelper m_InputHelper = InputHelper(); + bool m_DrawTitle = true; + unsigned long m_PrevTime = 0; //============================================================== //end of state @@ -30,12 +33,34 @@ public: m_LEDRunner.setup(); m_DisplayHelper.setup(); m_InputHelper.setup(); + UpdateModeDisplay(); } void loop() { bool inputEvent = m_InputHelper.readInputs(millis()); - + + switch(m_deviceMode) + { + case DeviceMode::RUN: ProcessRun(); break; + case DeviceMode::SETTINGS_MODE: ProcessSettingsRunMode(); break; + case DeviceMode::SETTINGS_TIME: ProcessSwitchTime(); break; + case DeviceMode::SETTINGS_BRIGHTNESS: ProcessBrightness(); break; + case DeviceMode::TEST: ProcessTest(); break; + } + + unsigned long deltaTime = millis() - m_PrevTime; + m_PrevTime = millis(); + + // m_LEDRunner.update(deltaTime); + + switch(m_Settings.m_RunMode) + { + case RunMode::FORWARD: m_LEDRunner.set(0, HIGH); break; + case RunMode::BACKWARD: m_LEDRunner.set(1, HIGH); break; + case RunMode::BOTH: m_LEDRunner.set(2, HIGH); break; + case RunMode::RANDOM: m_LEDRunner.set(3, HIGH); break; + } } void runTest() @@ -46,4 +71,164 @@ 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; + + int wrapInt(int _Num, int _Max) + { + 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; + } + + void ProcessChangeMode() + { + bool mainButtonShortClicked = m_InputHelper.getButtonMain().IsNowShortClicked(); + if(!mainButtonShortClicked) + return; + + m_DrawTitle = true; + m_deviceMode = static_cast(wrapInt(static_cast(m_deviceMode) + 1, 5)); + UpdateModeDisplay(); + } + + void UpdateModeDisplay() + { + switch(m_deviceMode) + { + case DeviceMode::RUN: m_DisplayHelper.DrawRunInfo(m_DrawTitle, m_LEDRunner.getRun()); break; + case DeviceMode::SETTINGS_MODE: m_DisplayHelper.DrawSettingsRunModeInfo(m_DrawTitle, m_Settings.m_RunMode); break; + case DeviceMode::SETTINGS_TIME: m_DisplayHelper.DrawSettingsTimeInfo(m_DrawTitle, m_Settings.SwitchTime / 10); break; + case DeviceMode::SETTINGS_BRIGHTNESS: m_DisplayHelper.DrawSettingsBrightnessInfo(m_DrawTitle, m_Settings.Brightness); break; + case DeviceMode::TEST: m_DisplayHelper.DrawTestInfo(m_DrawTitle); break; + } + } + + int GetUpDownButtonsShortClicked() + { + bool upButtonShortClicked = m_InputHelper.getButtonUp().IsNowShortClicked(); + bool downButtonShortClicked = m_InputHelper.getButtonUp().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(upDownButtons == BTN_UP_SHORT_CLICKED) + { + m_LEDRunner.setRun(true); + UpdateModeDisplay(); + } + if(upDownButtons == BTN_DOWN_SHORT_CLICKED) + { + m_LEDRunner.setRun(false); + UpdateModeDisplay(); + } + } + + void ProcessSettingsRunMode() + { + ProcessChangeMode(); + int upDownButtons = GetUpDownButtonsShortClicked(); + if(upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED) + { + m_DrawTitle = false; + m_Settings.switchRunMode(upDownButtons); + UpdateModeDisplay(); + } + } + + void ProcessSwitchTime() + { + ProcessChangeMode(); + + int upDownButtons = GetUpDownButtonsShortClicked(); + int upDownHoldButtons = GetUpDownButtonsHold(); + if(upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED) + { + m_LEDRunner.set(0, HIGH); + m_DrawTitle = false; + m_Settings.setSwitchTime(m_Settings.SwitchTime + upDownButtons * 50); + // m_Settings.setSwitchTime(m_Settings.SwitchTime); + UpdateModeDisplay(); + } + else + { + m_LEDRunner.set(0, LOW); + } + if(upDownHoldButtons != 0) + { + m_LEDRunner.set(1, HIGH); + m_DrawTitle = false; + m_Settings.setSwitchTime(m_Settings.SwitchTime + upDownHoldButtons * 50); + // m_Settings.setSwitchTime(m_Settings.SwitchTime); + UpdateModeDisplay(); + } + else + { + m_LEDRunner.set(1, LOW); + } + } + + void ProcessBrightness() + { + ProcessChangeMode(); + + int upDownButtons = GetUpDownButtonsShortClicked(); + int upDownHoldButtons = GetUpDownButtonsHold(); + if(upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED) + { + m_LEDRunner.setAllHigh(); + m_DrawTitle = false; + m_Settings.setBrightness(m_Settings.Brightness + upDownButtons); + m_LEDRunner.updateBrightness(m_Settings.Brightness); + UpdateModeDisplay(); + } + + if(upDownHoldButtons != 0) + { + m_LEDRunner.setAllHigh(); + m_DrawTitle = false; + m_Settings.setBrightness(m_Settings.Brightness + upDownHoldButtons); + m_LEDRunner.updateBrightness(m_Settings.Brightness); + UpdateModeDisplay(); + } + } + + void ProcessTest() + { + ProcessChangeMode(); + int upDownButtons = GetUpDownButtonsShortClicked(); + if(upDownButtons != BTN_DOWN_UP_NO_SHORT_CLICKED) + { + runTest(); + } + } }; \ No newline at end of file diff --git a/LEDRunner.h b/LEDRunner.h index 7846738..dd577f7 100644 --- a/LEDRunner.h +++ b/LEDRunner.h @@ -28,9 +28,36 @@ public: pinMode(PIN_LED_LATCH, OUTPUT); } - void loop() + void update(unsigned long _DeltaTime) { + if(!m_Run) + return; + m_CurrentTime += _DeltaTime; + + if((m_CurrentTime / 1000) % 2 != 0) + { + sr.setAllHigh(); + } + else + { + sr.setAllLow(); + } + } + + void setRun(bool _Run) + { + m_Run = _Run; + } + + void updateBrightness(float _Brightness) + { + analogWrite(PIN_LED_GND_BUS, (MAX_BRIGHTNESS - _Brightness) / MAX_BRIGHTNESS * 255); + } + + bool getRun() + { + return m_Run; } void runTest() @@ -139,8 +166,20 @@ public: sr.set(_LedIndex, _Value); } + void setAllHigh() + { + sr.setAllHigh(); + } + + void setAllLow() + { + sr.setAllLow(); + } + private: Settings* m_Settings; + bool m_Run = false; + unsigned long m_CurrentTime = 0; ShiftRegister74HC595 sr = ShiftRegister74HC595::ShiftRegister74HC595(PIN_LED_DATA, PIN_LED_CLOCK, PIN_LED_LATCH); }; diff --git a/Settings.h b/Settings.h index 3df79cf..21eddb3 100644 --- a/Settings.h +++ b/Settings.h @@ -9,12 +9,102 @@ class Settings public: enum RunMode m_RunMode = RunMode::RANDOM; enum TimeMode m_TimeMode = TimeMode::CONST; - float SwitchTime = 3; //In seconds - bool Changed = true; + int SwitchTime = 3000; //In milliseconds + // float Brightness = MAX_BRIGHTNESS; + float Brightness = 195; Settings() { } + + void setSwitchTime(int _SwitchTime) + { + if(_SwitchTime > 15000) + { + SwitchTime = 15000; + return; + } + if(_SwitchTime < 250) + { + SwitchTime = 250; + return; + } + + SwitchTime = _SwitchTime; + } + + void setBrightness(float _Brightness) + { + if(_Brightness > MAX_BRIGHTNESS) + { + Brightness = MAX_BRIGHTNESS; + return; + } + if(_Brightness < MIN_BRIGHTNESS) + { + Brightness = MIN_BRIGHTNESS; + return; + } + Brightness = _Brightness; + } + + void resetRunMode(RunMode _RunMode) + { + m_RunMode = RunMode::FORWARD; + } + + void switchRunMode(int _NextOrBack) + { + if(m_RunMode == RunMode::FORWARD) + { + m_RunMode = RunMode::BACKWARD; + } + else if(m_RunMode == RunMode::BACKWARD) + { + m_RunMode = RunMode::BOTH; + } + else if(m_RunMode == RunMode::BOTH) + { + m_RunMode = RunMode::RANDOM; + } + else if(m_RunMode == RunMode::RANDOM) + { + m_RunMode = RunMode::FORWARD; + } + + // RunMode newRunMode = static_cast(wrapInt(static_cast(m_RunMode) + _NextOrBack, 4)); + // m_RunMode = newRunMode; + } + + void setTimeMode(enum TimeMode _TimeMode) + { + m_TimeMode = _TimeMode; + } + + + +private: + int wrapInt(int _Num, int _Max) + { + 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; + } + }; #endif \ No newline at end of file