summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-04-21 10:30:52 +1000
committerjacqueline <me@jacqueline.id.au>2023-04-21 10:30:52 +1000
commitcc3fd9571eadf8ad49bd7a5806a504555dc885db (patch)
treeec951aee7289017204426c493da6fc88d208ef3d
parentf9558044955827b476ce21548d544530c1457026 (diff)
downloadtangara-fw-cc3fd9571eadf8ad49bd7a5806a504555dc885db.tar.gz
split out lvgl task into its own component
-rw-r--r--src/main/CMakeLists.txt2
-rw-r--r--src/main/main.cpp70
-rw-r--r--src/ui/CMakeLists.txt5
-rw-r--r--src/ui/include/lvgl_task.hpp16
-rw-r--r--src/ui/lvgl_task.cpp111
5 files changed, 137 insertions, 67 deletions
diff --git a/src/main/CMakeLists.txt b/src/main/CMakeLists.txt
index df0d929b..49a54cd5 100644
--- a/src/main/CMakeLists.txt
+++ b/src/main/CMakeLists.txt
@@ -1,5 +1,5 @@
idf_component_register(
SRCS "main.cpp" "app_console.cpp"
INCLUDE_DIRS "."
- REQUIRES "audio" "drivers" "dev_console" "drivers")
+ REQUIRES "audio" "drivers" "dev_console" "ui")
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
diff --git a/src/main/main.cpp b/src/main/main.cpp
index b1cee384..796c70f3 100644
--- a/src/main/main.cpp
+++ b/src/main/main.cpp
@@ -6,8 +6,6 @@
#include <cstdint>
#include <memory>
-#include "core/lv_disp.h"
-#include "core/lv_obj_pos.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "driver/sdspi_host.h"
@@ -16,17 +14,10 @@
#include "esp_freertos_hooks.h"
#include "esp_intr_alloc.h"
#include "esp_log.h"
-#include "font/lv_font.h"
#include "freertos/portmacro.h"
#include "freertos/projdefs.h"
-#include "freertos/timers.h"
#include "hal/gpio_types.h"
#include "hal/spi_types.h"
-#include "lvgl/lvgl.h"
-#include "misc/lv_color.h"
-#include "misc/lv_style.h"
-#include "misc/lv_timer.h"
-#include "widgets/lv_label.h"
#include "app_console.hpp"
#include "audio_playback.hpp"
@@ -36,64 +27,13 @@
#include "display_init.hpp"
#include "gpio_expander.hpp"
#include "i2c.hpp"
+#include "lvgl_task.hpp"
#include "spi.hpp"
#include "storage.hpp"
#include "touchwheel.hpp"
static const char* TAG = "MAIN";
-auto tick_hook(TimerHandle_t xTimer) -> void {
- lv_tick_inc(1);
-}
-
-static const size_t kLvglStackSize = 8 * 1024;
-static StaticTask_t sLvglTaskBuffer = {};
-static StackType_t sLvglStack[kLvglStackSize] = {0};
-
-struct LvglArgs {
- drivers::GpioExpander* gpio_expander;
-};
-
-extern "C" void lvgl_main(void* voidArgs) {
- ESP_LOGI(TAG, "starting LVGL task");
- LvglArgs* args = (LvglArgs*)voidArgs;
- drivers::GpioExpander* gpio_expander = args->gpio_expander;
-
- // Dispose of the args now that we've gotten everything out of them.
- delete args;
-
- ESP_LOGI(TAG, "init lvgl");
- lv_init();
-
- // LVGL has been initialised, so we can now start reporting ticks to it.
- xTimerCreate("lv_tick", pdMS_TO_TICKS(1), pdTRUE, NULL, &tick_hook);
-
- ESP_LOGI(TAG, "init display");
- std::unique_ptr<drivers::Display> display =
- drivers::Display::create(gpio_expander, drivers::displays::kST7735R);
-
- lv_style_t style;
- lv_style_init(&style);
- lv_style_set_text_color(&style, LV_COLOR_MAKE(0xFF, 0, 0));
- // TODO: find a nice bitmap font for this display size and density.
- // lv_style_set_text_font(&style, &lv_font_montserrat_24);
-
- auto label = lv_label_create(NULL);
- lv_label_set_text(label, "COLOURS!!");
- lv_obj_add_style(label, &style, 0);
-
- lv_obj_center(label);
- lv_scr_load(label);
-
- while (1) {
- lv_timer_handler();
- vTaskDelay(pdMS_TO_TICKS(10));
- }
-
- // TODO: break from the loop to kill this task, so that we can do our RAII
- // cleanup, unregister our tick callback and so on.
-}
-
extern "C" void app_main(void) {
ESP_LOGI(TAG, "Initialising peripherals");
@@ -129,11 +69,9 @@ extern "C" void app_main(void) {
std::unique_ptr<drivers::TouchWheel> touchwheel =
std::make_unique<drivers::TouchWheel>();
- LvglArgs* lvglArgs = (LvglArgs*)calloc(1, sizeof(LvglArgs));
- lvglArgs->gpio_expander = expander;
- xTaskCreateStaticPinnedToCore(&lvgl_main, "LVGL", kLvglStackSize,
- (void*)lvglArgs, 1, sLvglStack,
- &sLvglTaskBuffer, 1);
+ std::atomic<bool> lvgl_quit;
+ TaskHandle_t lvgl_task_handle;
+ ui::StartLvgl(expander, &lvgl_quit, &lvgl_task_handle);
std::unique_ptr<audio::AudioPlayback> playback;
if (storage) {
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt
new file mode 100644
index 00000000..bdccbbdb
--- /dev/null
+++ b/src/ui/CMakeLists.txt
@@ -0,0 +1,5 @@
+idf_component_register(
+ SRCS "lvgl_task.cpp"
+ INCLUDE_DIRS "include"
+ REQUIRES "drivers")
+target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
diff --git a/src/ui/include/lvgl_task.hpp b/src/ui/include/lvgl_task.hpp
new file mode 100644
index 00000000..1a2f425c
--- /dev/null
+++ b/src/ui/include/lvgl_task.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <atomic>
+#include <cstdbool>
+
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "gpio_expander.hpp"
+
+namespace ui {
+
+auto StartLvgl(drivers::GpioExpander* gpios,
+ std::atomic<bool>* quit,
+ TaskHandle_t* handle) -> bool;
+
+} // namespace ui
diff --git a/src/ui/lvgl_task.cpp b/src/ui/lvgl_task.cpp
new file mode 100644
index 00000000..d66f6282
--- /dev/null
+++ b/src/ui/lvgl_task.cpp
@@ -0,0 +1,111 @@
+#include "lvgl_task.hpp"
+
+#include <dirent.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include <cstddef>
+#include <cstdint>
+#include <memory>
+
+#include "core/lv_disp.h"
+#include "core/lv_obj.h"
+#include "core/lv_obj_pos.h"
+#include "core/lv_obj_tree.h"
+#include "esp_log.h"
+#include "font/lv_font.h"
+#include "freertos/portmacro.h"
+#include "freertos/projdefs.h"
+#include "freertos/timers.h"
+#include "hal/gpio_types.h"
+#include "hal/spi_types.h"
+#include "lvgl/lvgl.h"
+#include "misc/lv_color.h"
+#include "misc/lv_style.h"
+#include "misc/lv_timer.h"
+#include "widgets/lv_label.h"
+
+#include "display.hpp"
+#include "display_init.hpp"
+#include "gpio_expander.hpp"
+
+namespace ui {
+
+static const char* kTag = "lv_task";
+
+auto tick_hook(TimerHandle_t xTimer) -> void {
+ lv_tick_inc(1);
+}
+
+struct LvglArgs {
+ drivers::GpioExpander* gpio_expander;
+ std::atomic<bool>* quit;
+};
+
+void LvglMain(void* voidArgs) {
+ LvglArgs* args = (LvglArgs*)voidArgs;
+ drivers::GpioExpander* gpio_expander = args->gpio_expander;
+ std::atomic<bool>* quit = args->quit;
+
+ // Dispose of the args now that we've gotten everything out of them.
+ delete args;
+
+ {
+ ESP_LOGI(kTag, "init lvgl");
+ lv_init();
+
+ // LVGL has been initialised, so we can now start reporting ticks to it.
+ TimerHandle_t tick_timer =
+ xTimerCreate("lv_tick", pdMS_TO_TICKS(1), pdTRUE, NULL, &tick_hook);
+
+ ESP_LOGI(kTag, "init display");
+ std::unique_ptr<drivers::Display> display =
+ drivers::Display::create(gpio_expander, drivers::displays::kST7735R);
+
+ lv_style_t style;
+ lv_style_init(&style);
+ lv_style_set_text_color(&style, LV_COLOR_MAKE(0xFF, 0, 0));
+ // TODO: find a nice bitmap font for this display size and density.
+ // lv_style_set_text_font(&style, &lv_font_montserrat_24);
+
+ auto label = lv_label_create(NULL);
+ lv_label_set_text(label, "COLOURS!!");
+ lv_obj_add_style(label, &style, 0);
+
+ lv_obj_center(label);
+ lv_scr_load(label);
+
+ while (!quit->load()) {
+ lv_timer_handler();
+ vTaskDelay(pdMS_TO_TICKS(10));
+ }
+
+ // TODO(robin? daniel?): De-init the UI stack here.
+ lv_obj_del(label);
+ lv_style_reset(&style);
+
+ xTimerDelete(tick_timer, portMAX_DELAY);
+
+ lv_deinit();
+ }
+
+ vTaskDelete(NULL);
+}
+
+static const size_t kLvglStackSize = 8 * 1024;
+static StaticTask_t sLvglTaskBuffer = {};
+static StackType_t sLvglStack[kLvglStackSize] = {0};
+
+auto StartLvgl(drivers::GpioExpander* gpios,
+ std::atomic<bool>* quit,
+ TaskHandle_t* handle) -> bool {
+ LvglArgs* args = new LvglArgs();
+ args->gpio_expander = gpios;
+ args->quit = quit;
+
+ return xTaskCreateStaticPinnedToCore(&LvglMain, "LVGL", kLvglStackSize,
+ reinterpret_cast<void*>(args), 1,
+ sLvglStack, &sLvglTaskBuffer, 1);
+}
+
+} // namespace ui