From 6d20b29c38532cd7ba023214778f10f031f8e20f Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 19 Sep 2020 22:46:49 +0200 Subject: [PATCH] Motor inverted presets are now part of platformio.ini --- platformio.ini | 15 +++++++ src/actions/erasenvsaction.h | 41 ++++++++++++++++++ src/displays/menus/debugmenu.h | 2 + src/displays/statusdisplay.h | 4 ++ src/main.cpp | 4 +- src/presets.h | 26 ++++++------ src/screens.h | 9 ++-- src/settingspersister.h | 76 ++++++++++++++++++++++++---------- src/texts.h | 1 + src/widgets/label.h | 10 ++--- 10 files changed, 143 insertions(+), 45 deletions(-) create mode 100644 src/actions/erasenvsaction.h diff --git a/platformio.ini b/platformio.ini index 42288ec..7affcc7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -43,6 +43,13 @@ build_flags = -DDEFAULT_FIELDWEAKMAX=5 -DDEFAULT_FIELDADVMAX=40 +[default_wheels_inverted] +build_flags = +-DDEFAULT_INVERTFRONTLEFT=false +-DDEFAULT_INVERTFRONTRIGHT=true +-DDEFAULT_INVERTBACKLEFT=false +-DDEFAULT_INVERTBACKRIGHT=true + [peters_platine_common] build_flags = -DILI9341_DRIVER=1 @@ -108,6 +115,10 @@ build_flags = -DPINS_TX1=5 -DPINS_RX2=22 -DPINS_TX2=23 + -DDEFAULT_INVERTFRONTLEFT=false + -DDEFAULT_INVERTFRONTRIGHT=true + -DDEFAULT_INVERTBACKLEFT=true + -DDEFAULT_INVERTBACKRIGHT=false ; -DFEATURE_MOSFETS ; -DPINS_MOSFET0=18 ; -DPINS_MOSFET1=19 @@ -178,6 +189,7 @@ build_flags = ${peters_platine.build_flags} ${ota_common.build_flags} ${default_limits.build_flags} + ${default_wheels_inverted.build_flags} -DDEVICE_PREFIX=bobbycar -DAP_PASSWORD=Passwort_123 -DFEATURE_WEBSERVER @@ -236,6 +248,7 @@ build_flags = ${peters_platine.build_flags} ${ota_common.build_flags} ${default_limits.build_flags} + ${default_wheels_inverted.build_flags} -DDEVICE_PREFIX=bobbyquad -DAP_PASSWORD=Passwort_123 -DFEATURE_WEBSERVER @@ -284,6 +297,7 @@ build_flags = -DSPI_FREQUENCY=20000000 -DSPI_TOUCH_FREQUENCY=2500000 -DDEFAULT_SWAPSCREENBYTES=false + ${default_wheels_inverted.build_flags} ; TODO: actually assign pins -DPINS_RX1=22 -DPINS_TX1=25 @@ -311,6 +325,7 @@ build_flags = ${peters_platine_reversed.build_flags} ${ota_common.build_flags} ${default_limits.build_flags} + ${default_wheels_inverted.build_flags} -DDEVICE_PREFIX=bobbycar -DAP_PASSWORD=Passwort_123 -DFEATURE_WEBSERVER diff --git a/src/actions/erasenvsaction.h b/src/actions/erasenvsaction.h new file mode 100644 index 0000000..aef8a88 --- /dev/null +++ b/src/actions/erasenvsaction.h @@ -0,0 +1,41 @@ +#pragma once + +#include + +#include "actioninterface.h" +#include "globals.h" +#include "presets.h" + +namespace { +class EraseNvsAction : public virtual ActionInterface +{ +public: + void triggered() override + { + const auto profile = settingsPersister.currentlyOpenProfileIndex(); + + if (!settingsPersister.erase()) + { + Serial.println("EraseNvsAction::triggered() erase failed"); + return; + } + + settings = presets::defaultSettings; + + if (!profile) + return; + + if (!settingsPersister.openProfile(*profile)) + { + Serial.println("EraseNvsAction::triggered() openProfile failed"); + return; + } + + if (!settingsPersister.load(settings)) + { + Serial.println("EraseNvsAction::triggered() load failed"); + return; + } + } +}; +} diff --git a/src/displays/menus/debugmenu.h b/src/displays/menus/debugmenu.h index 8f9232c..b869294 100644 --- a/src/displays/menus/debugmenu.h +++ b/src/displays/menus/debugmenu.h @@ -6,6 +6,7 @@ #include "menuitem.h" #include "actions/loadsettingsaction.h" #include "actions/savesettingsaction.h" +#include "actions/erasenvsaction.h" #include "actions/switchscreenaction.h" #include "actions/dummyaction.h" #include "actions/toggleboolaction.h" @@ -40,6 +41,7 @@ class DebugMenu : public StaticMenuDefinition< makeComponent, LoadSettingsAction>, makeComponent, SaveSettingsAction>, + makeComponent, EraseNvsAction>, makeComponent, DummyAction>, makeComponent, SwitchScreenAction>, makeComponent, SwitchScreenAction>, diff --git a/src/displays/statusdisplay.h b/src/displays/statusdisplay.h index 115dd94..ce48a09 100644 --- a/src/displays/statusdisplay.h +++ b/src/displays/statusdisplay.h @@ -99,6 +99,7 @@ private: Label m_labelPerformance{85, bottomLines[2]}; // 40, 15 Label m_labelMode{165, bottomLines[2]}; // 75, 15 Label m_labelName{40, bottomLines[3]}; // 40, 15 + Label m_labelProfile{205, bottomLines[3]}; // 35, 15 static const constexpr int bottomLines[4] { 251, 266, 281, 296 }; }; @@ -136,6 +137,7 @@ void StatusDisplay::initScreen() m_labelMode.start(); tft.drawString("Name:", 0, bottomLines[3]); m_labelName.start(); + m_labelProfile.start(); tft.setTextColor(TFT_WHITE, TFT_BLACK); } @@ -161,6 +163,8 @@ void StatusDisplay::redraw() m_labelPerformance.redraw(String{performance.last}); m_labelMode.redraw(currentMode->displayName()); m_labelName.redraw(&deviceName[0]); + const auto profile = settingsPersister.currentlyOpenProfileIndex(); + m_labelProfile.redraw(profile?String{*profile}:"-"); } void StatusDisplay::rotate(int offset) diff --git a/src/main.cpp b/src/main.cpp index 47c399f..2404f8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -127,7 +127,8 @@ void setup() BluetoothConnectBmsAction{}.triggered(); } #endif - } else if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Slave) + } + else if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Slave) { bootLabel.redraw("bluetooth begin"); BluetoothBeginAction{}.triggered(); @@ -150,6 +151,7 @@ void setup() currentMode = &modes::defaultMode; #ifdef FEATURE_OTA + bootLabel.redraw("ota"); initOta(); #endif diff --git a/src/presets.h b/src/presets.h index 0e5d61c..5308026 100644 --- a/src/presets.h +++ b/src/presets.h @@ -25,10 +25,10 @@ constexpr Settings::ControllerHardware defaultControllerHardware { .enableBackLeft = true, .enableBackRight = true, - .invertFrontLeft = false, - .invertFrontRight = true, - .invertBackLeft = false, - .invertBackRight = true, + .invertFrontLeft = DEFAULT_INVERTFRONTLEFT, + .invertFrontRight = DEFAULT_INVERTFRONTRIGHT, + .invertBackLeft = DEFAULT_INVERTBACKLEFT, + .invertBackRight = DEFAULT_INVERTBACKRIGHT, .wheelDiameter = 165, .numMagnetPoles = 15, @@ -41,10 +41,10 @@ constexpr Settings::ControllerHardware mosfetsOffControllerHardware { .enableBackLeft = false, .enableBackRight = false, - .invertFrontLeft = false, - .invertFrontRight = true, - .invertBackLeft = false, - .invertBackRight = true, + .invertFrontLeft = DEFAULT_INVERTFRONTLEFT, + .invertFrontRight = DEFAULT_INVERTFRONTRIGHT, + .invertBackLeft = DEFAULT_INVERTBACKLEFT, + .invertBackRight = DEFAULT_INVERTBACKRIGHT, .wheelDiameter = 165, .numMagnetPoles = 15, @@ -57,7 +57,7 @@ constexpr Settings::WifiSettings defaultWifiSettings { }; constexpr Settings::BluetoothSettings defaultBluetoothSettings { - .autoBluetoothMode = BluetoothMode::Off + .autoBluetoothMode = BluetoothMode::Master }; constexpr Settings::ControllerHardware spinnerControllerHardware { @@ -66,10 +66,10 @@ constexpr Settings::ControllerHardware spinnerControllerHardware { .enableBackLeft = true, .enableBackRight = true, - .invertFrontLeft = false, - .invertFrontRight = false, - .invertBackLeft = false, - .invertBackRight = false, + .invertFrontLeft = DEFAULT_INVERTFRONTLEFT, + .invertFrontRight = !DEFAULT_INVERTFRONTRIGHT, + .invertBackLeft = DEFAULT_INVERTBACKLEFT, + .invertBackRight = !DEFAULT_INVERTBACKRIGHT, .wheelDiameter = 165, .numMagnetPoles = 15, diff --git a/src/screens.h b/src/screens.h index 5b54b47..e0508e6 100644 --- a/src/screens.h +++ b/src/screens.h @@ -54,7 +54,7 @@ #include "icons/logo.h" namespace { -Label bootLabel{32, 250, TFT_WHITE}; +Label bootLabel{32, 250}; union X { X() {} @@ -407,10 +407,11 @@ void initScreen() { tft.init(); tft.fillScreen(TFT_WHITE); - tft.setTextColor(TFT_BLACK); + tft.setTextColor(TFT_BLACK, TFT_WHITE); + tft.setTextFont(4); tft.pushImage(0, 40, icons::logo.WIDTH, icons::logo.HEIGHT, icons::logo.buffer); - tft.drawString("Bobbycar-OS", 32, 200, 4); - tft.drawString("booting...", 32, 225, 4); + tft.drawString("Bobbycar-OS", 32, 200); + tft.drawString("booting...", 32, 225); bootLabel.start(); } diff --git a/src/settingspersister.h b/src/settingspersister.h index d626ed2..d8bff67 100644 --- a/src/settingspersister.h +++ b/src/settingspersister.h @@ -17,12 +17,20 @@ class SettingsPersister { public: bool init(); + bool erase(); bool openProfile(uint8_t index); + void closeProfile(); bool load(Settings &settings); bool save(Settings &settings); + tl::optional currentlyOpenProfileIndex() const; + private: - tl::optional m_handle; + struct CurrentlyOpenProfile { + nvs_handle handle; + uint8_t profileIndex; + }; + tl::optional m_profile; }; bool SettingsPersister::init() @@ -32,14 +40,7 @@ bool SettingsPersister::init() { Serial.printf("nvs_flash_init() returned: %s, trying to erase\r\n", esp_err_to_name(err)); - err = nvs_flash_erase(); - if (err != ESP_OK) - { - Serial.printf("nvs_flash_erase() returned: %s, aborting\r\n", esp_err_to_name(err)); - return false; - } - - err = nvs_flash_init(); + return erase(); } if (err != ESP_OK) @@ -51,13 +52,28 @@ bool SettingsPersister::init() return true; } +bool SettingsPersister::erase() +{ + esp_err_t err = nvs_flash_erase(); + if (err != ESP_OK) + { + Serial.printf("nvs_flash_erase() returned: %s, aborting\r\n", esp_err_to_name(err)); + return false; + } + + err = nvs_flash_init(); + if (err != ESP_OK) + { + Serial.printf("nvs_flash_init() returned: %s\r\n", esp_err_to_name(err)); + return false; + } + + return true; +} + bool SettingsPersister::openProfile(uint8_t index) { - if (m_handle) - { - nvs_close(*m_handle); - m_handle = tl::nullopt; - } + closeProfile(); nvs_handle handle; esp_err_t err = nvs_open((String{"bobbycar"}+index).c_str(), NVS_READWRITE, &handle); @@ -67,11 +83,21 @@ bool SettingsPersister::openProfile(uint8_t index) return false; } - m_handle = handle; + m_profile = {handle, index}; return true; } +void SettingsPersister::closeProfile() +{ + if (!m_profile) + return; + + nvs_close(m_profile->handle); + + m_profile = tl::nullopt; +} + template struct nvsGetterHelper; template<> struct nvsGetterHelper { static constexpr auto nvs_get = &nvs_get_i8; }; template<> struct nvsGetterHelper { static constexpr auto nvs_get = &nvs_get_u8; }; @@ -138,9 +164,9 @@ template<> struct nvsGetterHelper { static esp_err_t nvs_get(nvs_ha bool SettingsPersister::load(Settings &settings) { - if (!m_handle) + if (!m_profile) { - Serial.println(""); + Serial.println("SettingsPersister::load() no profile open currently!"); return false; } @@ -148,7 +174,7 @@ bool SettingsPersister::load(Settings &settings) settings.executeForEverySetting([&](const char *key, auto &value) { - esp_err_t err = nvsGetterHelper>::nvs_get(*m_handle, key, &value); + esp_err_t err = nvsGetterHelper>::nvs_get(m_profile->handle, key, &value); if (err != ESP_OK) { Serial.printf("nvs_get_i32() for %s returned: %s\r\n", key, esp_err_to_name(err)); @@ -194,9 +220,9 @@ template<> struct nvsSetterHelper { static esp_err_t nvs_set(nvs_ha bool SettingsPersister::save(Settings &settings) { - if (!m_handle) + if (!m_profile) { - Serial.println(""); + Serial.println("SettingsPersister::save() no profile open currently!"); return false; } @@ -204,7 +230,7 @@ bool SettingsPersister::save(Settings &settings) settings.executeForEverySetting([&](const char *key, auto value) { - esp_err_t err = nvsSetterHelper::nvs_set(*m_handle, key, value); + esp_err_t err = nvsSetterHelper::nvs_set(m_profile->handle, key, value); if (err != ESP_OK) { Serial.printf("nvs_get_i32() for %s returned: %s\r\n", key, esp_err_to_name(err)); @@ -215,4 +241,12 @@ bool SettingsPersister::save(Settings &settings) return result; } + +tl::optional SettingsPersister::currentlyOpenProfileIndex() const +{ + if (m_profile) + return m_profile->profileIndex; + + return tl::nullopt; +} } diff --git a/src/texts.h b/src/texts.h index 196e66c..54a68af 100644 --- a/src/texts.h +++ b/src/texts.h @@ -36,6 +36,7 @@ constexpr char TEXT_TURNOFFDISCHARGE[] = "Turn off discharge"; //DebugMenu constexpr char TEXT_LOADSETTINGS[] = "Load settings"; constexpr char TEXT_SAVESETTINGS[] = "Save settings"; +constexpr char TEXT_ERASENVS[] = "Erase NVS"; constexpr char TEXT_FRONTCOMMAND[] = "Front command"; constexpr char TEXT_BACKCOMMAND[] = "Back command"; constexpr char TEXT_FRONTLEFTCOMMAND[] = "Front left command"; diff --git a/src/widgets/label.h b/src/widgets/label.h index db42451..5171033 100644 --- a/src/widgets/label.h +++ b/src/widgets/label.h @@ -8,7 +8,7 @@ namespace { class Label { public: - Label(int x, int y, uint32_t backgroundColor = TFT_BLACK); + Label(int x, int y); int x() const { return m_x; }; int y() const { return m_y; }; @@ -20,7 +20,6 @@ public: private: const int m_x; const int m_y; - const uint32_t m_backgroundColor; String m_lastStr; int m_lastFont; @@ -30,10 +29,9 @@ private: int m_lastHeight; }; -Label::Label(int x, int y, uint32_t backgroundColor) : +Label::Label(int x, int y) : m_x{x}, - m_y{y}, - m_backgroundColor{backgroundColor} + m_y{y} { } @@ -79,7 +77,7 @@ void Label::redraw(const String &str, bool forceRedraw) void Label::clear() { if (m_lastWidth || m_lastHeight) - tft.fillRect(m_x, m_y, m_lastWidth, m_lastHeight, m_backgroundColor); + tft.fillRect(m_x, m_y, m_lastWidth, m_lastHeight, tft.textbgcolor); start(); }