diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 664dc54..a6a6f90 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -38,6 +38,7 @@ set(headers bluetoothmode.h bluetoothtexthelpers.h bmsutils.h + bobbyblinker.h bobbybuttons.h bobbycheckbox.h bobbyerrorhandler.h @@ -281,6 +282,7 @@ set(sources bluetoothmode.cpp bluetoothtexthelpers.cpp bmsutils.cpp + bobbyblinker.cpp bobbybuttons.cpp bobbyerrorhandler.cpp bobbyhupe.cpp diff --git a/main/bobbyblinker.cpp b/main/bobbyblinker.cpp new file mode 100644 index 0000000..c4a7c59 --- /dev/null +++ b/main/bobbyblinker.cpp @@ -0,0 +1,87 @@ +#include "bobbyblinker.h" + +// system includes +#include + +// 3rdparty lib includes +#include + +// local includes +#include "globals.h" +#include "espnowfunctions.h" +#include "ledstrip.h" + +using namespace std::chrono_literals; + +namespace { + constexpr const char * const TAG = "BOBBY_BLINKER"; + + void sendState(const std::string& state) + { + if (const auto error = espnow::send_espnow_message(fmt::format("{}:0:0", state)); error != ESP_OK) + { + ESP_LOGE(TAG, "Error sending blinker message: %s", esp_err_to_name(error)); + } + } + + bool hasSendNegativePwm{false}; +} // namespace + +namespace bobbyblinker { + std::optional blinker_last_time_sent; + + void handle_blinker() + { + if (!configs.espnow.syncBlink.value) + return; + + const bool blinker_state = (cpputils::is_in(blinkAnimation, LEDSTRIP_OVERWRITE_BLINKLEFT, LEDSTRIP_OVERWRITE_BLINKRIGHT, LEDSTRIP_OVERWRITE_BLINKBOTH)); + if ((blinker_state && !blinker_last_time_sent) || (blinker_state && blinker_last_time_sent && espchrono::ago(*blinker_last_time_sent) > 1s)) + { + blinker_last_time_sent = espchrono::millis_clock::now(); + if (blinkAnimation == LEDSTRIP_OVERWRITE_BLINKLEFT) + { + sendState("BLINKLEFT"); + } + else if (blinkAnimation == LEDSTRIP_OVERWRITE_BLINKRIGHT) + { + sendState("BLINKRIGHT"); + } + else if (blinkAnimation == LEDSTRIP_OVERWRITE_BLINKBOTH) + { + sendState("BLINKBOTH"); + } + else + { + sendState("BLINKOFF"); + } + + if (configs.ledstrip.enableBrakeLights.value) + { + float avgPwm{}; + for (const Controller &controller: controllers) + { + avgPwm += + controller.command.left.pwm * (controller.invertLeft ? -1 : 1) + + controller.command.right.pwm * (controller.invertRight ? -1 : 1); + } + avgPwm /= 4; + + if (avgPwm < -1.f && !hasSendNegativePwm) + { + sendState("BRAKELIGHTSON"); + hasSendNegativePwm = true; + } + else if (avgPwm > -1.f && hasSendNegativePwm) + { + sendState("BRAKELIGHTSOFF"); + hasSendNegativePwm = false; + } + } + } + else if (!blinker_state && blinker_last_time_sent) + { + blinker_last_time_sent = std::nullopt; + } + } +} // namespace bobbyblinker diff --git a/main/bobbyblinker.h b/main/bobbyblinker.h new file mode 100644 index 0000000..b40ec9d --- /dev/null +++ b/main/bobbyblinker.h @@ -0,0 +1,9 @@ +#pragma once + +// 3rdparty lib includes +#include + +namespace bobbyblinker { + extern std::optional blinker_last_time_sent; + void handle_blinker(); +} // namespace bobbyhupe diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index a58b450..7b72704 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -12,6 +12,7 @@ #include "time_bobbycar.h" #include "newsettings.h" #include "bobbyhupe.h" +#include "bobbyblinker.h" namespace espnow { uint16_t lastYear; // Used for esp-now timesync @@ -146,6 +147,7 @@ void initESPNow() void handle() { bobbyhupe::handle_hupe(); + bobbyblinker::handle_blinker(); if (initialized < 255 && espnow_init_allowed()) { initESPNow();