From 49127d52b23a1a084d1528f9b642dc19b7118335 Mon Sep 17 00:00:00 2001 From: Fritz Heiden Date: Sat, 24 May 2025 15:03:03 +0200 Subject: [PATCH] refactor: put display logic into dedicated files --- src/main.cpp | 67 +++++-------------------------------------- src/ui/display.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++ src/ui/display.hpp | 10 +++++++ 3 files changed, 88 insertions(+), 60 deletions(-) create mode 100644 src/ui/display.cpp create mode 100644 src/ui/display.hpp diff --git a/src/main.cpp b/src/main.cpp index fa9930c..d32c1e0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,10 @@ #include #include -#include -#include -#include + +#include #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 240 -#define FONT_SIZE 2 - -#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8)) -uint32_t draw_buf[DRAW_BUF_SIZE / 4]; const int ONBOARD_LED_PIN = 2; const int ANALOG_INPUT_PIN = 34; @@ -21,24 +16,17 @@ const int ENCODER_B_PIN = 13; void handleToneGeneration(void *pvParameters); void handleInputControls(void *pvParameters); -void handle_lvgl_logs(lv_log_level_t level, const char *buf); void handle_pad_inputs(void *pvParameters); -void handle_update_display(void *pvParameters); -void handle_update_values(lv_timer_t *timer); bool toneOn = false; int frequency = 220; // A4 -String label_text; Versatile_RotaryEncoder *encoder; void handlePress(); void handleRelease(); void handleRotate(int8_t rotation); -uint32_t get_time(); -lv_obj_t *label; -lv_timer_t *update_values_timer; void setup() { @@ -60,36 +48,15 @@ void setup() xTaskCreate(handleInputControls, "handleInputContols", 2048, NULL, 1, NULL); xTaskCreate(handle_pad_inputs, "handle_pad_inputs", 2048, NULL, 1, NULL); - lv_init(); - lv_log_register_print_cb(handle_lvgl_logs); - lv_tick_set_cb(get_time); - update_values_timer = lv_timer_create(handle_update_values, 5, NULL); - lv_timer_ready(update_values_timer); - - lv_display_t *display; - display = lv_tft_espi_create(SCREEN_HEIGHT, SCREEN_WIDTH, draw_buf, sizeof(draw_buf)); - lv_display_set_rotation(display, LV_DISPLAY_ROTATION_90); - - label = lv_label_create(lv_screen_active()); - lv_obj_align(label, LV_ALIGN_CENTER, 0, 0); + Display::init(); Serial.println("setup complete"); - label_text = "Boot complete."; -} - -void handle_lvgl_logs(lv_log_level_t level, const char *buf) -{ - Serial.printf("LVGL: %s\r\n", buf); -} - -uint32_t get_time() -{ - return esp_timer_get_time() / 1000; + Display::set_label_text("Boot complete."); } void loop() { - lv_timer_handler(); + Display::update(); delay(5); } @@ -142,7 +109,7 @@ void handlePress() { Serial.println("Encoder pressed"); digitalWrite(ONBOARD_LED_PIN, HIGH); - label_text = "Button pressed."; + Display::set_label_text("Button pressed."); } void handleRelease() @@ -166,25 +133,5 @@ void handleRotate(int8_t rotation) Serial.print("Frequency: "); Serial.println(frequency); - label_text = "Frequency: " + String(frequency) + "Hz"; + Display::set_label_text("Frequency: " + String(frequency) + "Hz"); } - -void handle_update_display(void *pvParameters) -{ - while (true) - { - if (lv_label_get_text(label) != label_text.c_str()) - { - lv_label_set_text(label, label_text.c_str()); - } - delay(100); - } -} - -void handle_update_values(lv_timer_t *timer) -{ - if (label != NULL && lv_label_get_text(label) != label_text.c_str()) - { - lv_label_set_text(label, label_text.c_str()); - } -} \ No newline at end of file diff --git a/src/ui/display.cpp b/src/ui/display.cpp new file mode 100644 index 0000000..8504277 --- /dev/null +++ b/src/ui/display.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +#ifndef SCREEN_WIDTH +#define SCREEN_WIDTH 320 +#endif + +#ifndef SCREEN_HEIGHT +#define SCREEN_HEIGHT 240 +#endif + +#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8)) +uint32_t draw_buf[DRAW_BUF_SIZE / 4]; + +uint32_t get_time(); +void handle_lvgl_logs(lv_log_level_t level, const char *buf); +void handle_update_values(lv_timer_t *timer); + +String label_text; +lv_obj_t *label; +lv_timer_t *update_values_timer; + +namespace Display +{ + void init() + { + lv_init(); + lv_log_register_print_cb(handle_lvgl_logs); + lv_tick_set_cb(get_time); + + update_values_timer = lv_timer_create(handle_update_values, 5, NULL); + lv_timer_ready(update_values_timer); + + lv_display_t *display; + display = lv_tft_espi_create(SCREEN_HEIGHT, SCREEN_WIDTH, draw_buf, sizeof(draw_buf)); + lv_display_set_rotation(display, LV_DISPLAY_ROTATION_90); + + label = lv_label_create(lv_screen_active()); + lv_obj_align(label, LV_ALIGN_CENTER, 0, 0); + } + + void update() + { + lv_timer_handler(); + } + + void set_label_text(String text) + { + label_text = text; + } +} + +void handle_lvgl_logs(lv_log_level_t level, const char *buf) +{ + Serial.printf("LVGL: %s\r\n", buf); +} + +uint32_t get_time() +{ + return esp_timer_get_time() / 1000; +} + +void handle_update_values(lv_timer_t *timer) +{ + if (label != NULL && lv_label_get_text(label) != label_text.c_str()) + { + lv_label_set_text(label, label_text.c_str()); + } +} \ No newline at end of file diff --git a/src/ui/display.hpp b/src/ui/display.hpp new file mode 100644 index 0000000..a080857 --- /dev/null +++ b/src/ui/display.hpp @@ -0,0 +1,10 @@ +#ifndef DRUMZ_UI_DISPLAY_HPP +#define DRUMZ_UI_DISPLAY_HPP + +namespace Display { + void init(); + void update(); + void set_label_text(String text); +} + +#endif // DRUMZ_UI_DISPLAY_HPP \ No newline at end of file