more modules for the MCU code

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-04-11 23:54:25 +02:00
parent 5b82cd8189
commit 6e22bee95c
13 changed files with 170 additions and 83 deletions

View File

@@ -17,33 +17,24 @@
#include "u8g2.h"
#include "u8g2_esp32_hal.h"
#include "common.h"
#include "button_handling.h"
#define PIN_SDA GPIO_NUM_35
#define PIN_SCL GPIO_NUM_36
#define PIN_RST GPIO_NUM_NC
#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
#define DEBOUNCE_TIME_MS (500)
#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)
#define BUTTON_QUEUE_LENGTH 5
#define BUTTON_QUEUE_ITEM_SIZE sizeof(uint8_t)
uint8_t last_value = 0;
static const char* TAG = "main";
QueueHandle_t buttonQueue = NULL;
volatile int counter = 0;
volatile int64_t last_interrupt_time = 0;
extern QueueHandle_t buttonQueue;
rmt_channel_handle_t rmt_led_chan = NULL;
rmt_encoder_handle_t rmt_led_encoder = NULL;
@@ -53,22 +44,6 @@ int64_t wled_turn_off_time = 0;
u8g2_t u8g2;
uint8_t received_signal;
static void IRAM_ATTR button_isr_handler(void* arg) {
int64_t now = esp_timer_get_time();
if((now - last_interrupt_time) > (DEBOUNCE_TIME_MS * 1000)) {
last_interrupt_time = now;
uint8_t press_signal = 1;
BaseType_t higherPriorityTaskWoken = pdFALSE;
xQueueSendFromISR(buttonQueue, &press_signal, &higherPriorityTaskWoken);
if(higherPriorityTaskWoken) {
portYIELD_FROM_ISR();
}
}
}
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 = {
@@ -117,32 +92,7 @@ static void set_wled_color(uint8_t r, uint8_t g, uint8_t b) {
}
void setup(void) {
buttonQueue = xQueueCreate(BUTTON_QUEUE_LENGTH, BUTTON_QUEUE_ITEM_SIZE);
if(buttonQueue == NULL) {
ESP_LOGE(TAG, "Error while Queue creation!");
return;
}
ESP_LOGI(TAG, "Button Queue created.");
gpio_config_t gpio_button_up;
gpio_button_up.intr_type = GPIO_INTR_NEGEDGE;
gpio_button_up.pin_bit_mask = (1ULL << BUTTON_UP);
gpio_button_up.mode = GPIO_MODE_INPUT;
gpio_button_up.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_button_up.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpio_config(&gpio_button_up);
esp_err_t isr_service_err = gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
if(isr_service_err != ESP_OK && isr_service_err != ESP_ERR_INVALID_STATE) {
ESP_LOGE(TAG, "Error in gpio_install_isr_service: %s", esp_err_to_name(isr_service_err));
}
esp_err_t add_isr_err = gpio_isr_handler_add(BUTTON_UP, button_isr_handler, (void*)BUTTON_UP);
if(add_isr_err != ESP_OK) {
ESP_LOGE(TAG, "Error in gpio_isr_handler_add: %s", esp_err_to_name(add_isr_err));
}
ESP_LOGI(TAG, "Button interrupt configured for GPIO %d", BUTTON_UP);
setupButtons();
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
u8g2_esp32_hal.bus.i2c.sda = PIN_SDA;
@@ -170,23 +120,23 @@ void loop(void) {
u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
u8g2_DrawStr(&u8g2, 5, 20, "Ready!");
char count_str[12];
snprintf(count_str, sizeof(count_str), "Counter: %d", counter);
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!");
counter++;
last_value = received_signal;
u8g2_ClearBuffer(&u8g2);
u8g2_DrawStr(&u8g2, 5, 20, "Pressed!");
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
snprintf(count_str, sizeof(count_str), "Counter: %d", counter);
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_LOGD(TAG, "Display refreshed with counter: %d", counter);
ESP_LOGI(TAG, "Display refreshed with signal value: %u", last_value);
ESP_LOGD(TAG, "Switch WLED ON");
set_wled_color(255, 0, 255);