move into firmware subfolder

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-08-20 10:27:03 +02:00
parent d316bb9f2c
commit 5a08c2e09d
117 changed files with 0 additions and 0 deletions

16
firmware/main/CMakeLists.txt Executable file
View File

@@ -0,0 +1,16 @@
idf_component_register(SRCS
main.cpp
app_task.cpp
../components/persistence-manager/src/hal_esp32/PersistenceManager.cpp
button_handling.c
hal/u8g2_esp32_hal.c
INCLUDE_DIRS "."
PRIV_REQUIRES
insa
led-manager
persistence-manager
u8g2
driver
esp_timer
esp_event
)

View File

@@ -0,0 +1,7 @@
menu "Warnemuende Lighthouse"
config WLED_DIN_PIN
int "WLED Data In Pin"
default 14
help
The number of the WLED data in pin.
endmenu

185
firmware/main/app_task.cpp Normal file
View File

@@ -0,0 +1,185 @@
#include "app_task.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "hal/u8g2_esp32_hal.h"
#include "u8g2.h"
#include "button_handling.h"
#include "common/InactivityTracker.h"
#include "hal_esp32/PersistenceManager.h"
#include "ui/ScreenSaver.h"
#include "ui/SplashScreen.h"
#if defined(CONFIG_IDF_TARGET_ESP32S3)
#define PIN_SDA GPIO_NUM_35
#define PIN_SCL GPIO_NUM_36
#else
/// just dummy pins, because of compile check
#define PIN_SDA GPIO_NUM_20
#define PIN_SCL GPIO_NUM_21
#endif
#define PIN_RST GPIO_NUM_NC
static const char *TAG = "app_task";
u8g2_t u8g2;
uint8_t last_value = 0;
menu_options_t options;
uint8_t received_signal;
std::shared_ptr<Widget> m_widget;
std::vector<std::shared_ptr<Widget>> m_history;
std::unique_ptr<InactivityTracker> m_inactivityTracker;
extern QueueHandle_t buttonQueue;
static void setup_screen(void)
{
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
u8g2_esp32_hal.bus.i2c.sda = PIN_SDA;
u8g2_esp32_hal.bus.i2c.scl = PIN_SCL;
u8g2_esp32_hal.reset = PIN_RST;
u8g2_esp32_hal_init(u8g2_esp32_hal);
u8g2_Setup_sh1106_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb);
u8x8_SetI2CAddress(&u8g2.u8x8, 0x3C * 2);
ESP_LOGI(TAG, "u8g2_InitDisplay");
u8g2_InitDisplay(&u8g2);
ESP_LOGI(TAG, "u8g2_SetPowerSave");
u8g2_SetPowerSave(&u8g2, 0);
}
void setScreen(const std::shared_ptr<Widget> &screen)
{
if (screen != nullptr)
{
m_widget = screen;
m_history.clear();
m_history.emplace_back(m_widget);
m_widget->enter();
}
}
void pushScreen(const std::shared_ptr<Widget> &screen)
{
if (screen != nullptr)
{
if (m_widget)
{
m_widget->pause();
}
m_widget = screen;
m_widget->enter();
m_history.emplace_back(m_widget);
}
}
void popScreen()
{
if (m_history.size() >= 2)
{
m_history.pop_back();
if (m_widget)
{
m_widget->exit();
}
m_widget = m_history.back();
m_widget->resume();
}
}
static void init_ui(void)
{
options = {
.u8g2 = &u8g2,
.setScreen = [](const std::shared_ptr<Widget> &screen) { setScreen(screen); },
.pushScreen = [](const std::shared_ptr<Widget> &screen) { pushScreen(screen); },
.popScreen = []() { popScreen(); },
.onButtonClicked = nullptr,
.persistenceManager = std::make_shared<PersistenceManager>(),
};
m_widget = std::make_shared<SplashScreen>(&options);
m_inactivityTracker = std::make_unique<InactivityTracker>(60000, []() {
auto screensaver = std::make_shared<ScreenSaver>(&options);
options.pushScreen(screensaver);
});
}
static void handle_button(uint8_t button)
{
m_inactivityTracker->reset();
if (m_widget)
{
switch (button)
{
case 1:
m_widget->onButtonClicked(ButtonType::UP);
break;
case 3:
m_widget->onButtonClicked(ButtonType::LEFT);
break;
case 5:
m_widget->onButtonClicked(ButtonType::RIGHT);
break;
case 6:
m_widget->onButtonClicked(ButtonType::DOWN);
break;
case 16:
m_widget->onButtonClicked(ButtonType::BACK);
break;
case 18:
m_widget->onButtonClicked(ButtonType::SELECT);
break;
default:
ESP_LOGI(TAG, "Unhandled button: %u", button);
break;
}
}
}
void app_task(void *args)
{
setup_screen();
setup_buttons();
init_ui();
auto oldTime = esp_timer_get_time();
while (true)
{
u8g2_ClearBuffer(&u8g2);
if (m_widget != nullptr)
{
auto currentTime = esp_timer_get_time();
auto delta = currentTime - oldTime;
oldTime = currentTime;
uint64_t deltaMs = delta / 1000;
m_widget->update(deltaMs);
m_widget->render();
m_inactivityTracker->update(deltaMs);
}
u8g2_SendBuffer(&u8g2);
if (xQueueReceive(buttonQueue, &received_signal, pdMS_TO_TICKS(10)) == pdTRUE)
{
handle_button(received_signal);
}
}
cleanup_buttons();
}

3
firmware/main/app_task.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
void app_task(void *args);

View File

@@ -0,0 +1,88 @@
#include "button_handling.h"
#include "button_gpio.h"
#include "common.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "freertos/FreeRTOS.h"
#include "iot_button.h"
#include "sdkconfig.h"
#include <stdio.h>
#include <string.h>
static const char *TAG = "button_handling";
const uint8_t gpios[] = {BUTTON_DOWN, BUTTON_UP, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_SELECT, BUTTON_BACK};
typedef struct
{
uint8_t gpio;
} button_user_data_t;
static button_user_data_t button_data[6];
QueueHandle_t buttonQueue = NULL;
static void button_event_cb(void *arg, void *usr_data)
{
if (buttonQueue == NULL)
{
ESP_LOGE(TAG, "Button queue not initialized!");
return;
}
button_user_data_t *data = (button_user_data_t *)usr_data;
uint8_t gpio_num = data->gpio;
ESP_LOGI(TAG, "Button pressed on GPIO %d", gpio_num);
if (xQueueSend(buttonQueue, &gpio_num, 0) != pdTRUE)
{
ESP_LOGW(TAG, "Failed to send button press to queue");
}
}
static void create_button(uint8_t gpio, int index)
{
const button_config_t btn_cfg = {0};
const button_gpio_config_t btn_gpio_cfg = {
.gpio_num = gpio,
.active_level = 0,
.enable_power_save = true,
};
button_handle_t gpio_btn = NULL;
const esp_err_t ret = iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &gpio_btn);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Button create failed");
}
button_data[index].gpio = gpio;
iot_button_register_cb(gpio_btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb, &button_data[index]);
}
void setup_buttons(void)
{
buttonQueue = xQueueCreate(10, sizeof(uint8_t));
if (buttonQueue == NULL)
{
ESP_LOGE(TAG, "Failed to create button queue");
return;
}
ESP_LOGI(TAG, "Button queue created successfully");
for (int i = 0; i < sizeof(gpios) / sizeof(gpios[0]); i++)
{
create_button(gpios[i], i);
}
}
// Cleanup function (optional)
void cleanup_buttons(void)
{
if (buttonQueue != NULL)
{
vQueueDelete(buttonQueue);
}
}

View File

@@ -0,0 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
void setup_buttons(void);
void cleanup_buttons(void);
#ifdef __cplusplus
}
#endif

8
firmware/main/common.h Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
#define BUTTON_UP GPIO_NUM_1
#define BUTTON_DOWN GPIO_NUM_6
#define BUTTON_LEFT GPIO_NUM_3
#define BUTTON_RIGHT GPIO_NUM_5
#define BUTTON_SELECT GPIO_NUM_18
#define BUTTON_BACK GPIO_NUM_16

View File

@@ -0,0 +1,262 @@
#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "u8g2_esp32_hal.h"
static const char* TAG = "u8g2_hal";
static const unsigned int I2C_TIMEOUT_MS = 1000;
static spi_device_handle_t handle_spi; // SPI handle.
static i2c_cmd_handle_t handle_i2c; // I2C handle.
static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data.
#define HOST SPI2_HOST
#undef ESP_ERROR_CHECK
#define ESP_ERROR_CHECK(x) \
do { \
esp_err_t rc = (x); \
if (rc != ESP_OK) { \
ESP_LOGE("err", "esp_err_t = %d", rc); \
assert(0 && #x); \
} \
} while (0);
/*
* Initialze the ESP32 HAL.
*/
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) {
u8g2_esp32_hal = u8g2_esp32_hal_param;
} // u8g2_esp32_hal_init
/*
* HAL callback function as prescribed by the U8G2 library. This callback is
* invoked to handle SPI communications.
*/
uint8_t u8g2_esp32_spi_byte_cb(u8x8_t* u8x8,
uint8_t msg,
uint8_t arg_int,
void* arg_ptr) {
ESP_LOGD(TAG, "spi_byte_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p",
msg, arg_int, arg_ptr);
switch (msg) {
case U8X8_MSG_BYTE_SET_DC:
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.dc, arg_int);
}
break;
case U8X8_MSG_BYTE_INIT: {
if (u8g2_esp32_hal.bus.spi.clk == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.bus.spi.mosi == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.bus.spi.cs == U8G2_ESP32_HAL_UNDEFINED) {
break;
}
spi_bus_config_t bus_config;
memset(&bus_config, 0, sizeof(spi_bus_config_t));
bus_config.sclk_io_num = u8g2_esp32_hal.bus.spi.clk; // CLK
bus_config.mosi_io_num = u8g2_esp32_hal.bus.spi.mosi; // MOSI
bus_config.miso_io_num = GPIO_NUM_NC; // MISO
bus_config.quadwp_io_num = GPIO_NUM_NC; // Not used
bus_config.quadhd_io_num = GPIO_NUM_NC; // Not used
// ESP_LOGI(TAG, "... Initializing bus.");
ESP_ERROR_CHECK(spi_bus_initialize(HOST, &bus_config, 1));
spi_device_interface_config_t dev_config;
dev_config.address_bits = 0;
dev_config.command_bits = 0;
dev_config.dummy_bits = 0;
dev_config.mode = 0;
dev_config.duty_cycle_pos = 0;
dev_config.cs_ena_posttrans = 0;
dev_config.cs_ena_pretrans = 0;
dev_config.clock_speed_hz = 10000;
dev_config.spics_io_num = u8g2_esp32_hal.bus.spi.cs;
dev_config.flags = 0;
dev_config.queue_size = 200;
dev_config.pre_cb = NULL;
dev_config.post_cb = NULL;
// ESP_LOGI(TAG, "... Adding device bus.");
ESP_ERROR_CHECK(spi_bus_add_device(HOST, &dev_config, &handle_spi));
break;
}
case U8X8_MSG_BYTE_SEND: {
spi_transaction_t trans_desc;
trans_desc.addr = 0;
trans_desc.cmd = 0;
trans_desc.flags = 0;
trans_desc.length = 8 * arg_int; // Number of bits NOT number of bytes.
trans_desc.rxlength = 0;
trans_desc.tx_buffer = arg_ptr;
trans_desc.rx_buffer = NULL;
// ESP_LOGI(TAG, "... Transmitting %d bytes.", arg_int);
ESP_ERROR_CHECK(spi_device_transmit(handle_spi, &trans_desc));
break;
}
}
return 0;
} // u8g2_esp32_spi_byte_cb
/*
* HAL callback function as prescribed by the U8G2 library. This callback is
* invoked to handle I2C communications.
*/
uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t* u8x8,
uint8_t msg,
uint8_t arg_int,
void* arg_ptr) {
ESP_LOGD(TAG, "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg,
arg_int, arg_ptr);
switch (msg) {
case U8X8_MSG_BYTE_SET_DC: {
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.dc, arg_int);
}
break;
}
case U8X8_MSG_BYTE_INIT: {
if (u8g2_esp32_hal.bus.i2c.sda == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.bus.i2c.scl == U8G2_ESP32_HAL_UNDEFINED) {
break;
}
i2c_config_t conf = {0};
conf.mode = I2C_MODE_MASTER;
ESP_LOGI(TAG, "sda_io_num %d", u8g2_esp32_hal.bus.i2c.sda);
conf.sda_io_num = u8g2_esp32_hal.bus.i2c.sda;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
ESP_LOGI(TAG, "scl_io_num %d", u8g2_esp32_hal.bus.i2c.scl);
conf.scl_io_num = u8g2_esp32_hal.bus.i2c.scl;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
ESP_LOGI(TAG, "clk_speed %d", I2C_MASTER_FREQ_HZ);
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
ESP_LOGI(TAG, "i2c_param_config %d", conf.mode);
ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM);
ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode,
I2C_MASTER_RX_BUF_DISABLE,
I2C_MASTER_TX_BUF_DISABLE, 0));
break;
}
case U8X8_MSG_BYTE_SEND: {
uint8_t* data_ptr = (uint8_t*)arg_ptr;
ESP_LOG_BUFFER_HEXDUMP(TAG, data_ptr, arg_int, ESP_LOG_VERBOSE);
while (arg_int > 0) {
ESP_ERROR_CHECK(
i2c_master_write_byte(handle_i2c, *data_ptr, ACK_CHECK_EN));
data_ptr++;
arg_int--;
}
break;
}
case U8X8_MSG_BYTE_START_TRANSFER: {
uint8_t i2c_address = u8x8_GetI2CAddress(u8x8);
handle_i2c = i2c_cmd_link_create();
ESP_LOGD(TAG, "Start I2C transfer to %02X.", i2c_address >> 1);
ESP_ERROR_CHECK(i2c_master_start(handle_i2c));
ESP_ERROR_CHECK(i2c_master_write_byte(
handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN));
break;
}
case U8X8_MSG_BYTE_END_TRANSFER: {
ESP_LOGD(TAG, "End I2C transfer.");
ESP_ERROR_CHECK(i2c_master_stop(handle_i2c));
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c,
pdMS_TO_TICKS(I2C_TIMEOUT_MS)));
i2c_cmd_link_delete(handle_i2c);
break;
}
}
return 0;
} // u8g2_esp32_i2c_byte_cb
/*
* HAL callback function as prescribed by the U8G2 library. This callback is
* invoked to handle callbacks for GPIO and delay functions.
*/
uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t* u8x8,
uint8_t msg,
uint8_t arg_int,
void* arg_ptr) {
ESP_LOGD(TAG,
"gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p",
msg, arg_int, arg_ptr);
switch (msg) {
// Initialize the GPIO and DELAY HAL functions. If the pins for DC and
// RESET have been specified then we define those pins as GPIO outputs.
case U8X8_MSG_GPIO_AND_DELAY_INIT: {
uint64_t bitmask = 0;
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
bitmask = bitmask | (1ull << u8g2_esp32_hal.dc);
}
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
bitmask = bitmask | (1ull << u8g2_esp32_hal.reset);
}
if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED) {
bitmask = bitmask | (1ull << u8g2_esp32_hal.bus.spi.cs);
}
if (bitmask == 0) {
break;
}
gpio_config_t gpioConfig;
gpioConfig.pin_bit_mask = bitmask;
gpioConfig.mode = GPIO_MODE_OUTPUT;
gpioConfig.pull_up_en = GPIO_PULLUP_DISABLE;
gpioConfig.pull_down_en = GPIO_PULLDOWN_ENABLE;
gpioConfig.intr_type = GPIO_INTR_DISABLE;
gpio_config(&gpioConfig);
break;
}
// Set the GPIO reset pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_RESET:
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.reset, arg_int);
}
break;
// Set the GPIO client select pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_CS:
if (u8g2_esp32_hal.bus.spi.cs != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.bus.spi.cs, arg_int);
}
break;
// Set the Software I²C pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_I2C_CLOCK:
if (u8g2_esp32_hal.bus.i2c.scl != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.bus.i2c.scl, arg_int);
// printf("%c",(arg_int==1?'C':'c'));
}
break;
// Set the Software I²C pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_I2C_DATA:
if (u8g2_esp32_hal.bus.i2c.sda != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.bus.i2c.sda, arg_int);
// printf("%c",(arg_int==1?'D':'d'));
}
break;
// Delay for the number of milliseconds passed in through arg_int.
case U8X8_MSG_DELAY_MILLI:
vTaskDelay(arg_int / portTICK_PERIOD_MS);
break;
}
return 0;
} // u8g2_esp32_gpio_and_delay_cb

View File

@@ -0,0 +1,92 @@
#ifndef U8G2_ESP32_HAL_H
#define U8G2_ESP32_HAL_H
/*
* u8g2_esp32_hal.h
*
* Created on: Feb 12, 2017
* Author: kolban
*/
#ifndef U8G2_ESP32_HAL_H_
#define U8G2_ESP32_HAL_H_
#include "u8g2.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "driver/spi_master.h"
#define U8G2_ESP32_HAL_UNDEFINED GPIO_NUM_NC
#if SOC_I2C_NUM > 1
#define I2C_MASTER_NUM I2C_NUM_1 // I2C port number for master dev
#else
#define I2C_MASTER_NUM I2C_NUM_0 // I2C port number for master dev
#endif
#define I2C_MASTER_TX_BUF_DISABLE 0 // I2C master do not need buffer
#define I2C_MASTER_RX_BUF_DISABLE 0 // I2C master do not need buffer
#define I2C_MASTER_FREQ_HZ 400000 // I2C master clock frequency
#define ACK_CHECK_EN 0x1 // I2C master will check ack from slave
#define ACK_CHECK_DIS 0x0 // I2C master will not check ack from slave
/** @public
* HAL configuration structure.
*/
typedef struct
{
union {
/* SPI settings. */
struct
{
/* GPIO num for clock. */
gpio_num_t clk;
/* GPIO num for SPI mosi. */
gpio_num_t mosi;
/* GPIO num for SPI slave/chip select. */
gpio_num_t cs;
} spi;
/* I2C settings. */
struct
{
/* GPIO num for I2C data. */
gpio_num_t sda;
/* GPIO num for I2C clock. */
gpio_num_t scl;
} i2c;
} bus;
/* GPIO num for reset. */
gpio_num_t reset;
/* GPIO num for DC. */
gpio_num_t dc;
} u8g2_esp32_hal_t;
/**
* Construct a default HAL configuration with all fields undefined.
*/
#define U8G2_ESP32_HAL_DEFAULT \
{.bus = {.spi = {.clk = U8G2_ESP32_HAL_UNDEFINED, \
.mosi = U8G2_ESP32_HAL_UNDEFINED, \
.cs = U8G2_ESP32_HAL_UNDEFINED}}, \
.reset = U8G2_ESP32_HAL_UNDEFINED, \
.dc = U8G2_ESP32_HAL_UNDEFINED}
#ifdef __cplusplus
extern "C"
{
#endif
/**
* Initialize the HAL with the given configuration.
*
* @see u8g2_esp32_hal_t
* @see U8G2_ESP32_HAL_DEFAULT
*/
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param);
uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
#ifdef __cplusplus
}
#endif
#endif /* U8G2_ESP32_HAL_H_ */
#endif

View File

@@ -0,0 +1,6 @@
dependencies:
u8g2:
git: https://github.com/olikraus/u8g2.git
# u8g2_hal:
# git: https://github.com/mkfrey/u8g2-hal-esp-idf.git
espressif/button: ^4.1.3

20
firmware/main/main.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "app_task.h"
#include "esp_event.h"
#include "freertos/FreeRTOS.h"
#include "led_manager.h"
#include "sdkconfig.h"
#ifdef __cplusplus
extern "C"
{
#endif
void app_main(void)
{
xTaskCreatePinnedToCore(app_task, "main_loop", 4096, NULL, tskIDLE_PRIORITY + 1, NULL, portNUM_PROCESSORS - 1);
wled_init();
register_handler();
}
#ifdef __cplusplus
}
#endif

158
firmware/main/setup.c Normal file
View File

@@ -0,0 +1,158 @@
#include "setup.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include <stdio.h>
#include <string.h>
#include "driver/rmt_encoder.h"
#include "driver/rmt_tx.h"
#include "u8g2.h"
#include "u8g2_esp32_hal.h"
#include "button_handling.h"
#include "common.h"
#define PIN_SDA GPIO_NUM_35
#define PIN_SCL GPIO_NUM_36
#define PIN_RST GPIO_NUM_NC
#define WLED_GPIO GPIO_NUM_47
#define WLED_RMT_CHANNEL RMT_CHANNEL_0
#define WLED_RESOLUTION_HZ (10000000)
#define WLED_ON_DURATION_MS (100)
#define NUM_LEDS (1)
uint8_t last_value = 0;
static const char *TAG = "main";
extern QueueHandle_t buttonQueue;
rmt_channel_handle_t rmt_led_chan = NULL;
rmt_encoder_handle_t rmt_led_encoder = NULL;
bool wled_is_on = false;
int64_t wled_turn_off_time = 0;
u8g2_t u8g2;
uint8_t received_signal;
static void init_rmt_ws2812b(void)
{
ESP_LOGI(TAG, "Initialize RMT TX Channel for WS2812B on GPIO %d", WLED_GPIO);
rmt_tx_channel_config_t tx_chan_config = {
.gpio_num = WLED_GPIO,
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = WLED_RESOLUTION_HZ,
.mem_block_symbols = 64,
.trans_queue_depth = 4,
.intr_priority = 0,
};
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &rmt_led_chan));
ESP_LOGI(TAG, "Install RMT Bytes Encoder");
rmt_bytes_encoder_config_t bytes_encoder_config = {
.bit0 = {.duration0 = 4, .level0 = 1, .duration1 = 8, .level1 = 0},
.bit1 = {.duration0 = 8, .level0 = 1, .duration1 = 4, .level1 = 0},
.flags = {.msb_first = 1}};
ESP_ERROR_CHECK(rmt_new_bytes_encoder(&bytes_encoder_config, &rmt_led_encoder));
ESP_LOGI(TAG, "Activate RMT TX Kanal");
ESP_ERROR_CHECK(rmt_enable(rmt_led_chan));
}
static void set_wled_color(uint8_t r, uint8_t g, uint8_t b)
{
if (!rmt_led_chan || !rmt_led_encoder)
{
ESP_LOGE(TAG, "RMT Channel or Encoder not initialized!");
return;
}
size_t buffer_size = 3 * NUM_LEDS;
uint8_t led_data[buffer_size];
for (int i = 0; i < NUM_LEDS; i++)
{
led_data[i * 3 + 0] = g;
led_data[i * 3 + 1] = r;
led_data[i * 3 + 2] = b;
}
rmt_transmit_config_t tx_config = {
.loop_count = 0,
};
esp_err_t ret = rmt_transmit(rmt_led_chan, rmt_led_encoder, led_data, sizeof(led_data), &tx_config);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "RMT Transmit failed: %s", esp_err_to_name(ret));
}
ESP_ERROR_CHECK(rmt_tx_wait_all_done(rmt_led_chan, pdMS_TO_TICKS(100)));
}
void setup(void)
{
setup_buttons();
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
u8g2_esp32_hal.bus.i2c.sda = PIN_SDA;
u8g2_esp32_hal.bus.i2c.scl = PIN_SCL;
u8g2_esp32_hal.reset = PIN_RST;
u8g2_esp32_hal_init(u8g2_esp32_hal);
u8g2_Setup_sh1106_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb);
u8x8_SetI2CAddress(&u8g2.u8x8, 0x3C * 2);
ESP_LOGI(TAG, "u8g2_InitDisplay");
u8g2_InitDisplay(&u8g2);
ESP_LOGI(TAG, "u8g2_SetPowerSave");
u8g2_SetPowerSave(&u8g2, 0);
init_rmt_ws2812b();
set_wled_color(0, 0, 0);
ESP_LOGI(TAG, "Start of main loop. Waiting for button press...");
}
void loop(void)
{
u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
u8g2_DrawStr(&u8g2, 5, 20, "Ready!");
char count_str[50];
snprintf(count_str, sizeof(count_str), "Signal Value: %u", last_value);
u8g2_DrawStr(&u8g2, 5, 45, count_str);
u8g2_SendBuffer(&u8g2);
if (xQueueReceive(buttonQueue, &received_signal, pdMS_TO_TICKS(10)) == pdTRUE)
{
ESP_LOGI(TAG, "Button event from Queue received!");
last_value = received_signal;
u8g2_ClearBuffer(&u8g2);
u8g2_DrawStr(&u8g2, 5, 20, "Pressed!");
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
char count_str[50];
snprintf(count_str, sizeof(count_str), "Signal Value: %u", last_value);
u8g2_DrawStr(&u8g2, 5, 45, count_str);
u8g2_SendBuffer(&u8g2);
ESP_LOGI(TAG, "Display refreshed with signal value: %u", last_value);
ESP_LOGD(TAG, "Switch WLED ON");
set_wled_color(255, 0, 255);
wled_is_on = true;
wled_turn_off_time = esp_timer_get_time() + (WLED_ON_DURATION_MS * 1000);
}
if (wled_is_on && esp_timer_get_time() >= wled_turn_off_time)
{
ESP_LOGD(TAG, "Switch WLED OFF");
set_wled_color(0, 0, 0);
wled_is_on = false;
}
}

71
firmware/main/setup.h Normal file
View File

@@ -0,0 +1,71 @@
/**
* @file setup.h
* @brief System initialization and main loop declarations for embedded application
* @details This header defines the core system initialization and main loop functions
* required for embedded ESP32 applications. It provides the essential entry
* points for hardware setup, system configuration, and continuous operation
* management following standard embedded system patterns.
* @author System Control Team
* @date 2025-06-20
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Initializes all system components and hardware peripherals
*
* @details This function performs complete system initialization including:
* - Hardware peripheral configuration (GPIO, I2C, SPI, etc.)
* - Display system initialization
* - Button and input device setup
* - Communication subsystem initialization
* - Memory and storage system preparation
* - Application-specific component initialization
*
* This function is called once during system startup before entering
* the main application loop. It ensures all required subsystems are
* properly configured and ready for operation.
*
* @pre System must be in a clean startup state
* @post All system components are initialized and ready for operation
*
* @note This function must complete successfully before loop() is called
* @note Any initialization failures should be handled gracefully with
* appropriate error reporting or system recovery
*
* @see loop() for the main application execution function
*/
void setup(void);
/**
* @brief Main application execution loop for continuous operation
*
* @details This function contains the main application logic that executes
* continuously after system initialization. It typically handles:
* - User input processing and event handling
* - Display updates and rendering operations
* - System state management and transitions
* - Background tasks and periodic operations
* - Communication handling and data processing
* - Power management and system monitoring
*
* The loop function is called repeatedly in an infinite cycle, providing
* the main execution context for the embedded application. It should be
* designed to execute efficiently without blocking to maintain system
* responsiveness.
*
* @note This function runs continuously and should not block indefinitely
* @note All operations within this function should be non-blocking or
* use appropriate task scheduling for time-consuming operations
* @note The function should handle all runtime errors gracefully
*
* @see setup() for system initialization before loop execution
*/
void loop(void);
#ifdef __cplusplus
}
#endif