From 86e2d3a6b885b090e59fcc95cf612e66046b24e6 Mon Sep 17 00:00:00 2001 From: erki Date: Mon, 26 Jun 2023 15:46:35 +0300 Subject: [PATCH] Make input and output buffer sizes configurable --- main/Kconfig.projbuild | 7 +++++++ main/app_networking.cpp | 6 +++--- main/app_networking.hpp | 4 ++-- main/app_serial.cpp | 2 +- main/esp_tunnel.cpp | 10 +++++----- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index c310a28..4a9613d 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -25,6 +25,13 @@ menu "EspTunnel Configuration" help The UART interface to use for RX and TX. + config ESPTNL_BUFFER_SIZE + int "Buffer size" + default 128 + range 40 218 + help + The maximum size of the buffer to transmit via ESPNOW. + choice ESPTNL_LOG_LEVEL prompt "Log level" default ESPTNL_LOG_DEBUG diff --git a/main/app_networking.cpp b/main/app_networking.cpp index 1b865d2..98f5ab2 100644 --- a/main/app_networking.cpp +++ b/main/app_networking.cpp @@ -22,7 +22,7 @@ static_assert(std::is_standard_layout_v && std::is_trivial_v s_rx_buffer; +std::array s_rx_buffer; std::variant s_peer; bool s_isBroadcastAddress(const std::uint8_t* mac) @@ -123,12 +123,12 @@ void sendHello() ESP_ERROR_CHECK(esp_now_send(broadcast_mac.data(), (uint8_t*)buffer, length)); } -const std::array& getRxBuffer() +const std::array& getRxBuffer() { return s_rx_buffer; } -void sendData(const std::array& buffer, const std::size_t length) +void sendData(const std::array& buffer, const std::size_t length) { if (const MacAddress* peer_mac = std::get_if(&s_peer); peer_mac != nullptr) diff --git a/main/app_networking.hpp b/main/app_networking.hpp index 1ad9c4e..2e81b64 100644 --- a/main/app_networking.hpp +++ b/main/app_networking.hpp @@ -28,7 +28,7 @@ void setupWifi(); [[nodiscard]] QueueHandle_t setupEspNow(); void sendHello(); -void sendData(const std::array& buffer, const std::size_t length); -const std::array& getRxBuffer(); +void sendData(const std::array& buffer, const std::size_t length); +const std::array& getRxBuffer(); }// namespace App diff --git a/main/app_serial.cpp b/main/app_serial.cpp index c8e4a94..bd6a9c4 100644 --- a/main/app_serial.cpp +++ b/main/app_serial.cpp @@ -28,7 +28,7 @@ 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, CONFIG_ESPTNL_BUFFER_SIZE * 2, CONFIG_ESPTNL_BUFFER_SIZE * 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)); diff --git a/main/esp_tunnel.cpp b/main/esp_tunnel.cpp index 8cc0175..873be05 100644 --- a/main/esp_tunnel.cpp +++ b/main/esp_tunnel.cpp @@ -29,10 +29,10 @@ QueueHandle_t s_uart_queue = nullptr; void s_sendUartData(std::size_t len) { - len = std::clamp(len, 1, 128); + len = std::clamp(len, 1, CONFIG_ESPTNL_BUFFER_SIZE); ESP_LOGD(LOG_TAG, "Sending %u bytes via UART.", len); - std::array tx_buffer; + std::array tx_buffer; uart_read_bytes(CONFIG_ESPTNL_UART, tx_buffer.data(), len, 0); xTimerStop(s_timer, 0); @@ -43,7 +43,7 @@ std::size_t s_uartBytesWaiting() { std::size_t len = 0; uart_get_buffered_data_len(CONFIG_ESPTNL_UART, &len); - return std::min(len, 128); + return std::min(len, CONFIG_ESPTNL_BUFFER_SIZE); } [[noreturn]] void s_mainTask(void*) @@ -81,7 +81,7 @@ std::size_t s_uartBytesWaiting() ESP_LOGD(LOG_TAG, "ESP-NOW event: TX completed. Checking to send data."); const std::size_t bytes_waiting = s_uartBytesWaiting(); - if (s_uartBytesWaiting() >= 128) + if (s_uartBytesWaiting() >= CONFIG_ESPTNL_BUFFER_SIZE) { s_sendUartData(bytes_waiting); } @@ -97,7 +97,7 @@ std::size_t s_uartBytesWaiting() if (event.type == UART_DATA) { const std::size_t recv_size = s_uartBytesWaiting(); - if (recv_size >= 128) + if (recv_size >= CONFIG_ESPTNL_BUFFER_SIZE) s_sendUartData(recv_size); else xTimerReset(s_timer, 0);