diff --git a/.gitignore b/.gitignore index aa0e1fd..af02648 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ build/ sdkconfig sdkconfig.old +managed_components/ + .vscode/ cmake-build-*/ diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index a35874f..caab1b0 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -2,4 +2,5 @@ idf_component_register(SRCS "esp_tunnel.cpp" "app_networking.cpp" "app_serial.cpp" "app_config.cpp" + "app_led.cpp" INCLUDE_DIRS ".") diff --git a/main/app_led.cpp b/main/app_led.cpp new file mode 100644 index 0000000..58321fc --- /dev/null +++ b/main/app_led.cpp @@ -0,0 +1,64 @@ +// +// Created by erki on 26/06/23. +// + +#include "app_led.hpp" + +#include "led_strip.h" + +namespace +{ + +led_strip_handle_t s_strip; +bool s_tx_on = false; +bool s_rx_on = false; + +void s_updateStrip() +{ + const int red = s_tx_on ? 10 : 0; + const int blue = s_rx_on ? 10 : 0; + + led_strip_set_pixel(s_strip, 0, red, 10, blue); + led_strip_refresh(s_strip); +} + +} + +namespace Led +{ + +void setupLed(const gpio_num_t gpio) +{ + led_strip_config_t config = { + .strip_gpio_num = gpio, + .max_leds = 1, + .led_pixel_format = LED_PIXEL_FORMAT_GRB, + .led_model = LED_MODEL_WS2812, + .flags = { 0 } + }; + + led_strip_rmt_config_t rmt_config = { + .clk_src = RMT_CLK_SRC_DEFAULT, + .resolution_hz = 10 * 1000 * 1000, + .mem_block_symbols = 0, + .flags = { 0 } + }; + + ESP_ERROR_CHECK(led_strip_new_rmt_device(&config, &rmt_config, &s_strip)); + led_strip_clear(s_strip); + s_updateStrip(); +} + +void flashRx() +{ + s_rx_on = !s_rx_on; + s_updateStrip(); +} + +void flashTx() +{ + s_tx_on = !s_tx_on; + s_updateStrip(); +} + +} diff --git a/main/app_led.hpp b/main/app_led.hpp new file mode 100644 index 0000000..05165ce --- /dev/null +++ b/main/app_led.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace Led +{ + +void setupLed(const gpio_num_t gpio); +void flashRx(); +void flashTx(); + +} // namespace Led + diff --git a/main/esp_tunnel.cpp b/main/esp_tunnel.cpp index 873be05..7bdaf5d 100644 --- a/main/esp_tunnel.cpp +++ b/main/esp_tunnel.cpp @@ -6,6 +6,7 @@ #include "app_networking.hpp" #include "app_serial.hpp" #include "app_config.hpp" +#include "app_led.hpp" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" @@ -36,6 +37,8 @@ void s_sendUartData(std::size_t len) uart_read_bytes(CONFIG_ESPTNL_UART, tx_buffer.data(), len, 0); xTimerStop(s_timer, 0); + Led::flashTx(); + Networking::sendData(tx_buffer, len); } @@ -65,9 +68,13 @@ std::size_t s_uartBytesWaiting() { std::size_t uart_free = 0; uart_get_tx_buffer_free_size(CONFIG_ESPTNL_UART, &uart_free); + if (uart_free >= event.rx_length) { ESP_LOGD(LOG_TAG, "ESP-NOW event: wrote %ull bytes to UART RX.", uart_free); + + Led::flashRx(); + const auto& esp_rx_buffer = Networking::getRxBuffer(); uart_write_bytes(CONFIG_ESPTNL_UART, esp_rx_buffer.data(), event.rx_length); } @@ -151,6 +158,8 @@ extern "C" void app_main(void) xQueueAddToSet(s_esp_now_queue, s_queue_set); xQueueAddToSet(s_uart_queue, s_queue_set); + Led::setupLed(GPIO_NUM_8); + ESP_LOGI(LOG_TAG, "Setup completed. Sending hello."); Networking::sendHello(); diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..234f978 --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,2 @@ +dependencies: + espressif/led_strip: "^2.0.0"