From cdc154528adb6ee98f0cfe44bc8c2488439386f9 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 19 Sep 2020 23:56:24 +0200 Subject: [PATCH] Implemented profiles menu --- platformio.ini | 10 +- src/actions/switchprofileaction.h | 24 +++ .../menus/boardcomputerhardwaresettingsmenu.h | 4 +- src/displays/menus/mainmenu.h | 2 + src/displays/menus/profilesmenu.h | 27 +++ src/dpad5wire.h | 187 ++++++++++++++++++ src/main.cpp | 10 + src/presets.h | 2 +- src/screens.h | 7 +- src/settings.h | 4 +- src/settingsaccessors.h | 2 +- src/texts.h | 9 + 12 files changed, 276 insertions(+), 12 deletions(-) create mode 100644 src/actions/switchprofileaction.h create mode 100644 src/displays/menus/profilesmenu.h create mode 100644 src/dpad5wire.h diff --git a/platformio.ini b/platformio.ini index 7affcc7..b86297a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -128,10 +128,12 @@ build_flags = -DDEVICE_PREFIX=bobbyquad -DAP_PASSWORD=Passwort_123 -DFEATURE_WEBSERVER -; -DFEATURE_DPAD_3WIRESW -; -DPINS_DPAD_3WIRESW_OUT=0 -; -DPINS_DPAD_3WIRESW_IN1=16 -; -DPINS_DPAD_3WIRESW_IN2=27 + -DFEATURE_DPAD_5WIRESW + -DPINS_DPAD_5WIRESW_OUT=0 + -DPINS_DPAD_5WIRESW_IN1=16 + -DPINS_DPAD_5WIRESW_IN2=27 + -DPINS_DPAD_5WIRESW_IN3=16 + -DPINS_DPAD_5WIRESW_IN4=27 -DDEFAULT_GASMIN=850 -DDEFAULT_GASMAX=3700 -DDEFAULT_BREMSMIN=1300 diff --git a/src/actions/switchprofileaction.h b/src/actions/switchprofileaction.h new file mode 100644 index 0000000..84137d5 --- /dev/null +++ b/src/actions/switchprofileaction.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "actioninterface.h" +#include "globals.h" + +namespace { +template +class SwitchProfileAction : public virtual ActionInterface +{ +public: + void triggered() override + { + if (settingsPersister.openProfile(profile)) + { + if (!settingsPersister.load(settings)) + Serial.println("SwitchProfileAction::triggered() load failed"); + } + else + Serial.println("SwitchProfileAction::triggered() openProfile failed"); + } +}; +} diff --git a/src/displays/menus/boardcomputerhardwaresettingsmenu.h b/src/displays/menus/boardcomputerhardwaresettingsmenu.h index f52394e..26b7d41 100644 --- a/src/displays/menus/boardcomputerhardwaresettingsmenu.h +++ b/src/displays/menus/boardcomputerhardwaresettingsmenu.h @@ -66,7 +66,7 @@ using BremsMaxChangeScreen = makeComponent< SwitchScreenAction >; -#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) +#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) || defined(FEATURE_DPAD_5WIRESW) using DPadDebounceChangeScreen = makeComponent< ChangeValueDisplay, StaticText, @@ -147,7 +147,7 @@ class BoardcomputerHardwareSettingsMenu : makeComponent, SwitchScreenAction>, makeComponent, SwitchScreenAction>, makeComponent, SwitchScreenAction>, -#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) +#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) || defined(FEATURE_DPAD_5WIRESW) makeComponent, SwitchScreenAction>, #endif #ifdef FEATURE_GAMETRAK diff --git a/src/displays/menus/mainmenu.h b/src/displays/menus/mainmenu.h index e1374ef..aee6a1f 100644 --- a/src/displays/menus/mainmenu.h +++ b/src/displays/menus/mainmenu.h @@ -21,6 +21,7 @@ namespace { class StatusDisplay; class SelectModeMenu; +class ProfilesMenu; class PresetsMenu; class GraphsMenu; class BmsMenu; @@ -42,6 +43,7 @@ class MainMenu : makeComponent, SwitchScreenAction, StaticMenuItemIcon<&icons::modes>>, makeComponent, ModeSettingsAction>, makeComponent, SwitchScreenAction, StaticMenuItemIcon<&icons::presets>>, + makeComponent, SwitchScreenAction>, makeComponent, SwitchScreenAction, StaticMenuItemIcon<&icons::graph>>, #ifdef FEATURE_BMS makeComponent, SwitchScreenAction, StaticMenuItemIcon<&icons::bms>>, diff --git a/src/displays/menus/profilesmenu.h b/src/displays/menus/profilesmenu.h new file mode 100644 index 0000000..56a3a84 --- /dev/null +++ b/src/displays/menus/profilesmenu.h @@ -0,0 +1,27 @@ +#pragma once + +#include "menudisplay.h" +#include "staticmenudefinition.h" +#include "actions/switchprofileaction.h" +#include "actions/switchscreenaction.h" +#include "icons/back.h" +#include "texts.h" + +namespace { +class MainMenu; +} + +namespace { +class ProfilesMenu : + public MenuDisplay, + public StaticText, + public BackActionInterface>, + public StaticMenuDefinition< + makeComponent, SwitchProfileAction<0>>, + makeComponent, SwitchProfileAction<1>>, + makeComponent, SwitchProfileAction<2>>, + makeComponent, SwitchProfileAction<3>>, + makeComponent, SwitchScreenAction, StaticMenuItemIcon<&icons::back>> + > +{}; +} diff --git a/src/dpad5wire.h b/src/dpad5wire.h new file mode 100644 index 0000000..d0ca12a --- /dev/null +++ b/src/dpad5wire.h @@ -0,0 +1,187 @@ +#pragma once + +#include + +#include +#include + +#include "globals.h" +#include "types.h" + +namespace { +namespace dpad5wire +{ +using State = std::tuple; + +template +class Helper +{ +public: + static constexpr auto OutPin = OUT; + static constexpr auto In1Pin = IN1; + static constexpr auto In2Pin = IN2; + static constexpr auto In3Pin = IN3; + static constexpr auto In4Pin = IN4; + + void begin(); + + State read(); +}; + +template +void Helper::begin() +{ + pinMode(OUT, OUTPUT); +} + +template +State Helper::read() +{ + digitalWrite(OUT, LOW); + + pinMode(IN1, INPUT_PULLUP); + pinMode(IN2, INPUT_PULLUP); + pinMode(IN3, INPUT_PULLUP); + pinMode(IN4, INPUT_PULLUP); + + delay(1); + + const bool result0 = digitalRead(IN1)==LOW; + const bool result1 = digitalRead(IN2)==LOW; + const bool result2 = digitalRead(IN3)==LOW; + const bool result3 = digitalRead(IN4)==LOW; + + digitalWrite(OUT, HIGH); + + pinMode(IN1, INPUT_PULLUP); + pinMode(IN2, INPUT_PULLUP); + pinMode(IN3, INPUT_PULLUP); + pinMode(IN4, INPUT_PULLUP); + + delay(1); + + const bool result4 = digitalRead(IN1); + const bool result5 = digitalRead(IN2); + const bool result6 = digitalRead(IN3); + const bool result7 = digitalRead(IN4); + + return std::make_tuple(result0, result1, result2, result3, result4, result5, result6, result7); +} + +#ifdef FEATURE_DPAD_5WIRESW +Helper helper; +State lastState; +millis_t debounceUp, debounceDown, debounceConfirm, debounceBack, debounceProfile0, debounceProfile1, debounceProfile2, debounceProfile3; + +void init() +{ + helper.begin(); + debounceUp = debounceDown = debounceConfirm = debounceBack = debounceProfile0 = debounceProfile1 = debounceProfile2 = debounceProfile3 = millis(); +} + +void update() +{ + const auto state = helper.read(); + const auto now = millis(); + + enum { + ButtonUp = 3, + ButtonDown = 0, + ButtonConfirm = 1, + ButtonBack = 2, + ButtonProfile0 = 4, + ButtonProfile1 = 5, + ButtonProfile2 = 6, + ButtonProfile3 = 7, + }; + + if (std::get(lastState) != std::get(state) && now-debounceUp > settings.boardcomputerHardware.dpadDebounce) + { + if (std::get(state)) + InputDispatcher::rotate(-1); + std::get(lastState) = std::get(state); + debounceUp = now; + } + if (std::get(lastState) != std::get(state) && now-debounceDown > settings.boardcomputerHardware.dpadDebounce) + { + if (std::get(state)) + InputDispatcher::rotate(1); + std::get(lastState) = std::get(state); + debounceDown = now; + } + if (std::get(lastState) != std::get(state) && now-debounceConfirm > settings.boardcomputerHardware.dpadDebounce) + { + InputDispatcher::confirmButton(std::get(state)); + std::get(lastState) = std::get(state); + debounceConfirm = now; + } + if (std::get(lastState) != std::get(state) && now-debounceBack > settings.boardcomputerHardware.dpadDebounce) + { + InputDispatcher::backButton(std::get(state)); + std::get(lastState) = std::get(state); + debounceBack = now; + } + if (std::get(lastState) != std::get(state) && now-debounceProfile0 > settings.boardcomputerHardware.dpadDebounce) + { + if (std::get(state)) + { + if (settingsPersister.openProfile(0)) + { + if (!settingsPersister.load(settings)) + Serial.println("dpad5wire::update() load 0 failed"); + } + else + Serial.println("dpad5wire::update() openProfile 0 failed"); + } + std::get(lastState) = std::get(state); + debounceProfile0 = now; + } + if (std::get(lastState) != std::get(state) && now-debounceProfile1 > settings.boardcomputerHardware.dpadDebounce) + { + if (std::get(state)) + { + if (settingsPersister.openProfile(1)) + { + if (!settingsPersister.load(settings)) + Serial.println("dpad5wire::update() load 1 failed"); + } + else + Serial.println("dpad5wire::update() openProfile 1 failed"); + } + std::get(lastState) = std::get(state); + debounceProfile1 = now; + } + if (std::get(lastState) != std::get(state) && now-debounceProfile2 > settings.boardcomputerHardware.dpadDebounce) + { + if (std::get(state)) + { + if (settingsPersister.openProfile(2)) + { + if (!settingsPersister.load(settings)) + Serial.println("dpad5wire::update() load 2 failed"); + } + else + Serial.println("dpad5wire::update() openProfile 2 failed"); + } + std::get(lastState) = std::get(state); + debounceProfile2 = now; + } + if (std::get(lastState) != std::get(state) && now-debounceProfile3 > settings.boardcomputerHardware.dpadDebounce) + { + if (std::get(state)) + { + if (settingsPersister.openProfile(3)) + { + if (!settingsPersister.load(settings)) + Serial.println("dpad5wire::update() load 3 failed"); + } + else + Serial.println("dpad5wire::update() openProfile 3 failed"); + } + std::get(lastState) = std::get(state); + debounceProfile3 = now; + } +} +#endif +} +} diff --git a/src/main.cpp b/src/main.cpp index 2404f8a..22684b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ #include "screens.h" #include "dpad.h" #include "dpad3wire.h" +#include "dpad5wire.h" #include "rotary.h" #include "serialhandler.h" #include "ota.h" @@ -57,6 +58,11 @@ void setup() dpad3wire::init(); #endif +#ifdef FEATURE_DPAD_5WIRESW + bootLabel.redraw("dpad5wire"); + dpad5wire::init(); +#endif + #ifdef FEATURE_ROTARY bootLabel.redraw("rotary"); initRotary(); @@ -179,6 +185,10 @@ void loop() dpad3wire::update(); #endif +#ifdef FEATURE_DPAD_5WIRESW + dpad5wire::update(); +#endif + if (!lastPotiRead || now - lastPotiRead >= 1000/settings.boardcomputerHardware.timersSettings.potiReadRate) { readPotis(); diff --git a/src/presets.h b/src/presets.h index 5308026..e4b4e98 100644 --- a/src/presets.h +++ b/src/presets.h @@ -90,7 +90,7 @@ constexpr Settings::BoardcomputerHardware defaultBoardcomputerHardware { .gasMax = DEFAULT_GASMAX, .bremsMin = DEFAULT_BREMSMIN, .bremsMax = DEFAULT_BREMSMAX, -#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) +#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) || defined(FEATURE_DPAD_5WIRESW) .dpadDebounce = 25, #endif #ifdef FEATURE_GAMETRAK diff --git a/src/screens.h b/src/screens.h index e0508e6..2569496 100644 --- a/src/screens.h +++ b/src/screens.h @@ -27,6 +27,7 @@ #include "displays/menus/mosfetsmenu.h" #include "displays/menus/motorfeedbackdebugmenu.h" #include "displays/menus/motorstatedebugmenu.h" +#include "displays/menus/profilesmenu.h" #include "displays/menus/presetsmenu.h" #include "displays/menus/boardcomputerhardwaresettingsmenu.h" #include "displays/menus/selectmodemenu.h" @@ -100,6 +101,7 @@ union X { BackLeftMotorFeedbackDebugMenu backLeftMotorFeedbackDebugMenu; BackRightMotorFeedbackDebugMenu backRightMotorFeedbackDebugMenu; BoardcomputerHardwareSettingsMenu boardcomputerHardwareSettingsMenu; + ProfilesMenu profilesMenu; PresetsMenu presetsMenu; SelectModeMenu selectModeMenu; SettingsMenu settingsMenu; @@ -172,7 +174,7 @@ union X { GasMaxChangeScreen changeGasMax; BremsMinChangeScreen changeBremsMin; BremsMaxChangeScreen changeBremsMax; -#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) +#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) || defined(FEATURE_DPAD_5WIRESW) DPadDebounceChangeScreen dPadDebounceChangeScreen; #endif #ifdef FEATURE_GAMETRAK @@ -254,6 +256,7 @@ template<> decltype(displays.frontLeftMotorFeedbackDebugMenu) & template<> decltype(displays.frontRightMotorFeedbackDebugMenu) &getRefByType() { return displays.frontRightMotorFeedbackDebugMenu; } template<> decltype(displays.backLeftMotorFeedbackDebugMenu) &getRefByType() { return displays.backLeftMotorFeedbackDebugMenu; } template<> decltype(displays.backRightMotorFeedbackDebugMenu) &getRefByType() { return displays.backRightMotorFeedbackDebugMenu; } +template<> decltype(displays.profilesMenu) &getRefByType() { return displays.profilesMenu; } template<> decltype(displays.presetsMenu) &getRefByType() { return displays.presetsMenu; } template<> decltype(displays.selectModeMenu) &getRefByType() { return displays.selectModeMenu; } template<> decltype(displays.settingsMenu) &getRefByType() { return displays.settingsMenu; } @@ -327,7 +330,7 @@ template<> decltype(displays.changeGasMin) & template<> decltype(displays.changeGasMax) &getRefByType() { return displays.changeGasMax; } template<> decltype(displays.changeBremsMin) &getRefByType() { return displays.changeBremsMin; } template<> decltype(displays.changeBremsMax) &getRefByType() { return displays.changeBremsMax; } -#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) +#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) || defined(FEATURE_DPAD_5WIRESW) template<> decltype(displays.dPadDebounceChangeScreen) &getRefByType() { return displays.dPadDebounceChangeScreen; } #endif #ifdef FEATURE_GAMETRAK diff --git a/src/settings.h b/src/settings.h index 447b1f7..27a9385 100644 --- a/src/settings.h +++ b/src/settings.h @@ -53,7 +53,7 @@ struct Settings struct BoardcomputerHardware { int16_t sampleCount; int16_t gasMin, gasMax, bremsMin, bremsMax; -#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) +#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) || defined(FEATURE_DPAD_5WIRESW) uint8_t dpadDebounce; #endif #ifdef FEATURE_GAMETRAK @@ -141,7 +141,7 @@ void Settings::executeForEverySetting(T &&callable) callable("gasMax", boardcomputerHardware.gasMax); callable("bremsMin", boardcomputerHardware.bremsMin); callable("bremsMax", boardcomputerHardware.bremsMax); -#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) +#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) || defined(FEATURE_DPAD_5WIRESW) callable("dpadDebounce", boardcomputerHardware.dpadDebounce); #endif #ifdef FEATURE_GAMETRAK diff --git a/src/settingsaccessors.h b/src/settingsaccessors.h index e130259..08d5794 100644 --- a/src/settingsaccessors.h +++ b/src/settingsaccessors.h @@ -64,7 +64,7 @@ struct GasMinAccessor : public RefAccessorSaveSettings { int16_t &getRe struct GasMaxAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.gasMax; } }; struct BremsMinAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.bremsMin; } }; struct BremsMaxAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.bremsMax; } }; -#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) +#if defined(FEATURE_DPAD) || defined(FEATURE_DPAD_3WIRESW) || defined(FEATURE_DPAD_5WIRESW) struct DPadDebounceAccessor : public RefAccessorSaveSettings { uint8_t &getRef() const override { return settings.boardcomputerHardware.dpadDebounce; } }; #endif #ifdef FEATURE_GAMETRAK diff --git a/src/texts.h b/src/texts.h index 54a68af..3d8fd8e 100644 --- a/src/texts.h +++ b/src/texts.h @@ -57,6 +57,7 @@ constexpr char TEXT_STATUS[] = "Status"; constexpr char TEXT_SELECTMODE[] = "Select mode"; constexpr char TEXT_MODESETTINGS[] = "Mode settings"; constexpr char TEXT_PRESETS[] = "Presets"; +constexpr char TEXT_PROFILES[] = "Profiles"; constexpr char TEXT_GRAPHS[] = "Graphs"; //constexpr char TEXT_BMS[] = "BMS"; constexpr char TEXT_SETTINGS[] = "Settings"; @@ -255,6 +256,14 @@ constexpr char TEXT_SWAPSCREENBYTES[] = "Swap screen bytes"; constexpr char TEXT_TIMERS[] = "Timers"; //constexpr char TEXT_BACK[] = "Back"; +//ProfilesMenu +//constexpr char TEXT_PROFILES[] = "Profiles"; +constexpr char TEXT_PROFILE0[] = "Profile 0"; +constexpr char TEXT_PROFILE1[] = "Profile 1"; +constexpr char TEXT_PROFILE2[] = "Profile 2"; +constexpr char TEXT_PROFILE3[] = "Profile 3"; +//constexpr char TEXT_BACK[] = "Back"; + //PresetsMenu //constexpr char TEXT_PRESETS[] = "Presets"; constexpr char TEXT_DEFAULTEVERYTHING[] = "Default everything";