Added bobby blinker for esp now

This commit is contained in:
CommanderRedYT
2022-05-03 01:33:15 +02:00
parent 4540a2afbf
commit 950d46b109
4 changed files with 100 additions and 0 deletions

View File

@@ -38,6 +38,7 @@ set(headers
bluetoothmode.h bluetoothmode.h
bluetoothtexthelpers.h bluetoothtexthelpers.h
bmsutils.h bmsutils.h
bobbyblinker.h
bobbybuttons.h bobbybuttons.h
bobbycheckbox.h bobbycheckbox.h
bobbyerrorhandler.h bobbyerrorhandler.h
@@ -281,6 +282,7 @@ set(sources
bluetoothmode.cpp bluetoothmode.cpp
bluetoothtexthelpers.cpp bluetoothtexthelpers.cpp
bmsutils.cpp bmsutils.cpp
bobbyblinker.cpp
bobbybuttons.cpp bobbybuttons.cpp
bobbyerrorhandler.cpp bobbyerrorhandler.cpp
bobbyhupe.cpp bobbyhupe.cpp

87
main/bobbyblinker.cpp Normal file
View File

@@ -0,0 +1,87 @@
#include "bobbyblinker.h"
// system includes
#include <esp_log.h>
// 3rdparty lib includes
#include <cpputils.h>
// 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<espchrono::millis_clock::time_point> 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

9
main/bobbyblinker.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
// 3rdparty lib includes
#include <espchrono.h>
namespace bobbyblinker {
extern std::optional<espchrono::millis_clock::time_point> blinker_last_time_sent;
void handle_blinker();
} // namespace bobbyhupe

View File

@@ -12,6 +12,7 @@
#include "time_bobbycar.h" #include "time_bobbycar.h"
#include "newsettings.h" #include "newsettings.h"
#include "bobbyhupe.h" #include "bobbyhupe.h"
#include "bobbyblinker.h"
namespace espnow { namespace espnow {
uint16_t lastYear; // Used for esp-now timesync uint16_t lastYear; // Used for esp-now timesync
@@ -146,6 +147,7 @@ void initESPNow()
void handle() void handle()
{ {
bobbyhupe::handle_hupe(); bobbyhupe::handle_hupe();
bobbyblinker::handle_blinker();
if (initialized < 255 && espnow_init_allowed()) if (initialized < 255 && espnow_init_allowed())
{ {
initESPNow(); initESPNow();