From 304ce9925f38786ad0e73526f2a12d60bd6e7b9d Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 19 Dec 2021 01:55:48 +0100 Subject: [PATCH 1/2] More tasks in task manager --- main/CMakeLists.txt | 4 ++ main/accessors/settingsaccessors.h | 1 - main/displays/menus/timersmenu.cpp | 9 ---- main/main.cpp | 9 ---- main/potis.cpp | 82 ++++++++++++++++++++++++++++++ main/potis.h | 4 ++ main/presets.h | 1 - main/serial.cpp | 0 main/serial.h | 0 main/settings.h | 2 - main/taskmanager.cpp | 2 + main/texts.cpp | 1 - main/texts.h | 1 - main/utils.cpp | 63 ----------------------- 14 files changed, 92 insertions(+), 87 deletions(-) create mode 100644 main/potis.cpp create mode 100644 main/potis.h create mode 100644 main/serial.cpp create mode 100644 main/serial.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 8762632..9c2bb83 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -179,10 +179,12 @@ set(headers mosfets.h newsettings.h ota.h + potis.h presets.h qrimport.h rotary.h screens.h + serial.h serialhandler.h settings.h settingspersister.h @@ -392,10 +394,12 @@ set(sources mosfets.cpp newsettings.cpp ota.cpp + potis.cpp presets.cpp qrimport.cpp rotary.cpp screens.cpp + serial.cpp serialhandler.cpp settings.cpp settingspersister.cpp diff --git a/main/accessors/settingsaccessors.h b/main/accessors/settingsaccessors.h index 3e94162..654b9e4 100644 --- a/main/accessors/settingsaccessors.h +++ b/main/accessors/settingsaccessors.h @@ -125,7 +125,6 @@ struct GametrakYMaxAccessor : public RefAccessorSaveSettings { int16_t struct GametrakDistMinAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.gametrakDistMin; } }; struct GametrakDistMaxAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.gametrakDistMax; } }; #endif -struct PotiReadRateAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.timersSettings.potiReadRate; } }; struct ModeUpdateRateAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.timersSettings.modeUpdateRate; } }; struct StatsUpdateRateAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.timersSettings.statsUpdateRate; } }; struct DisplayUpdateRateAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.timersSettings.displayUpdateRate; } }; diff --git a/main/displays/menus/timersmenu.cpp b/main/displays/menus/timersmenu.cpp index fe6e196..7d72040 100644 --- a/main/displays/menus/timersmenu.cpp +++ b/main/displays/menus/timersmenu.cpp @@ -11,14 +11,6 @@ using namespace espgui; namespace { -using PotiReadRateChangeDisplay = makeComponent< - ChangeValueDisplay, - StaticText, - PotiReadRateAccessor, - BackActionInterface>, - SwitchScreenAction ->; - using ModeUpdateRateChangeDisplay = makeComponent< ChangeValueDisplay, StaticText, @@ -64,7 +56,6 @@ using CanReceiveRateChangeDisplay = makeComponent< TimersMenu::TimersMenu() { - constructMenuItem, SwitchScreenAction>>(); constructMenuItem, SwitchScreenAction>>(); constructMenuItem, SwitchScreenAction>>(); constructMenuItem, SwitchScreenAction>>(); diff --git a/main/main.cpp b/main/main.cpp index 4410d5f..e93f669 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -71,7 +71,6 @@ using namespace std::chrono_literals; #include "taskmanager.h" namespace { -std::optional lastPotiRead; std::optional lastModeUpdate; std::optional lastStatsUpdate; std::optional lastDisplayUpdate; @@ -250,14 +249,6 @@ extern "C" void app_main() for (auto &schedulerTask : schedulerTasks) { schedulerTask.loop(); - - } - - if (!lastPotiRead || now - *lastPotiRead >= 1000ms/settings.boardcomputerHardware.timersSettings.potiReadRate) - { - readPotis(); - - lastPotiRead = now; } if (!lastModeUpdate || now - *lastModeUpdate >= 1000ms/settings.boardcomputerHardware.timersSettings.modeUpdateRate) diff --git a/main/potis.cpp b/main/potis.cpp new file mode 100644 index 0000000..1c80c90 --- /dev/null +++ b/main/potis.cpp @@ -0,0 +1,82 @@ +#include "potis.h" + +// 3rdparty lib includes +#include +#include + +// local includes +#include "globals.h" + +#ifdef FEATURE_CAN +#include "can.h" +#endif + +using namespace std::chrono_literals; + +void initPotis() +{ + +} + +void readPotis() +{ + [[maybe_unused]] + constexpr auto sampleMultipleTimes = [](int pin){ + analogRead(pin); + double sum{}; + const auto sampleCount = settings.boardcomputerHardware.sampleCount; + for (int i = 0; i < sampleCount; i++) + sum += analogRead(pin); + return sum / sampleCount; + }; + + raw_gas = std::nullopt; + raw_brems = std::nullopt; + +#ifdef FEATURE_CAN + const auto now = espchrono::millis_clock::now(); + + if (can::can_gas) + { + if (now - can::last_can_gas < 100ms) + raw_gas = *can::can_gas; + else + can::can_gas = std::nullopt; + } + + if (can::can_brems) + { + if (now - can::last_can_brems < 100ms) + raw_brems = *can::can_brems; + else + can::can_brems = std::nullopt; + } +#endif + +#ifdef FEATURE_ADC_IN + if (!raw_gas) + raw_gas = sampleMultipleTimes(PINS_GAS); + if (!raw_brems) + raw_brems = sampleMultipleTimes(PINS_BREMS); +#endif + + if (raw_gas) + gas = cpputils::mapValueClamped(*raw_gas, settings.boardcomputerHardware.gasMin, settings.boardcomputerHardware.gasMax, 0., 1000.); + else + gas = std::nullopt; + if (raw_brems) + brems = cpputils::mapValueClamped(*raw_brems, settings.boardcomputerHardware.bremsMin, settings.boardcomputerHardware.bremsMax, 0., 1000.); + else + brems = std::nullopt; + +#ifdef FEATURE_GAMETRAK + raw_gametrakX = sampleMultipleTimes(PINS_GAMETRAKX); + gametrakX = cpputils::mapValueClamped(raw_gametrakX, settings.boardcomputerHardware.gametrakXMin, settings.boardcomputerHardware.gametrakXMax, 0., 1000.); + + raw_gametrakY = sampleMultipleTimes(PINS_GAMETRAKY); + gametrakY = cpputils::mapValueClamped(raw_gametrakY, settings.boardcomputerHardware.gametrakYMin, settings.boardcomputerHardware.gametrakYMax, 0., 1000.); + + raw_gametrakDist = sampleMultipleTimes(PINS_GAMETRAKDIST); + gametrakDist = cpputils::mapValueClamped(raw_gametrakDist, settings.boardcomputerHardware.gametrakDistMin, settings.boardcomputerHardware.gametrakDistMax, 0., 1000.); +#endif +} diff --git a/main/potis.h b/main/potis.h new file mode 100644 index 0000000..ffa8bf5 --- /dev/null +++ b/main/potis.h @@ -0,0 +1,4 @@ +#pragma once + +void initPotis(); +void readPotis(); diff --git a/main/presets.h b/main/presets.h index 7997adf..7310782 100644 --- a/main/presets.h +++ b/main/presets.h @@ -136,7 +136,6 @@ constexpr Settings::ControllerHardware spinnerControllerHardware { }; constexpr Settings::BoardcomputerHardware::TimersSettings defaultTimersSettings { - .potiReadRate = 50, .modeUpdateRate = 50, .statsUpdateRate = 50, .displayUpdateRate = 50, diff --git a/main/serial.cpp b/main/serial.cpp new file mode 100644 index 0000000..e69de29 diff --git a/main/serial.h b/main/serial.h new file mode 100644 index 0000000..e69de29 diff --git a/main/settings.h b/main/settings.h index 93915ae..a37096e 100644 --- a/main/settings.h +++ b/main/settings.h @@ -103,7 +103,6 @@ struct Settings #endif struct TimersSettings { - int16_t potiReadRate; int16_t modeUpdateRate; int16_t statsUpdateRate; int16_t displayUpdateRate; @@ -300,7 +299,6 @@ void Settings::executeForEveryCommonSetting(T &&callable) callable("gametrakDistMax", boardcomputerHardware.gametrakDistMax); #endif - callable("potiReadRate", boardcomputerHardware.timersSettings.potiReadRate); callable("modeUpdateRate", boardcomputerHardware.timersSettings.modeUpdateRate); callable("statsUpdateRate", boardcomputerHardware.timersSettings.statsUpdateRate); callable("displayUpdateRa", boardcomputerHardware.timersSettings.displayUpdateRate); diff --git a/main/taskmanager.cpp b/main/taskmanager.cpp index 9055d1f..dce2b77 100644 --- a/main/taskmanager.cpp +++ b/main/taskmanager.cpp @@ -37,6 +37,7 @@ #ifdef FEATURE_NTP #include "time_bobbycar.h" #endif +#include "potis.h" using namespace std::chrono_literals; @@ -71,6 +72,7 @@ espcpputils::SchedulerTask schedulerTasksArr[] { #ifdef FEATURE_NTP espcpputils::SchedulerTask { "time", initTime, updateTime, 100ms }, #endif + espcpputils::SchedulerTask { "potis", initPotis, readPotis, 20ms }, }; } // namespace diff --git a/main/texts.cpp b/main/texts.cpp index 7e54aee..9f58ec6 100644 --- a/main/texts.cpp +++ b/main/texts.cpp @@ -424,7 +424,6 @@ char TEXT_MOTORTEST[] = "Motortest"; //TimersMenu //char TEXT_TIMERS[] = "Timers"; -char TEXT_POTIREADRATE[] = "Poti read rate"; char TEXT_MODEUPDATERATE[] = "Mode update rate"; char TEXT_STATSUPDATERATE[] = "Stats update rate"; char TEXT_DISPLAYUPDATERATE[] = "Display update rate"; diff --git a/main/texts.h b/main/texts.h index afa48d6..92f706f 100644 --- a/main/texts.h +++ b/main/texts.h @@ -424,7 +424,6 @@ extern char TEXT_MOTORTEST[]; //TimersMenu //extern char TEXT_TIMERS[]; -extern char TEXT_POTIREADRATE[]; extern char TEXT_MODEUPDATERATE[]; extern char TEXT_STATSUPDATERATE[]; extern char TEXT_DISPLAYUPDATERATE[]; diff --git a/main/utils.cpp b/main/utils.cpp index 0c8edea..2a71798 100644 --- a/main/utils.cpp +++ b/main/utils.cpp @@ -247,69 +247,6 @@ void updateAccumulators() avgSpeedKmh = convertToKmh(avgSpeed); } -void readPotis() -{ - [[maybe_unused]] - constexpr auto sampleMultipleTimes = [](int pin){ - analogRead(pin); - double sum{}; - const auto sampleCount = settings.boardcomputerHardware.sampleCount; - for (int i = 0; i < sampleCount; i++) - sum += analogRead(pin); - return sum / sampleCount; - }; - - raw_gas = std::nullopt; - raw_brems = std::nullopt; - -#ifdef FEATURE_CAN - const auto now = espchrono::millis_clock::now(); - - if (can::can_gas) - { - if (now - can::last_can_gas < 100ms) - raw_gas = *can::can_gas; - else - can::can_gas = std::nullopt; - } - - if (can::can_brems) - { - if (now - can::last_can_brems < 100ms) - raw_brems = *can::can_brems; - else - can::can_brems = std::nullopt; - } -#endif - -#ifdef FEATURE_ADC_IN - if (!raw_gas) - raw_gas = sampleMultipleTimes(PINS_GAS); - if (!raw_brems) - raw_brems = sampleMultipleTimes(PINS_BREMS); -#endif - - if (raw_gas) - gas = cpputils::mapValueClamped(*raw_gas, settings.boardcomputerHardware.gasMin, settings.boardcomputerHardware.gasMax, 0., 1000.); - else - gas = std::nullopt; - if (raw_brems) - brems = cpputils::mapValueClamped(*raw_brems, settings.boardcomputerHardware.bremsMin, settings.boardcomputerHardware.bremsMax, 0., 1000.); - else - brems = std::nullopt; - -#ifdef FEATURE_GAMETRAK - raw_gametrakX = sampleMultipleTimes(PINS_GAMETRAKX); - gametrakX = cpputils::mapValueClamped(raw_gametrakX, settings.boardcomputerHardware.gametrakXMin, settings.boardcomputerHardware.gametrakXMax, 0., 1000.); - - raw_gametrakY = sampleMultipleTimes(PINS_GAMETRAKY); - gametrakY = cpputils::mapValueClamped(raw_gametrakY, settings.boardcomputerHardware.gametrakYMin, settings.boardcomputerHardware.gametrakYMax, 0., 1000.); - - raw_gametrakDist = sampleMultipleTimes(PINS_GAMETRAKDIST); - gametrakDist = cpputils::mapValueClamped(raw_gametrakDist, settings.boardcomputerHardware.gametrakDistMin, settings.boardcomputerHardware.gametrakDistMax, 0., 1000.); -#endif -} - float wattToAmpere(float watt) { float voltage = std::max(controllers.front.feedback.batVoltage, controllers.back.feedback.batVoltage); if (voltage > 50) voltage = 50; From 2ec19d2def9e38651eb5a9dbc48e9eac858e4794 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 19 Dec 2021 02:37:48 +0100 Subject: [PATCH 2/2] bluetooth to taskmanager migrated --- main/CMakeLists.txt | 2 ++ main/bluetooth_bobby.cpp | 37 +++++++++++++++++++++++++++++++++++++ main/bluetooth_bobby.h | 6 ++++++ main/main.cpp | 32 -------------------------------- main/potis.cpp | 7 +++++-- main/taskmanager.cpp | 6 ++++++ 6 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 main/bluetooth_bobby.cpp create mode 100644 main/bluetooth_bobby.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 9c2bb83..6a2efba 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -30,6 +30,7 @@ set(headers battery.h ble_bobby.h bletexthelpers.h + bluetooth_bobby.h bluetoothmode.h bluetoothtexthelpers.h bmsutils.h @@ -244,6 +245,7 @@ set(sources battery.cpp ble_bobby.cpp bletexthelpers.cpp + bluetooth_bobby.cpp bluetoothmode.cpp bluetoothtexthelpers.cpp bmsutils.cpp diff --git a/main/bluetooth_bobby.cpp b/main/bluetooth_bobby.cpp new file mode 100644 index 0000000..f41f358 --- /dev/null +++ b/main/bluetooth_bobby.cpp @@ -0,0 +1,37 @@ +#include "bluetooth_bobby.h" + +// local includes +#ifdef FEATURE_BLUETOOTH +#include "actions/bluetoothbeginaction.h" +#include "actions/bluetoothbeginmasteraction.h" +#ifdef FEATURE_BMS +#include "actions/bluetoothconnectbmsaction.h" +#endif +#endif + +#ifdef FEATURE_BLUETOOTH +void bluetooth_init() +{ + if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Master) + { + bootLabel.redraw("bluetooth begin master"); + BluetoothBeginMasterAction{}.triggered(); +#ifdef FEATURE_BMS + if (settings.autoConnectBms) + { + bootLabel.redraw("connect BMS"); + BluetoothConnectBmsAction{}.triggered(); + } +#endif + } + else if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Slave) + { + bootLabel.redraw("bluetooth begin"); + BluetoothBeginAction{}.triggered(); + } +} + +void bluetooth_update() +{ +} +#endif diff --git a/main/bluetooth_bobby.h b/main/bluetooth_bobby.h new file mode 100644 index 0000000..2bc4634 --- /dev/null +++ b/main/bluetooth_bobby.h @@ -0,0 +1,6 @@ +#pragma once + +#ifdef FEATURE_BLUETOOTH +void bluetooth_init(); +void bluetooth_update(); +#endif diff --git a/main/main.cpp b/main/main.cpp index e93f669..b1823d3 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -32,13 +32,6 @@ using namespace std::chrono_literals; #endif #include "presets.h" #include "statistics.h" -#ifdef FEATURE_BLUETOOTH -#include "actions/bluetoothbeginaction.h" -#include "actions/bluetoothbeginmasteraction.h" -#ifdef FEATURE_BMS -#include "actions/bluetoothconnectbmsaction.h" -#endif -#endif #ifdef FEATURE_BLE #include "ble_bobby.h" #endif @@ -140,26 +133,6 @@ extern "C" void app_main() task.setup(); } -#ifdef FEATURE_BLUETOOTH - if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Master) - { - bootLabel.redraw("bluetooth begin master"); - BluetoothBeginMasterAction{}.triggered(); -#ifdef FEATURE_BMS - if (settings.autoConnectBms) - { - bootLabel.redraw("connect BMS"); - BluetoothConnectBmsAction{}.triggered(); - } -#endif - } - else if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Slave) - { - bootLabel.redraw("bluetooth begin"); - BluetoothBeginAction{}.triggered(); - } -#endif - #ifdef FEATURE_CAN bootLabel.redraw("can"); can::initCan(); @@ -181,11 +154,6 @@ extern "C" void app_main() initLedStrip(); #endif - raw_gas = std::nullopt; - raw_brems = std::nullopt; - gas = std::nullopt; - brems = std::nullopt; - for (Controller &controller : controllers) controller.command.buzzer = {}; diff --git a/main/potis.cpp b/main/potis.cpp index 1c80c90..4a0ed5c 100644 --- a/main/potis.cpp +++ b/main/potis.cpp @@ -15,13 +15,16 @@ using namespace std::chrono_literals; void initPotis() { - + raw_gas = std::nullopt; + raw_brems = std::nullopt; + gas = std::nullopt; + brems = std::nullopt; } void readPotis() { [[maybe_unused]] - constexpr auto sampleMultipleTimes = [](int pin){ + constexpr auto sampleMultipleTimes = [](uint8_t pin){ analogRead(pin); double sum{}; const auto sampleCount = settings.boardcomputerHardware.sampleCount; diff --git a/main/taskmanager.cpp b/main/taskmanager.cpp index dce2b77..45d672f 100644 --- a/main/taskmanager.cpp +++ b/main/taskmanager.cpp @@ -38,6 +38,9 @@ #include "time_bobbycar.h" #endif #include "potis.h" +#ifdef FEATURE_BLUETOOTH +#include "bluetooth_bobby.h" +#endif using namespace std::chrono_literals; @@ -73,6 +76,9 @@ espcpputils::SchedulerTask schedulerTasksArr[] { espcpputils::SchedulerTask { "time", initTime, updateTime, 100ms }, #endif espcpputils::SchedulerTask { "potis", initPotis, readPotis, 20ms }, +#ifdef FEATURE_BLUETOOTH + espcpputils::SchedulerTask { "bluetooth", bluetooth_init, bluetooth_update, 100ms }, +#endif }; } // namespace