diff --git a/.gitignore b/.gitignore index 49b4153..9547ce9 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,9 @@ compile_commands.json !.vscode/extensions.json .history/* __pycache__ +_deps/ +.cmake/ +.ninja_* +bin/ +components/**/*.a +*.ninja diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c857d0..ede93bf 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,7 @@ endfunction() 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_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) diff --git a/Testing/Temporary/LastTest.log b/Testing/Temporary/LastTest.log new file mode 100644 index 0000000..2e1de7c --- /dev/null +++ b/Testing/Temporary/LastTest.log @@ -0,0 +1,3 @@ +Start testing: Apr 11 23:54 CEST +---------------------------------------------------------- +End testing: Apr 11 23:54 CEST diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 21f4858..5b1e63f 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,4 +1,4 @@ idf_component_register( SRCS - "main.c" "setup.c" + "main.c" "setup.c" "button_handling.c" INCLUDE_DIRS ".") diff --git a/main/button_handling.c b/main/button_handling.c new file mode 100644 index 0000000..c626d19 --- /dev/null +++ b/main/button_handling.c @@ -0,0 +1,83 @@ +#include "button_handling.h" + +#include +#include +#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); + } +} diff --git a/main/button_handling.h b/main/button_handling.h new file mode 100644 index 0000000..eacbe31 --- /dev/null +++ b/main/button_handling.h @@ -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 \ No newline at end of file diff --git a/main/common.h b/main/common.h new file mode 100644 index 0000000..7176640 --- /dev/null +++ b/main/common.h @@ -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 \ No newline at end of file diff --git a/main/setup.c b/main/setup.c index 05cab95..8aabf9a 100644 --- a/main/setup.c +++ b/main/setup.c @@ -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); diff --git a/src/ResourceManager.cpp b/src/ResourceManager.cpp index a9a484d..3c31e48 100644 --- a/src/ResourceManager.cpp +++ b/src/ResourceManager.cpp @@ -3,6 +3,7 @@ #include #include #include +#include ResourceManager& ResourceManager::getInstance() { static ResourceManager instance; @@ -10,13 +11,13 @@ ResourceManager& ResourceManager::getInstance() { } ResourceManager::ResourceManager() { - SDL_Log("ResourceManager instance created."); + SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "ResourceManager instance created."); } ResourceManager::~ResourceManager() { - for(const auto& pair : m_textures) { - if(pair.second != nullptr) { - SDL_DestroyTexture(pair.second); + for(const auto& texture : m_textures | std::views::values) { + if(texture != nullptr) { + SDL_DestroyTexture(texture); } } 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()); 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; } m_textures[path] = texture; return texture; } - diff --git a/src/Version.h b/src/Version.h new file mode 100644 index 0000000..e9742ec --- /dev/null +++ b/src/Version.h @@ -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 = ""; diff --git a/src/Version.h.in b/src/Version.h.in index 881b051..5b7b371 100644 --- a/src/Version.h.in +++ b/src/Version.h.in @@ -1,7 +1,10 @@ #pragma once -#define MY_VERSION "@PROJECT_VERSION@" -#define MY_PROJECT "@PROJECT_NAME@" -#define MY_VERSION_MAJOR @PROJECT_VERSION_MAJOR@ -#define MY_VERSION_MINOR @PROJECT_VERSION_MINOR@ -#define MY_VERSION_PATCH @PROJECT_VERSION_PATCH@ +#include "string" + +const std::string MyProject = "@PROJECT_NAME@"; +const std::string MyProjectVersion = "@PROJECT_VERSION@"; +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@"; diff --git a/src/debug/DebugOverlay.cpp b/src/debug/DebugOverlay.cpp index 02573be..be6a964 100644 --- a/src/debug/DebugOverlay.cpp +++ b/src/debug/DebugOverlay.cpp @@ -58,8 +58,9 @@ void render(const AppContext* context) { if(ImGui::BeginMenu("Help")) { ImGui::Text("FPS: %.2f", ImGui::GetIO().Framerate); ImGui::SeparatorText("App Info"); - ImGui::Text("Project: %s", MY_PROJECT); - ImGui::Text("Version: %s", MY_VERSION); + ImGui::Text("Project: %s", MyProject.c_str()); + ImGui::Text("Version: %s", MyProjectVersion.c_str()); + ImGui::Text("Build Date: %s", MyProjectBuildDate.c_str()); ImGui::Text("ImGui Version: %s", ImGui::GetVersion()); ImGui::EndMenu(); diff --git a/src/main.cc b/src/main.cc index 8d19c19..fbc0dac 100644 --- a/src/main.cc +++ b/src/main.cc @@ -18,7 +18,8 @@ #include "ui/widgets/D_Pad.h" 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); std::shared_ptr device; @@ -26,19 +27,21 @@ std::vector> widgets; SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) { if(SDL_Init(SDL_INIT_VIDEO) == false) { - SDL_ShowSimpleMessageBox( - SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), nullptr); + SDL_LogError( + SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL! -> $s", SDL_GetError()); return SDL_APP_FAILURE; } if(TTF_Init() == false) { - SDL_ShowSimpleMessageBox( - SDL_MESSAGEBOX_ERROR, "Couldn't initialize TTF", SDL_GetError(), nullptr); + SDL_LogError( + SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize TTF! -> %s", SDL_GetError()); return SDL_APP_FAILURE; } const auto win = createWindow("System Control (Simulator)", WINDOW_WIDTH, WINDOW_HEIGHT); if(!win) { + SDL_LogError( + SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window! -> %s", SDL_GetError()); return SDL_APP_FAILURE; } SDL_SetRenderVSync(win->renderer(), SDL_RENDERER_VSYNC_ADAPTIVE); @@ -100,6 +103,9 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) { case SDLK_BACKSPACE: device->onButtonClicked(BUTTON_BACK); break; + + default: + break; } break; @@ -115,7 +121,7 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) { default: { if(DebugOverlay::show_unhandled_events) { - SDL_Log("Unused event: %d", event->type); + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Unused event: %d", event->type); } } break; }