From d4955349a8570394e198c80fea1c23c883437c9d Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sat, 11 Dec 2021 23:11:43 +0100 Subject: [PATCH 01/36] Added QR code library --- .gitmodules | 3 +++ components/TFT_eSPI_QRcode | 1 + 2 files changed, 4 insertions(+) create mode 160000 components/TFT_eSPI_QRcode diff --git a/.gitmodules b/.gitmodules index 6630b4c..47f842d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -61,3 +61,6 @@ [submodule "components/espconfiglib"] path = components/espconfiglib url = ../../0xFEEDC0DE64/espconfiglib.git +[submodule "components/TFT_eSPI_QRcode"] + path = components/TFT_eSPI_QRcode + url = ../TFT_eSPI_QRcode.git diff --git a/components/TFT_eSPI_QRcode b/components/TFT_eSPI_QRcode new file mode 160000 index 0000000..355d712 --- /dev/null +++ b/components/TFT_eSPI_QRcode @@ -0,0 +1 @@ +Subproject commit 355d712c0bc64fc2c433dac2cd706125becb963e From 2616af5a30cb3019cb8aac9abf41b44777638e42 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sun, 12 Dec 2021 01:21:12 +0100 Subject: [PATCH 02/36] Updated QR library --- components/TFT_eSPI_QRcode | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/TFT_eSPI_QRcode b/components/TFT_eSPI_QRcode index 355d712..7e66dda 160000 --- a/components/TFT_eSPI_QRcode +++ b/components/TFT_eSPI_QRcode @@ -1 +1 @@ -Subproject commit 355d712c0bc64fc2c433dac2cd706125becb963e +Subproject commit 7e66dda7a0bfdfc2f5815f7916cc91cce906fe54 From 9f18bd980cd6f12914b1ef11270c5aae0c7d1472 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sun, 12 Dec 2021 01:23:19 +0100 Subject: [PATCH 03/36] Added qr code debug menu --- main/CMakeLists.txt | 4 +++- main/displays/menus/debugmenu.cpp | 2 ++ main/displays/qrcodedebug.cpp | 36 +++++++++++++++++++++++++++++++ main/displays/qrcodedebug.h | 21 ++++++++++++++++++ main/texts.cpp | 2 ++ main/texts.h | 3 +++ 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 main/displays/qrcodedebug.cpp create mode 100644 main/displays/qrcodedebug.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 925c32f..54f5599 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -117,6 +117,7 @@ set(headers displays/popups/alertdisplay.h displays/poweroffdisplay.h displays/powersupplydisplay.h + displays/qrcodedebug.h displays/spirodisplay.h displays/starfielddisplay.h displays/statusdisplay.h @@ -316,6 +317,7 @@ set(sources displays/popups/alertdisplay.cpp displays/poweroffdisplay.cpp displays/powersupplydisplay.cpp + displays/qrcodedebug.cpp displays/spirodisplay.cpp displays/starfielddisplay.cpp displays/statusdisplay.cpp @@ -399,7 +401,7 @@ set(sources set(dependencies libsodium freertos nvs_flash esp_http_server esp_https_ota mdns app_update esp_system esp_websocket_client driver - arduino-esp32 ArduinoJson esp-nimble-cpp FastLED-idf TFT_eSPI + arduino-esp32 ArduinoJson esp-nimble-cpp FastLED-idf TFT_eSPI TFT_eSPI_QRcode bobbycar-protocol cpputils cxx-ring-buffer date espasynchttpreq espasyncota espchrono espcpputils espconfiglib esp-gui-lib esphttpdutils espwifistack expected fmt ) diff --git a/main/displays/menus/debugmenu.cpp b/main/displays/menus/debugmenu.cpp index 66b6fbb..ad293b8 100644 --- a/main/displays/menus/debugmenu.cpp +++ b/main/displays/menus/debugmenu.cpp @@ -19,6 +19,7 @@ #include "icons/battery.h" #include "debugcolorhelpers.h" #include "esptexthelpers.h" +#include "displays/qrcodedebug.h" #include "displays/menus/commanddebugmenu.h" #include "displays/menus/motorstatedebugmenu.h" #include "displays/menus/feedbackdebugmenu.h" @@ -47,6 +48,7 @@ using namespace espgui; DebugMenu::DebugMenu() { constructMenuItem(); + constructMenuItem, SwitchScreenAction>>(); constructMenuItem, LoadSettingsAction>>(); constructMenuItem, SaveSettingsAction>>(); constructMenuItem, EraseNvsAction>>(); diff --git a/main/displays/qrcodedebug.cpp b/main/displays/qrcodedebug.cpp new file mode 100644 index 0000000..feade40 --- /dev/null +++ b/main/displays/qrcodedebug.cpp @@ -0,0 +1,36 @@ +#include "qrcodedebug.h" + +#include + +#include "screenmanager.h" +#include "displays/menus/debugmenu.h" + +using namespace espgui; + +QrCodeDebugDisplay::QrCodeDebugDisplay() +{ +} + +void QrCodeDebugDisplay::back() +{ + switchScreen(); +} + +void QrCodeDebugDisplay::initScreen() +{ + Base::initScreen(); + m_qrcode.init(); +} + +void QrCodeDebugDisplay::confirm() +{ + m_qrcode.createScaleToFit("WIFI:T:WPA;S:realraum;P:r3alraum;"); +} + +void QrCodeDebugDisplay::rotate(int offset) +{ + m_mult += offset; + m_qrcode.setMultiply(m_mult); + ESP_LOGW("BOBBY", "Multiply: %i", m_mult); + m_qrcode.create("Hello World!"); +} diff --git a/main/displays/qrcodedebug.h b/main/displays/qrcodedebug.h new file mode 100644 index 0000000..84ff09e --- /dev/null +++ b/main/displays/qrcodedebug.h @@ -0,0 +1,21 @@ +#pragma once + +// 3rdparty lib includes +#include +#include + +class QrCodeDebugDisplay : + public espgui::Display +{ + using Base = espgui::Display; +public: + QrCodeDebugDisplay(); + // std::string text() const override; + void initScreen() override; + void confirm() override; + void back() override; + void rotate(int offset) override; +private: + qrcode::QRcode m_qrcode{}; + uint8_t m_mult{2}; +}; diff --git a/main/texts.cpp b/main/texts.cpp index baed3b2..6a99d0c 100644 --- a/main/texts.cpp +++ b/main/texts.cpp @@ -537,4 +537,6 @@ char TEXT_REENABLE_MENUITEMS[] = "Show advanced"; //SelectBuildserverBranchMenu char TEXT_SELECT_BRANCH[] = "Select Branch"; char TEXT_SELECT_BRANCH_CLEAR[] = "Clear Branch"; + +char TEXT_QRCODE_DEBUG[] = "QR Debug"; } // namespace diff --git a/main/texts.h b/main/texts.h index b3b9a74..37f8c10 100644 --- a/main/texts.h +++ b/main/texts.h @@ -536,6 +536,9 @@ extern char TEXT_REENABLE_MENUITEMS[]; //SelectBuildserverBranchMenu extern char TEXT_SELECT_BRANCH[]; extern char TEXT_SELECT_BRANCH_CLEAR[]; + +//QrCodeDebug +extern char TEXT_QRCODE_DEBUG[]; } // namespace using namespace bobbytexts; From c1a4058a22942caca9ef1d4769a9768e2cf47ab7 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sun, 12 Dec 2021 01:23:40 +0100 Subject: [PATCH 04/36] Added useful qr code --- main/displays/qrcodedebug.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main/displays/qrcodedebug.cpp b/main/displays/qrcodedebug.cpp index feade40..f59a3fc 100644 --- a/main/displays/qrcodedebug.cpp +++ b/main/displays/qrcodedebug.cpp @@ -1,9 +1,10 @@ #include "qrcodedebug.h" -#include +#include -#include "screenmanager.h" #include "displays/menus/debugmenu.h" +#include "globals.h" +#include "screenmanager.h" using namespace espgui; @@ -24,13 +25,12 @@ void QrCodeDebugDisplay::initScreen() void QrCodeDebugDisplay::confirm() { - m_qrcode.createScaleToFit("WIFI:T:WPA;S:realraum;P:r3alraum;"); + m_qrcode.createScaleToFit(fmt::format("WIFI:T:WPA;S:{};P:{};", deviceName, stringSettings.ap_password)); } void QrCodeDebugDisplay::rotate(int offset) { m_mult += offset; m_qrcode.setMultiply(m_mult); - ESP_LOGW("BOBBY", "Multiply: %i", m_mult); m_qrcode.create("Hello World!"); } From c6f2392289737436b52ece32cf82c593422b5c28 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Thu, 16 Dec 2021 20:42:47 +0100 Subject: [PATCH 05/36] Non-working commit --- config_comred.cmake | 1 + icons/greenpass.png | Bin 0 -> 409 bytes main/CMakeLists.txt | 8 +++++ main/displays/menus/greenpassmenu.cpp | 49 ++++++++++++++++++++++++++ main/displays/menus/greenpassmenu.h | 30 ++++++++++++++++ main/displays/menus/mainmenu.cpp | 3 ++ main/espnowfunctions.cpp | 41 +++++++++++++++++++++ main/espnowfunctions.h | 11 ++++++ main/espnowwrapper.cpp | 42 ++++++++++++++++++++++ main/espnowwrapper.h | 24 +++++++++++++ main/icons/greenpass.cpp | 42 ++++++++++++++++++++++ main/icons/greenpass.h | 7 ++++ main/main.cpp | 7 +++- main/texts.cpp | 5 +++ main/texts.h | 4 +++ 15 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 icons/greenpass.png create mode 100644 main/displays/menus/greenpassmenu.cpp create mode 100644 main/displays/menus/greenpassmenu.h create mode 100644 main/espnowfunctions.cpp create mode 100644 main/espnowfunctions.h create mode 100644 main/espnowwrapper.cpp create mode 100644 main/espnowwrapper.h create mode 100644 main/icons/greenpass.cpp create mode 100644 main/icons/greenpass.h diff --git a/config_comred.cmake b/config_comred.cmake index f06480e..75835e5 100644 --- a/config_comred.cmake +++ b/config_comred.cmake @@ -118,6 +118,7 @@ set(BOBBYCAR_BUILDFLAGS -DOLD_NVS -DFEATURE_DNS_NS -DSWITCH_BLINK + -DFEATURE_ESPNOW ) if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/ignore/lockscreen_plugin.cmake") diff --git a/icons/greenpass.png b/icons/greenpass.png new file mode 100644 index 0000000000000000000000000000000000000000..fbd57d3356176e17076e592e4693cce69267d55d GIT binary patch literal 409 zcmeAS@N?(olHy`uVBq!ia0y~yV2}V|4mJh`h6m-gKNuJoI14-?iy0VX+d-JI|LMn6 z1_nlNPZ!4!kIv*TbEW>wZ}iw;RHw(x%*^ck<4{V{R)Y-&1_lNvxKA0C{4{+KGG*_w zo`+|zZ`Vlr!*9cMjNzB}fmmj(rnSDy)UR|ws zWuiOB27$09**o>$1@i3z1STE)!nm + +// local includes +#include "actions/switchscreenaction.h" +#include "displays/menus/mainmenu.h" +#include "icons/back.h" + +using namespace espgui; + +namespace greenpassmenu { +bool showingQRCode{false}; +} // namespace + +void GreenPassMenu::ShowCertAction::triggered() { + greenpassmenu::showingQRCode = true; + m_qrcode.init(); + m_qrcode.createScaleToFit(""); +} + +GreenPassMenu::GreenPassMenu() +{ + constructMenuItem, ShowCertAction>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); +} + +void GreenPassMenu::initScreen() +{ + Base::initScreen(); + greenpassmenu::showingQRCode = false; +} + +void GreenPassMenu::rotate(int offset) +{ + if (greenpassmenu::showingQRCode) + GreenPassMenu::initScreen(); + Base::rotate(offset); +} + +void GreenPassMenu::back() +{ + if (greenpassmenu::showingQRCode) + { + GreenPassMenu::initScreen(); + } + else + switchScreen(); +} diff --git a/main/displays/menus/greenpassmenu.h b/main/displays/menus/greenpassmenu.h new file mode 100644 index 0000000..a024dc3 --- /dev/null +++ b/main/displays/menus/greenpassmenu.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +// local includes +#include "menudisplay.h" +#include "texts.h" + +namespace greenpassmenu { +extern bool showingQRCode; +} // namespace + +class GreenPassMenu : + public espgui::MenuDisplay, + public espgui::StaticText +{ + using Base = espgui::MenuDisplay; +public: + GreenPassMenu(); + void back() override; + void rotate(int offset) override; + void initScreen() override; + + class ShowCertAction : public virtual espgui::ActionInterface { + public: + void triggered() override; + private: + qrcode::QRcode m_qrcode{}; + }; +}; diff --git a/main/displays/menus/mainmenu.cpp b/main/displays/menus/mainmenu.cpp index 9625b8c..6a675b4 100644 --- a/main/displays/menus/mainmenu.cpp +++ b/main/displays/menus/mainmenu.cpp @@ -19,6 +19,7 @@ #include "displays/menus/settingsmenu.h" #include "displays/menus/mosfetsmenu.h" #include "displays/menus/demosmenu.h" +#include "displays/menus/greenpassmenu.h" #include "displays/lockscreen.h" #include "displays/garagedisplay.h" #include "displays/menus/otamenu.h" @@ -45,6 +46,7 @@ #include "icons/poweroff.h" #include "icons/reboot.h" #include "icons/statistics.h" +#include "icons/greenpass.h" using namespace espgui; @@ -72,6 +74,7 @@ MainMenu::MainMenu() if (SHOWITEM) { constructMenuItem, SwitchScreenAction>>(); } #endif constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&bobbyicons::demos>>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&bobbyicons::greenpass>>>(); constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&bobbyicons::lock>>>(); #ifdef FEATURE_GARAGE if (SHOWITEM) { constructMenuItem, SwitchScreenAction>>(); } diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp new file mode 100644 index 0000000..befadfb --- /dev/null +++ b/main/espnowfunctions.cpp @@ -0,0 +1,41 @@ +constexpr const char * const TAG = "BOBBY_ESP_NOW"; +#ifdef FEATURE_ESPNOW +#include "espnowfunctions.h" + +#include +#include +#include + +namespace espnow { + +void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) +{ + const std::string data_str(data, data+data_len); + + size_t sep_pos = data_str.find(":"); + if (std::string::npos != sep_pos) + { + std::string msg_type = data_str.substr(0, sep_pos); + std::string msg = data_str.substr(sep_pos, data_str.length()-1); + ESP_LOGI(TAG, "Type: %s - Message: %s", msg_type.c_str(), msg.c_str()); + } + else + { + ESP_LOGW(TAG, "Invalid message: Could not find ':'"); + } +} + +void initESPNow() +{ + ESP_LOGI(TAG, "Initializing esp now..."); + ESPNow.reg_recv_cb(onReceive); + ESPNow.init(); +} + +void onRecvTs(uint64_t millis) +{ + +} + +} // namespace espnow +#endif diff --git a/main/espnowfunctions.h b/main/espnowfunctions.h new file mode 100644 index 0000000..f3b25be --- /dev/null +++ b/main/espnowfunctions.h @@ -0,0 +1,11 @@ +#pragma once +#ifdef FEATURE_ESPNOW +#include + +namespace espnow { +constexpr const uint8_t broadcast_address[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; +void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len); +void initESPNow(); +void onRecvTs(uint64_t millis); +} // namespace espnow +#endif diff --git a/main/espnowwrapper.cpp b/main/espnowwrapper.cpp new file mode 100644 index 0000000..a6c5ef3 --- /dev/null +++ b/main/espnowwrapper.cpp @@ -0,0 +1,42 @@ +#include "espnowwrapper.h" + +#include +#include + +namespace espnow { +class ESPNowW32 : public IESPNowW { + public: + virtual esp_err_t add_peer(uint8_t *mac, int channel); + virtual esp_err_t remove_peer(uint8_t *mac); + virtual esp_err_t send_message(uint8_t *mac, uint8_t *data, size_t datalen); + + private: + std::list peers; +}; + +esp_err_t ESPNowW32::add_peer(uint8_t *mac, int channel) { + peers.push_back(esp_now_peer_info_t{}); + esp_now_peer_info_t &peer = peers.back(); + std::memcpy(peer.peer_addr, mac, sizeof(peer.peer_addr)); + peer.channel = channel; + peer.ifidx = WIFI_IF_STA; + auto success = esp_now_add_peer(&peers.back()); + if (success != ESP_OK) + remove_peer(mac); + return success; +} +esp_err_t ESPNowW32::remove_peer(uint8_t *mac) { + // find peer in peers + for (auto it = peers.begin(); it != peers.end();) { + if (0 == memcmp(mac, it->peer_addr, sizeof(it->peer_addr))) { + it = peers.erase(it); // remove it + } + } + return esp_now_del_peer(mac); +} +esp_err_t ESPNowW32::send_message(uint8_t *mac, uint8_t *data, size_t datalen) { + return esp_now_send(mac, data, datalen); +} +ESPNowW32 espnow = ESPNowW32(); +IESPNowW &ESPNow = espnow; +} diff --git a/main/espnowwrapper.h b/main/espnowwrapper.h new file mode 100644 index 0000000..b3d6620 --- /dev/null +++ b/main/espnowwrapper.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +namespace espnow { +class IESPNowW { + public: + virtual int add_peer(uint8_t *mac, int channel = 0) = 0; + virtual int remove_peer(uint8_t *mac) = 0; + virtual int send_message(uint8_t *mac, uint8_t *data, size_t datalen) = 0; + virtual int init() { return esp_now_init(); } + int reg_send_cb(esp_now_send_cb_t cb) { + return esp_now_register_send_cb(cb); + } + int reg_recv_cb(esp_now_recv_cb_t cb) { + return esp_now_register_recv_cb(cb); + } + int unreg_send_cb() { return esp_now_unregister_send_cb(); } + int unreg_recv_cb() { return esp_now_unregister_recv_cb(); } +}; + +extern IESPNowW &ESPNow; +} // namespace diff --git a/main/icons/greenpass.cpp b/main/icons/greenpass.cpp new file mode 100644 index 0000000..0a24589 --- /dev/null +++ b/main/icons/greenpass.cpp @@ -0,0 +1,42 @@ +#include "greenpass.h" + +namespace bobbyicons { +const espgui::Icon<24, 24> greenpass{{ + 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, // 0x0010 (16) pixels + 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x6D2E, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, // 0x0020 (32) pixels + 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0x650D, 0x4CAA, // 0x0030 (48) pixels + 0x4CAA, 0x6D2E, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, // 0x0040 (64) pixels + 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0x650D, 0x4CAA, 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x0050 (80) pixels + 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650D, 0x4CAA, // 0x0060 (96) pixels + 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, // 0x0070 (112) pixels + 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650D, 0x4CAA, 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x0080 (128) pixels + 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650D, 0x4CAA, // 0x0090 (144) pixels + 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, // 0x00A0 (160) pixels + 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650D, 0x4CAA, 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x00B0 (176) pixels + 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650D, 0x4CAA, // 0x00C0 (192) pixels + 0x4CAA, 0x6D2E, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, // 0x00D0 (208) pixels + 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0x650D, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x00E0 (224) pixels + 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x00F0 (240) pixels + 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, // 0x0100 (256) pixels + 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, // 0x0110 (272) pixels + 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, // 0x0120 (288) pixels + 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, // 0x0130 (304) pixels + 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x0140 (320) pixels + 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0xB676, 0xF7BE, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x0150 (336) pixels + 0x4CAA, 0x4CAA, 0x54CB, 0x54CB, 0x54CB, 0x54CB, 0x54CB, 0x54CB, 0x54CB, 0x4CAA, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, // 0x0160 (352) pixels + 0xF7BE, 0xF7BE, 0x4CAA, 0x54CB, 0x54CB, 0x54CB, 0x4CAA, 0x4CAA, 0x4CAA, 0x6D2E, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, // 0x0170 (368) pixels + 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, 0xF7BE, 0xF7BE, 0x4CAA, 0xF7BE, 0xF7BE, 0xF7BE, 0x650C, 0x4CAA, // 0x0180 (384) pixels + 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, // 0x0190 (400) pixels + 0xF7BE, 0xF7BE, 0x4CAA, 0xF7BE, 0xF7BE, 0xF7BE, 0x650C, 0x4CAA, 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x01A0 (416) pixels + 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0xF7BE, 0xF7BE, 0x8590, 0x4CAA, // 0x01B0 (432) pixels + 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, // 0x01C0 (448) pixels + 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x01D0 (464) pixels + 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x6D2E, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x8590, // 0x01E0 (480) pixels + 0x4CAA, 0x6D2E, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, // 0x01F0 (496) pixels + 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x6D2E, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, // 0x0200 (512) pixels + 0xF7BE, 0x650C, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x6D2E, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, 0xF7BE, // 0x0210 (528) pixels + 0x4CAA, 0x54CB, 0x7D6F, 0x7D6F, 0x7D6F, 0x7D6F, 0x7D6F, 0x7D6F, 0x7D6F, 0x54CB, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x54CB, 0x7D6F, // 0x0220 (544) pixels + 0x7D6F, 0x7D6F, 0x7D6F, 0x7D6F, 0x7D6F, 0x7D6F, 0x7D6F, 0xF79D, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x0230 (560) pixels + 0x4CAA, 0x4CAA, 0x4CAA, 0xF7BE, 0xF7BE, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, 0x4CAA, // 0x0240 (576) pixels +}, "greenpass"}; +} // namespace bobbyicons diff --git a/main/icons/greenpass.h b/main/icons/greenpass.h new file mode 100644 index 0000000..437b069 --- /dev/null +++ b/main/icons/greenpass.h @@ -0,0 +1,7 @@ +#pragma once + +#include "icon.h" + +namespace bobbyicons { +extern const espgui::Icon<24, 24> greenpass; +} // namespace bobbyicons diff --git a/main/main.cpp b/main/main.cpp index 8682c81..c2fce5e 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -81,7 +81,9 @@ using namespace std::chrono_literals; #endif #include "drivingstatistics.h" #include "newsettings.h" - +#ifdef FEATURE_ESPNOW +#include "espnowfunctions.h" +#endif namespace { std::optional lastWifiUpdate; std::optional lastPotiRead; @@ -291,6 +293,9 @@ extern "C" void app_main() else espgui::switchScreen(); #endif +#ifdef FEATURE_ESPNOW + espnow::initESPNow(); +#endif while (true) { diff --git a/main/texts.cpp b/main/texts.cpp index 6a99d0c..1111207 100644 --- a/main/texts.cpp +++ b/main/texts.cpp @@ -539,4 +539,9 @@ char TEXT_SELECT_BRANCH[] = "Select Branch"; char TEXT_SELECT_BRANCH_CLEAR[] = "Clear Branch"; char TEXT_QRCODE_DEBUG[] = "QR Debug"; + +//GreenPassMenu +char TEXT_GREENPASS[] = "Green Pass"; +char TEXT_SHOWCERT[] = "Show cert"; + } // namespace diff --git a/main/texts.h b/main/texts.h index 37f8c10..ebbe24f 100644 --- a/main/texts.h +++ b/main/texts.h @@ -539,6 +539,10 @@ extern char TEXT_SELECT_BRANCH_CLEAR[]; //QrCodeDebug extern char TEXT_QRCODE_DEBUG[]; + +//GreenPassMenu +extern char TEXT_GREENPASS[]; +extern char TEXT_SHOWCERT[]; } // namespace using namespace bobbytexts; From 4357f1c7a56df131a12609989d99d7d1f6af3b19 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sun, 12 Dec 2021 19:29:09 +0100 Subject: [PATCH 06/36] Updated --- components/TFT_eSPI_QRcode | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/TFT_eSPI_QRcode b/components/TFT_eSPI_QRcode index 7e66dda..80da304 160000 --- a/components/TFT_eSPI_QRcode +++ b/components/TFT_eSPI_QRcode @@ -1 +1 @@ -Subproject commit 7e66dda7a0bfdfc2f5815f7916cc91cce906fe54 +Subproject commit 80da3047ad397ca9c7ff9d462e848f33f6647af1 From 24e8543ff3a2a3224380021478d79c184b407863 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Dec 2021 02:13:22 +0100 Subject: [PATCH 07/36] Removed coredump from my config --- sdkconfig_comred | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/sdkconfig_comred b/sdkconfig_comred index 34a523b..fcdd9ef 100644 --- a/sdkconfig_comred +++ b/sdkconfig_comred @@ -701,19 +701,8 @@ CONFIG_ESP32_PHY_MAX_TX_POWER=20 # Core dump # # CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH is not set -CONFIG_ESP_COREDUMP_ENABLE_TO_UART=y -# CONFIG_ESP_COREDUMP_ENABLE_TO_NONE is not set -# CONFIG_ESP_COREDUMP_DATA_FORMAT_BIN is not set -CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF=y -# CONFIG_ESP_COREDUMP_CHECKSUM_CRC32 is not set -CONFIG_ESP_COREDUMP_CHECKSUM_SHA256=y -CONFIG_ESP_COREDUMP_ENABLE=y -CONFIG_ESP_COREDUMP_MAX_TASKS_NUM=64 -CONFIG_ESP_COREDUMP_UART_DELAY=0 -CONFIG_ESP_COREDUMP_STACK_SIZE=0 -CONFIG_ESP_COREDUMP_DECODE_INFO=y -# CONFIG_ESP_COREDUMP_DECODE_DISABLE is not set -CONFIG_ESP_COREDUMP_DECODE="info" +# CONFIG_ESP_COREDUMP_ENABLE_TO_UART is not set +CONFIG_ESP_COREDUMP_ENABLE_TO_NONE=y # end of Core dump # @@ -1473,19 +1462,8 @@ CONFIG_ESP32S2_PANIC_PRINT_REBOOT=y CONFIG_TIMER_TASK_STACK_SIZE=3584 CONFIG_SW_COEXIST_ENABLE=y # CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set -CONFIG_ESP32_ENABLE_COREDUMP_TO_UART=y -# CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE is not set -# CONFIG_ESP32_COREDUMP_DATA_FORMAT_BIN is not set -CONFIG_ESP32_COREDUMP_DATA_FORMAT_ELF=y -# CONFIG_ESP32_COREDUMP_CHECKSUM_CRC32 is not set -CONFIG_ESP32_COREDUMP_CHECKSUM_SHA256=y -CONFIG_ESP32_ENABLE_COREDUMP=y -CONFIG_ESP32_CORE_DUMP_MAX_TASKS_NUM=64 -CONFIG_ESP32_CORE_DUMP_UART_DELAY=0 -CONFIG_ESP32_CORE_DUMP_STACK_SIZE=0 -CONFIG_ESP32_CORE_DUMP_DECODE_INFO=y -# CONFIG_ESP32_CORE_DUMP_DECODE_DISABLE is not set -CONFIG_ESP32_CORE_DUMP_DECODE="info" +# CONFIG_ESP32_ENABLE_COREDUMP_TO_UART is not set +CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE=y CONFIG_MB_MASTER_TIMEOUT_MS_RESPOND=150 CONFIG_MB_MASTER_DELAY_MS_CONVERT=200 CONFIG_MB_QUEUE_LENGTH=20 From c50787ba8435068562a70faacf8b2aa96c9c694e Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Dec 2021 02:14:03 +0100 Subject: [PATCH 08/36] Fixed esp_now_init(), added message queue to not block the callback function --- main/espnowfunctions.cpp | 57 +++++++++++++++++++++++++++++++++++++--- main/espnowfunctions.h | 10 +++++++ main/main.cpp | 6 ++++- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index befadfb..d5d75af 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -6,18 +6,29 @@ constexpr const char * const TAG = "BOBBY_ESP_NOW"; #include #include +#include "globals.h" + namespace espnow { +std::deque message_queue{}; + void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) { + ESP_LOGI(TAG, "Received data"); const std::string data_str(data, data+data_len); size_t sep_pos = data_str.find(":"); if (std::string::npos != sep_pos) { std::string msg_type = data_str.substr(0, sep_pos); - std::string msg = data_str.substr(sep_pos, data_str.length()-1); + std::string msg = data_str.substr(sep_pos+1, data_str.length()-3); // - 3 may needs to be converted to sep_pos+1 ESP_LOGI(TAG, "Type: %s - Message: %s", msg_type.c_str(), msg.c_str()); + + const esp_now_message_t message{ + .content = msg, + .type = msg_type + }; + message_queue.push_back(message); } else { @@ -27,9 +38,47 @@ void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) void initESPNow() { - ESP_LOGI(TAG, "Initializing esp now..."); - ESPNow.reg_recv_cb(onReceive); - ESPNow.init(); + ESP_LOGI(TAG, "Initializing esp-now..."); + + if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled) + { + ESP_LOGW(TAG, "cannot execute esp_now_init(): tcp stack is down."); + return; + } + + if (const auto error = esp_now_init(); error != ESP_OK) + { + ESP_LOGE(TAG, "esp_now_init() failed with %s", esp_err_to_name(error)); + return; + } + + if (const auto error = esp_now_register_recv_cb(onReceive); error != ESP_OK) + { + ESP_LOGE(TAG, "esp_now_register_recv_cb() failed with %s", esp_err_to_name(error)); + return; + } +} + +void handle() +{ + if(message_queue.size()) + { + for (const esp_now_message_t &msg : message_queue) + { + ESP_LOGW(TAG, "queue has processed message of type '%s' with content '%s'", msg.type.c_str(), msg.content.c_str()); + message_queue.pop_front(); + + if (msg.type == "T") + { + // convert msg.content into uint64_t + // onRecvTs(converted); + } + } + for (const esp_now_message_t &msg : message_queue) + { + ESP_LOGI(TAG, "%s", msg.content.c_str()); + } + } } void onRecvTs(uint64_t millis) diff --git a/main/espnowfunctions.h b/main/espnowfunctions.h index f3b25be..0e18189 100644 --- a/main/espnowfunctions.h +++ b/main/espnowfunctions.h @@ -1,11 +1,21 @@ #pragma once #ifdef FEATURE_ESPNOW #include +#include +#include namespace espnow { constexpr const uint8_t broadcast_address[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; +struct esp_now_message_t { + const std::string content; + const std::string type; +}; + +extern std::deque message_queue; + void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len); void initESPNow(); +void handle(); void onRecvTs(uint64_t millis); } // namespace espnow #endif diff --git a/main/main.cpp b/main/main.cpp index c2fce5e..4678423 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -294,7 +294,7 @@ extern "C" void app_main() espgui::switchScreen(); #endif #ifdef FEATURE_ESPNOW - espnow::initESPNow(); + espnow::initESPNow(); #endif while (true) @@ -412,6 +412,10 @@ extern "C" void app_main() } #endif +#ifdef FEATURE_ESPNOW + espnow::handle(); +#endif + #ifdef FEATURE_CLOUD if (!lastCloudCollect || now - *lastCloudCollect >= std::chrono::milliseconds{settings.boardcomputerHardware.timersSettings.cloudCollectRate}) { From f9bc8b341d9f6c53dc762807bf45d2e8370dd0f3 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Dec 2021 20:24:49 +0100 Subject: [PATCH 09/36] Forgot to set error timeout --- main/udpcloud.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main/udpcloud.cpp b/main/udpcloud.cpp index fc67270..9397570 100644 --- a/main/udpcloud.cpp +++ b/main/udpcloud.cpp @@ -25,7 +25,6 @@ constexpr const char * const TAG = "bobbycloud"; using namespace std::chrono_literals; // Little "flash" on statusdisplay when udp stuff is happening bool visualSendUdpPacket; - espchrono::millis_clock::time_point timestampLastFailed; void spamUdpBroadcast() @@ -294,7 +293,7 @@ std::string buildUdpCloudString() void sendUdpCloudPacket() { EVERY_N_MILLIS(settings.boardcomputerHardware.timersSettings.udpSendRateMs) { - if (espchrono::ago(timestampLastFailed) < 3s) + if (espchrono::ago(timestampLastFailed) < 2s) { visualSendUdpPacket = false; return; @@ -332,6 +331,7 @@ void sendUdpCloudPacket() if (udpCloudIp.type != IPADDR_TYPE_V4) { ESP_LOGE(TAG, "unsupported ip type: %hhu", udpCloudIp.type); + timestampLastFailed = espchrono::millis_clock::now(); visualSendUdpPacket = false; return; } @@ -348,6 +348,7 @@ void sendUdpCloudPacket() if (const auto result = udpCloudSender.send(receipient, buf); !result) { + timestampLastFailed = espchrono::millis_clock::now(); ESP_LOGE(TAG, "send to cloud failed: %.*s (ip=%s)", result.error().size(), result.error().data(), wifi_stack::toString(udpCloudIp.u_addr.ip4).c_str()); } From e07cb9d3736c0fa671f3e66a427a066f95a45182 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Dec 2021 22:13:39 +0100 Subject: [PATCH 10/36] Added time sync via esp now --- main/CMakeLists.txt | 2 -- main/displays/menus/mainmenu.cpp | 29 ++++++++++++++++++++++++ main/displays/menus/mainmenu.h | 4 ++++ main/displays/menus/timesettingsmenu.cpp | 3 ++- main/utils.cpp | 23 +++++++++++++++++++ main/utils.h | 2 ++ 6 files changed, 60 insertions(+), 3 deletions(-) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index ff686f3..ec7fc45 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -132,7 +132,6 @@ set(headers drivingstatistics.h esptexthelpers.h espnowfunctions.h - espnowwrapper.h feedbackparser.h globals.h handbremse.h @@ -336,7 +335,6 @@ set(sources drivingstatistics.cpp esptexthelpers.cpp espnowfunctions.cpp - espnowwrapper.cpp feedbackparser.cpp globals.cpp handbremse.cpp diff --git a/main/displays/menus/mainmenu.cpp b/main/displays/menus/mainmenu.cpp index 6a675b4..453b2b8 100644 --- a/main/displays/menus/mainmenu.cpp +++ b/main/displays/menus/mainmenu.cpp @@ -47,11 +47,27 @@ #include "icons/reboot.h" #include "icons/statistics.h" #include "icons/greenpass.h" +#include "icons/time.h" +#include "tftinstance.h" using namespace espgui; +namespace mainmenu { +/* +class CurrentTimeText : public virtual TextInterface +{ +public: + std::string text() const override + { + return fmt::format("&7Time: {}", local_clock_string()); + } +}; +*/ +} // namespace + MainMenu::MainMenu() { + // constructMenuItem>>(); constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); #ifdef FEATURE_LEDSTRIP constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&bobbyicons::neopixel>>>(); @@ -94,3 +110,16 @@ void MainMenu::back() { switchScreen(); } + +void MainMenu::start() +{ + Base::start(); + m_label_currentTime.start(); +} + +void MainMenu::redraw() +{ + Base::redraw(); + tft.setTextFont(2); + m_label_currentTime.redraw(fmt::format("&7Time: {}", local_clock_string())); +} diff --git a/main/displays/menus/mainmenu.h b/main/displays/menus/mainmenu.h index ba241b2..0a6338e 100644 --- a/main/displays/menus/mainmenu.h +++ b/main/displays/menus/mainmenu.h @@ -25,8 +25,12 @@ class MainMenu : public espgui::MenuDisplay, public espgui::StaticText { + using Base = espgui::MenuDisplay; public: MainMenu(); void back() override; + void start() override; + void redraw() override; + espgui::Label m_label_currentTime{145, 6}; }; diff --git a/main/displays/menus/timesettingsmenu.cpp b/main/displays/menus/timesettingsmenu.cpp index c71dfe2..ff8ab82 100644 --- a/main/displays/menus/timesettingsmenu.cpp +++ b/main/displays/menus/timesettingsmenu.cpp @@ -37,7 +37,8 @@ public: #ifdef CONFIG_ESPCHRONO_SUPPORT_DEFAULT_TIMEZONE return fmt::format("local: {}", espchrono::toString(espchrono::toDateTime(espchrono::local_clock::now()))); #else - return "TODO"; + // Crude local time implementation + return fmt::format("local: {}", espchrono::toString(espchrono::toDateTime(espchrono::utc_clock::now() + settings.timeSettings.timezoneOffset))); #endif } }; diff --git a/main/utils.cpp b/main/utils.cpp index 358e768..0c8edea 100644 --- a/main/utils.cpp +++ b/main/utils.cpp @@ -337,3 +337,26 @@ uint8_t time_to_percent(espchrono::milliseconds32 repeat, espchrono::millisecond else return invert ? numLeds : 0; } + +void time_set_now(espchrono::utc_clock::time_point now) +{ + using namespace espchrono; + // ESP_LOGI("BOBBY", "%s (%lld)%s", toString(toDateTime(now)).c_str(), std::chrono::floor(now.time_since_epoch()).count(), time_valid(now) ? "":" (probably invalid)"); + const auto seconds = std::chrono::floor(now.time_since_epoch()); + + timeval ts { + .tv_sec = (long int)seconds.count(), + .tv_usec = (long int)std::chrono::floor(now.time_since_epoch() - seconds).count() + }; + timezone tz { + .tz_minuteswest = 0, + .tz_dsttime = 0 + }; + settimeofday(&ts, &tz); +} + +std::string local_clock_string() +{ + const auto dt = espchrono::toDateTime(espchrono::utc_clock::now() + settings.timeSettings.timezoneOffset); + return fmt::format("{:02d}:{:02d}:{:02d}", dt.hour, dt.minute, dt.second); +} diff --git a/main/utils.h b/main/utils.h index 1b35324..ba20812 100644 --- a/main/utils.h +++ b/main/utils.h @@ -59,3 +59,5 @@ void readPotis(); float wattToAmpere(float watt); float wattToMotorCurrent(float watt); uint8_t time_to_percent(espchrono::milliseconds32 repeat, espchrono::milliseconds32 riseTime, espchrono::milliseconds32 fullTime, size_t numLeds, bool invert); +void time_set_now(espchrono::utc_clock::time_point now); +std::string local_clock_string(); From 0e3053a45b933f726537789fb567020b091060c9 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Dec 2021 23:09:37 +0100 Subject: [PATCH 11/36] Added esp now garage opener --- main/displays/garagedisplay.cpp | 9 +++ main/espnowfunctions.cpp | 122 +++++++++++++++++++++++++++----- main/espnowfunctions.h | 4 ++ main/espnowwrapper.cpp | 42 ----------- main/espnowwrapper.h | 24 ------- 5 files changed, 116 insertions(+), 85 deletions(-) delete mode 100644 main/espnowwrapper.cpp delete mode 100644 main/espnowwrapper.h diff --git a/main/displays/garagedisplay.cpp b/main/displays/garagedisplay.cpp index dba370f..8592dbd 100644 --- a/main/displays/garagedisplay.cpp +++ b/main/displays/garagedisplay.cpp @@ -14,6 +14,7 @@ #include "displays/menus/mainmenu.h" #include "globals.h" #include "texts.h" +#include "espnowfunctions.h" #ifdef FEATURE_GARAGE void GarageDisplay::start() @@ -41,6 +42,14 @@ void GarageDisplay::redraw() void GarageDisplay::confirm() { +#ifdef FEATURE_ESPNOW + if (const auto error = espnow::send_espnow_message(fmt::format("BOBBYOPEN:garage:{}", "TOKEN")); error != ESP_OK) + { + ESP_LOGE("BOBBY", "send_espnow_message() failed with: %s", esp_err_to_name(error)); + return; + } + espgui::switchScreen(); +#endif } void GarageDisplay::back() diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index d5d75af..775bc88 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -3,18 +3,23 @@ constexpr const char * const TAG = "BOBBY_ESP_NOW"; #include "espnowfunctions.h" #include -#include + +#include #include +#include #include "globals.h" +#include "utils.h" namespace espnow { std::deque message_queue{}; +std::list peers{}; +uint8_t initialized{0}; void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) { - ESP_LOGI(TAG, "Received data"); + ESP_LOGD(TAG, "Received data"); const std::string data_str(data, data+data_len); size_t sep_pos = data_str.find(":"); @@ -22,7 +27,7 @@ void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) { std::string msg_type = data_str.substr(0, sep_pos); std::string msg = data_str.substr(sep_pos+1, data_str.length()-3); // - 3 may needs to be converted to sep_pos+1 - ESP_LOGI(TAG, "Type: %s - Message: %s", msg_type.c_str(), msg.c_str()); + ESP_LOGD(TAG, "Type: %s - Message: %s", msg_type.c_str(), msg.c_str()); const esp_now_message_t message{ .content = msg, @@ -40,50 +45,129 @@ void initESPNow() { ESP_LOGI(TAG, "Initializing esp-now..."); - if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled) + if (initialized < 1) { - ESP_LOGW(TAG, "cannot execute esp_now_init(): tcp stack is down."); - return; + if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled) + { + ESP_LOGW(TAG, "cannot execute esp_now_init(): tcp stack is down."); + return; + } + else + initialized = 1; } - if (const auto error = esp_now_init(); error != ESP_OK) + if (initialized < 2) { - ESP_LOGE(TAG, "esp_now_init() failed with %s", esp_err_to_name(error)); - return; + if (const auto error = esp_now_init(); error != ESP_OK) + { + ESP_LOGE(TAG, "esp_now_init() failed with %s", esp_err_to_name(error)); + return; + } + else + initialized = 2; } - if (const auto error = esp_now_register_recv_cb(onReceive); error != ESP_OK) + if (initialized < 3) { - ESP_LOGE(TAG, "esp_now_register_recv_cb() failed with %s", esp_err_to_name(error)); - return; + if (const auto error = esp_now_register_recv_cb(onReceive); error != ESP_OK) + { + ESP_LOGE(TAG, "esp_now_register_recv_cb() failed with %s", esp_err_to_name(error)); + return; + } + else + initialized = 3; } + + if (initialized < 4) + { + peers.push_back(esp_now_peer_info_t{}); + esp_now_peer_info_t &peer = peers.back(); + std::memcpy(peer.peer_addr, broadcast_address, sizeof(peer.peer_addr)); + peer.channel = 0; +/* + if (settings.wifiSettings.wifiApEnabled) + peer.ifidx = WIFI_IF_AP; + else if (settings.wifiSettings.wifiStaEnabled)*/ + peer.ifidx = WIFI_IF_STA; + + if (const auto error = esp_now_add_peer(&peers.back()); error != ESP_OK) + { + ESP_LOGE(TAG, "esp_now_add_peer() failed with %s", esp_err_to_name(error)); + return; + } + else + initialized = 4; + } + + initialized = 255; } void handle() { + if (initialized < 255 && (settings.wifiSettings.wifiApEnabled || settings.wifiSettings.wifiStaEnabled)) + { + initESPNow(); + return; + } + if(message_queue.size()) { for (const esp_now_message_t &msg : message_queue) { - ESP_LOGW(TAG, "queue has processed message of type '%s' with content '%s'", msg.type.c_str(), msg.content.c_str()); + ESP_LOGD(TAG, "queue has processed message of type '%s' with content '%s'", msg.type.c_str(), msg.content.c_str()); message_queue.pop_front(); if (msg.type == "T") { - // convert msg.content into uint64_t - // onRecvTs(converted); + if (const auto result = cpputils::fromString(msg.content); result) + { + onRecvTs(*result); + } + else + { + ESP_LOGW(TAG, "could not parse number: %.*s", result.error().size(), result.error().data()); + } + } + else + { + ESP_LOGI(TAG, "Unkown Type: %s - Message: %s", msg.type.c_str(), msg.content.c_str()); } - } - for (const esp_now_message_t &msg : message_queue) - { - ESP_LOGI(TAG, "%s", msg.content.c_str()); } } } void onRecvTs(uint64_t millis) { + const auto milliseconds = std::chrono::milliseconds(millis); + const auto timepoint = espchrono::utc_clock::time_point(milliseconds); + time_set_now(timepoint); +} +esp_err_t send_espnow_message(std::string message) +{ + if (initialized < 255) + return ESP_ERR_ESPNOW_NOT_INIT; + + if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled) + { + return ESP_ERR_ESPNOW_IF; + } + + if (peers.size() < 1) + { + return ESP_FAIL; + } + + for (const auto &peer : peers) + { + if(const auto error = esp_now_send(peer.peer_addr, (const uint8_t*)message.data(), message.size()); error != ESP_OK) + { + return error; + } + else + ESP_LOGI(TAG, "Successfully executed esp_now_send()"); + } + return ESP_OK; } } // namespace espnow diff --git a/main/espnowfunctions.h b/main/espnowfunctions.h index 0e18189..5d7794b 100644 --- a/main/espnowfunctions.h +++ b/main/espnowfunctions.h @@ -3,6 +3,8 @@ #include #include #include +#include +#include namespace espnow { constexpr const uint8_t broadcast_address[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; @@ -12,10 +14,12 @@ struct esp_now_message_t { }; extern std::deque message_queue; +extern std::list peers; void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len); void initESPNow(); void handle(); void onRecvTs(uint64_t millis); +esp_err_t send_espnow_message(std::string message); } // namespace espnow #endif diff --git a/main/espnowwrapper.cpp b/main/espnowwrapper.cpp deleted file mode 100644 index a6c5ef3..0000000 --- a/main/espnowwrapper.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "espnowwrapper.h" - -#include -#include - -namespace espnow { -class ESPNowW32 : public IESPNowW { - public: - virtual esp_err_t add_peer(uint8_t *mac, int channel); - virtual esp_err_t remove_peer(uint8_t *mac); - virtual esp_err_t send_message(uint8_t *mac, uint8_t *data, size_t datalen); - - private: - std::list peers; -}; - -esp_err_t ESPNowW32::add_peer(uint8_t *mac, int channel) { - peers.push_back(esp_now_peer_info_t{}); - esp_now_peer_info_t &peer = peers.back(); - std::memcpy(peer.peer_addr, mac, sizeof(peer.peer_addr)); - peer.channel = channel; - peer.ifidx = WIFI_IF_STA; - auto success = esp_now_add_peer(&peers.back()); - if (success != ESP_OK) - remove_peer(mac); - return success; -} -esp_err_t ESPNowW32::remove_peer(uint8_t *mac) { - // find peer in peers - for (auto it = peers.begin(); it != peers.end();) { - if (0 == memcmp(mac, it->peer_addr, sizeof(it->peer_addr))) { - it = peers.erase(it); // remove it - } - } - return esp_now_del_peer(mac); -} -esp_err_t ESPNowW32::send_message(uint8_t *mac, uint8_t *data, size_t datalen) { - return esp_now_send(mac, data, datalen); -} -ESPNowW32 espnow = ESPNowW32(); -IESPNowW &ESPNow = espnow; -} diff --git a/main/espnowwrapper.h b/main/espnowwrapper.h deleted file mode 100644 index b3d6620..0000000 --- a/main/espnowwrapper.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include - -namespace espnow { -class IESPNowW { - public: - virtual int add_peer(uint8_t *mac, int channel = 0) = 0; - virtual int remove_peer(uint8_t *mac) = 0; - virtual int send_message(uint8_t *mac, uint8_t *data, size_t datalen) = 0; - virtual int init() { return esp_now_init(); } - int reg_send_cb(esp_now_send_cb_t cb) { - return esp_now_register_send_cb(cb); - } - int reg_recv_cb(esp_now_recv_cb_t cb) { - return esp_now_register_recv_cb(cb); - } - int unreg_send_cb() { return esp_now_unregister_send_cb(); } - int unreg_recv_cb() { return esp_now_unregister_recv_cb(); } -}; - -extern IESPNowW &ESPNow; -} // namespace From 27019b7d758dd682064d6d58d96582af906ddc7e Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 09:47:09 +0100 Subject: [PATCH 12/36] Added vscode ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 756c3ff..6f35eeb 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ desktop.ini /.idea /.ccache /main/certs/* +/.vscode/* From 02999bcde184fdeaa3cb4bb0603775f4af78b904 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 09:56:40 +0100 Subject: [PATCH 13/36] Refactored ledstripanimationactions, ledstripactions, bmsdisplay, accesspointwifisettingsmenu, feedbackdebugmenu, ledstripmenu, ledstripselectblinkmenu, ledstripselectotamode, mosfetsmenu --- main/actions/ledstripanimationactions.h | 28 +--- main/actions/ledstripblinkactions.cpp | 42 ++++++ main/actions/ledstripblinkactions.h | 40 ++---- main/displays/bmsdisplay.cpp | 105 +++++++++++++++ main/displays/bmsdisplay.h | 125 +++--------------- .../menus/accesspointwifisettingsmenu.cpp | 33 +++++ .../menus/accesspointwifisettingsmenu.h | 32 +---- main/displays/menus/feedbackdebugmenu.h | 1 + main/displays/menus/ledstripmenu.cpp | 36 ++--- .../menus/ledstripselectblinkmenu.cpp | 53 ++++++++ main/displays/menus/ledstripselectblinkmenu.h | 66 ++------- main/displays/menus/ledstripselectotamode.cpp | 24 ++++ main/displays/menus/ledstripselectotamode.h | 37 ++---- main/displays/menus/mosfetsmenu.cpp | 26 ++++ main/displays/menus/mosfetsmenu.h | 30 +---- 15 files changed, 368 insertions(+), 310 deletions(-) diff --git a/main/actions/ledstripanimationactions.h b/main/actions/ledstripanimationactions.h index d79671e..9f705c5 100644 --- a/main/actions/ledstripanimationactions.h +++ b/main/actions/ledstripanimationactions.h @@ -1,40 +1,14 @@ #pragma once #include "actioninterface.h" -#include "utils.h" -#include "globals.h" #include "ledstrip.h" // #include "ledstripdefines.h" -using namespace espgui; - -namespace { #ifdef FEATURE_LEDSTRIP template -class LedStripSetAnimationAction : public virtual ActionInterface +class LedStripSetAnimationAction : public virtual espgui::ActionInterface { public: void triggered() override { animation_type = type; } }; - -/* -class LedstripAnimationDefaultRainbowAction : public virtual ActionInterface -{ -public: - void triggered() override { animation_type = LEDSTRIP_ANIMATION_TYPE_DEFAULTRAINBOW; } -}; - -class LedstripAnimationBetterRainbowAction : public virtual ActionInterface -{ -public: - void triggered() override { animation_type = LEDSTRIP_ANIMATION_TYPE_BETTERRAINBOW; } -}; - -class LedstripAnimationSyncToSpeedAction : public virtual ActionInterface -{ -public: - void triggered() override { animation_type = LEDSTRIP_ANIMATION_TYPE_SPEEDSYNCANIMATION; } -}; -*/ #endif -} diff --git a/main/actions/ledstripblinkactions.cpp b/main/actions/ledstripblinkactions.cpp index e69de29..8d903b8 100644 --- a/main/actions/ledstripblinkactions.cpp +++ b/main/actions/ledstripblinkactions.cpp @@ -0,0 +1,42 @@ +#include "ledstripblinkactions.h" + +#ifdef FEATURE_LEDSTRIP +#include "ledstrip.h" +#include "ledstripdefines.h" + +using namespace espgui; + +void LedstripAnimationBlinkNoneAction::triggered() +{ + blinkAnimation = LEDSTRIP_OVERWRITE_NONE; +} + +#ifndef LEDSTRIP_WRONG_DIRECTION +void LedstripAnimationBlinkLeftAction::triggered() +{ + blinkAnimation = LEDSTRIP_OVERWRITE_BLINKLEFT; +} +#else +void LedstripAnimationBlinkLeftAction::triggered() +{ + blinkAnimation = LEDSTRIP_OVERWRITE_BLINKRIGHT; +} +#endif + +#ifndef LEDSTRIP_WRONG_DIRECTION +void LedstripAnimationBlinkRightAction::triggered() +{ + blinkAnimation = LEDSTRIP_OVERWRITE_BLINKRIGHT; +} +#else +void LedstripAnimationBlinkRightAction::triggered() +{ + blinkAnimation = LEDSTRIP_OVERWRITE_BLINKLEFT; +} +#endif + +void LedstripAnimationBlinkBothAction::triggered() +{ + blinkAnimation = LEDSTRIP_OVERWRITE_BLINKBOTH; +} +#endif diff --git a/main/actions/ledstripblinkactions.h b/main/actions/ledstripblinkactions.h index f22efa5..4ea7541 100644 --- a/main/actions/ledstripblinkactions.h +++ b/main/actions/ledstripblinkactions.h @@ -1,53 +1,37 @@ #pragma once -#include "actioninterface.h" -#include "utils.h" -#include "globals.h" -#include "ledstrip.h" -#include "ledstripdefines.h" - -using namespace espgui; - #ifdef FEATURE_LEDSTRIP -namespace { -class LedstripAnimationBlinkNoneAction : public virtual ActionInterface +#include "actioninterface.h" + +class LedstripAnimationBlinkNoneAction : public virtual espgui::ActionInterface { public: - void triggered() override { blinkAnimation = LEDSTRIP_OVERWRITE_NONE; } + void triggered() override; }; -class LedstripAnimationBlinkLeftAction : public virtual ActionInterface +class LedstripAnimationBlinkLeftAction : public virtual espgui::ActionInterface { public: #ifndef LEDSTRIP_WRONG_DIRECTION - void triggered() override { - blinkAnimation = LEDSTRIP_OVERWRITE_BLINKLEFT; - } + void triggered() override; #else - void triggered() override { - blinkAnimation = LEDSTRIP_OVERWRITE_BLINKRIGHT; - } + void triggered() override; #endif }; -class LedstripAnimationBlinkRightAction : public virtual ActionInterface +class LedstripAnimationBlinkRightAction : public virtual espgui::ActionInterface { public: #ifndef LEDSTRIP_WRONG_DIRECTION - void triggered() override { - blinkAnimation = LEDSTRIP_OVERWRITE_BLINKRIGHT; - } + void triggered() override; #else - void triggered() override { - blinkAnimation = LEDSTRIP_OVERWRITE_BLINKLEFT; - } + void triggered() override; #endif }; -class LedstripAnimationBlinkBothAction : public virtual ActionInterface +class LedstripAnimationBlinkBothAction : public virtual espgui::ActionInterface { public: - void triggered() override { blinkAnimation = LEDSTRIP_OVERWRITE_BLINKBOTH; } + void triggered() override; }; -} #endif diff --git a/main/displays/bmsdisplay.cpp b/main/displays/bmsdisplay.cpp index e69de29..bf89591 100644 --- a/main/displays/bmsdisplay.cpp +++ b/main/displays/bmsdisplay.cpp @@ -0,0 +1,105 @@ +#include "bmsdisplay.h" + +#if defined(FEATURE_BLUETOOTH) && defined(FEATURE_BMS) +#include "displays/menus/mainmenu.h" +#include "displays/metersdisplay.h" +#include "displays/statusdisplay.h" +#include "screenmanager.h" +#include "tftinstance.h" + +using namespace espgui; + +void BmsDisplay::initScreen() +{ + tft.fillScreen(TFT_BLACK); + tft.setTextColor(TFT_WHITE, TFT_BLACK); + + tft.setTextFont(2); + m_statusLabel.start(); + m_statusLabel.redraw("init"); + + tft.setTextFont(4); + tft.drawString("Voltage:", 0, m_voltageLabel.y()); + m_voltageLabel.start(); + tft.drawString("Capacity:", 0, m_capacityLabel.y()); + m_capacityLabel.start(); + tft.drawString("SOC:", 0, m_socLabel.y()); + m_socLabel.start(); + tft.drawString("Power:", 0, m_powerLabel.y()); + m_powerLabel.start(); + tft.drawString("Current:", 0, m_currentLabel.y()); + m_currentLabel.start(); + tft.drawString("Speed:", 0, m_speedLabel.y()); + m_speedLabel.start(); + tft.drawString("PpS:", 0, m_powerPerSpeedLabel.y()); + m_powerPerSpeedLabel.start(); + + for (auto &label : m_battLabels) + label.start(); + + tft.drawString("Cycle:", 0, m_cycleLabel.y()); + m_cycleLabel.start(); +} + +void BmsDisplay::redraw() +{ + if (bluetoothSerial.hasClient()) + tft.setTextColor(TFT_GREEN, TFT_BLACK); + else + { + tft.setTextColor(TFT_RED, TFT_BLACK); + tft.setTextFont(2); + } + + m_statusLabel.redraw(bluetoothSerial.hasClient() ? "OK" : "FAIL"); + + tft.setTextColor(TFT_WHITE, TFT_BLACK); + if (!bluetoothSerial.hasClient()) + tft.setTextFont(4); + + if (bluetoothSerial.hasClient()) + { + m_voltageLabel.redraw(fmt::format("{:.02f}V", bms::voltage)); + m_capacityLabel.redraw(fmt::format("{:.02f} mAh", bms::capacity)); + m_socLabel.redraw(fmt::format("{:.02f}%", bms::soc)); + m_powerLabel.redraw(fmt::format("{:.02f}W", bms::power)); + m_currentLabel.redraw(fmt::format("{:.02f}A", bms::current)); + } + else + { + m_voltageLabel.clear(); + m_capacityLabel.clear(); + m_socLabel.clear(); + m_powerLabel.clear(); + m_currentLabel.clear(); + } + + m_speedLabel.redraw(fmt::format("{:.02f}kmh", avgSpeedKmh)); + + if (bluetoothSerial.hasClient()) + m_powerPerSpeedLabel.redraw(fmt::format("{:.02f}W/kmh", avgSpeedKmh < 1 ? 0 : bms::power / avgSpeedKmh)); + else + m_powerPerSpeedLabel.clear(); + + for (int i = 0; i < 12; i++) + m_battLabels[i].redraw(fmt::format("{:.02f}", bms::batt[i])); + + if (bluetoothSerial.hasClient()) + m_cycleLabel.redraw(fmt::format("{:.02f}AH", bms::cycle)); + else + m_cycleLabel.clear(); +} + +void BmsDisplay::rotate(int offset) +{ + if (offset < 0) + switchScreen(); + else if (offset > 0) + switchScreen(); +} + +void BmsDisplay::confirm() +{ + switchScreen(); +} +#endif diff --git a/main/displays/bmsdisplay.h b/main/displays/bmsdisplay.h index a9b60c2..46701cc 100644 --- a/main/displays/bmsdisplay.h +++ b/main/displays/bmsdisplay.h @@ -1,5 +1,6 @@ #pragma once +#if defined(FEATURE_BLUETOOTH) && defined(FEATURE_BMS) // 3rdparty lib includes #include @@ -11,122 +12,32 @@ #include "widgets/label.h" #include "screenmanager.h" -namespace { -#if defined(FEATURE_BLUETOOTH) && defined(FEATURE_BMS) -class BmsDisplay : public Display, public ConfirmActionInterface>, public DummyBack +class BmsDisplay : + public espgui::Display, + public espgui::DummyBack { public: void initScreen() override; void redraw() override; - + void confirm() override; void rotate(int offset) override; - Label m_statusLabel{200, 0}; + espgui::Label m_statusLabel{200, 0}; - Label m_voltageLabel{107, 0}; - Label m_capacityLabel{107, 25}; - Label m_socLabel{107, 50}; - Label m_powerLabel{107, 75}; - Label m_currentLabel{107, 100}; - Label m_speedLabel{107, 125}; - Label m_powerPerSpeedLabel{107, 150}; + espgui::Label m_voltageLabel{107, 0}; + espgui::Label m_capacityLabel{107, 25}; + espgui::Label m_socLabel{107, 50}; + espgui::Label m_powerLabel{107, 75}; + espgui::Label m_currentLabel{107, 100}; + espgui::Label m_speedLabel{107, 125}; + espgui::Label m_powerPerSpeedLabel{107, 150}; - std::array m_battLabels{{ - Label{0, 225}, Label{60, 225}, Label{120, 225}, Label{180, 225}, - Label{0, 250}, Label{60, 250}, Label{120, 250}, Label{180, 250}, - Label{0, 275}, Label{60, 275}, Label{120, 275}, Label{180, 275} + std::array m_battLabels{{ + espgui::Label{0, 225}, espgui::Label{60, 225}, espgui::Label{120, 225}, espgui::Label{180, 225}, + espgui::Label{0, 250}, espgui::Label{60, 250}, espgui::Label{120, 250}, espgui::Label{180, 250}, + espgui::Label{0, 275}, espgui::Label{60, 275}, espgui::Label{120, 275}, espgui::Label{180, 275} }}; - Label m_cycleLabel{105, 300}; + espgui::Label m_cycleLabel{105, 300}; }; - -void BmsDisplay::initScreen() -{ - tft.fillScreen(TFT_BLACK); - tft.setTextColor(TFT_WHITE, TFT_BLACK); - - tft.setTextFont(2); - m_statusLabel.start(); - m_statusLabel.redraw("init"); - - tft.setTextFont(4); - tft.drawString("Voltage:", 0, m_voltageLabel.y()); - m_voltageLabel.start(); - tft.drawString("Capacity:", 0, m_capacityLabel.y()); - m_capacityLabel.start(); - tft.drawString("SOC:", 0, m_socLabel.y()); - m_socLabel.start(); - tft.drawString("Power:", 0, m_powerLabel.y()); - m_powerLabel.start(); - tft.drawString("Current:", 0, m_currentLabel.y()); - m_currentLabel.start(); - tft.drawString("Speed:", 0, m_speedLabel.y()); - m_speedLabel.start(); - tft.drawString("PpS:", 0, m_powerPerSpeedLabel.y()); - m_powerPerSpeedLabel.start(); - - for (auto &label : m_battLabels) - label.start(); - - tft.drawString("Cycle:", 0, m_cycleLabel.y()); - m_cycleLabel.start(); -} - -void BmsDisplay::redraw() -{ - if (bluetoothSerial.hasClient()) - tft.setTextColor(TFT_GREEN, TFT_BLACK); - else - { - tft.setTextColor(TFT_RED, TFT_BLACK); - tft.setTextFont(2); - } - - m_statusLabel.redraw(bluetoothSerial.hasClient() ? "OK" : "FAIL"); - - tft.setTextColor(TFT_WHITE, TFT_BLACK); - if (!bluetoothSerial.hasClient()) - tft.setTextFont(4); - - if (bluetoothSerial.hasClient()) - { - m_voltageLabel.redraw(fmt::format("{:.02f}V", bms::voltage)); - m_capacityLabel.redraw(fmt::format("{:.02f} mAh", bms::capacity)); - m_socLabel.redraw(fmt::format("{:.02f}%", bms::soc)); - m_powerLabel.redraw(fmt::format("{:.02f}W", bms::power)); - m_currentLabel.redraw(fmt::format("{:.02f}A", bms::current)); - } - else - { - m_voltageLabel.clear(); - m_capacityLabel.clear(); - m_socLabel.clear(); - m_powerLabel.clear(); - m_currentLabel.clear(); - } - - m_speedLabel.redraw(fmt::format("{:.02f}kmh", avgSpeedKmh)); - - if (bluetoothSerial.hasClient()) - m_powerPerSpeedLabel.redraw(fmt::format("{:.02f}W/kmh", avgSpeedKmh < 1 ? 0 : bms::power / avgSpeedKmh)); - else - m_powerPerSpeedLabel.clear(); - - for (int i = 0; i < 12; i++) - m_battLabels[i].redraw(fmt::format("{:.02f}", bms::batt[i])); - - if (bluetoothSerial.hasClient()) - m_cycleLabel.redraw(fmt::format("{:.02f}AH", bms::cycle)); - else - m_cycleLabel.clear(); -} - -void BmsDisplay::rotate(int offset) -{ - if (offset < 0) - switchScreen(); - else if (offset > 0) - switchScreen(); -} #endif -} diff --git a/main/displays/menus/accesspointwifisettingsmenu.cpp b/main/displays/menus/accesspointwifisettingsmenu.cpp index e69de29..a37981e 100644 --- a/main/displays/menus/accesspointwifisettingsmenu.cpp +++ b/main/displays/menus/accesspointwifisettingsmenu.cpp @@ -0,0 +1,33 @@ +#include "accesspointwifisettingsmenu.h" + +// local includes +#include "accessors/settingsaccessors.h" +#include "actions/dummyaction.h" +#include "actions/switchscreenaction.h" +#include "actions/toggleboolaction.h" +#include "checkboxicon.h" +#include "displays/menus/wifisettingsmenu.h" +#include "icons/back.h" +#include "menuitem.h" +#include "wifitexthelpers.h" + +using namespace espgui; + +AccessPointWifiSettingsMenu::AccessPointWifiSettingsMenu() +{ + constructMenuItem, ToggleBoolAction, CheckboxIcon, WifiApEnabledAccessor>>(); + constructMenuItem, DisabledColor, DummyAction>>(); + constructMenuItem, DisabledColor, DummyAction>>(); + constructMenuItem, DisabledColor, DummyAction>>(); + constructMenuItem, DisabledColor, DummyAction>>(); + constructMenuItem, DisabledColor, DummyAction>>(); + constructMenuItem, DisabledColor, DummyAction>>(); + constructMenuItem, DisabledColor, DummyAction>>(); + constructMenuItem, DisabledColor, DummyAction>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); +} + +void AccessPointWifiSettingsMenu::back() +{ + switchScreen(); +} diff --git a/main/displays/menus/accesspointwifisettingsmenu.h b/main/displays/menus/accesspointwifisettingsmenu.h index 11be9f9..49c39f8 100644 --- a/main/displays/menus/accesspointwifisettingsmenu.h +++ b/main/displays/menus/accesspointwifisettingsmenu.h @@ -2,37 +2,13 @@ // local includes #include "menudisplay.h" -#include "menuitem.h" -#include "actions/dummyaction.h" -#include "actions/switchscreenaction.h" -#include "actions/toggleboolaction.h" -#include "checkboxicon.h" -#include "icons/back.h" -#include "wifitexthelpers.h" -#include "accessors/settingsaccessors.h" #include "texts.h" -using namespace espgui; - -namespace { class AccessPointWifiSettingsMenu : - public MenuDisplay, - public StaticText, - public BackActionInterface> + public espgui::MenuDisplay, + public espgui::StaticText { public: - AccessPointWifiSettingsMenu() - { - constructMenuItem, ToggleBoolAction, CheckboxIcon, WifiApEnabledAccessor>>(); - constructMenuItem, DisabledColor, DummyAction>>(); - constructMenuItem, DisabledColor, DummyAction>>(); - constructMenuItem, DisabledColor, DummyAction>>(); - constructMenuItem, DisabledColor, DummyAction>>(); - constructMenuItem, DisabledColor, DummyAction>>(); - constructMenuItem, DisabledColor, DummyAction>>(); - constructMenuItem, DisabledColor, DummyAction>>(); - constructMenuItem, DisabledColor, DummyAction>>(); - constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); - } + AccessPointWifiSettingsMenu(); + void back() override; }; -} // namespace diff --git a/main/displays/menus/feedbackdebugmenu.h b/main/displays/menus/feedbackdebugmenu.h index 5e1fb06..92104a6 100644 --- a/main/displays/menus/feedbackdebugmenu.h +++ b/main/displays/menus/feedbackdebugmenu.h @@ -10,6 +10,7 @@ #include "texts.h" #include "debugtexthelpers.h" #include "debugcolorhelpers.h" +#include "displays/menus/debugmenu.h" using namespace espgui; diff --git a/main/displays/menus/ledstripmenu.cpp b/main/displays/menus/ledstripmenu.cpp index cdeba51..e5d99f1 100644 --- a/main/displays/menus/ledstripmenu.cpp +++ b/main/displays/menus/ledstripmenu.cpp @@ -1,28 +1,30 @@ #include "ledstripmenu.h" +#ifdef FEATURE_LEDSTRIP // 3rdparty lib includes #include -#include "menuitem.h" -#include "actions/toggleboolaction.h" -#include "actions/switchscreenaction.h" -#include "icons/back.h" -#include "checkboxicon.h" -#include "changevaluedisplay.h" -#include "actioninterface.h" +#include +#include +#include +#include +#include +#include +#include // local includes +#include "accessors/settingsaccessors.h" +#include "displays/ledstripcolorsdisplay.h" +#include "displays/menus/ledstripselectotamode.h" +#include "displays/menus/mainmenu.h" +#include "globals.h" +#include "ledstrip.h" #include "ledstripselectanimationmenu.h" #include "ledstripselectblinkmenu.h" -#include "globals.h" -#include "accessors/settingsaccessors.h" -#ifdef FEATURE_LEDSTRIP -#include "ledstrip.h" -#include "displays/menus/ledstripselectotamode.h" -#endif -#include "displays/ledstripcolorsdisplay.h" -#include "displays/menus/mainmenu.h" -#ifdef FEATURE_LEDSTRIP +// clang-format off + +using namespace espgui; + namespace { using LedsCountChangeScreen = makeComponent< ChangeValueDisplay, @@ -109,8 +111,6 @@ public: }; } // namespace -using namespace espgui; - LedstripMenu::LedstripMenu() { constructMenuItem, SwitchScreenAction>>(); diff --git a/main/displays/menus/ledstripselectblinkmenu.cpp b/main/displays/menus/ledstripselectblinkmenu.cpp index e69de29..6e44234 100644 --- a/main/displays/menus/ledstripselectblinkmenu.cpp +++ b/main/displays/menus/ledstripselectblinkmenu.cpp @@ -0,0 +1,53 @@ +#include "ledstripselectblinkmenu.h" + +#ifdef FEATURE_LEDSTRIP +// Local includes +#include "accessors/settingsaccessors.h" +#include "actions/dummyaction.h" +#include "actions/ledstripblinkactions.h" +#include "actions/switchscreenaction.h" +#include "actions/toggleboolaction.h" +#include "checkboxicon.h" +#include "displays/menus/ledstripmenu.h" +#include "icons/back.h" + +using namespace espgui; + +std::string currentSelectedBlinkAnimationText::text() const { + switch (blinkAnimation) { + case LEDSTRIP_OVERWRITE_BLINKLEFT: +#ifndef LEDSTRIP_WRONG_DIRECTION + return TEXT_ANIMATION_BLINKLEFT; +#else + return TEXT_ANIMATION_BLINKRIGHT; +#endif + case LEDSTRIP_OVERWRITE_BLINKRIGHT: +#ifndef LEDSTRIP_WRONG_DIRECTION + return TEXT_ANIMATION_BLINKRIGHT; +#else + return TEXT_ANIMATION_BLINKLEFT; +#endif + case LEDSTRIP_OVERWRITE_BLINKBOTH: + return TEXT_ANIMATION_BLINKBOTH; + default: + return TEXT_ANIMATION_BLINKNONE; + } +} + +LedstripSelectBlinkMenu::LedstripSelectBlinkMenu() +{ + constructMenuItem>(); + constructMenuItem>(); + constructMenuItem, LedstripAnimationBlinkNoneAction>>(); + constructMenuItem, LedstripAnimationBlinkLeftAction>>(); + constructMenuItem, LedstripAnimationBlinkRightAction>>(); + constructMenuItem, LedstripAnimationBlinkBothAction>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, LedstripEnableBlinkAnimationAccessor>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); +} + +void LedstripSelectBlinkMenu::back() +{ + switchScreen(); +} +#endif diff --git a/main/displays/menus/ledstripselectblinkmenu.h b/main/displays/menus/ledstripselectblinkmenu.h index ca37a95..1819c14 100644 --- a/main/displays/menus/ledstripselectblinkmenu.h +++ b/main/displays/menus/ledstripselectblinkmenu.h @@ -2,64 +2,22 @@ // Local includes #include "menudisplay.h" -#include "utils.h" -#include "menuitem.h" -#include "ledstrip.h" -#include "ledstripselectanimationmenu.h" -#include "icons/back.h" #include "texts.h" -#include "actions/dummyaction.h" -#include "actions/ledstripblinkactions.h" -#include "actions/switchscreenaction.h" -#include "actions/toggleboolaction.h" -#include "checkboxicon.h" +#include "ledstrip.h" #include "ledstripdefines.h" -#include "accessors/settingsaccessors.h" -#include "ledstripmenu.h" #ifdef FEATURE_LEDSTRIP -class currentSelectedBlinkAnimationText : public virtual TextInterface { public: std::string text() const override { - switch (blinkAnimation) { - case LEDSTRIP_OVERWRITE_BLINKLEFT: -#ifndef LEDSTRIP_WRONG_DIRECTION - return TEXT_ANIMATION_BLINKLEFT; -#else - return TEXT_ANIMATION_BLINKRIGHT; -#endif - case LEDSTRIP_OVERWRITE_BLINKRIGHT: -#ifndef LEDSTRIP_WRONG_DIRECTION - return TEXT_ANIMATION_BLINKRIGHT; -#else - return TEXT_ANIMATION_BLINKLEFT; -#endif - case LEDSTRIP_OVERWRITE_BLINKBOTH: - return TEXT_ANIMATION_BLINKBOTH; - default: - return TEXT_ANIMATION_BLINKNONE; - } - }; +class currentSelectedBlinkAnimationText : public virtual espgui::TextInterface +{ +public: std::string text() const override; }; -using namespace espgui; - -namespace { - class LedstripSelectBlinkMenu : - public MenuDisplay, - public StaticText, - public BackActionInterface> - { - public: - LedstripSelectBlinkMenu() - { - constructMenuItem>(); - constructMenuItem>(); - constructMenuItem, LedstripAnimationBlinkNoneAction>>(); - constructMenuItem, LedstripAnimationBlinkLeftAction>>(); - constructMenuItem, LedstripAnimationBlinkRightAction>>(); - constructMenuItem, LedstripAnimationBlinkBothAction>>(); - constructMenuItem, ToggleBoolAction, CheckboxIcon, LedstripEnableBlinkAnimationAccessor>>(); - constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); - } - }; -} // Namespace +class LedstripSelectBlinkMenu : + public espgui::MenuDisplay, + public espgui::StaticText +{ +public: + LedstripSelectBlinkMenu(); + void back() override; +}; #endif diff --git a/main/displays/menus/ledstripselectotamode.cpp b/main/displays/menus/ledstripselectotamode.cpp index e69de29..97ce863 100644 --- a/main/displays/menus/ledstripselectotamode.cpp +++ b/main/displays/menus/ledstripselectotamode.cpp @@ -0,0 +1,24 @@ +#include "ledstripselectotamode.h" + +#include "actions/switchscreenaction.h" +#include "icons/back.h" +#include "ledstripmenu.h" + +// Local includes + +#if defined(FEATURE_LEDSTRIP) && defined(FEATURE_OTA) +using namespace espgui; + +ledstripOtaAnimationChangeMenu::ledstripOtaAnimationChangeMenu() +{ + constructMenuItem, LedstripChangeOtaAnimModeAction>>(); + constructMenuItem, LedstripChangeOtaAnimModeAction>>(); + constructMenuItem, LedstripChangeOtaAnimModeAction>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); +} + +void ledstripOtaAnimationChangeMenu::back() +{ + switchScreen(); +} +#endif diff --git a/main/displays/menus/ledstripselectotamode.h b/main/displays/menus/ledstripselectotamode.h index f49e54d..02ffc51 100644 --- a/main/displays/menus/ledstripselectotamode.h +++ b/main/displays/menus/ledstripselectotamode.h @@ -2,20 +2,15 @@ // Local includes #include "menudisplay.h" -#include "utils.h" -#include "menuitem.h" -#include "ledstrip.h" -#include "icons/back.h" #include "texts.h" -#include "actions/switchscreenaction.h" -#include "accessors/settingsaccessors.h" -#include "ledstripmenu.h" +#include "ledstrip.h" +#include "globals.h" +#include "utils.h" #if defined(FEATURE_LEDSTRIP) && defined(FEATURE_OTA) -using namespace espgui; template -class LedstripChangeOtaAnimModeAction : public virtual ActionInterface +class LedstripChangeOtaAnimModeAction : public virtual espgui::ActionInterface { public: void triggered() override @@ -25,20 +20,12 @@ public: } }; -namespace { - class ledstripOtaAnimationChangeMenu : - public MenuDisplay, - public StaticText, - public BackActionInterface> - { - public: - ledstripOtaAnimationChangeMenu() - { - constructMenuItem, LedstripChangeOtaAnimModeAction>>(); - constructMenuItem, LedstripChangeOtaAnimModeAction>>(); - constructMenuItem, LedstripChangeOtaAnimModeAction>>(); - constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); - } - }; -} // Namespace +class ledstripOtaAnimationChangeMenu : + public espgui::MenuDisplay, + public espgui::StaticText +{ +public: + ledstripOtaAnimationChangeMenu(); + void back() override; +}; #endif diff --git a/main/displays/menus/mosfetsmenu.cpp b/main/displays/menus/mosfetsmenu.cpp index e69de29..2c2073d 100644 --- a/main/displays/menus/mosfetsmenu.cpp +++ b/main/displays/menus/mosfetsmenu.cpp @@ -0,0 +1,26 @@ +#include "mosfetsmenu.h" + +#ifdef FEATURE_MOSFETS + +#include "actions/switchscreenaction.h" +#include "actions/toggleboolaction.h" +#include "checkboxicon.h" +#include "displays/menus/mainmenu.h" +#include "icons/back.h" + +using namespace espgui; + +MosfetsMenu::MosfetsMenu() +{ + constructMenuItem, ToggleBoolAction, CheckboxIcon, Mosfet0Accessor>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, Mosfet1Accessor>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, Mosfet2Accessor>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); +} + +void MosfetsMenu::back() +{ + switchScreen(); +} + +#endif diff --git a/main/displays/menus/mosfetsmenu.h b/main/displays/menus/mosfetsmenu.h index 92e7587..ee1b88b 100644 --- a/main/displays/menus/mosfetsmenu.h +++ b/main/displays/menus/mosfetsmenu.h @@ -1,22 +1,13 @@ #pragma once -// local includes +#ifdef FEATURE_MOSFETS #include "menudisplay.h" -#include "utils.h" -#include "menuitem.h" -#include "actions/toggleboolaction.h" -#include "actions/switchscreenaction.h" -#include "checkboxicon.h" -#include "icons/back.h" +#include "accessorinterface.h" #include "texts.h" #include "types.h" -using namespace espgui; - -namespace { -#ifdef FEATURE_MOSFETS template -class GPIOAccessor : public virtual AccessorInterface +class GPIOAccessor : public virtual espgui::AccessorInterface { public: bool getValue() const override { return digitalRead(PIN); } @@ -28,18 +19,11 @@ using Mosfet1Accessor = GPIOAccessor; using Mosfet2Accessor = GPIOAccessor; class MosfetsMenu : - public MenuDisplay, - public StaticText, - public BackActionInterface> + public espgui::MenuDisplay, + public espgui::StaticText { public: - MosfetsMenu() - { - constructMenuItem, ToggleBoolAction, CheckboxIcon, Mosfet0Accessor>>(); - constructMenuItem, ToggleBoolAction, CheckboxIcon, Mosfet1Accessor>>(); - constructMenuItem, ToggleBoolAction, CheckboxIcon, Mosfet2Accessor>>(); - constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); - } + MosfetsMenu(); + void back() override; }; #endif -} // namespace From aabae8b8b6faf71b7ccb0fdcf3a4e553743b4cf5 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 10:06:01 +0100 Subject: [PATCH 14/36] Refactored settingsutils --- main/settingsutils.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++ main/settingsutils.h | 42 ++------------------------------------- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/main/settingsutils.cpp b/main/settingsutils.cpp index e69de29..1960411 100644 --- a/main/settingsutils.cpp +++ b/main/settingsutils.cpp @@ -0,0 +1,45 @@ +#include "settingsutils.h" + +// esp-idf includes +#include + +// local includes +#include "globals.h" +#include "presets.h" + +namespace { +void switchProfile(uint8_t index) +{ +#ifdef SIMPLIFIED_TRIGGER_TRIGGERONPRESET + if (index == SIMPLIFIED_TRIGGER_TRIGGERONPRESET) + { + simplified = true; +#ifdef SETTINGSUTILS_PLUGIN +#include SETTINGSUTILS_PLUGIN +#endif + return; + } +#endif + + settings = presets::defaultSettings; + stringSettings = presets::makeDefaultStringSettings(); + + if (!settingsPersister.openProfile(index)) + { + ESP_LOGE("BOBBY", "openProfile() failed"); + return; + } + + if (!settingsPersister.load(settings)) + { + ESP_LOGE("BOBBY", "load() for settings failed"); + return; + } + + if (!settingsPersister.load(stringSettings)) + { + ESP_LOGE("BOBBY", "load() for stringSettings failed"); + return; + } +} +} // namespace diff --git a/main/settingsutils.h b/main/settingsutils.h index a72191e..84c5b79 100644 --- a/main/settingsutils.h +++ b/main/settingsutils.h @@ -1,45 +1,7 @@ #pragma once -// esp-idf includes -#include - -// local includes -#include "globals.h" -#include "presets.h" +#include namespace { -void switchProfile(uint8_t index) -{ -#ifdef SIMPLIFIED_TRIGGER_TRIGGERONPRESET - if (index == SIMPLIFIED_TRIGGER_TRIGGERONPRESET) - { - simplified = true; -#ifdef SETTINGSUTILS_PLUGIN -#include SETTINGSUTILS_PLUGIN -#endif - return; - } -#endif - - settings = presets::defaultSettings; - stringSettings = presets::makeDefaultStringSettings(); - - if (!settingsPersister.openProfile(index)) - { - ESP_LOGE("BOBBY", "openProfile() failed"); - return; - } - - if (!settingsPersister.load(settings)) - { - ESP_LOGE("BOBBY", "load() for settings failed"); - return; - } - - if (!settingsPersister.load(stringSettings)) - { - ESP_LOGE("BOBBY", "load() for stringSettings failed"); - return; - } -} +void switchProfile(uint8_t index); } From 24607b3a0c2b1b83d367030db104d27a978283bc Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 11:51:10 +0100 Subject: [PATCH 15/36] Fixed compiling issues --- main/actions/switchprofileaction.h | 2 +- main/buttons.cpp | 4 +++- main/can.cpp | 2 ++ main/settingsutils.cpp | 2 +- main/settingsutils.h | 2 +- main/webserver_dumpnvs.cpp | 4 ++-- 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/main/actions/switchprofileaction.h b/main/actions/switchprofileaction.h index 735bf06..e321ca4 100644 --- a/main/actions/switchprofileaction.h +++ b/main/actions/switchprofileaction.h @@ -12,7 +12,7 @@ class SwitchProfileAction : public virtual ActionInterface public: void triggered() override { - switchProfile(index); + settingsutils::switchProfile(index); } }; } diff --git a/main/buttons.cpp b/main/buttons.cpp index 5879359..0d45dbf 100644 --- a/main/buttons.cpp +++ b/main/buttons.cpp @@ -1,6 +1,8 @@ #include "buttons.h" #include "modes/defaultmode.h" +using namespace std::chrono_literals; + int rotated{}; bool requestFullRedraw{}; @@ -118,7 +120,7 @@ void InputDispatcher::profileButton(uint8_t index, bool pressed) if (profileButtonDisabled) return; - switchProfile(index); + settingsutils::switchProfile(index); } #ifdef SWITCH_BLINK diff --git a/main/can.cpp b/main/can.cpp index 09d955e..aa9cb82 100644 --- a/main/can.cpp +++ b/main/can.cpp @@ -19,6 +19,8 @@ #include "globals.h" #include "buttons.h" +using namespace std::chrono_literals; + namespace can { namespace { constexpr const char * const TAG = "BOBBYCAN"; diff --git a/main/settingsutils.cpp b/main/settingsutils.cpp index 1960411..87b64bb 100644 --- a/main/settingsutils.cpp +++ b/main/settingsutils.cpp @@ -7,7 +7,7 @@ #include "globals.h" #include "presets.h" -namespace { +namespace settingsutils { void switchProfile(uint8_t index) { #ifdef SIMPLIFIED_TRIGGER_TRIGGERONPRESET diff --git a/main/settingsutils.h b/main/settingsutils.h index 84c5b79..54dba3d 100644 --- a/main/settingsutils.h +++ b/main/settingsutils.h @@ -2,6 +2,6 @@ #include -namespace { +namespace settingsutils { void switchProfile(uint8_t index); } diff --git a/main/webserver_dumpnvs.cpp b/main/webserver_dumpnvs.cpp index 506a5af..0e75496 100644 --- a/main/webserver_dumpnvs.cpp +++ b/main/webserver_dumpnvs.cpp @@ -148,7 +148,7 @@ esp_err_t webserver_dump_nvs_handler(httpd_req_t *req) continue; } #endif - switchProfile(profile_num); + settingsutils::switchProfile(profile_num); const auto cur_profile = settingsPersister.currentlyOpenProfileIndex(); const auto profile_str = cur_profile ? std::to_string(*cur_profile) : "-"; @@ -166,7 +166,7 @@ esp_err_t webserver_dump_nvs_handler(httpd_req_t *req) }); } - switchProfile(switchBackProfile); + settingsutils::switchProfile(switchBackProfile); std::string body; serializeJson(doc, body); From 5d424a43775c995c4bd7fe2f20a024999e67f04e Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 12:23:54 +0100 Subject: [PATCH 16/36] Fixed chrono literals --- main/webserver.cpp | 2 ++ main/webserver_displaycontrol.cpp | 1 + main/webserver_dumpnvs.cpp | 1 + 3 files changed, 4 insertions(+) diff --git a/main/webserver.cpp b/main/webserver.cpp index 233a1f2..e9fe6e2 100644 --- a/main/webserver.cpp +++ b/main/webserver.cpp @@ -1,6 +1,8 @@ #include "webserver.h" #include "sdkconfig.h" +using namespace std::chrono_literals; + #ifdef FEATURE_WEBSERVER namespace { constexpr const char * const TAG = "BOBBYWEB"; diff --git a/main/webserver_displaycontrol.cpp b/main/webserver_displaycontrol.cpp index d7757b7..78f96f8 100644 --- a/main/webserver_displaycontrol.cpp +++ b/main/webserver_displaycontrol.cpp @@ -2,6 +2,7 @@ #ifdef FEATURE_WEBSERVER using esphttpdutils::HtmlTag; +using namespace std::chrono_literals; namespace { constexpr const char * const TAG = "BOBBYWEB"; diff --git a/main/webserver_dumpnvs.cpp b/main/webserver_dumpnvs.cpp index 0e75496..8750640 100644 --- a/main/webserver_dumpnvs.cpp +++ b/main/webserver_dumpnvs.cpp @@ -2,6 +2,7 @@ using esphttpdutils::HtmlTag; using namespace espchrono; +using namespace std::chrono_literals; namespace { constexpr const char * const TAG = "BOBBYWEB"; From 66e40248fe1f2bf18889fa51220984e9bd41f537 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 14:16:53 +0100 Subject: [PATCH 17/36] Do not accept update if time is already set by ntp --- main/espnowfunctions.cpp | 23 +++++++++++++++++++++++ main/espnowfunctions.h | 8 ++++++++ 2 files changed, 31 insertions(+) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index 775bc88..1b5f25f 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -10,9 +10,20 @@ constexpr const char * const TAG = "BOBBY_ESP_NOW"; #include "globals.h" #include "utils.h" +#include "time_bobbycar.h" namespace espnow { +uint16_t lastYear; // Used for esp-now timesync + +esp_now_settings_t::Enable DefaultEnable { + .receiveTimeStamp = true, +}; + +esp_now_settings_t esp_now_settings{ + .enable = DefaultEnable, +}; + std::deque message_queue{}; std::list peers{}; uint8_t initialized{0}; @@ -110,6 +121,15 @@ void handle() return; } + if (esp_now_settings.enable.receiveTimeStamp) + { + const auto thisYear = int(espchrono::toDateTime(espchrono::utc_clock::now()).date.year()); + if (abs(thisYear - espnow::lastYear) > 1) + { + esp_now_settings.enable.receiveTimeStamp = false; + } + } + if(message_queue.size()) { for (const esp_now_message_t &msg : message_queue) @@ -119,6 +139,9 @@ void handle() if (msg.type == "T") { + if (!esp_now_settings.enable.receiveTimeStamp) + return; + if (const auto result = cpputils::fromString(msg.content); result) { onRecvTs(*result); diff --git a/main/espnowfunctions.h b/main/espnowfunctions.h index 5d7794b..512d8dd 100644 --- a/main/espnowfunctions.h +++ b/main/espnowfunctions.h @@ -7,12 +7,20 @@ #include namespace espnow { +extern uint16_t lastYear; constexpr const uint8_t broadcast_address[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; struct esp_now_message_t { const std::string content; const std::string type; }; +struct esp_now_settings_t { + struct Enable { + bool receiveTimeStamp; + } enable; +} +extern esp_now_settings; + extern std::deque message_queue; extern std::list peers; From 42067b1a3b2c9a64f792cfd2db441c8669419737 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 15:23:58 +0100 Subject: [PATCH 18/36] Added esp now menu and timesync between bobbycars --- main/CMakeLists.txt | 2 ++ main/displays/menus/espnowmenu.cpp | 28 +++++++++++++++++++ main/displays/menus/espnowmenu.h | 42 ++++++++++++++++++++++++++++ main/displays/menus/settingsmenu.cpp | 4 +++ main/espnowfunctions.cpp | 23 +++++++-------- main/espnowfunctions.h | 8 ++---- main/texts.cpp | 6 ++++ main/texts.h | 7 +++++ 8 files changed, 103 insertions(+), 17 deletions(-) create mode 100644 main/displays/menus/espnowmenu.cpp create mode 100644 main/displays/menus/espnowmenu.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index ec7fc45..a136156 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -77,6 +77,7 @@ set(headers displays/menus/demosmenu.h displays/menus/dynamicdebugmenu.h displays/menus/enablemenu.h + displays/menus/espnowmenu.h displays/menus/feedbackdebugmenu.h displays/menus/gametrakmodesettingsmenu.h displays/menus/genericwifisettingsmenu.h @@ -280,6 +281,7 @@ set(sources displays/menus/demosmenu.cpp displays/menus/dynamicdebugmenu.cpp displays/menus/enablemenu.cpp + displays/menus/espnowmenu.cpp displays/menus/feedbackdebugmenu.cpp displays/menus/gametrakmodesettingsmenu.cpp displays/menus/genericwifisettingsmenu.cpp diff --git a/main/displays/menus/espnowmenu.cpp b/main/displays/menus/espnowmenu.cpp new file mode 100644 index 0000000..effc935 --- /dev/null +++ b/main/displays/menus/espnowmenu.cpp @@ -0,0 +1,28 @@ +#include "espnowmenu.h" + +#ifdef FEATURE_ESPNOW +#include "actions/dummyaction.h" +#include "actions/switchscreenaction.h" +#include "actions/toggleboolaction.h" +#include "checkboxicon.h" +#include "icons/back.h" +#include "icons/time.h" +#include "settingsmenu.h" + +using namespace espgui; + +namespace espnowmenu { +} // namespace + +EspNowMenu::EspNowMenu() { + constructMenuItem, espnowmenu::SendBobbycarTimesyncMessageAction, StaticMenuItemIcon<&bobbyicons::time>>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, espnowmenu::ReceiveTimeStampAccessor>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, espnowmenu::ReceiveTsFromOtherBobbycarsAccessor>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); +} + +void EspNowMenu::back() +{ + switchScreen(); +} +#endif diff --git a/main/displays/menus/espnowmenu.h b/main/displays/menus/espnowmenu.h new file mode 100644 index 0000000..d4155bf --- /dev/null +++ b/main/displays/menus/espnowmenu.h @@ -0,0 +1,42 @@ +#pragma once + +#ifdef FEATURE_ESPNOW + +#include +#include +#include +#include +#include +#include +#include + +#include "espnowfunctions.h" + +using namespace espgui; + +namespace espnowmenu { + +struct ReceiveTimeStampAccessor : public RefAccessor { bool &getRef() const override { return espnow::receiveTimeStamp; } }; +struct ReceiveTsFromOtherBobbycarsAccessor : public RefAccessor { bool &getRef() const override { return espnow::receiveTsFromOtherBobbycars; } }; + +class SendBobbycarTimesyncMessageAction : public virtual ActionInterface { +public: + void triggered() override + { + const auto message = fmt::format("BOBBYT:{}", espchrono::utc_clock::now().time_since_epoch().count()); + espnow::send_espnow_message(message); + } +}; + +} // namespace + +class EspNowMenu : + public espgui::MenuDisplay, + public espgui::StaticText +{ +public: + EspNowMenu(); + void back() override; +}; + +#endif diff --git a/main/displays/menus/settingsmenu.cpp b/main/displays/menus/settingsmenu.cpp index db53ff4..a4c0901 100644 --- a/main/displays/menus/settingsmenu.cpp +++ b/main/displays/menus/settingsmenu.cpp @@ -28,6 +28,7 @@ #include "displays/menus/bluetoothsettingsmenu.h" #include "displays/menus/blesettingsmenu.h" #include "displays/menus/cloudsettingsmenu.h" +#include "displays/menus/espnowmenu.h" #include "displays/menus/selectbuildservermenu.h" #include "displays/menus/timesettingsmenu.h" #include "displays/menus/modessettingsmenu.h" @@ -61,6 +62,9 @@ SettingsMenu::SettingsMenu() constructMenuItem, SwitchScreenAction>>(); constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&bobbyicons::wifi>>>(); +#ifdef FEATURE_ESPNOW + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&bobbyicons::wifi>>>(); +#endif #ifdef FEATURE_BLUETOOTH constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&bobbyicons::bluetooth>>>(); #endif diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index 1b5f25f..22d2c97 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -16,18 +16,13 @@ namespace espnow { uint16_t lastYear; // Used for esp-now timesync -esp_now_settings_t::Enable DefaultEnable { - .receiveTimeStamp = true, -}; - -esp_now_settings_t esp_now_settings{ - .enable = DefaultEnable, -}; - std::deque message_queue{}; std::list peers{}; uint8_t initialized{0}; +bool receiveTimeStamp{true}; +bool receiveTsFromOtherBobbycars{true}; + void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) { ESP_LOGD(TAG, "Received data"); @@ -121,12 +116,12 @@ void handle() return; } - if (esp_now_settings.enable.receiveTimeStamp) + if (receiveTimeStamp) { const auto thisYear = int(espchrono::toDateTime(espchrono::utc_clock::now()).date.year()); if (abs(thisYear - espnow::lastYear) > 1) { - esp_now_settings.enable.receiveTimeStamp = false; + receiveTimeStamp = false; } } @@ -139,7 +134,7 @@ void handle() if (msg.type == "T") { - if (!esp_now_settings.enable.receiveTimeStamp) + if (!receiveTimeStamp) return; if (const auto result = cpputils::fromString(msg.content); result) @@ -151,6 +146,11 @@ void handle() ESP_LOGW(TAG, "could not parse number: %.*s", result.error().size(), result.error().data()); } } + else if (msg.type == "BOBBYT") + { + if (!receiveTsFromOtherBobbycars) + return; + } else { ESP_LOGI(TAG, "Unkown Type: %s - Message: %s", msg.type.c_str(), msg.content.c_str()); @@ -164,6 +164,7 @@ void onRecvTs(uint64_t millis) const auto milliseconds = std::chrono::milliseconds(millis); const auto timepoint = espchrono::utc_clock::time_point(milliseconds); time_set_now(timepoint); + receiveTsFromOtherBobbycars = false; } esp_err_t send_espnow_message(std::string message) diff --git a/main/espnowfunctions.h b/main/espnowfunctions.h index 512d8dd..009adb3 100644 --- a/main/espnowfunctions.h +++ b/main/espnowfunctions.h @@ -14,12 +14,8 @@ struct esp_now_message_t { const std::string type; }; -struct esp_now_settings_t { - struct Enable { - bool receiveTimeStamp; - } enable; -} -extern esp_now_settings; +extern bool receiveTimeStamp; +extern bool receiveTsFromOtherBobbycars; extern std::deque message_queue; extern std::list peers; diff --git a/main/texts.cpp b/main/texts.cpp index 1111207..ec23560 100644 --- a/main/texts.cpp +++ b/main/texts.cpp @@ -544,4 +544,10 @@ char TEXT_QRCODE_DEBUG[] = "QR Debug"; char TEXT_GREENPASS[] = "Green Pass"; char TEXT_SHOWCERT[] = "Show cert"; +//EspNowMenu +char TEXT_ESPNOW[] = "ESP-Now"; +char TEXT_ESPNOW_MENU[] = "ESP-Now Menu"; +char TEXT_ESPNOW_RECEIVETS[] = "Recv Ts State"; +char TEXT_ESPNOW_RECEIVETSFROMBOBBY[] = "Recv BobbyTs State"; +char TEXT_ESPNOW_SENDTSMSG[] = "Broadcast Time"; } // namespace diff --git a/main/texts.h b/main/texts.h index ebbe24f..ed83a05 100644 --- a/main/texts.h +++ b/main/texts.h @@ -543,6 +543,13 @@ extern char TEXT_QRCODE_DEBUG[]; //GreenPassMenu extern char TEXT_GREENPASS[]; extern char TEXT_SHOWCERT[]; + +//EspNowMenu +extern char TEXT_ESPNOW[]; +extern char TEXT_ESPNOW_MENU[]; +extern char TEXT_ESPNOW_RECEIVETS[]; +extern char TEXT_ESPNOW_RECEIVETSFROMBOBBY[]; +extern char TEXT_ESPNOW_SENDTSMSG[]; } // namespace using namespace bobbytexts; From 647f86906bd7b0095039ea25e3a6dda582b9f3a3 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 19:14:45 +0100 Subject: [PATCH 19/36] Changed back action to a menu that makes more sense --- main/displays/menus/selectbuildservermenu.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main/displays/menus/selectbuildservermenu.cpp b/main/displays/menus/selectbuildservermenu.cpp index 702a4cc..deff708 100644 --- a/main/displays/menus/selectbuildservermenu.cpp +++ b/main/displays/menus/selectbuildservermenu.cpp @@ -10,7 +10,7 @@ // local includes #include "actions/dummyaction.h" #include "buildserver.h" -#include "displays/menus/settingsmenu.h" +#include "displays/menus/otamenu.h" #include "utils.h" #ifdef FEATURE_OTA @@ -67,11 +67,11 @@ SelectBuildServerMenu::SelectBuildServerMenu() constructMenuItem, DefaultFont, StaticColor, DummyAction>>(); } - constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); } void SelectBuildServerMenu::back() { - espgui::switchScreen(); + espgui::switchScreen(); } #endif From 595a508d93da3daf21dd69c6faf9c02ea1368588 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 19:15:14 +0100 Subject: [PATCH 20/36] Added esp-now timesync --- main/CMakeLists.txt | 2 ++ main/accessors/settingsaccessors.h | 30 +++++++++++++++++- main/displays/menus/espnowmenu.cpp | 5 ++- main/displays/menus/espnowsettingsmenu.cpp | 29 +++++++++++++++++ main/displays/menus/espnowsettingsmenu.h | 29 +++++++++++++++++ main/espnowfunctions.cpp | 37 ++++++++++++++-------- main/espnowfunctions.h | 2 +- main/presets.h | 18 ++++++++++- main/settings.h | 15 +++++++++ main/texts.cpp | 6 ++++ main/texts.h | 6 ++++ 11 files changed, 162 insertions(+), 17 deletions(-) create mode 100644 main/displays/menus/espnowsettingsmenu.cpp create mode 100644 main/displays/menus/espnowsettingsmenu.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index a136156..8bc82a1 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -78,6 +78,7 @@ set(headers displays/menus/dynamicdebugmenu.h displays/menus/enablemenu.h displays/menus/espnowmenu.h + displays/menus/espnowsettingsmenu.h displays/menus/feedbackdebugmenu.h displays/menus/gametrakmodesettingsmenu.h displays/menus/genericwifisettingsmenu.h @@ -282,6 +283,7 @@ set(sources displays/menus/dynamicdebugmenu.cpp displays/menus/enablemenu.cpp displays/menus/espnowmenu.cpp + displays/menus/espnowsettingsmenu.cpp displays/menus/feedbackdebugmenu.cpp displays/menus/gametrakmodesettingsmenu.cpp displays/menus/genericwifisettingsmenu.cpp diff --git a/main/accessors/settingsaccessors.h b/main/accessors/settingsaccessors.h index 57a61f3..19cc59b 100644 --- a/main/accessors/settingsaccessors.h +++ b/main/accessors/settingsaccessors.h @@ -14,15 +14,19 @@ struct RefAccessorSaveSettings : public virtual espgui::RefAccessor void setValue(T value) override { espgui::RefAccessor::setValue(value); saveSettings(); }; }; +// Bms #ifdef FEATURE_BMS struct AutoConnectBmsAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.autoConnectBms; } }; #endif + +// Buzzer struct ReverseBeepAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.buzzer.reverseBeep; } }; struct ReverseBeepFreq0Accessor : public RefAccessorSaveSettings { uint8_t &getRef() const override { return settings.buzzer.reverseBeepFreq0; } }; struct ReverseBeepFreq1Accessor : public RefAccessorSaveSettings { uint8_t &getRef() const override { return settings.buzzer.reverseBeepFreq1; } }; struct ReverseBeepDuration0Accessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.buzzer.reverseBeepDuration0; } }; struct ReverseBeepDuration1Accessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.buzzer.reverseBeepDuration1; } }; +// Limits struct IMotMaxAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.limits.iMotMax; } }; struct IDcMaxAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.limits.iDcMax; } }; struct NMotMaxKmhAccessor : public virtual espgui::AccessorInterface @@ -34,22 +38,27 @@ struct NMotMaxRpmAccessor : public RefAccessorSaveSettings { int16_t &g struct FieldWeakMaxAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.limits.fieldWeakMax; } }; struct PhaseAdvMaxAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.limits.phaseAdvMax; } }; +// WiFi struct WifiStaEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.wifiSettings.wifiStaEnabled; } }; struct WifiApEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.wifiSettings.wifiApEnabled; } }; +// Bluetooth #ifdef FEATURE_BLUETOOTH struct AutoBluetoothModeAccessor : public RefAccessorSaveSettings { BluetoothMode &getRef() const override { return settings.bluetoothSettings.autoBluetoothMode; } }; #endif +// Bluetooth Low Energy #ifdef FEATURE_BLE struct BleEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.bleSettings.bleEnabled; } }; #endif +// Cloud #ifdef FEATURE_CLOUD struct CloudEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.cloudSettings.cloudEnabled; } }; struct CloudTransmitTimeoutAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.cloudSettings.cloudTransmitTimeout; } }; #endif +// Time struct TimezoneOffsetAccessor : public virtual espgui::AccessorInterface { int32_t getValue() const override { return settings.timeSettings.timezoneOffset.count(); } @@ -66,6 +75,7 @@ struct TimeSyncIntervalAccessor : public virtual espgui::AccessorInterface { bool &getRef() const override { return settings.controllerHardware.enableFrontLeft; } }; struct FrontRightEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.controllerHardware.enableFrontRight; } }; struct BackLeftEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.controllerHardware.enableBackLeft; } }; @@ -89,6 +99,8 @@ struct SwapFrontBackAccessor : public RefAccessorSaveSettings { void setValue(bool value) override { RefAccessorSaveSettings::setValue(value); updateSwapFrontBack(); }; #endif }; + +// CAN #ifdef FEATURE_CAN struct SendFrontCanCmdAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.controllerHardware.sendFrontCanCmd; } }; struct SendBackCanCmdAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.controllerHardware.sendBackCanCmd; } }; @@ -96,6 +108,7 @@ struct CanTransmitTimeoutAccessor : public RefAccessorSaveSettings { in struct CanReceiveTimeoutAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.controllerHardware.canReceiveTimeout; } }; #endif +// Input devices struct SampleCountAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.sampleCount; } }; struct GasMinAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.gasMin; } }; struct GasMaxAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.gasMax; } }; @@ -117,9 +130,13 @@ struct ModeUpdateRateAccessor : public RefAccessorSaveSettings { int16_ 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; } }; struct DisplayRedrawRateAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.timersSettings.displayRedrawRate; } }; + +// CAN #ifdef FEATURE_CAN struct CanReceiveRateAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.timersSettings.canReceiveRate; } }; #endif + +// Cloud #ifdef FEATURE_CLOUD struct CloudCollectRateAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.timersSettings.cloudCollectRate; } }; struct CloudSendRateAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.boardcomputerHardware.timersSettings.cloudSendRate; } }; @@ -129,6 +146,7 @@ struct CloudDebugEnableAccessor : public RefAccessorSaveSettings { bool &g struct UdpUseStdStringAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.cloudSettings.udpUseStdString; } }; #endif +// DefaultMode struct DefaultModeModelModeAccessor : public RefAccessorSaveSettings { UnifiedModelMode &getRef() const override { return settings.defaultMode.modelMode; } }; struct DefaultModeHybridModelModeAccessor : public RefAccessorSaveSettings { UnifiedModelMode &getRef() const override { return settings.hybrid.hybridMode; } }; struct DefaultModeSquareGasAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.defaultMode.squareGas; } }; @@ -153,15 +171,19 @@ struct DefaultModeEnableHybridAccessor : public RefAccessorSaveSettings { struct DefaultModeHybridActivationLimitAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.hybrid.activationLimit; } }; struct DefaultModeHybridDeactivationLimitAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.hybrid.deactivationLimit; } }; +// TempomatMode struct TempomatModeModelModeAccessor : public RefAccessorSaveSettings { UnifiedModelMode &getRef() const override { return settings.tempomatMode.modelMode; } }; +// LarsmMode struct LarsmModeModelModeAccessor : public RefAccessorSaveSettings { UnifiedModelMode &getRef() const override { return settings.larsmMode.modelMode; } }; struct LarsmModeModeAccessor : public RefAccessorSaveSettings { LarsmModeMode &getRef() const override { return settings.larsmMode.mode; } }; struct LarsmModeIterationsAccessor : public RefAccessorSaveSettings { uint8_t &getRef() const override { return settings.larsmMode.iterations; } }; +// MotortestMode struct MotortestModeMultiplikatorAccessor : public RefAccessorSaveSettings { uint8_t &getRef() const override { return settings.motortestMode.multiplikator; } }; struct MotortestMaxPwmAccessor : public RefAccessorSaveSettings { uint16_t &getRef() const override { return settings.motortestMode.maxPwm; } }; +// Ledstrip #ifdef FEATURE_LEDSTRIP struct EnableLedAnimationAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.ledstrip.enableLedAnimation; } }; struct EnableBrakeLightsAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.ledstrip.enableBrakeLights; } }; @@ -190,13 +212,19 @@ struct BatteryParallelCellsAccessor : public RefAccessorSaveSettings { struct BatteryWHperKMAccessor : public RefAccessorSaveSettings { uint16_t &getRef() const override { return settings.battery.watthoursPerKilometer; } }; struct BatteryApplyCalibrationAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.battery.applyCalibration; } }; - +// Lockscreen 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]; } }; +// Handbremse struct HandbremsEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.handbremse.enable; } }; struct HandbremsModeAccessor : public RefAccessorSaveSettings { HandbremseMode &getRef() const override { return settings.handbremse.mode; } }; struct HandbremsTimeoutAccessor : public RefAccessorSaveSettings { uint16_t &getRef() const override { return settings.handbremse.triggerTimeout; } }; struct HandbremsAutomaticAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.handbremse.automatic; } }; struct HandbremsVisualizeAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.handbremse.visualize; } }; + +// ESP Now +struct ESPNowSyncTimeEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.espnow.syncTime; } }; +struct ESPNowSyncTimeWithOthersEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.espnow.syncTimeWithOthers; } }; +struct ESPNowSyncBlinkEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.espnow.syncBlink; } }; diff --git a/main/displays/menus/espnowmenu.cpp b/main/displays/menus/espnowmenu.cpp index effc935..c9f1aee 100644 --- a/main/displays/menus/espnowmenu.cpp +++ b/main/displays/menus/espnowmenu.cpp @@ -6,8 +6,10 @@ #include "actions/toggleboolaction.h" #include "checkboxicon.h" #include "icons/back.h" +#include "icons/settings.h" #include "icons/time.h" -#include "settingsmenu.h" +#include "displays/menus/espnowsettingsmenu.h" +#include "displays/menus/settingsmenu.h" using namespace espgui; @@ -18,6 +20,7 @@ EspNowMenu::EspNowMenu() { constructMenuItem, espnowmenu::SendBobbycarTimesyncMessageAction, StaticMenuItemIcon<&bobbyicons::time>>>(); constructMenuItem, ToggleBoolAction, CheckboxIcon, espnowmenu::ReceiveTimeStampAccessor>>(); constructMenuItem, ToggleBoolAction, CheckboxIcon, espnowmenu::ReceiveTsFromOtherBobbycarsAccessor>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&bobbyicons::settings>>>(); constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); } diff --git a/main/displays/menus/espnowsettingsmenu.cpp b/main/displays/menus/espnowsettingsmenu.cpp new file mode 100644 index 0000000..e642012 --- /dev/null +++ b/main/displays/menus/espnowsettingsmenu.cpp @@ -0,0 +1,29 @@ +#include "espnowsettingsmenu.h" + +#ifdef FEATURE_ESPNOW +#include "accessors/settingsaccessors.h" +#include "actions/dummyaction.h" +#include "actions/switchscreenaction.h" +#include "actions/toggleboolaction.h" +#include "checkboxicon.h" +#include "icons/back.h" +#include "icons/time.h" +#include "espnowmenu.h" + +using namespace espgui; + +namespace espnowsettingsmenu { +} // namespace + +EspNowSettingsMenu::EspNowSettingsMenu() { + constructMenuItem, ToggleBoolAction, CheckboxIcon, ESPNowSyncTimeEnabledAccessor>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, ESPNowSyncTimeWithOthersEnabledAccessor>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, ESPNowSyncBlinkEnabledAccessor>>(); + constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); +} + +void EspNowSettingsMenu::back() +{ + switchScreen(); +} +#endif diff --git a/main/displays/menus/espnowsettingsmenu.h b/main/displays/menus/espnowsettingsmenu.h new file mode 100644 index 0000000..577d463 --- /dev/null +++ b/main/displays/menus/espnowsettingsmenu.h @@ -0,0 +1,29 @@ +#pragma once + +#ifdef FEATURE_ESPNOW + +#include +#include +#include +#include +#include +#include +#include + +#include "espnowfunctions.h" + +using namespace espgui; + +namespace espnowsettingsmenu { +} // namespace + +class EspNowSettingsMenu : + public espgui::MenuDisplay, + public espgui::StaticText +{ +public: + EspNowSettingsMenu(); + void back() override; +}; + +#endif diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index 22d2c97..34cc4a2 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -43,7 +43,7 @@ void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) } else { - ESP_LOGW(TAG, "Invalid message: Could not find ':'"); + ESP_LOGW(TAG, "Invalid message: Could not find ':' (%s)", data_str.c_str()); } } @@ -116,15 +116,6 @@ void handle() return; } - if (receiveTimeStamp) - { - const auto thisYear = int(espchrono::toDateTime(espchrono::utc_clock::now()).date.year()); - if (abs(thisYear - espnow::lastYear) > 1) - { - receiveTimeStamp = false; - } - } - if(message_queue.size()) { for (const esp_now_message_t &msg : message_queue) @@ -134,7 +125,7 @@ void handle() if (msg.type == "T") { - if (!receiveTimeStamp) + if (!receiveTimeStamp || !settings.espnow.syncTime) return; if (const auto result = cpputils::fromString(msg.content); result) @@ -148,8 +139,17 @@ void handle() } else if (msg.type == "BOBBYT") { - if (!receiveTsFromOtherBobbycars) + if (!receiveTsFromOtherBobbycars || !settings.espnow.syncTimeWithOthers) return; + + if (const auto result = cpputils::fromString(msg.content); result) + { + onRecvTs(*result, true); + } + else + { + ESP_LOGW(TAG, "could not parse number: %.*s", result.error().size(), result.error().data()); + } } else { @@ -159,11 +159,22 @@ void handle() } } -void onRecvTs(uint64_t millis) +void onRecvTs(uint64_t millis, bool isFromBobbycar) { const auto milliseconds = std::chrono::milliseconds(millis); const auto timepoint = espchrono::utc_clock::time_point(milliseconds); + ESP_LOGW(TAG, "setting current time to %s", espchrono::toString(timepoint.time_since_epoch()).c_str()); time_set_now(timepoint); + + if (receiveTimeStamp) + { + if (const auto thisYear = int(espchrono::toDateTime(espchrono::utc_clock::now()).date.year()); abs(thisYear - espnow::lastYear) > 1) + { + espnow::lastYear = thisYear; + receiveTimeStamp = false; + } + } + receiveTsFromOtherBobbycars = false; } diff --git a/main/espnowfunctions.h b/main/espnowfunctions.h index 009adb3..988900a 100644 --- a/main/espnowfunctions.h +++ b/main/espnowfunctions.h @@ -23,7 +23,7 @@ extern std::list peers; void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len); void initESPNow(); void handle(); -void onRecvTs(uint64_t millis); +void onRecvTs(uint64_t millis, bool isFromBobbycar = false); esp_err_t send_espnow_message(std::string message); } // namespace espnow #endif diff --git a/main/presets.h b/main/presets.h index 34c1f72..bec092c 100644 --- a/main/presets.h +++ b/main/presets.h @@ -300,6 +300,19 @@ constexpr Settings::SavedStatistics defaultSavedStatistics { .totalCentimeters = 0, }; +#ifdef FEATURE_ESPNOW +constexpr Settings::ESPNOW defaultEspNowSettings { +#ifndef FEATURE_NTP + .syncTime = true, + .syncTimeWithOthers = true, +#else + .syncTime = false, + .syncTimeWithOthers = false, +#endif + .syncBlink = false +}; +#endif + constexpr Settings defaultSettings { #ifdef FEATURE_BMS .autoConnectBms = false, @@ -330,7 +343,10 @@ constexpr Settings defaultSettings { .hybrid = defaultHybrid, .lockscreen = defaultLockscreen, .savedStatistics = defaultSavedStatistics, - .handbremse = defaultHandbremse + .handbremse = defaultHandbremse, +#ifdef FEATURE_ESPNOW + .espnow = defaultEspNowSettings, +#endif }; StringSettings makeDefaultStringSettings(); diff --git a/main/settings.h b/main/settings.h index 87549e4..9840a25 100644 --- a/main/settings.h +++ b/main/settings.h @@ -224,6 +224,13 @@ struct Settings bool enable; bool visualize; } handbremse; +#ifdef FEATURE_ESPNOW + struct ESPNOW { + bool syncTime; + bool syncTimeWithOthers; + bool syncBlink; + } espnow; +#endif template void executeForEveryCommonSetting(T &&callable); @@ -362,6 +369,14 @@ void Settings::executeForEveryCommonSetting(T &&callable) callable("handBremsM", handbremse.mode); callable("handBremsT", handbremse.triggerTimeout); callable("handBremsV", handbremse.visualize); + +#ifdef FEATURE_ESPNOW + callable("espnowSyncT", espnow.syncTime); + callable("espnowSyncTWO", espnow.syncTimeWithOthers); +#ifdef FEATURE_LEDSTRIP + callable("espnowSyncBl", espnow.syncBlink); +#endif +#endif } template diff --git a/main/texts.cpp b/main/texts.cpp index ec23560..dca1e63 100644 --- a/main/texts.cpp +++ b/main/texts.cpp @@ -550,4 +550,10 @@ char TEXT_ESPNOW_MENU[] = "ESP-Now Menu"; char TEXT_ESPNOW_RECEIVETS[] = "Recv Ts State"; char TEXT_ESPNOW_RECEIVETSFROMBOBBY[] = "Recv BobbyTs State"; char TEXT_ESPNOW_SENDTSMSG[] = "Broadcast Time"; + +//EspNowSettingsMenu +char TEXT_ESPNOW_SETTINGS[] = "ESP-Now settings"; +char TEXT_ESPNOW_SYNCTIME[] = "Sync time (no NTP)"; +char TEXT_ESPNOW_SYNCWITHOTHERS[] = "Sync time with others"; +char TEXT_ESPNOW_SYNCBLINK[] = "Sync blink"; } // namespace diff --git a/main/texts.h b/main/texts.h index ed83a05..22ec3b5 100644 --- a/main/texts.h +++ b/main/texts.h @@ -550,6 +550,12 @@ extern char TEXT_ESPNOW_MENU[]; extern char TEXT_ESPNOW_RECEIVETS[]; extern char TEXT_ESPNOW_RECEIVETSFROMBOBBY[]; extern char TEXT_ESPNOW_SENDTSMSG[]; + +//EspNowSettingsMenu +extern char TEXT_ESPNOW_SETTINGS[]; +extern char TEXT_ESPNOW_SYNCTIME[]; +extern char TEXT_ESPNOW_SYNCWITHOTHERS[]; +extern char TEXT_ESPNOW_SYNCBLINK[]; } // namespace using namespace bobbytexts; From 6adca753aa9e18ab58e60beb5f516bbdabf12fe7 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 19:52:39 +0100 Subject: [PATCH 21/36] Back button now does something and lockscreen can OPTIONALLY be set to stay over reboot (Implemented #146) --- main/accessors/settingsaccessors.h | 1 + main/displays/lockscreen.cpp | 25 ++++++++++++++++--- main/displays/lockscreen.h | 1 + .../displays/menus/lockscreensettingsmenu.cpp | 1 + main/main.cpp | 12 ++++++++- main/presets.h | 2 ++ main/settings.h | 4 +++ main/texts.cpp | 1 + main/texts.h | 1 + 9 files changed, 44 insertions(+), 4 deletions(-) diff --git a/main/accessors/settingsaccessors.h b/main/accessors/settingsaccessors.h index 19cc59b..64d6988 100644 --- a/main/accessors/settingsaccessors.h +++ b/main/accessors/settingsaccessors.h @@ -216,6 +216,7 @@ struct BatteryApplyCalibrationAccessor : public RefAccessorSaveSettings { 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]; } }; +struct LockscreenKeepLockedAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.lockscreen.keepLockedAfterReboot; } }; // Handbremse struct HandbremsEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.handbremse.enable; } }; diff --git a/main/displays/lockscreen.cpp b/main/displays/lockscreen.cpp index beb8d5f..6a1551b 100644 --- a/main/displays/lockscreen.cpp +++ b/main/displays/lockscreen.cpp @@ -23,6 +23,11 @@ void Lockscreen::start() profileButtonDisabled = !settings.lockscreen.allowPresetSwitch; isLocked = true; + if (settings.lockscreen.keepLockedAfterReboot && !settings.lockscreen.locked) + { + settings.lockscreen.locked = true; + saveSettings(); + } } void Lockscreen::initScreen() @@ -64,12 +69,12 @@ void Lockscreen::update() void Lockscreen::redraw() { - if (m_pressed) + if (m_pressed || m_back_pressed) { drawRect(m_currentIndex, 1, TFT_BLACK); drawRect(m_currentIndex, 2, TFT_BLACK); - if (++m_currentIndex>=4) + if (!m_back_pressed && ++m_currentIndex>=4) { if (m_numbers == settings.lockscreen.pin) { @@ -83,7 +88,12 @@ void Lockscreen::redraw() m_numbers = {0,0,0,0}; m_currentIndex = 0; - std::for_each(std::begin(m_labels) + 1, std::end(m_labels), [](auto &label){ label.redraw({}); }); + std::for_each(std::begin(m_labels) + 1, std::end(m_labels), [](auto &label){ label.redraw("0"); }); + } + else if (m_back_pressed && m_currentIndex < 3) + { + drawRect(m_currentIndex+1, 1, TFT_BLACK); + drawRect(m_currentIndex+1, 2, TFT_BLACK); } m_labels[m_currentIndex].redraw(std::to_string(m_numbers[m_currentIndex])); @@ -92,6 +102,7 @@ void Lockscreen::redraw() drawRect(m_currentIndex, 2, TFT_YELLOW); m_pressed = false; + m_back_pressed = false; } if (m_rotated) @@ -124,6 +135,11 @@ void Lockscreen::stop() profileButtonDisabled = false; isLocked = false; + if (settings.lockscreen.keepLockedAfterReboot && settings.lockscreen.locked) + { + settings.lockscreen.locked = false; + saveSettings(); + } } void Lockscreen::confirm() @@ -133,6 +149,9 @@ void Lockscreen::confirm() void Lockscreen::back() { + if (m_currentIndex > 0) + m_currentIndex--; + m_back_pressed = true; } void Lockscreen::rotate(int offset) diff --git a/main/displays/lockscreen.h b/main/displays/lockscreen.h index 5af1037..57fd890 100644 --- a/main/displays/lockscreen.h +++ b/main/displays/lockscreen.h @@ -46,6 +46,7 @@ private: uint8_t m_currentIndex{}; bool m_pressed; + bool m_back_pressed; int m_rotated; ModeInterface *m_oldMode; diff --git a/main/displays/menus/lockscreensettingsmenu.cpp b/main/displays/menus/lockscreensettingsmenu.cpp index 57b9a08..b3fdffa 100644 --- a/main/displays/menus/lockscreensettingsmenu.cpp +++ b/main/displays/menus/lockscreensettingsmenu.cpp @@ -52,6 +52,7 @@ using namespace espgui; LockscreenSettingsMenu::LockscreenSettingsMenu() { constructMenuItem, ToggleBoolAction, CheckboxIcon, LockscreenAllowPresetSwitchAccessor>>(); + constructMenuItem, ToggleBoolAction, CheckboxIcon, LockscreenKeepLockedAccessor>>(); constructMenuItem>, SwitchScreenAction>>(); constructMenuItem>, SwitchScreenAction>>(); constructMenuItem>, SwitchScreenAction>>(); diff --git a/main/main.cpp b/main/main.cpp index 4678423..79f2b06 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -75,6 +75,7 @@ using namespace std::chrono_literals; #endif #include "modes/defaultmode.h" #include "displays/statusdisplay.h" +#include "displays/lockscreen.h" #include "displays/calibratedisplay.h" #ifdef FEATURE_DNS_NS #include "dnsannounce.h" @@ -291,7 +292,16 @@ extern "C" void app_main() if (!gas || !brems || *gas > 200.f || *brems > 200.f) espgui::switchScreen(true); else - espgui::switchScreen(); + { + if (settings.lockscreen.keepLockedAfterReboot && settings.lockscreen.locked) + { + espgui::switchScreen(); + } + else + { + espgui::switchScreen(); + } + } #endif #ifdef FEATURE_ESPNOW espnow::initESPNow(); diff --git a/main/presets.h b/main/presets.h index bec092c..7997adf 100644 --- a/main/presets.h +++ b/main/presets.h @@ -274,6 +274,8 @@ constexpr Settings::Ledstrip defaultLedstrip { constexpr Settings::LockscreenSettings defaultLockscreen { .allowPresetSwitch = true, + .keepLockedAfterReboot = false, + .locked = false, .pin = { 1, 2, 3, 4 } }; diff --git a/main/settings.h b/main/settings.h index 9840a25..93915ae 100644 --- a/main/settings.h +++ b/main/settings.h @@ -210,6 +210,8 @@ struct Settings struct LockscreenSettings { bool allowPresetSwitch; + bool keepLockedAfterReboot; + bool locked; std::array pin; } lockscreen; @@ -360,6 +362,8 @@ void Settings::executeForEveryCommonSetting(T &&callable) callable("hybridDeacL", hybrid.deactivationLimit); callable("lockAlwPresetSw", lockscreen.allowPresetSwitch); + callable("keepLocked", lockscreen.keepLockedAfterReboot); + callable("currentlyLocked", lockscreen.locked); callable("lockscreenPin", lockscreen.pin); callable("totalCentimeter", savedStatistics.totalCentimeters); diff --git a/main/texts.cpp b/main/texts.cpp index dca1e63..71da3e8 100644 --- a/main/texts.cpp +++ b/main/texts.cpp @@ -332,6 +332,7 @@ char TEXT_ANIMATION_BLINKBOTH[] = "Blink Both"; //LockscreenSettingsMenu char TEXT_LOCKSCREENSETTINGS[] = "Lockscreen Settings"; char TEXT_ALLOWPRESETSWITCH[] = "Allow preset switch"; +char TEXT_KEEPLOCKED[] = "Keep locked"; char TEXT_PINDIGIT0[] = "PIN digit0"; char TEXT_PINDIGIT1[] = "PIN digit1"; char TEXT_PINDIGIT2[] = "PIN digit2"; diff --git a/main/texts.h b/main/texts.h index 22ec3b5..42d2bd4 100644 --- a/main/texts.h +++ b/main/texts.h @@ -332,6 +332,7 @@ extern char TEXT_ANIMATION_BLINKBOTH[]; //LockscreenSettingsMenu extern char TEXT_LOCKSCREENSETTINGS[]; extern char TEXT_ALLOWPRESETSWITCH[]; +extern char TEXT_KEEPLOCKED[]; extern char TEXT_PINDIGIT0[]; extern char TEXT_PINDIGIT1[]; extern char TEXT_PINDIGIT2[]; From 4a2254df0ba0cc2d6e8a26ad1c22e5b234a0c977 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 20:04:46 +0100 Subject: [PATCH 22/36] Fixed compiling for non-espnow-users --- main/accessors/settingsaccessors.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main/accessors/settingsaccessors.h b/main/accessors/settingsaccessors.h index 64d6988..3e94162 100644 --- a/main/accessors/settingsaccessors.h +++ b/main/accessors/settingsaccessors.h @@ -226,6 +226,8 @@ struct HandbremsAutomaticAccessor : public RefAccessorSaveSettings { bool struct HandbremsVisualizeAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.handbremse.visualize; } }; // ESP Now +#ifdef FEATURE_ESPNOW struct ESPNowSyncTimeEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.espnow.syncTime; } }; struct ESPNowSyncTimeWithOthersEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.espnow.syncTimeWithOthers; } }; struct ESPNowSyncBlinkEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.espnow.syncBlink; } }; +#endif From 4f109d4d08b7388b1a521d4ce99740d91f61b751 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 20:32:26 +0100 Subject: [PATCH 23/36] Moved time to special menudisplay --- main/CMakeLists.txt | 2 ++ main/displays/menus/mainmenu.cpp | 13 ------------- main/displays/menus/mainmenu.h | 8 ++------ main/widgets/menudisplaywithtime.cpp | 21 +++++++++++++++++++++ main/widgets/menudisplaywithtime.h | 15 +++++++++++++++ 5 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 main/widgets/menudisplaywithtime.cpp create mode 100644 main/widgets/menudisplaywithtime.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 8bc82a1..46b1dfc 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -199,6 +199,7 @@ set(headers webserver_settings.h webserver_stringsettings.h widgets/doubleprogressbar.h + widgets/menudisplaywithtime.h wifi_bobbycar.h wifitexthelpers.h ) @@ -405,6 +406,7 @@ set(sources webserver_settings.cpp webserver_stringsettings.cpp widgets/doubleprogressbar.cpp + widgets/menudisplaywithtime.cpp wifi_bobbycar.cpp wifitexthelpers.cpp ) diff --git a/main/displays/menus/mainmenu.cpp b/main/displays/menus/mainmenu.cpp index 453b2b8..259f234 100644 --- a/main/displays/menus/mainmenu.cpp +++ b/main/displays/menus/mainmenu.cpp @@ -110,16 +110,3 @@ void MainMenu::back() { switchScreen(); } - -void MainMenu::start() -{ - Base::start(); - m_label_currentTime.start(); -} - -void MainMenu::redraw() -{ - Base::redraw(); - tft.setTextFont(2); - m_label_currentTime.redraw(fmt::format("&7Time: {}", local_clock_string())); -} diff --git a/main/displays/menus/mainmenu.h b/main/displays/menus/mainmenu.h index 0a6338e..5499ca0 100644 --- a/main/displays/menus/mainmenu.h +++ b/main/displays/menus/mainmenu.h @@ -1,11 +1,11 @@ #pragma once // 3rdparty lib includes -#include "menudisplay.h" #include "menuitem.h" // local includes #include "texts.h" +#include "widgets/menudisplaywithtime.h" #ifdef MAINMENU_PLUGIN #include MAINMENU_PLUGIN #endif @@ -22,15 +22,11 @@ namespace { } // namespace class MainMenu : - public espgui::MenuDisplay, + public bobbygui::MenuDisplayWithTime, public espgui::StaticText { using Base = espgui::MenuDisplay; public: MainMenu(); - void back() override; - void start() override; - void redraw() override; - espgui::Label m_label_currentTime{145, 6}; }; diff --git a/main/widgets/menudisplaywithtime.cpp b/main/widgets/menudisplaywithtime.cpp new file mode 100644 index 0000000..337f6cf --- /dev/null +++ b/main/widgets/menudisplaywithtime.cpp @@ -0,0 +1,21 @@ +#include "menudisplaywithtime.h" +#include "tftinstance.h" +#include "utils.h" + +using namespace espgui; + +namespace bobbygui { +void MenuDisplayWithTime::start() +{ + Base::start(); + m_label_currentTime.start(); +} + +void MenuDisplayWithTime::redraw() +{ + Base::redraw(); + tft.setTextFont(2); + m_label_currentTime.redraw(fmt::format("&7Time: {}", local_clock_string())); +} + +} // namespace diff --git a/main/widgets/menudisplaywithtime.h b/main/widgets/menudisplaywithtime.h new file mode 100644 index 0000000..2f809d6 --- /dev/null +++ b/main/widgets/menudisplaywithtime.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace bobbygui { +class MenuDisplayWithTime : + public espgui::MenuDisplay +{ + using Base = espgui::MenuDisplay; +public: + void start() override; + void redraw() override; + espgui::Label m_label_currentTime{145, 6}; +}; +} // namespace From 92f41a0ce14ed50bc708daeefeca2f8197092b2b Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 20:32:56 +0100 Subject: [PATCH 24/36] Changed some menus to show how new displaywithtime class works --- main/displays/menus/ledstripmenu.h | 4 ++-- main/displays/menus/settingsmenu.h | 4 ++-- main/displays/menus/timesettingsmenu.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/main/displays/menus/ledstripmenu.h b/main/displays/menus/ledstripmenu.h index d093463..d6e6a62 100644 --- a/main/displays/menus/ledstripmenu.h +++ b/main/displays/menus/ledstripmenu.h @@ -1,14 +1,14 @@ #pragma once // 3rdparty lib includes -#include "menudisplay.h" +#include "widgets/menudisplaywithtime.h" // local includes #include "texts.h" #ifdef FEATURE_LEDSTRIP class LedstripMenu : - public espgui::MenuDisplay, + public bobbygui::MenuDisplayWithTime, public espgui::StaticText { public: diff --git a/main/displays/menus/settingsmenu.h b/main/displays/menus/settingsmenu.h index 346ecd5..43b3c31 100644 --- a/main/displays/menus/settingsmenu.h +++ b/main/displays/menus/settingsmenu.h @@ -1,13 +1,13 @@ #pragma once // 3rdparty lib includes -#include "menudisplay.h" +#include "widgets/menudisplaywithtime.h" // local includes #include "texts.h" class SettingsMenu : - public espgui::MenuDisplay, + public bobbygui::MenuDisplayWithTime, public espgui::StaticText { public: diff --git a/main/displays/menus/timesettingsmenu.h b/main/displays/menus/timesettingsmenu.h index b8b238c..1ea7eb7 100644 --- a/main/displays/menus/timesettingsmenu.h +++ b/main/displays/menus/timesettingsmenu.h @@ -1,13 +1,13 @@ #pragma once // 3rdparty lib includes -#include "menudisplay.h" +#include "widgets/menudisplaywithtime.h" // local includes #include "texts.h" class TimeSettingsMenu : - public espgui::MenuDisplay, + public bobbygui::MenuDisplayWithTime, public espgui::StaticText { public: From b59d9dff9f814f0310897ae5200be883f061cbef Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 20:42:45 +0100 Subject: [PATCH 25/36] Fixed includes and changed to new MenuDisplayWithTime --- main/displays/menus/statisticsmenu.cpp | 2 ++ main/displays/menus/statisticsmenu.h | 8 ++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/main/displays/menus/statisticsmenu.cpp b/main/displays/menus/statisticsmenu.cpp index 4c5075b..4852ad0 100644 --- a/main/displays/menus/statisticsmenu.cpp +++ b/main/displays/menus/statisticsmenu.cpp @@ -3,9 +3,11 @@ // local includes #include "mainmenu.h" #include "actions/dummyaction.h" +#include "actions/switchscreenaction.h" #include "actioninterface.h" #include "fmt/core.h" #include "utils.h" +#include "icons/back.h" #include "icons/time.h" #include "icons/reboot.h" #include "icons/update.h" diff --git a/main/displays/menus/statisticsmenu.h b/main/displays/menus/statisticsmenu.h index 661790d..b03b5ca 100644 --- a/main/displays/menus/statisticsmenu.h +++ b/main/displays/menus/statisticsmenu.h @@ -1,18 +1,14 @@ #pragma once // 3rdparty lib includes -#include +#include "widgets/menudisplaywithtime.h" #include -#include -#include -#include // Local includes -#include "utils.h" #include "texts.h" class StatisticsMenu : - public espgui::MenuDisplay, + public bobbygui::MenuDisplayWithTime, public espgui::StaticText { public: From 5903fc21b1482875c609db7784ef99fdf515d9cf Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 21:52:34 +0100 Subject: [PATCH 26/36] MenuDisplayWithTime now supports font size --- main/widgets/menudisplaywithtime.cpp | 2 +- main/widgets/menudisplaywithtime.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/main/widgets/menudisplaywithtime.cpp b/main/widgets/menudisplaywithtime.cpp index 337f6cf..d27294b 100644 --- a/main/widgets/menudisplaywithtime.cpp +++ b/main/widgets/menudisplaywithtime.cpp @@ -14,7 +14,7 @@ void MenuDisplayWithTime::start() void MenuDisplayWithTime::redraw() { Base::redraw(); - tft.setTextFont(2); + tft.setTextFont(m_use_big_font() ? 4 : 2); m_label_currentTime.redraw(fmt::format("&7Time: {}", local_clock_string())); } diff --git a/main/widgets/menudisplaywithtime.h b/main/widgets/menudisplaywithtime.h index 2f809d6..6964beb 100644 --- a/main/widgets/menudisplaywithtime.h +++ b/main/widgets/menudisplaywithtime.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace bobbygui { class MenuDisplayWithTime : @@ -11,5 +12,14 @@ public: void start() override; void redraw() override; espgui::Label m_label_currentTime{145, 6}; +private: + virtual bool m_use_big_font() const + { +#ifdef MENU_DISPLAY_USE_BIG_TIME + return true; +#else + return false; +#endif + } }; } // namespace From 16ebdfc795b75219ef2f728c045c7311af320900 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Dec 2021 22:00:18 +0100 Subject: [PATCH 27/36] Added deinit() for esp now --- main/espnowfunctions.cpp | 62 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index 34cc4a2..6e1efd0 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -7,6 +7,7 @@ constexpr const char * const TAG = "BOBBY_ESP_NOW"; #include #include #include +#include #include "globals.h" #include "utils.h" @@ -53,7 +54,7 @@ void initESPNow() if (initialized < 1) { - if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled) + if (!settings.wifiSettings.wifiApEnabled && (!settings.wifiSettings.wifiStaEnabled && wifi_stack::get_sta_status() == wifi_stack::WiFiStaStatus::NO_SHIELD) || (wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_STA && wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_AP && wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_APSTA)) { ESP_LOGW(TAG, "cannot execute esp_now_init(): tcp stack is down."); return; @@ -106,15 +107,72 @@ void initESPNow() } initialized = 255; + ESP_LOGI(TAG, "Init done."); } void handle() { - if (initialized < 255 && (settings.wifiSettings.wifiApEnabled || settings.wifiSettings.wifiStaEnabled)) + if (initialized < 255 && !(!settings.wifiSettings.wifiApEnabled && (!settings.wifiSettings.wifiStaEnabled && wifi_stack::get_sta_status() == wifi_stack::WiFiStaStatus::NO_SHIELD) || (wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_STA && wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_AP && wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_APSTA))) { initESPNow(); return; } + else if (!settings.wifiSettings.wifiApEnabled && (!settings.wifiSettings.wifiStaEnabled && wifi_stack::get_sta_status() == wifi_stack::WiFiStaStatus::NO_SHIELD) || (wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_STA && wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_AP && wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_APSTA)) + { + if (initialized > 0) + { + if (initialized >= 4) // peer + { + for (const auto &peer : peers) + { + if (const auto error = esp_now_del_peer(peer.peer_addr); error != ESP_OK) + { + ESP_LOGE(TAG, "esp_now_del_peer() failed with %s", esp_err_to_name(error)); + return; + } + else if (error == ESP_ERR_ESPNOW_NOT_FOUND) + { + initialized = 0; + } + } + initialized--; + } + + if (initialized >= 3) // callback + { + if (const auto error = esp_now_unregister_recv_cb(); error != ESP_OK) + { + ESP_LOGE(TAG, "esp_now_unregister_recv_cb() failed with %s", esp_err_to_name(error)); + return; + } + else if (error == ESP_ERR_ESPNOW_NOT_FOUND) + { + initialized = 0; + } + else + initialized--; + } + + if (initialized >= 2) // esp deinit + { + if (const auto error = esp_now_deinit(); error != ESP_OK) + { + ESP_LOGE(TAG, "esp_now_deinit() failed with %s", esp_err_to_name(error)); + return; + } + else if (error == ESP_ERR_ESPNOW_NOT_FOUND) + { + initialized = 0; + } + else + initialized--; + } + + initialized = 0; + ESP_LOGI(TAG, "Deinit done."); + } + return; + } if(message_queue.size()) { From 180e3fbaa8fd9fa93b25cf579a2c57a469c8a229 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Thu, 16 Dec 2021 21:10:11 +0100 Subject: [PATCH 28/36] Fixed if else --- main/espnowfunctions.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index 6e1efd0..8df01f4 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -127,13 +127,14 @@ void handle() { if (const auto error = esp_now_del_peer(peer.peer_addr); error != ESP_OK) { + if (error == ESP_ERR_ESPNOW_NOT_FOUND) + { + initialized = 0; + return; + } ESP_LOGE(TAG, "esp_now_del_peer() failed with %s", esp_err_to_name(error)); return; } - else if (error == ESP_ERR_ESPNOW_NOT_FOUND) - { - initialized = 0; - } } initialized--; } From 6492531183f31f24ba58768f162ab554d2519a5c Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Thu, 16 Dec 2021 21:56:18 +0100 Subject: [PATCH 29/36] Fixed compiling issues because of includes --- main/dpad3wire.h | 1 + main/dpad5wire.h | 1 + main/dpad5wire_2out.h | 1 + main/dpad6wire.h | 1 + 4 files changed, 4 insertions(+) diff --git a/main/dpad3wire.h b/main/dpad3wire.h index a507729..c2403d7 100644 --- a/main/dpad3wire.h +++ b/main/dpad3wire.h @@ -11,6 +11,7 @@ #include "dpad.h" #include "buttons.h" #include "types.h" +#include "globals.h" namespace { namespace dpad3wire diff --git a/main/dpad5wire.h b/main/dpad5wire.h index 7666f98..1296e8c 100644 --- a/main/dpad5wire.h +++ b/main/dpad5wire.h @@ -13,6 +13,7 @@ // local includes #include "buttons.h" #include "types.h" +#include "globals.h" namespace { namespace dpad5wire { diff --git a/main/dpad5wire_2out.h b/main/dpad5wire_2out.h index 96e870b..34c1aff 100644 --- a/main/dpad5wire_2out.h +++ b/main/dpad5wire_2out.h @@ -13,6 +13,7 @@ // local includes #include "buttons.h" #include "types.h" +#include "globals.h" namespace { namespace dpad5wire_2out { diff --git a/main/dpad6wire.h b/main/dpad6wire.h index 8eb1eef..b353811 100644 --- a/main/dpad6wire.h +++ b/main/dpad6wire.h @@ -12,6 +12,7 @@ // local includes #include "buttons.h" #include "types.h" +#include "globals.h" namespace { namespace dpad6wire { From bcfbb9836255ebea75d077b4f87f9ec31236270d Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Fri, 17 Dec 2021 22:39:36 +0100 Subject: [PATCH 30/36] Moved log --- main/espnowfunctions.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index 8df01f4..387f6bc 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -203,6 +203,7 @@ void handle() if (const auto result = cpputils::fromString(msg.content); result) { + ESP_LOGI(TAG, "setting current time to %" PRIu64, *result); onRecvTs(*result, true); } else @@ -222,7 +223,7 @@ void onRecvTs(uint64_t millis, bool isFromBobbycar) { const auto milliseconds = std::chrono::milliseconds(millis); const auto timepoint = espchrono::utc_clock::time_point(milliseconds); - ESP_LOGW(TAG, "setting current time to %s", espchrono::toString(timepoint.time_since_epoch()).c_str()); + // ESP_LOGW(TAG, "setting current time to %s", espchrono::toString(timepoint.time_since_epoch()).c_str()); time_set_now(timepoint); if (receiveTimeStamp) From b48648f3e25f1fc780582192b256e5dc84fc81e3 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Fri, 17 Dec 2021 22:40:08 +0100 Subject: [PATCH 31/36] advanced lock --- main/displays/calibratedisplay.cpp | 7 +++++++ main/displays/lockscreen.cpp | 15 +++++++++++---- main/main.cpp | 12 ++++++------ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/main/displays/calibratedisplay.cpp b/main/displays/calibratedisplay.cpp index a537308..d007897 100644 --- a/main/displays/calibratedisplay.cpp +++ b/main/displays/calibratedisplay.cpp @@ -5,6 +5,7 @@ #include // local includes +#include "displays/menus/mainmenu.h" #include "displays/statusdisplay.h" #include "displays/menus/boardcomputerhardwaresettingsmenu.h" @@ -241,6 +242,12 @@ void CalibrateDisplay::back() case Status::Begin: if (m_bootup) espgui::switchScreen(); + else if (settings.lockscreen.keepLockedAfterReboot && settings.lockscreen.locked) + { + espgui::switchScreen(); + settings.lockscreen.locked = false; + saveSettings(); + } else espgui::switchScreen(); break; diff --git a/main/displays/lockscreen.cpp b/main/displays/lockscreen.cpp index 6a1551b..efe33c7 100644 --- a/main/displays/lockscreen.cpp +++ b/main/displays/lockscreen.cpp @@ -10,6 +10,7 @@ #include "texts.h" #include "buttons.h" #include "displays/menus/mainmenu.h" +#include "displays/calibratedisplay.h" void Lockscreen::start() { @@ -78,7 +79,10 @@ void Lockscreen::redraw() { if (m_numbers == settings.lockscreen.pin) { - espgui::switchScreen(); + if (!gas || !brems || *gas > 200.f || *brems > 200.f) + espgui::switchScreen(true); + else + espgui::switchScreen(); #ifdef LOCKSCREEN_PLUGIN #pragma message "Activating Lockscreen Plugin" #include LOCKSCREEN_PLUGIN @@ -135,10 +139,13 @@ void Lockscreen::stop() profileButtonDisabled = false; isLocked = false; - if (settings.lockscreen.keepLockedAfterReboot && settings.lockscreen.locked) + if (!(!gas || !brems || *gas > 200.f || *brems > 200.f)) { - settings.lockscreen.locked = false; - saveSettings(); + if (settings.lockscreen.keepLockedAfterReboot && settings.lockscreen.locked) + { + settings.lockscreen.locked = false; + saveSettings(); + } } } diff --git a/main/main.cpp b/main/main.cpp index e4f416c..36240dd 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -234,14 +234,14 @@ extern "C" void app_main() switchScreen(); #else - if (!gas || !brems || *gas > 200.f || *brems > 200.f) - espgui::switchScreen(true); + if (settings.lockscreen.keepLockedAfterReboot && settings.lockscreen.locked) + { + espgui::switchScreen(); + } else { - if (settings.lockscreen.keepLockedAfterReboot && settings.lockscreen.locked) - { - espgui::switchScreen(); - } + if (!gas || !brems || *gas > 200.f || *brems > 200.f) + espgui::switchScreen(true); else { espgui::switchScreen(); From 9ddf2468050f851ba2d0dbfb0ba2d3ce70c15909 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Fri, 17 Dec 2021 23:13:07 +0100 Subject: [PATCH 32/36] Change --- .gitmodules | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitmodules b/.gitmodules index 47f842d..301c25b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,9 +7,6 @@ [submodule "components/cpputils"] path = components/cpputils url = ../../0xFEEDC0DE64/cpputils.git -[submodule "components/espcpputils"] - path = components/espcpputils - url = ../../0xFEEDC0DE64/espcpputils.git [submodule "components/cxx-ring-buffer"] path = components/cxx-ring-buffer url = ../../0xFEEDC0DE64/cxx-ring-buffer.git From a4d80c640dbc7983a3f6ff97ba4d411e48fc31c7 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Fri, 17 Dec 2021 23:28:48 +0100 Subject: [PATCH 33/36] Added error helping --- main/espnowfunctions.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index 387f6bc..8aa4724 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -91,11 +91,16 @@ void initESPNow() esp_now_peer_info_t &peer = peers.back(); std::memcpy(peer.peer_addr, broadcast_address, sizeof(peer.peer_addr)); peer.channel = 0; -/* + if (settings.wifiSettings.wifiApEnabled) peer.ifidx = WIFI_IF_AP; - else if (settings.wifiSettings.wifiStaEnabled)*/ + else if (settings.wifiSettings.wifiStaEnabled) peer.ifidx = WIFI_IF_STA; + else + { + ESP_LOGE(TAG, "Interfaces not ready."); + return; + } if (const auto error = esp_now_add_peer(&peers.back()); error != ESP_OK) { @@ -253,14 +258,27 @@ esp_err_t send_espnow_message(std::string message) return ESP_FAIL; } - for (const auto &peer : peers) + for (auto &peer : peers) { - if(const auto error = esp_now_send(peer.peer_addr, (const uint8_t*)message.data(), message.size()); error != ESP_OK) + + if (settings.wifiSettings.wifiApEnabled) + peer.ifidx = WIFI_IF_AP; + else if (settings.wifiSettings.wifiStaEnabled) + peer.ifidx = WIFI_IF_STA; + else + return ESP_ERR_ESPNOW_IF; + + const auto timeBefore = espchrono::millis_clock::now(); + + if(const auto error = esp_now_send(broadcast_address, (const uint8_t*)message.data(), message.size()); error != ESP_OK) { return error; } else - ESP_LOGI(TAG, "Successfully executed esp_now_send()"); + { + const auto timeAfter = espchrono::millis_clock::now(); + ESP_LOGI(TAG, "Successfully executed esp_now_send(): Took %lldms", std::chrono::milliseconds{timeAfter-timeBefore}.count()); + } } return ESP_OK; } From cef961c174e18b9379a42db576cbe9b435237bb6 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Fri, 17 Dec 2021 23:30:53 +0100 Subject: [PATCH 34/36] Move into anonymous namespace --- main/espnowfunctions.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index 8aa4724..c96b188 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -1,4 +1,3 @@ -constexpr const char * const TAG = "BOBBY_ESP_NOW"; #ifdef FEATURE_ESPNOW #include "espnowfunctions.h" @@ -14,6 +13,9 @@ constexpr const char * const TAG = "BOBBY_ESP_NOW"; #include "time_bobbycar.h" namespace espnow { +namespace { +constexpr const char * const TAG = "BOBBY_ESP_NOW"; +} // namespace uint16_t lastYear; // Used for esp-now timesync From 93556a00fe92d378fdbe8ad585c64bf4293a5ef0 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 18 Dec 2021 00:10:58 +0100 Subject: [PATCH 35/36] =?UTF-8?q?Schei=C3=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config_comred.cmake | 1 + config_feedc0de.cmake | 3 +++ main/espnowfunctions.cpp | 27 +++++++++++++-------------- main/espnowfunctions.h | 14 ++++++++------ 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/config_comred.cmake b/config_comred.cmake index 75835e5..212d602 100644 --- a/config_comred.cmake +++ b/config_comred.cmake @@ -118,6 +118,7 @@ set(BOBBYCAR_BUILDFLAGS -DOLD_NVS -DFEATURE_DNS_NS -DSWITCH_BLINK +# -DFEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT -DFEATURE_ESPNOW ) diff --git a/config_feedc0de.cmake b/config_feedc0de.cmake index 7897f01..27c9c97 100644 --- a/config_feedc0de.cmake +++ b/config_feedc0de.cmake @@ -92,10 +92,13 @@ set(BOBBYCAR_BUILDFLAGS -DFEATURE_LEDSTRIP -DPINS_LEDSTRIP=26 -DLEDSTRIP_LENGTH=200 +# -DHEAP_LRGST_CRASH_TEXT_FIX # -DLEDSTRIP_WRONG_DIRECTION -DLEDSTRIP_ANIMATION_DEFAULT=0 -DLEDS_PER_METER=144 -DOLD_NVS # -DFEATURE_DNS_NS +# -DSWITCH_BLINK -DFEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT + -DFEATURE_ESPNOW ) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index c96b188..bca6bd8 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -20,33 +20,32 @@ constexpr const char * const TAG = "BOBBY_ESP_NOW"; uint16_t lastYear; // Used for esp-now timesync std::deque message_queue{}; -std::list peers{}; +std::vector peers{}; uint8_t initialized{0}; bool receiveTimeStamp{true}; bool receiveTsFromOtherBobbycars{true}; -void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) +extern "C" void onReceive(const uint8_t *mac_addr, const char *data, int data_len) { ESP_LOGD(TAG, "Received data"); - const std::string data_str(data, data+data_len); + const std::string_view data_str{data, size_t(data_len)}; size_t sep_pos = data_str.find(":"); - if (std::string::npos != sep_pos) + if (std::string_view::npos != sep_pos) { - std::string msg_type = data_str.substr(0, sep_pos); - std::string msg = data_str.substr(sep_pos+1, data_str.length()-3); // - 3 may needs to be converted to sep_pos+1 - ESP_LOGD(TAG, "Type: %s - Message: %s", msg_type.c_str(), msg.c_str()); + std::string_view msg_type = data_str.substr(0, sep_pos); + std::string_view msg = data_str.substr(sep_pos+1, data_str.length()-3); // - 3 may needs to be converted to sep_pos+1 + ESP_LOGD(TAG, "Type: %.*s - Message: %.*s", msg_type.size(), msg_type.data(), msg.size(), msg.data()); - const esp_now_message_t message{ - .content = msg, - .type = msg_type - }; - message_queue.push_back(message); + message_queue.push_back(esp_now_message_t { + .content = std::string{msg}, + .type = std::string{msg_type} + }); } else { - ESP_LOGW(TAG, "Invalid message: Could not find ':' (%s)", data_str.c_str()); + ESP_LOGW(TAG, "Invalid message: Could not find ':' (%.*s)", data_str.size(), data_str.data()); } } @@ -245,7 +244,7 @@ void onRecvTs(uint64_t millis, bool isFromBobbycar) receiveTsFromOtherBobbycars = false; } -esp_err_t send_espnow_message(std::string message) +esp_err_t send_espnow_message(std::string_view message) { if (initialized < 255) return ESP_ERR_ESPNOW_NOT_INIT; diff --git a/main/espnowfunctions.h b/main/espnowfunctions.h index 988900a..3f5203c 100644 --- a/main/espnowfunctions.h +++ b/main/espnowfunctions.h @@ -3,27 +3,29 @@ #include #include #include -#include +#include +#include #include namespace espnow { extern uint16_t lastYear; constexpr const uint8_t broadcast_address[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; -struct esp_now_message_t { - const std::string content; - const std::string type; +struct esp_now_message_t +{ + std::string content; + std::string type; }; extern bool receiveTimeStamp; extern bool receiveTsFromOtherBobbycars; extern std::deque message_queue; -extern std::list peers; +extern std::vector peers; void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len); void initESPNow(); void handle(); void onRecvTs(uint64_t millis, bool isFromBobbycar = false); -esp_err_t send_espnow_message(std::string message); +esp_err_t send_espnow_message(std::string_view message); } // namespace espnow #endif From f47bf772efff3560a66607f749dbf6365d16073b Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 18 Dec 2021 00:24:56 +0100 Subject: [PATCH 36/36] Dodln --- main/espnowfunctions.cpp | 6 ++++-- main/espnowfunctions.h | 1 - 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index bca6bd8..841a2b6 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -26,10 +26,11 @@ uint8_t initialized{0}; bool receiveTimeStamp{true}; bool receiveTsFromOtherBobbycars{true}; -extern "C" void onReceive(const uint8_t *mac_addr, const char *data, int data_len) +namespace { +extern "C" void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) { ESP_LOGD(TAG, "Received data"); - const std::string_view data_str{data, size_t(data_len)}; + const std::string_view data_str{(const char *)data, size_t(data_len)}; size_t sep_pos = data_str.find(":"); if (std::string_view::npos != sep_pos) @@ -48,6 +49,7 @@ extern "C" void onReceive(const uint8_t *mac_addr, const char *data, int data_le ESP_LOGW(TAG, "Invalid message: Could not find ':' (%.*s)", data_str.size(), data_str.data()); } } +} // namespace void initESPNow() { diff --git a/main/espnowfunctions.h b/main/espnowfunctions.h index 3f5203c..99f7726 100644 --- a/main/espnowfunctions.h +++ b/main/espnowfunctions.h @@ -22,7 +22,6 @@ extern bool receiveTsFromOtherBobbycars; extern std::deque message_queue; extern std::vector peers; -void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len); void initESPNow(); void handle(); void onRecvTs(uint64_t millis, bool isFromBobbycar = false);