Compare commits
No commits in common. "3720ec76d23ffb3766e471dd4e14ecdb8ba47d30" and "2e4171fac4779b711098239c22b52afa96aa489e" have entirely different histories.
3720ec76d2
...
2e4171fac4
@ -3,7 +3,6 @@
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <variant>
|
||||
#include <cstdio>
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_mac.h"
|
||||
@ -36,11 +35,6 @@ bool s_isBroadcastAddress(const esp_now_recv_info_t* sender)
|
||||
return s_isBroadcastAddress(sender->src_addr);
|
||||
}
|
||||
|
||||
constexpr MacAddress s_getBroadcastAddress()
|
||||
{
|
||||
return MacAddress{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
}
|
||||
|
||||
esp_err_t s_addPeer(const MacAddress& peer_address)
|
||||
{
|
||||
esp_now_peer_info_t broadcast_peer;
|
||||
@ -102,7 +96,7 @@ QueueHandle_t setupEspNow()
|
||||
ESP_ERROR_CHECK(esp_now_register_send_cb(s_cbEspNowSendComplete));
|
||||
ESP_ERROR_CHECK(esp_now_register_recv_cb(s_cbEspNowReceiveComplete));
|
||||
|
||||
const auto broadcast_mac = s_getBroadcastAddress();
|
||||
const auto broadcast_mac = MacAddress{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
s_peer = broadcast_mac;
|
||||
|
||||
ESP_ERROR_CHECK(s_addPeer(broadcast_mac));
|
||||
@ -110,19 +104,6 @@ QueueHandle_t setupEspNow()
|
||||
return s_esp_now_queue;
|
||||
}
|
||||
|
||||
void sendHello()
|
||||
{
|
||||
MacAddress mac;
|
||||
ESP_ERROR_CHECK(esp_read_mac(mac.data(), ESP_MAC_WIFI_STA));
|
||||
|
||||
char buffer[100];
|
||||
const std::size_t length = std::sprintf(buffer, "Hello from %02X:%02X:%02X:%02X:%02X:%02X.\n\r",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
|
||||
const auto broadcast_mac = s_getBroadcastAddress();
|
||||
ESP_ERROR_CHECK(esp_now_send(broadcast_mac.data(), (uint8_t*)buffer, length));
|
||||
}
|
||||
|
||||
const std::array<std::uint8_t, 128>& getRxBuffer()
|
||||
{
|
||||
return s_rx_buffer;
|
||||
|
||||
@ -26,7 +26,6 @@ using MacAddress = std::array<std::uint8_t, 6>;
|
||||
|
||||
void setupWifi();
|
||||
[[nodiscard]] QueueHandle_t setupEspNow();
|
||||
void sendHello();
|
||||
|
||||
void sendData(const std::array<std::uint8_t, 128>& buffer, const std::size_t length);
|
||||
const std::array<std::uint8_t, 128>& getRxBuffer();
|
||||
|
||||
@ -28,16 +28,11 @@ QueueHandle_t setupSerial(const uart_port_t uart)
|
||||
.source_clk = UART_SCLK_DEFAULT,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(uart_driver_install(uart, 128 * 2, 128 * 2, 4, &s_uart_queue, 0));
|
||||
ESP_ERROR_CHECK(uart_driver_install(uart, 132, 128 * 2, 4, &s_uart_queue, 0));
|
||||
ESP_ERROR_CHECK(uart_param_config(uart, &config));
|
||||
ESP_ERROR_CHECK(uart_set_pin(uart, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
||||
|
||||
return s_uart_queue;
|
||||
}
|
||||
|
||||
void discardRxBuffer(const uart_port_t uart)
|
||||
{
|
||||
ESP_ERROR_CHECK(uart_flush(uart));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -13,6 +13,4 @@ namespace Serial
|
||||
|
||||
[[nodiscard]] QueueHandle_t setupSerial(const uart_port_t uart);
|
||||
|
||||
void discardRxBuffer(const uart_port_t uart);
|
||||
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <stdio.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "app_networking.hpp"
|
||||
#include "app_serial.hpp"
|
||||
@ -27,15 +26,13 @@ QueueSetHandle_t s_queue_set = nullptr;
|
||||
QueueHandle_t s_esp_now_queue = nullptr;
|
||||
QueueHandle_t s_uart_queue = nullptr;
|
||||
|
||||
void s_sendUartData(std::size_t len)
|
||||
void s_sendUartData(const std::size_t len)
|
||||
{
|
||||
len = std::clamp<std::size_t>(len, 1, 128);
|
||||
ESP_LOGD(LOG_TAG, "Sending %u bytes via UART.", len);
|
||||
|
||||
std::array<std::uint8_t, 128> tx_buffer;
|
||||
uart_read_bytes(CONFIG_ESPTNL_UART, tx_buffer.data(), len, 0);
|
||||
|
||||
xTimerStop(s_timer, 0);
|
||||
Networking::sendData(tx_buffer, len);
|
||||
}
|
||||
|
||||
@ -57,7 +54,7 @@ std::size_t s_uartBytesWaiting()
|
||||
if (queue_select == s_esp_now_queue)
|
||||
{
|
||||
Networking::EspNowEvent event;
|
||||
xQueueReceive(s_esp_now_queue, &event, 0);
|
||||
xQueueReceive(queue_select, &event, 0);
|
||||
|
||||
ESP_LOGD(LOG_TAG, "ESP-NOW event: %u, rx-length: %ull.", event.type, event.rx_length);
|
||||
|
||||
@ -84,6 +81,7 @@ std::size_t s_uartBytesWaiting()
|
||||
if (s_uartBytesWaiting() >= 128)
|
||||
{
|
||||
s_sendUartData(bytes_waiting);
|
||||
xTimerStop(s_timer, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -96,26 +94,11 @@ std::size_t s_uartBytesWaiting()
|
||||
|
||||
if (event.type == UART_DATA)
|
||||
{
|
||||
const std::size_t recv_size = s_uartBytesWaiting();
|
||||
if (recv_size >= 128)
|
||||
s_sendUartData(recv_size);
|
||||
if (event.size >= 128)
|
||||
s_sendUartData(event.size);
|
||||
else
|
||||
xTimerReset(s_timer, 0);
|
||||
}
|
||||
else if (event.type == UART_BUFFER_FULL)
|
||||
{
|
||||
const std::size_t recv_size = s_uartBytesWaiting();
|
||||
s_sendUartData(recv_size);
|
||||
}
|
||||
else if (event.type == UART_FIFO_OVF)
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "UART FIFO overflow. Flushing.");
|
||||
Serial::discardRxBuffer(CONFIG_ESPTNL_UART);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(LOG_TAG, "Unknown event type.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -151,9 +134,6 @@ extern "C" void app_main(void)
|
||||
xQueueAddToSet(s_esp_now_queue, s_queue_set);
|
||||
xQueueAddToSet(s_uart_queue, s_queue_set);
|
||||
|
||||
ESP_LOGI(LOG_TAG, "Setup completed. Sending hello.");
|
||||
|
||||
Networking::sendHello();
|
||||
|
||||
ESP_LOGI(LOG_TAG, "Setup completed.");
|
||||
xTaskCreate(s_mainTask, "main_task", 2048, nullptr, 4, &s_main_task);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user