Added bobby blinker for esp now
This commit is contained in:
@@ -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
|
||||
|
87
main/bobbyblinker.cpp
Normal file
87
main/bobbyblinker.cpp
Normal 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
9
main/bobbyblinker.h
Normal 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
|
@@ -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();
|
||||
|
Reference in New Issue
Block a user