This commit is contained in:
2021-12-18 00:10:58 +01:00
parent cef961c174
commit 93556a00fe
4 changed files with 25 additions and 20 deletions

View File

@ -118,6 +118,7 @@ set(BOBBYCAR_BUILDFLAGS
-DOLD_NVS
-DFEATURE_DNS_NS
-DSWITCH_BLINK
# -DFEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT
-DFEATURE_ESPNOW
)

View File

@ -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
)

View File

@ -20,33 +20,32 @@ 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::list<esp_now_peer_info_t> peers{};
std::vector<esp_now_peer_info_t> 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;

View File

@ -3,27 +3,29 @@
#include <cstdint>
#include <deque>
#include <string>
#include <list>
#include <string_view>
#include <vector>
#include <esp_now.h>
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<esp_now_message_t> message_queue;
extern std::list<esp_now_peer_info_t> peers;
extern std::vector<esp_now_peer_info_t> 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