diff --git a/.clang-format b/.clang-format index c420689..8d1d1c0 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,5 @@ --- +BasedOnStyle: Chromium Language: Cpp AccessModifierOffset: -4 AlignAfterOpenBracket: AlwaysBreak diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e0b568..5c857d0 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,14 +45,14 @@ include_dependency(u8g2 https://github.com/olikraus/u8g2 1e92781) add_subdirectory(components) configure_file( - "${PROJECT_SOURCE_DIR}/include/Version.h.in" - "${PROJECT_BINARY_DIR}/include/Version.h" + "${CMAKE_SOURCE_DIR}/Version.h.in" + "${CMAKE_SOURCE_DIR}/Version.h" ) include_directories( ${CMAKE_INCLUDE_PATH} ${CMAKE_SOURCE_DIR} - ${PROJECT_BINARY_DIR}/include + ${PROJECT_BINARY_DIR}/src ) add_executable(${PROJECT_NAME} diff --git a/README.md b/README.md index ecaa93d..17b3fc9 100755 --- a/README.md +++ b/README.md @@ -1,11 +1,15 @@ ## Systen Control -### ESP32-S3 +### ESP32-S3 (folder: main) This is an implementation of my custom system control project (custom pcb with Lolin ESP32-S3 Mini) and LED strip. The build process is straight forward with ESP-IDF. We used version 5.4 while development and the github actions tried to compile for multiple ESP-IDF versions, so we are safe. -### Desktop +### Desktop (folder: src) It's included also a desktop application (with SDL3), so you can test the project without any MCU. + +### Global Information + +The projects can be generated from the root, because here is the starting CMakeLists.txt file. diff --git a/include/debug/DebugOverlay.h b/include/debug/DebugOverlay.h deleted file mode 100644 index 013d550..0000000 --- a/include/debug/DebugOverlay.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -#include "../model/AppContext.h" - -namespace DebugOverlay { - inline bool show_debug_window = false; - inline bool show_unhandled_events = false; - inline bool show_led_matrix = true; - - void init(const AppContext *context); - - void update(AppContext *context, const SDL_Event *event); - - void render(const AppContext *context); - - void cleanup(); -} diff --git a/include/model/AppContext.h b/include/model/AppContext.h deleted file mode 100644 index 6d07eb0..0000000 --- a/include/model/AppContext.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include "SDL3_ttf/SDL_ttf.h" -#include "Window.h" - -class Matrix; - -class AppContext { -public: - explicit AppContext(const Window *window) : m_window(window) { - m_font_default = TTF_OpenFont( - "assets/haxrcorp-4089.otf", 21); - m_font_text = TTF_OpenFont( - "assets/Helvetica-Bold.otf", 21); - } - - ~AppContext() { - TTF_CloseFont(m_font_default); - TTF_CloseFont(m_font_text); - } - - [[nodiscard]] auto window() const -> SDL_Window *; - - [[nodiscard]] auto renderer() const -> SDL_Renderer *; - - [[nodiscard]] auto surface() const -> SDL_Surface *; - - void setMatrix(Matrix *matrix); - - [[nodiscard]] auto matrix() const -> Matrix *; - - [[nodiscard]] auto matrix_window() const -> SDL_Window *; - - [[nodiscard]] auto matrix_renderer() const -> SDL_Renderer *; - - void matrix_render() const; - - TTF_Font *m_font_default = nullptr; - -private: - const Window *m_window; - Matrix *m_matrix = nullptr; - TTF_Font *m_font_text = nullptr; -}; diff --git a/include/Common.h b/src/Common.h similarity index 100% rename from include/Common.h rename to src/Common.h diff --git a/include/ResourceManager.h b/src/ResourceManager.h similarity index 100% rename from include/ResourceManager.h rename to src/ResourceManager.h diff --git a/include/Version.h.in b/src/Version.h.in similarity index 100% rename from include/Version.h.in rename to src/Version.h.in diff --git a/src/debug/DebugOverlay.cpp b/src/debug/DebugOverlay.cpp index caada9a..02573be 100644 --- a/src/debug/DebugOverlay.cpp +++ b/src/debug/DebugOverlay.cpp @@ -1,81 +1,80 @@ #include "debug/DebugOverlay.h" +#include +#include "Common.h" +#include "Version.h" #include "imgui.h" #include "imgui_impl_sdl3.h" -#include - -#include "Common.h" #include "ui/Matrix.h" -#include "Version.h" namespace DebugOverlay { - void init(const AppContext *context) { - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO &io{ImGui::GetIO()}; +void init(const AppContext* context) { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io{ImGui::GetIO()}; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; - ImGui::StyleColorsDark(); + ImGui::StyleColorsDark(); - ImGui_ImplSDL3_InitForSDLRenderer(context->window(), context->renderer()); - ImGui_ImplSDLRenderer3_Init(context->renderer()); - } + ImGui_ImplSDL3_InitForSDLRenderer(context->window(), context->renderer()); + ImGui_ImplSDLRenderer3_Init(context->renderer()); +} - void update(AppContext *context, const SDL_Event *event) { - ImGui_ImplSDL3_ProcessEvent(event); +void update(AppContext* context, const SDL_Event* event) { + ImGui_ImplSDL3_ProcessEvent(event); - if (show_led_matrix) { - if (context->matrix_window() == nullptr) { - const auto win = createWindow("LED Matrix", 32 * 50, 8 * 50); - SDL_SetWindowFocusable(win->window(), false); - SDL_SetRenderVSync(win->renderer(), SDL_RENDERER_VSYNC_ADAPTIVE); - SDL_SetWindowPosition(win->window(), 0, 0); - SDL_ShowWindow(win->window()); + if(show_led_matrix) { + if(context->matrix_window() == nullptr) { + const auto win = createWindow("LED Matrix", 32 * 50, 8 * 50); + SDL_SetWindowFocusable(win->window(), false); + SDL_SetRenderVSync(win->renderer(), SDL_RENDERER_VSYNC_ADAPTIVE); + SDL_SetWindowPosition(win->window(), 0, 0); + SDL_ShowWindow(win->window()); - context->setMatrix(new Matrix(win)); - } - } else { - if (context->matrix_window() != nullptr) { - SDL_DestroyWindow(context->matrix_window()); - - context->setMatrix(nullptr); - } + context->setMatrix(new Matrix(win)); } - } + } else { + if(context->matrix_window() != nullptr) { + SDL_DestroyWindow(context->matrix_window()); - void render(const AppContext *context) { - ImGui_ImplSDLRenderer3_NewFrame(); - ImGui_ImplSDL3_NewFrame(); - ImGui::NewFrame(); - - if (show_debug_window && ImGui::BeginMainMenuBar()) { - if (ImGui::BeginMenu("Config")) { - ImGui::Checkbox("Show LED Matrix", &show_led_matrix); - ImGui::Checkbox("Show Unhandled Events", &show_unhandled_events); - - ImGui::EndMenu(); - } - 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("ImGui Version: %s", ImGui::GetVersion()); - - ImGui::EndMenu(); - } - ImGui::EndMainMenuBar(); + context->setMatrix(nullptr); } - - // Rendering - ImGui::Render(); - ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), context->renderer()); - } - - void cleanup() { - ImGui_ImplSDLRenderer3_Shutdown(); - ImGui_ImplSDL3_Shutdown(); - ImGui::DestroyContext(); } } + +void render(const AppContext* context) { + ImGui_ImplSDLRenderer3_NewFrame(); + ImGui_ImplSDL3_NewFrame(); + ImGui::NewFrame(); + + if(show_debug_window && ImGui::BeginMainMenuBar()) { + if(ImGui::BeginMenu("Config")) { + ImGui::Checkbox("Show LED Matrix", &show_led_matrix); + ImGui::Checkbox("Show Unhandled Events", &show_unhandled_events); + + ImGui::EndMenu(); + } + 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("ImGui Version: %s", ImGui::GetVersion()); + + ImGui::EndMenu(); + } + ImGui::EndMainMenuBar(); + } + + // Rendering + ImGui::Render(); + ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), context->renderer()); +} + +void cleanup() { + ImGui_ImplSDLRenderer3_Shutdown(); + ImGui_ImplSDL3_Shutdown(); + ImGui::DestroyContext(); +} +} // namespace DebugOverlay diff --git a/src/debug/DebugOverlay.h b/src/debug/DebugOverlay.h new file mode 100644 index 0000000..2ca4d6f --- /dev/null +++ b/src/debug/DebugOverlay.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +#include "model/AppContext.h" + +namespace DebugOverlay { +inline bool show_debug_window = false; +inline bool show_unhandled_events = false; +inline bool show_led_matrix = true; + +void init(const AppContext* context); + +void update(AppContext* context, const SDL_Event* event); + +void render(const AppContext* context); + +void cleanup(); +} diff --git a/include/hal/u8g2_hal_sdl.h b/src/hal/u8g2_hal_sdl.h similarity index 71% rename from include/hal/u8g2_hal_sdl.h rename to src/hal/u8g2_hal_sdl.h index bdf5fbb..8667488 100644 --- a/include/hal/u8g2_hal_sdl.h +++ b/src/hal/u8g2_hal_sdl.h @@ -2,9 +2,9 @@ #include "u8g2.h" -#define U8G2_SCREEN_WIDTH (128) -#define U8G2_SCREEN_HEIGHT (64) -#define U8G2_SCREEN_FACTOR (3) +#define U8G2_SCREEN_WIDTH (128) +#define U8G2_SCREEN_HEIGHT (64) +#define U8G2_SCREEN_FACTOR (3) #define U8G2_SCREEN_PADDING (25) uint8_t u8x8_byte_sdl_hw_spi(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr); diff --git a/src/model/AppContext.cpp b/src/model/AppContext.cpp index 9eac196..9d33a6b 100644 --- a/src/model/AppContext.cpp +++ b/src/model/AppContext.cpp @@ -2,42 +2,42 @@ #include "ui/Matrix.h" -auto AppContext::window() const -> SDL_Window * { +auto AppContext::window() const -> SDL_Window* { return m_window->window(); } -auto AppContext::renderer() const -> SDL_Renderer * { +auto AppContext::renderer() const -> SDL_Renderer* { return m_window->renderer(); } -auto AppContext::surface() const -> SDL_Surface * { +auto AppContext::surface() const -> SDL_Surface* { return SDL_GetWindowSurface(m_window->window()); } -void AppContext::setMatrix(Matrix *matrix) { +void AppContext::setMatrix(Matrix* matrix) { m_matrix = matrix; } -auto AppContext::matrix() const -> Matrix * { +auto AppContext::matrix() const -> Matrix* { return m_matrix; } -auto AppContext::matrix_window() const -> SDL_Window * { - if (m_matrix && m_matrix->window()) { +auto AppContext::matrix_window() const -> SDL_Window* { + if(m_matrix && m_matrix->window()) { return m_matrix->window()->window(); } return nullptr; } -auto AppContext::matrix_renderer() const -> SDL_Renderer * { - if (m_matrix && m_matrix->window()) { +auto AppContext::matrix_renderer() const -> SDL_Renderer* { + if(m_matrix && m_matrix->window()) { return m_matrix->window()->renderer(); } return nullptr; } void AppContext::matrix_render() const { - if (m_matrix && m_matrix->window()) { + if(m_matrix && m_matrix->window()) { m_matrix->render(); } } diff --git a/src/model/AppContext.h b/src/model/AppContext.h new file mode 100644 index 0000000..272f927 --- /dev/null +++ b/src/model/AppContext.h @@ -0,0 +1,43 @@ +#pragma once + +#include "SDL3_ttf/SDL_ttf.h" +#include "Window.h" + +class Matrix; + +class AppContext { +public: + explicit AppContext(const Window* window) + : m_window(window) { + m_font_default = TTF_OpenFont("assets/haxrcorp-4089.otf", 21); + m_font_text = TTF_OpenFont("assets/Helvetica-Bold.otf", 21); + } + + ~AppContext() { + TTF_CloseFont(m_font_default); + TTF_CloseFont(m_font_text); + } + + [[nodiscard]] auto window() const -> SDL_Window*; + + [[nodiscard]] auto renderer() const -> SDL_Renderer*; + + [[nodiscard]] auto surface() const -> SDL_Surface*; + + void setMatrix(Matrix* matrix); + + [[nodiscard]] auto matrix() const -> Matrix*; + + [[nodiscard]] auto matrix_window() const -> SDL_Window*; + + [[nodiscard]] auto matrix_renderer() const -> SDL_Renderer*; + + void matrix_render() const; + + TTF_Font* m_font_default = nullptr; + +private: + const Window* m_window; + Matrix* m_matrix = nullptr; + TTF_Font* m_font_text = nullptr; +}; diff --git a/src/model/Window.cpp b/src/model/Window.cpp index 72375f5..baadbf1 100644 --- a/src/model/Window.cpp +++ b/src/model/Window.cpp @@ -1,9 +1,9 @@ #include "model/Window.h" -auto Window::window() const -> SDL_Window * { +auto Window::window() const -> SDL_Window* { return m_window; } -auto Window::renderer() const -> SDL_Renderer * { +auto Window::renderer() const -> SDL_Renderer* { return SDL_GetRenderer(m_window); } diff --git a/include/model/Window.h b/src/model/Window.h similarity index 80% rename from include/model/Window.h rename to src/model/Window.h index 5145fe8..0ee46e4 100644 --- a/include/model/Window.h +++ b/src/model/Window.h @@ -10,7 +10,7 @@ public: [[nodiscard]] auto window() const -> SDL_Window*; - [[nodiscard]] auto renderer() const -> SDL_Renderer *; + [[nodiscard]] auto renderer() const -> SDL_Renderer*; private: SDL_Window* m_window = nullptr; diff --git a/include/ui/Device.h b/src/ui/Device.h similarity index 96% rename from include/ui/Device.h rename to src/ui/Device.h index e6c056c..09a3355 100644 --- a/include/ui/Device.h +++ b/src/ui/Device.h @@ -5,7 +5,7 @@ #include #include "UIWidget.h" -#include "../model/AppContext.h" +#include "model/AppContext.h" #include "common/Widget.h" class Device final : public UIWidget { diff --git a/include/ui/Matrix.h b/src/ui/Matrix.h similarity index 55% rename from include/ui/Matrix.h rename to src/ui/Matrix.h index 3b8f743..e872a43 100644 --- a/include/ui/Matrix.h +++ b/src/ui/Matrix.h @@ -1,16 +1,17 @@ #pragma once -#include "../model/Window.h" + +#include "model/Window.h" class Matrix { public: - explicit Matrix(Window *window); + explicit Matrix(Window* window); - [[nodiscard]] Window *window() const; + [[nodiscard]] Window* window() const; void render() const; private: void draw_colored_grid(int rows, int cols, float cellSize, float spacing) const; - Window *m_window; + Window* m_window; }; diff --git a/include/ui/UIWidget.h b/src/ui/UIWidget.h similarity index 87% rename from include/ui/UIWidget.h rename to src/ui/UIWidget.h index 09454f1..7e55371 100644 --- a/include/ui/UIWidget.h +++ b/src/ui/UIWidget.h @@ -1,6 +1,6 @@ #pragma once -#include "../model/AppContext.h" +#include "model/AppContext.h" class UIWidget { public: diff --git a/src/ui/widgets/Button.cpp b/src/ui/widgets/Button.cpp index 5d93b0b..26873e1 100644 --- a/src/ui/widgets/Button.cpp +++ b/src/ui/widgets/Button.cpp @@ -21,8 +21,10 @@ Button::Button( } void Button::render() const { - const auto button = ResourceManager::getInstance().get_texture(get_context()->renderer(), "assets/button_normal.png"); - const auto overlay =ResourceManager::getInstance().get_texture(get_context()->renderer(), "assets/button_pressed_overlay.png"); + const auto button = ResourceManager::getInstance().get_texture( + get_context()->renderer(), "assets/button_normal.png"); + const auto overlay = ResourceManager::getInstance().get_texture( + get_context()->renderer(), "assets/button_pressed_overlay.png"); const auto dst = SDL_FRect(m_x, m_y, m_width, m_width); SDL_RenderTexture(get_context()->renderer(), button, nullptr, &dst); diff --git a/include/ui/widgets/Button.h b/src/ui/widgets/Button.h similarity index 93% rename from include/ui/widgets/Button.h rename to src/ui/widgets/Button.h index 7653510..1f788e1 100644 --- a/include/ui/widgets/Button.h +++ b/src/ui/widgets/Button.h @@ -1,6 +1,6 @@ #pragma once -#include "../UIWidget.h" +#include "ui/UIWidget.h" #include diff --git a/src/ui/widgets/D_Pad.cpp b/src/ui/widgets/D_Pad.cpp index c9079ef..aec9ce1 100644 --- a/src/ui/widgets/D_Pad.cpp +++ b/src/ui/widgets/D_Pad.cpp @@ -1,6 +1,6 @@ #include "ui/widgets/D_Pad.h" -#include +#include "ResourceManager.h" D_Pad::D_Pad( void* appState, diff --git a/include/ui/widgets/D_Pad.h b/src/ui/widgets/D_Pad.h similarity index 99% rename from include/ui/widgets/D_Pad.h rename to src/ui/widgets/D_Pad.h index db56ee0..7833214 100644 --- a/include/ui/widgets/D_Pad.h +++ b/src/ui/widgets/D_Pad.h @@ -1,7 +1,6 @@ #pragma once #include "functional" - #include "ui/UIWidget.h" #define DPAD_WIDTH (105)