Merge pull request #210 from bobbycar-graz/esp-now-cleanup
This commit is contained in:
@ -254,3 +254,6 @@ struct ButtonProfile0Accessor : public NewSettingsAccessor<uint8_t> { ConfigWrap
|
||||
struct ButtonProfile1Accessor : public NewSettingsAccessor<uint8_t> { ConfigWrapper<uint8_t> &getConfig() const override { return configs.dpadMappingProfile1; } };
|
||||
struct ButtonProfile2Accessor : public NewSettingsAccessor<uint8_t> { ConfigWrapper<uint8_t> &getConfig() const override { return configs.dpadMappingProfile2; } };
|
||||
struct ButtonProfile3Accessor : public NewSettingsAccessor<uint8_t> { ConfigWrapper<uint8_t> &getConfig() const override { return configs.dpadMappingProfile3; } };
|
||||
|
||||
// Can
|
||||
struct CanResetOnErrorAccessor : public NewSettingsAccessor<bool> { ConfigWrapper<bool> &getConfig() const override { return configs.canResetOnError; } };
|
||||
|
30
main/can.cpp
30
main/can.cpp
@ -273,14 +273,13 @@ void sendCanCommands()
|
||||
|
||||
const auto timestamp_before = espchrono::millis_clock::now();
|
||||
const auto result = twai_transmit(&message, timeout);
|
||||
|
||||
if (result != ESP_OK && result != ESP_ERR_TIMEOUT)
|
||||
{
|
||||
ESP_LOGE(TAG, "ERROR: twai_transmit() failed with %s", esp_err_to_name(result));
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (result == ESP_ERR_TIMEOUT) {
|
||||
else if (result == ESP_ERR_TIMEOUT)
|
||||
{
|
||||
++can_sequential_error_cnt;
|
||||
++can_total_error_cnt;
|
||||
|
||||
@ -288,20 +287,29 @@ void sendCanCommands()
|
||||
espchrono::ago(timestamp_before).count(),
|
||||
can_sequential_error_cnt,
|
||||
can_total_error_cnt);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
can_sequential_error_cnt = 0;
|
||||
}
|
||||
|
||||
|
||||
if (can_sequential_error_cnt > 3) {
|
||||
if (can_sequential_error_cnt > 3)
|
||||
{
|
||||
can_sequential_error_cnt = 0;
|
||||
if (configs.canBusResetOnError.value) {
|
||||
ESP_LOGE(TAG, "CAN BUS RESET: twai_stop(): %s", esp_err_to_name(twai_stop()));
|
||||
ESP_LOGE(TAG, "CAN BUS RESET: twai_start(): %s", esp_err_to_name(twai_start()));
|
||||
|
||||
if (configs.canResetOnError.value)
|
||||
{
|
||||
ESP_LOGW(TAG, "WARNING: Something isn't right, trying to restart can ic...");
|
||||
if (const auto err = twai_stop(); err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ERROR: twai_stop() failed with %s", esp_err_to_name(err));
|
||||
}
|
||||
if (const auto err = twai_start(); err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ERROR: twai_start() failed with %s", esp_err_to_name(err));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
|
@ -31,6 +31,7 @@ constexpr char TEXT_FRONTSENDCAN[] = "Front send CAN";
|
||||
constexpr char TEXT_BACKSENDCAN[] = "Back send CAN";
|
||||
constexpr char TEXT_CANTRANSMITTIMEOUT[] = "CanTransmitTimeout";
|
||||
constexpr char TEXT_CANRECEIVETIMEOUT[] = "CanReceiveTimeout";
|
||||
constexpr char TEXT_CANRESETONERROR[] = "Reset on error";
|
||||
#endif
|
||||
constexpr char TEXT_BACK[] = "Back";
|
||||
|
||||
@ -77,19 +78,20 @@ using CanReceiveTimeoutChangeScreen = espgui::makeComponent<
|
||||
ControllerHardwareSettingsMenu::ControllerHardwareSettingsMenu()
|
||||
{
|
||||
using namespace espgui;
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_SETENABLED>, SwitchScreenAction<EnableMenu>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_SETINVERTED>, SwitchScreenAction<InvertMenu>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_WHEELDIAMETERMM>, SwitchScreenAction<WheelDiameterMmChangeScreen>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_WHEELDIAMETERINCH>, SwitchScreenAction<WheelDiameterInchChangeScreen>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_NUMMAGNETPOLES>, SwitchScreenAction<NumMagnetPolesChangeScreen>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_SWAPFRONTBACK>, BobbyCheckbox, SwapFrontBackAccessor>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_SETENABLED>, SwitchScreenAction<EnableMenu>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_SETINVERTED>, SwitchScreenAction<InvertMenu>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_WHEELDIAMETERMM>, SwitchScreenAction<WheelDiameterMmChangeScreen>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_WHEELDIAMETERINCH>, SwitchScreenAction<WheelDiameterInchChangeScreen>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_NUMMAGNETPOLES>, SwitchScreenAction<NumMagnetPolesChangeScreen>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_SWAPFRONTBACK>, BobbyCheckbox, SwapFrontBackAccessor>>();
|
||||
#ifdef FEATURE_CAN
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_FRONTSENDCAN>, BobbyCheckbox, SendFrontCanCmdAccessor>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACKSENDCAN>, BobbyCheckbox, SendBackCanCmdAccessor>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_CANTRANSMITTIMEOUT>, SwitchScreenAction<CanTransmitTimeoutChangeScreen>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_CANRECEIVETIMEOUT>, SwitchScreenAction<CanReceiveTimeoutChangeScreen>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_CANRESETONERROR>, BobbyCheckbox, CanResetOnErrorAccessor>>();
|
||||
#endif
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<SettingsMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<SettingsMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
|
||||
}
|
||||
|
||||
std::string ControllerHardwareSettingsMenu::text() const
|
||||
|
@ -1,8 +1,6 @@
|
||||
#ifdef FEATURE_ESPNOW
|
||||
#include "espnowfunctions.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <espchrono.h>
|
||||
#include <esp_log.h>
|
||||
#include <numberparsing.h>
|
||||
@ -20,7 +18,6 @@ constexpr const char * const TAG = "BOBBY_ESP_NOW";
|
||||
|
||||
uint16_t lastYear; // Used for esp-now timesync
|
||||
|
||||
std::deque<esp_now_message_t> message_queue{};
|
||||
std::vector<esp_now_peer_info_t> peers{};
|
||||
uint8_t initialized{0};
|
||||
|
||||
@ -36,14 +33,46 @@ extern "C" void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data
|
||||
size_t sep_pos = data_str.find(":");
|
||||
if (std::string_view::npos != sep_pos)
|
||||
{
|
||||
std::string_view msg_type = data_str.substr(0, sep_pos);
|
||||
std::string_view msg = data_str.substr(sep_pos+1, data_str.length()-sep_pos-1);
|
||||
ESP_LOGD(TAG, "Type: %.*s - Message: %.*s", msg_type.size(), msg_type.data(), msg.size(), msg.data());
|
||||
esp_now_message_t msg{
|
||||
.content = std::string{data_str.substr(sep_pos+1, data_str.length()-sep_pos-1)},
|
||||
.type = std::string{data_str.substr(0, sep_pos)}
|
||||
};
|
||||
|
||||
message_queue.push_back(esp_now_message_t {
|
||||
.content = std::string{msg},
|
||||
.type = std::string{msg_type}
|
||||
});
|
||||
ESP_LOGD(TAG, "Type: %s - Message: %s", msg.type.c_str(), msg.content.c_str());
|
||||
|
||||
if (msg.type == "T")
|
||||
{
|
||||
if (!receiveTimeStamp || !settings.espnow.syncTime)
|
||||
return;
|
||||
|
||||
if (const auto result = cpputils::fromString<uint64_t>(msg.content); result)
|
||||
{
|
||||
onRecvTs(*result);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "could not parse number: %.*s", result.error().size(), result.error().data());
|
||||
}
|
||||
}
|
||||
else if (msg.type == "BOBBYT")
|
||||
{
|
||||
if (!receiveTsFromOtherBobbycars || !settings.espnow.syncTimeWithOthers)
|
||||
return;
|
||||
|
||||
if (const auto result = cpputils::fromString<uint64_t>(msg.content); result)
|
||||
{
|
||||
ESP_LOGI(TAG, "setting current time to %" PRIu64, *result);
|
||||
onRecvTs(*result, true);
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -183,49 +212,6 @@ void handle()
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if(message_queue.size())
|
||||
{
|
||||
for (const esp_now_message_t &msg : message_queue)
|
||||
{
|
||||
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")
|
||||
{
|
||||
if (!receiveTimeStamp || !settings.espnow.syncTime)
|
||||
return;
|
||||
|
||||
if (const auto result = cpputils::fromString<uint64_t>(msg.content); result)
|
||||
{
|
||||
onRecvTs(*result);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "could not parse number: %.*s", result.error().size(), result.error().data());
|
||||
}
|
||||
}
|
||||
else if (msg.type == "BOBBYT")
|
||||
{
|
||||
if (!receiveTsFromOtherBobbycars || !settings.espnow.syncTimeWithOthers)
|
||||
return;
|
||||
|
||||
if (const auto result = cpputils::fromString<uint64_t>(msg.content); result)
|
||||
{
|
||||
ESP_LOGI(TAG, "setting current time to %" PRIu64, *result);
|
||||
onRecvTs(*result, true);
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void onRecvTs(uint64_t millis, bool isFromBobbycar)
|
||||
|
@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
#ifdef FEATURE_ESPNOW
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
@ -19,7 +18,6 @@ struct esp_now_message_t
|
||||
extern bool receiveTimeStamp;
|
||||
extern bool receiveTsFromOtherBobbycars;
|
||||
|
||||
extern std::deque<esp_now_message_t> message_queue;
|
||||
extern std::vector<esp_now_peer_info_t> peers;
|
||||
|
||||
void initESPNow();
|
||||
|
@ -118,7 +118,7 @@ public:
|
||||
ConfigWrapper<espchrono::minutes32> timezoneOffset{espchrono::minutes32{60}, DoReset, {}, "timezoneOffset" }; // MinMaxValue<minutes32, -1440m, 1440m>
|
||||
ConfigWrapper<espchrono::DayLightSavingMode>timeDst{espchrono::DayLightSavingMode::EuropeanSummerTime, DoReset, {}, "time_dst" };
|
||||
|
||||
ConfigWrapper<bool> canBusResetOnError {false, DoReset, {}, "canBusRstErr" };
|
||||
ConfigWrapper<bool> canResetOnError {false, DoReset, {}, "canBusRstErr" };
|
||||
|
||||
ConfigWrapper<int16_t> sampleCount {50, DoReset, {}, "sampleCount" };
|
||||
ConfigWrapper<int16_t> gasMin {0, DoReset, MinMaxValue<int16_t, 0, 4095>, "gasMin" };
|
||||
@ -301,7 +301,7 @@ public:
|
||||
x(timezoneOffset) \
|
||||
x(timeDst) \
|
||||
\
|
||||
x(canBusResetOnError) \
|
||||
x(canResetOnError) \
|
||||
\
|
||||
x(sampleCount) \
|
||||
x(gasMin) \
|
||||
|
Reference in New Issue
Block a user