From cb0c02bc8f1daa4c334500bdfb7cca30fb49af57 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 18 Sep 2021 16:17:27 +0200 Subject: [PATCH] Lock screen pin now configurable --- components/cpputils | 2 +- components/espcpputils | 2 +- esp-idf | 2 +- main/CMakeLists.txt | 1 + main/accessors/settingsaccessors.h | 4 ++ main/bluetoothtexthelpers.h | 9 ++- main/buttons.h | 5 ++ main/displays/lockscreen.h | 17 ++++- .../menus/boardcomputerhardwaresettingsmenu.h | 19 ++--- main/displays/menus/lockscreensettingsmenu.h | 72 +++++++++++++++++++ main/displays/starfielddisplay.h | 8 +-- main/main.cpp | 1 + main/presets.h | 6 ++ main/settings.h | 8 +++ main/settingspersister.h | 25 +++++++ main/texts.h | 10 +++ 16 files changed, 171 insertions(+), 20 deletions(-) create mode 100644 main/displays/menus/lockscreensettingsmenu.h diff --git a/components/cpputils b/components/cpputils index 3cfb45a..4368243 160000 --- a/components/cpputils +++ b/components/cpputils @@ -1 +1 @@ -Subproject commit 3cfb45a8ecba4e8920c4d22ac3cab742edc0e664 +Subproject commit 43682439fe86041f635c9c70e52bbf0b8085641c diff --git a/components/espcpputils b/components/espcpputils index 1e0af13..6274c10 160000 --- a/components/espcpputils +++ b/components/espcpputils @@ -1 +1 @@ -Subproject commit 1e0af13a5246e823fc4ac2db464fe5620ac2ca8b +Subproject commit 6274c103b48e0e9130b0518047750c143ff0a346 diff --git a/esp-idf b/esp-idf index 9e9abdf..0e76163 160000 --- a/esp-idf +++ b/esp-idf @@ -1 +1 @@ -Subproject commit 9e9abdf483b982210f04b869ea104ff56dd77827 +Subproject commit 0e761638599b19d8de894940c638408b68d344a9 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index b02c297..2015d47 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -71,6 +71,7 @@ set(headers displays/menus/boardcomputerhardwaresettingsmenu.h displays/menus/dynamicdebugmenu.h displays/menus/limitssettingsmenu.h + displays/menus/lockscreensettingsmenu.h displays/menus/mainmenu.h displays/menus/motorfeedbackdebugmenu.h displays/menus/wifiscanmenu.h diff --git a/main/accessors/settingsaccessors.h b/main/accessors/settingsaccessors.h index ae77ab9..bc59c07 100644 --- a/main/accessors/settingsaccessors.h +++ b/main/accessors/settingsaccessors.h @@ -151,4 +151,8 @@ struct CenterOffsetAccessor : public RefAccessorSaveSettings { int16_t struct SmallOffsetAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.ledstrip.smallOffset; } }; struct BigOffsetAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.ledstrip.bigOffset; } }; #endif + +struct LockscreenAllowPresetSwitchAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.lockscreen.allowPresetSwitch; } }; +template +struct LockscreenPinDigitAccessor : public RefAccessorSaveSettings { int8_t &getRef() const override { return settings.lockscreen.pin[index]; } }; } diff --git a/main/bluetoothtexthelpers.h b/main/bluetoothtexthelpers.h index 5616fce..c88f8a8 100644 --- a/main/bluetoothtexthelpers.h +++ b/main/bluetoothtexthelpers.h @@ -1,5 +1,8 @@ #pragma once +// 3rdparty lib includes +#include + // local includes #include "textinterface.h" #include "globals.h" @@ -20,21 +23,21 @@ using BluetoothHasClientText = BluetoothStatusTextHelper; struct BluetoothConnectedText : public virtual TextInterface { public: - std::string text() const override { return std::string{"connected: "} + to_string(bluetoothSerial.connected()); } + std::string text() const override { return fmt::format("{}: {}", TEXT_BLUETOOTHCONNECTED, to_string(bluetoothSerial.connected())); } }; //constexpr char TEXT_BLUETOOTHISREADY[] = "Is ready: "; //using BluetoothIsReadyText = BluetoothStatusTextHelper; struct BluetoothIsReadyText : public virtual TextInterface { public: - std::string text() const override { return std::string{"isReady: "} + to_string(bluetoothSerial.isReady()); } + std::string text() const override { return fmt::format("{}: {}", TEXT_BLUETOOTHISREADY, to_string(bluetoothSerial.isReady())); } }; //constexpr char TEXT_BLUETOOTHISREADYMASTER[] = "Is ready (M): "; //using BluetoothIsReadyMasterText = BluetoothStatusTextHelper; class BluetoothIsReadyMasterText : public virtual TextInterface { public: - std::string text() const override { return std::string{"isReady (M): "} + to_string(bluetoothSerial.isReady(true)); } + std::string text() const override { return fmt::format("{}: {}", TEXT_BLUETOOTHISREADYMASTER, to_string(bluetoothSerial.isReady(true))); } }; #endif } diff --git a/main/buttons.h b/main/buttons.h index b9a6b0a..c951a2a 100644 --- a/main/buttons.h +++ b/main/buttons.h @@ -19,6 +19,8 @@ bool confirmButtonLongPressed{}; bool backButtonPressed{}; bool backButtonLongPressed{}; +bool profileButtonDisabled{}; + std::optional upPressedSince; int upPressRepeat{}; std::optional downPressedSince; @@ -126,6 +128,9 @@ public: if (!pressed) return; + if (profileButtonDisabled) + return; + switchProfile(index); } }; diff --git a/main/displays/lockscreen.h b/main/displays/lockscreen.h index d4ce8cc..5f9e4f8 100644 --- a/main/displays/lockscreen.h +++ b/main/displays/lockscreen.h @@ -1,13 +1,16 @@ #pragma once +// system includes #include +// local includes #include "display.h" #include "widgets/label.h" #include "globals.h" #include "utils.h" #include "texts.h" #include "modes/ignoreinputmode.h" +#include "buttons.h" namespace { class MainMenu; @@ -25,7 +28,7 @@ class Lockscreen : public Display, public DummyBack public: void start() override; void initScreen() override; - void update() override {} + void update() override; void redraw() override; void stop() override; @@ -62,6 +65,8 @@ void Lockscreen::start() m_oldMode = currentMode; currentMode = &m_mode; + + profileButtonDisabled = !settings.lockscreen.allowPresetSwitch; } void Lockscreen::initScreen() @@ -95,6 +100,12 @@ void Lockscreen::initScreen() m_labels[0].redraw(std::to_string(m_numbers[0])); } +void Lockscreen::update() +{ + // just in case someone changes that settings somehow + profileButtonDisabled = !settings.lockscreen.allowPresetSwitch; +} + void Lockscreen::redraw() { if (m_pressed) @@ -104,7 +115,7 @@ void Lockscreen::redraw() if (++m_currentIndex>=4) { - if (m_numbers == decltype(m_numbers){1,2,3,4}) + if (m_numbers == settings.lockscreen.pin) { switchScreen(); return; @@ -150,6 +161,8 @@ void Lockscreen::stop() currentMode = m_oldMode; } + + profileButtonDisabled = false; } void Lockscreen::confirm() diff --git a/main/displays/menus/boardcomputerhardwaresettingsmenu.h b/main/displays/menus/boardcomputerhardwaresettingsmenu.h index e38e613..29d0e85 100644 --- a/main/displays/menus/boardcomputerhardwaresettingsmenu.h +++ b/main/displays/menus/boardcomputerhardwaresettingsmenu.h @@ -10,6 +10,7 @@ #include "actions/dummyaction.h" #include "actions/switchscreenaction.h" #include "actions/toggleboolaction.h" +#include "icons/lock.h" #include "icons/back.h" #include "checkboxicon.h" #include "texts.h" @@ -18,6 +19,7 @@ // forward declares namespace { class BoardcomputerHardwareSettingsMenu; +class LockscreenSettingsMenu; class CalibrateDisplay; class GametrakCalibrateDisplay; class TimersMenu; @@ -31,20 +33,20 @@ struct GasText : public virtual TextInterface { public: std::string text() const override { - return std::string{"gas: "} + - (raw_gas ? std::to_string(*raw_gas) : "?") + - ": " + - (gas ? fmt::format("{:.02f}", *gas) : "?"); + return fmt::format("{}: {}: {}", + "gas", + raw_gas ? std::to_string(*raw_gas) : "?", + gas ? fmt::format("{:.02f}", *gas) : "?"); } }; struct BremsText : public virtual TextInterface { public: std::string text() const override { - return std::string{"brems: "} + - (raw_brems ? std::to_string(*raw_brems) : "?") + - ": " + - (brems ? fmt::format("{:.02f}", *brems) : "?"); + return fmt::format("{}: {}: {}", + "brems", + raw_brems ? std::to_string(*raw_brems) : "?", + brems ? fmt::format("{:.02f}", *brems) : "?"); } }; @@ -160,6 +162,7 @@ class BoardcomputerHardwareSettingsMenu : public: BoardcomputerHardwareSettingsMenu() { + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&icons::lock>>>(); constructMenuItem, DummyAction>>(); constructMenuItem, DummyAction>>(); constructMenuItem, SwitchScreenAction>>(); diff --git a/main/displays/menus/lockscreensettingsmenu.h b/main/displays/menus/lockscreensettingsmenu.h new file mode 100644 index 0000000..4429605 --- /dev/null +++ b/main/displays/menus/lockscreensettingsmenu.h @@ -0,0 +1,72 @@ +#pragma once + +// local includes +#include "menudisplay.h" +#include "menuitem.h" +#include "actions/toggleboolaction.h" +#include "actions/switchscreenaction.h" +#include "texts.h" +#include "icons/back.h" +#include "checkboxicon.h" +#include "globals.h" +#include "accessors/settingsaccessors.h" +#include "changevaluedisplay.h" + +// forward declares +namespace { +class BoardcomputerHardwareSettingsMenu; +class LockscreenSettingsMenu; +} // namespace + +using namespace espgui; + +namespace { +using LockscreenPinDigit0ChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + LockscreenPinDigitAccessor<0>, + BackActionInterface>, + SwitchScreenAction +>; + +using LockscreenPinDigit1ChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + LockscreenPinDigitAccessor<1>, + BackActionInterface>, + SwitchScreenAction +>; + +using LockscreenPinDigit2ChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + LockscreenPinDigitAccessor<2>, + BackActionInterface>, + SwitchScreenAction +>; + +using LockscreenPinDigit3ChangeScreen = makeComponent< + ChangeValueDisplay, + StaticText, + LockscreenPinDigitAccessor<3>, + BackActionInterface>, + SwitchScreenAction +>; + +class LockscreenSettingsMenu : + public MenuDisplay, + public StaticText, + public BackActionInterface> +{ +public: + LockscreenSettingsMenu() + { + constructMenuItem, ToggleBoolAction, CheckboxIcon, LockscreenAllowPresetSwitchAccessor>>(); + constructMenuItem>, SwitchScreenAction>>(); + constructMenuItem>, SwitchScreenAction>>(); + constructMenuItem>, SwitchScreenAction>>(); + constructMenuItem>, SwitchScreenAction>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); + } +}; +} // namespace diff --git a/main/displays/starfielddisplay.h b/main/displays/starfielddisplay.h index b8dfd18..328b80e 100644 --- a/main/displays/starfielddisplay.h +++ b/main/displays/starfielddisplay.h @@ -40,10 +40,10 @@ private: }; StarfieldDisplay::StarfieldDisplay() : - za(cpputils::randomNumber(256, espcpputils::esp_random_device{})), - zb(cpputils::randomNumber(256, espcpputils::esp_random_device{})), - zc(cpputils::randomNumber(256, espcpputils::esp_random_device{})), - zx(cpputils::randomNumber(256, espcpputils::esp_random_device{})) + za(cpputils::randomNumber(espcpputils::esp_random_device{})), + zb(cpputils::randomNumber(espcpputils::esp_random_device{})), + zc(cpputils::randomNumber(espcpputils::esp_random_device{})), + zx(cpputils::randomNumber(espcpputils::esp_random_device{})) { } diff --git a/main/main.cpp b/main/main.cpp index 64c61ad..2c382bb 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -56,6 +56,7 @@ using namespace std::chrono_literals; #include "displays/menus/ledstripmenu.h" #endif #include "displays/menus/limitssettingsmenu.h" +#include "displays/menus/lockscreensettingsmenu.h" #include "displays/menus/mainmenu.h" #include "displays/menus/tempomatmodesettingsmenu.h" #include "displays/menus/modessettingsmenu.h" diff --git a/main/presets.h b/main/presets.h index 713f6b7..c962f26 100644 --- a/main/presets.h +++ b/main/presets.h @@ -224,6 +224,11 @@ constexpr Settings::Ledstrip defaultLedstrip { }; #endif +constexpr Settings::LockscreenSettings defaultLockscreen { + .allowPresetSwitch = true, + .pin = { 1, 2, 3, 4 } +}; + constexpr Settings defaultSettings { #ifdef FEATURE_BMS .autoConnectBms = false, @@ -249,6 +254,7 @@ constexpr Settings defaultSettings { #ifdef FEATURE_LEDSTRIP .ledstrip = defaultLedstrip, #endif + .lockscreen = defaultLockscreen }; StringSettings makeDefaultStringSettings() diff --git a/main/settings.h b/main/settings.h index 77122d5..aef1fe2 100644 --- a/main/settings.h +++ b/main/settings.h @@ -158,6 +158,11 @@ struct Settings } ledstrip; #endif + struct LockscreenSettings { + bool allowPresetSwitch; + std::array pin; + } lockscreen; + template void executeForEveryCommonSetting(T &&callable); @@ -248,6 +253,9 @@ void Settings::executeForEveryCommonSetting(T &&callable) callable("smallOffset", ledstrip.smallOffset); callable("bigOffset", ledstrip.bigOffset); #endif + + callable("lockAlwPresetSw", lockscreen.allowPresetSwitch); + callable("lockscreenPin", lockscreen.pin); } template diff --git a/main/settingspersister.h b/main/settingspersister.h index 686fc33..6dcd314 100644 --- a/main/settingspersister.h +++ b/main/settingspersister.h @@ -17,6 +17,7 @@ #include #include #include +#include // local includes #include "settings.h" @@ -276,6 +277,22 @@ template<> struct nvsGetterHelper { static esp_err_t nvs_get(n return err; }}; #endif +template<> struct nvsGetterHelper> { static esp_err_t nvs_get(nvs_handle handle, const char* key, std::array* out_value) +{ + uint32_t tempValue; + esp_err_t err = nvs_get_u32(handle, key, &tempValue); + if (err == ESP_OK) + *out_value = std::bit_cast>(tempValue); + return err; +}}; +template<> struct nvsGetterHelper> { static esp_err_t nvs_get(nvs_handle handle, const char* key, std::array* out_value) +{ + uint32_t tempValue; + esp_err_t err = nvs_get_u32(handle, key, &tempValue); + if (err == ESP_OK) + *out_value = std::bit_cast>(tempValue); + return err; +}}; template bool SettingsPersister::load(T &settings) @@ -385,6 +402,14 @@ template<> struct nvsSetterHelper { static esp_err_t nvs_set(n return nvs_set_u8(handle, key, uint8_t(value)); }}; #endif +template<> struct nvsSetterHelper> { static esp_err_t nvs_set(nvs_handle handle, const char* key, std::array value) +{ + return nvs_set_u32(handle, key, std::bit_cast(value)); +}}; +template<> struct nvsSetterHelper> { static esp_err_t nvs_set(nvs_handle handle, const char* key, std::array value) +{ + return nvs_set_u32(handle, key, std::bit_cast(value)); +}}; template bool SettingsPersister::save(T &settings) diff --git a/main/texts.h b/main/texts.h index 3c7e4a6..ca25734 100644 --- a/main/texts.h +++ b/main/texts.h @@ -243,6 +243,15 @@ constexpr char TEXT_SMALLOFFSET[] = "Small Offset"; constexpr char TEXT_BIGOFFSET[] = "Big Offset"; //constexpr char TEXT_BACK[] = "Back"; +//LockscreenSettingsMenu +constexpr char TEXT_LOCKSCREENSETTINGS[] = "Lockscreen Settings"; +constexpr char TEXT_ALLOWPRESETSWITCH[] = "Allow preset switch"; +constexpr char TEXT_PINDIGIT0[] = "PIN digit0"; +constexpr char TEXT_PINDIGIT1[] = "PIN digit1"; +constexpr char TEXT_PINDIGIT2[] = "PIN digit2"; +constexpr char TEXT_PINDIGIT3[] = "PIN digit3"; +//constexpr char TEXT_BACK[] = "Back"; + //ModesSettingsMenu //constexpr char TEXT_MODESSETTINGS[] = "Modes settings"; constexpr char TEXT_DEFAULTMODESETTIGNS[] = "Default mode settings"; @@ -271,6 +280,7 @@ constexpr char TEXT_NCRUISEMOTTGT[] = "nCruiseMotTgt"; //BoardcomputerHardwareSettingsMenu //constexpr char TEXT_BOARDCOMPUTERHARDWARESETTINGS[] = "Boardcomputer H/W settings"; +//constexpr char TEXT_LOCKSCREENSETTINGS[] = "Lockscreen Settings"; constexpr char TEXT_CALIBRATE[] = "Calibrate"; constexpr char TEXT_SAMPLECOUNT[] = "sampleCount"; constexpr char TEXT_GASMIN[] = "gasMin";