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

6
.gitignore vendored
View File

@@ -31,3 +31,9 @@ compile_commands.json
!.vscode/extensions.json !.vscode/extensions.json
.history/* .history/*
__pycache__ __pycache__
_deps/
.cmake/
.ninja_*
bin/
components/**/*.a
*.ninja

View File

@@ -40,7 +40,7 @@ endfunction()
include_dependency(SDL3 https://github.com/libsdl-org/SDL release-3.2.8) include_dependency(SDL3 https://github.com/libsdl-org/SDL release-3.2.8)
include_dependency(SDL_image https://github.com/libsdl-org/SDL_image release-3.2.4) include_dependency(SDL_image https://github.com/libsdl-org/SDL_image release-3.2.4)
include_dependency(SDL_ttf https://github.com/libsdl-org/SDL_ttf release-3.2.0) include_dependency(SDL_ttf https://github.com/libsdl-org/SDL_ttf release-3.2.0)
include_dependency(u8g2 https://github.com/olikraus/u8g2 1e92781) include_dependency(u8g2 https://github.com/olikraus/u8g2 82efd57)
add_subdirectory(components) add_subdirectory(components)

View File

@@ -0,0 +1,3 @@
Start testing: Apr 11 23:54 CEST
----------------------------------------------------------
End testing: Apr 11 23:54 CEST

View File

@@ -1,4 +1,4 @@
idf_component_register( idf_component_register(
SRCS SRCS
"main.c" "setup.c" "main.c" "setup.c" "button_handling.c"
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

83
main/button_handling.c Normal file
View File

@@ -0,0 +1,83 @@
#include "button_handling.h"
#include <stdio.h>
#include <string.h>
#include "driver/gpio.h"
#include "driver/i2c.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 "common.h"
static const char* TAG = "button_handling";
#define DEBOUNCE_TIME_MS (500)
#define BUTTON_QUEUE_LENGTH 5
#define BUTTON_QUEUE_ITEM_SIZE sizeof(uint8_t)
#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)
const uint8_t pins[] =
{BUTTON_DOWN, BUTTON_UP, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_SELECT, BUTTON_BACK};
QueueHandle_t buttonQueue = NULL;
volatile int64_t last_interrupt_time = 0;
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;
uintptr_t pin_value = (uintptr_t)arg;
uint8_t press_signal = (uint8_t)pin_value;
BaseType_t higherPriorityTaskWoken = pdFALSE;
xQueueSendFromISR(buttonQueue, &press_signal, &higherPriorityTaskWoken);
if(higherPriorityTaskWoken) {
portYIELD_FROM_ISR();
}
}
}
void setupButtons(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.");
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));
}
for(int i = 0; i < sizeof(pins) / sizeof(pins[0]); i++) {
const uint8_t pin = pins[i];
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_NEGEDGE;
io_conf.pin_bit_mask = (1ULL << pin);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpio_config(&io_conf);
uintptr_t pin_as_arg = (uintptr_t)pin;
esp_err_t add_isr_err = gpio_isr_handler_add(pin, button_isr_handler, (void*)pin_as_arg);
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", pin);
}
}

13
main/button_handling.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#include "driver/i2c.h"
void IRAM_ATTR button_isr_handler(void* arg);
#ifdef __cplusplus
extern "C" {
#endif
void setupButtons(void);
#ifdef __cplusplus
}
#endif

8
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

@@ -17,33 +17,24 @@
#include "u8g2.h" #include "u8g2.h"
#include "u8g2_esp32_hal.h" #include "u8g2_esp32_hal.h"
#include "common.h"
#include "button_handling.h"
#define PIN_SDA GPIO_NUM_35 #define PIN_SDA GPIO_NUM_35
#define PIN_SCL GPIO_NUM_36 #define PIN_SCL GPIO_NUM_36
#define PIN_RST GPIO_NUM_NC #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_GPIO GPIO_NUM_47
#define WLED_RMT_CHANNEL RMT_CHANNEL_0 #define WLED_RMT_CHANNEL RMT_CHANNEL_0
#define WLED_RESOLUTION_HZ (10000000) #define WLED_RESOLUTION_HZ (10000000)
#define WLED_ON_DURATION_MS (100) #define WLED_ON_DURATION_MS (100)
#define NUM_LEDS (1) #define NUM_LEDS (1)
#define BUTTON_QUEUE_LENGTH 5 uint8_t last_value = 0;
#define BUTTON_QUEUE_ITEM_SIZE sizeof(uint8_t)
static const char* TAG = "main"; static const char* TAG = "main";
QueueHandle_t buttonQueue = NULL; extern QueueHandle_t buttonQueue;
volatile int counter = 0;
volatile int64_t last_interrupt_time = 0;
rmt_channel_handle_t rmt_led_chan = NULL; rmt_channel_handle_t rmt_led_chan = NULL;
rmt_encoder_handle_t rmt_led_encoder = NULL; rmt_encoder_handle_t rmt_led_encoder = NULL;
@@ -53,22 +44,6 @@ int64_t wled_turn_off_time = 0;
u8g2_t u8g2; u8g2_t u8g2;
uint8_t received_signal; 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) { static void init_rmt_ws2812b(void) {
ESP_LOGI(TAG, "Initialize RMT TX Channel for WS2812B on GPIO %d", WLED_GPIO); ESP_LOGI(TAG, "Initialize RMT TX Channel for WS2812B on GPIO %d", WLED_GPIO);
rmt_tx_channel_config_t tx_chan_config = { 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) { void setup(void) {
buttonQueue = xQueueCreate(BUTTON_QUEUE_LENGTH, BUTTON_QUEUE_ITEM_SIZE); setupButtons();
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);
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT; u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
u8g2_esp32_hal.bus.i2c.sda = PIN_SDA; u8g2_esp32_hal.bus.i2c.sda = PIN_SDA;
@@ -170,23 +120,23 @@ void loop(void) {
u8g2_ClearBuffer(&u8g2); u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr); u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
u8g2_DrawStr(&u8g2, 5, 20, "Ready!"); u8g2_DrawStr(&u8g2, 5, 20, "Ready!");
char count_str[12]; char count_str[50];
snprintf(count_str, sizeof(count_str), "Counter: %d", counter); snprintf(count_str, sizeof(count_str), "Signal Value: %u", last_value);
u8g2_DrawStr(&u8g2, 5, 45, count_str); u8g2_DrawStr(&u8g2, 5, 45, count_str);
u8g2_SendBuffer(&u8g2); u8g2_SendBuffer(&u8g2);
if(xQueueReceive(buttonQueue, &received_signal, pdMS_TO_TICKS(10)) == pdTRUE) { if(xQueueReceive(buttonQueue, &received_signal, pdMS_TO_TICKS(10)) == pdTRUE) {
ESP_LOGI(TAG, "Button event from Queue received!"); ESP_LOGI(TAG, "Button event from Queue received!");
counter++; last_value = received_signal;
u8g2_ClearBuffer(&u8g2); u8g2_ClearBuffer(&u8g2);
u8g2_DrawStr(&u8g2, 5, 20, "Pressed!"); u8g2_DrawStr(&u8g2, 5, 20, "Pressed!");
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr); 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_DrawStr(&u8g2, 5, 45, count_str);
u8g2_SendBuffer(&u8g2); 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"); ESP_LOGD(TAG, "Switch WLED ON");
set_wled_color(255, 0, 255); set_wled_color(255, 0, 255);

View File

@@ -3,6 +3,7 @@
#include <memory> #include <memory>
#include <iostream> #include <iostream>
#include <SDL3_image/SDL_image.h> #include <SDL3_image/SDL_image.h>
#include <ranges>
ResourceManager& ResourceManager::getInstance() { ResourceManager& ResourceManager::getInstance() {
static ResourceManager instance; static ResourceManager instance;
@@ -10,13 +11,13 @@ ResourceManager& ResourceManager::getInstance() {
} }
ResourceManager::ResourceManager() { ResourceManager::ResourceManager() {
SDL_Log("ResourceManager instance created."); SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "ResourceManager instance created.");
} }
ResourceManager::~ResourceManager() { ResourceManager::~ResourceManager() {
for(const auto& pair : m_textures) { for(const auto& texture : m_textures | std::views::values) {
if(pair.second != nullptr) { if(texture != nullptr) {
SDL_DestroyTexture(pair.second); SDL_DestroyTexture(texture);
} }
} }
m_textures.clear(); m_textures.clear();
@@ -32,11 +33,14 @@ SDL_Texture* ResourceManager::get_texture(SDL_Renderer* renderer, const std::str
SDL_Texture* texture = IMG_LoadTexture(renderer, path.c_str()); SDL_Texture* texture = IMG_LoadTexture(renderer, path.c_str());
if(!texture) { if(!texture) {
std::cerr << "ResourceManager Fehler: Konnte Textur nicht laden '" << path << std::endl; SDL_LogError(
SDL_LOG_CATEGORY_APPLICATION,
"Could not load %s -> %s",
path.c_str(),
SDL_GetError());
return nullptr; return nullptr;
} }
m_textures[path] = texture; m_textures[path] = texture;
return texture; return texture;
} }

10
src/Version.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include "string"
const std::string MyProject = "system_control";
const std::string MyProjectVersion = "0.0.1";
constexpr uint8_t MyProjectVersionMajor = 0;
constexpr uint8_t MyProjectVersionMinor = 0;
constexpr uint8_t MyProjectVersionPatch = 1;
const std::string MyProjectBuildDate = "";

View File

@@ -1,7 +1,10 @@
#pragma once #pragma once
#define MY_VERSION "@PROJECT_VERSION@" #include "string"
#define MY_PROJECT "@PROJECT_NAME@"
#define MY_VERSION_MAJOR @PROJECT_VERSION_MAJOR@ const std::string MyProject = "@PROJECT_NAME@";
#define MY_VERSION_MINOR @PROJECT_VERSION_MINOR@ const std::string MyProjectVersion = "@PROJECT_VERSION@";
#define MY_VERSION_PATCH @PROJECT_VERSION_PATCH@ constexpr uint8_t MyProjectVersionMajor = @PROJECT_VERSION_MAJOR@;
constexpr uint8_t MyProjectVersionMinor = @PROJECT_VERSION_MINOR@;
constexpr uint8_t MyProjectVersionPatch = @PROJECT_VERSION_PATCH@;
const std::string MyProjectBuildDate = "@PROJECT_BUILD_DATE@";

View File

@@ -58,8 +58,9 @@ void render(const AppContext* context) {
if(ImGui::BeginMenu("Help")) { if(ImGui::BeginMenu("Help")) {
ImGui::Text("FPS: %.2f", ImGui::GetIO().Framerate); ImGui::Text("FPS: %.2f", ImGui::GetIO().Framerate);
ImGui::SeparatorText("App Info"); ImGui::SeparatorText("App Info");
ImGui::Text("Project: %s", MY_PROJECT); ImGui::Text("Project: %s", MyProject.c_str());
ImGui::Text("Version: %s", MY_VERSION); ImGui::Text("Version: %s", MyProjectVersion.c_str());
ImGui::Text("Build Date: %s", MyProjectBuildDate.c_str());
ImGui::Text("ImGui Version: %s", ImGui::GetVersion()); ImGui::Text("ImGui Version: %s", ImGui::GetVersion());
ImGui::EndMenu(); ImGui::EndMenu();

View File

@@ -18,7 +18,8 @@
#include "ui/widgets/D_Pad.h" #include "ui/widgets/D_Pad.h"
constexpr unsigned int WINDOW_WIDTH = constexpr unsigned int WINDOW_WIDTH =
(U8G2_SCREEN_WIDTH * U8G2_SCREEN_FACTOR + 3 * U8G2_SCREEN_PADDING + 2 * BUTTON_WIDTH + DPAD_WIDTH + 2 * U8G2_SCREEN_PADDING); (U8G2_SCREEN_WIDTH * U8G2_SCREEN_FACTOR + 3 * U8G2_SCREEN_PADDING + 2 * BUTTON_WIDTH +
DPAD_WIDTH + 2 * U8G2_SCREEN_PADDING);
constexpr unsigned int WINDOW_HEIGHT = (U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR + 50); constexpr unsigned int WINDOW_HEIGHT = (U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR + 50);
std::shared_ptr<Device> device; std::shared_ptr<Device> device;
@@ -26,19 +27,21 @@ std::vector<std::shared_ptr<UIWidget>> widgets;
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) { SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_VIDEO) == false) { if(SDL_Init(SDL_INIT_VIDEO) == false) {
SDL_ShowSimpleMessageBox( SDL_LogError(
SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), nullptr); SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL! -> $s", SDL_GetError());
return SDL_APP_FAILURE; return SDL_APP_FAILURE;
} }
if(TTF_Init() == false) { if(TTF_Init() == false) {
SDL_ShowSimpleMessageBox( SDL_LogError(
SDL_MESSAGEBOX_ERROR, "Couldn't initialize TTF", SDL_GetError(), nullptr); SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize TTF! -> %s", SDL_GetError());
return SDL_APP_FAILURE; return SDL_APP_FAILURE;
} }
const auto win = createWindow("System Control (Simulator)", WINDOW_WIDTH, WINDOW_HEIGHT); const auto win = createWindow("System Control (Simulator)", WINDOW_WIDTH, WINDOW_HEIGHT);
if(!win) { if(!win) {
SDL_LogError(
SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window! -> %s", SDL_GetError());
return SDL_APP_FAILURE; return SDL_APP_FAILURE;
} }
SDL_SetRenderVSync(win->renderer(), SDL_RENDERER_VSYNC_ADAPTIVE); SDL_SetRenderVSync(win->renderer(), SDL_RENDERER_VSYNC_ADAPTIVE);
@@ -100,6 +103,9 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
case SDLK_BACKSPACE: case SDLK_BACKSPACE:
device->onButtonClicked(BUTTON_BACK); device->onButtonClicked(BUTTON_BACK);
break; break;
default:
break;
} }
break; break;
@@ -115,7 +121,7 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
default: { default: {
if(DebugOverlay::show_unhandled_events) { if(DebugOverlay::show_unhandled_events) {
SDL_Log("Unused event: %d", event->type); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Unused event: %d", event->type);
} }
} break; } break;
} }