2025-02-14 15:02:49 -05:00
|
|
|
#ifndef DISPLAY_H
|
|
|
|
#define DISPLAY_H
|
|
|
|
|
2025-02-15 18:26:26 -05:00
|
|
|
#include <widgets/label/lv_label.h>
|
|
|
|
|
2025-02-14 17:19:13 -05:00
|
|
|
#include <unordered_map>
|
2025-02-16 07:21:16 -05:00
|
|
|
#include <esp_timer.h>
|
2025-02-16 08:58:40 -05:00
|
|
|
#include <memory>
|
2025-02-14 15:02:49 -05:00
|
|
|
|
2025-02-15 17:51:57 -05:00
|
|
|
#include "panel.h"
|
2025-02-15 10:11:49 -05:00
|
|
|
|
2025-02-14 15:02:49 -05:00
|
|
|
#define LVGL_TICK_PERIOD_MS 5
|
|
|
|
#define LVGL_TASK_STACK_SIZE (4 * 1024)
|
|
|
|
#define LVGL_TASK_PRIORITY 2
|
|
|
|
|
|
|
|
class Display {
|
2025-02-16 08:58:40 -05:00
|
|
|
struct ScopedLock {
|
|
|
|
explicit ScopedLock() { _lock_acquire(&lv_lock_); }
|
|
|
|
|
|
|
|
~ScopedLock() { _lock_release(&lv_lock_); }
|
|
|
|
|
|
|
|
// LVGL library is not thread-safe, this example calls LVGL APIs from tasks.
|
|
|
|
// We must use a mutex to protect it.
|
|
|
|
static _lock_t lv_lock_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Timer {
|
|
|
|
explicit Timer(esp_timer_create_args_t args) : args_(args)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Creating esp_timer with name: %s", args.name);
|
|
|
|
ESP_ERROR_CHECK(esp_timer_create(&args, &esp_timer_));
|
|
|
|
}
|
|
|
|
|
|
|
|
~Timer()
|
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(esp_timer_delete(esp_timer_));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[[maybe_unused]] esp_timer_create_args_t args_{};
|
|
|
|
esp_timer_handle_t esp_timer_ = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TimeKeeper {
|
|
|
|
using TimerHandle = Timer *;
|
|
|
|
|
|
|
|
// Timers should only be accessed using this method.
|
|
|
|
// For this reason managed_timers_ is private.
|
2025-02-16 09:12:24 -05:00
|
|
|
TimerHandle get_handle(const char *name)
|
2025-02-16 08:58:40 -05:00
|
|
|
{
|
|
|
|
return &managed_timers_.at(name);
|
|
|
|
}
|
|
|
|
|
2025-02-16 09:12:24 -05:00
|
|
|
TimerHandle operator[](const char * name) { return get_handle(name); }
|
|
|
|
|
|
|
|
TimerHandle create_timer(esp_timer_create_args_t args)
|
2025-02-16 08:58:40 -05:00
|
|
|
{
|
|
|
|
auto rt = managed_timers_.emplace(args.name, args);
|
|
|
|
if (!rt.second) {
|
|
|
|
ESP_LOGE(TAG, "Failed to insert timer into Display::managed_timers_");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &rt.first->second;
|
|
|
|
}
|
|
|
|
|
2025-02-16 09:12:24 -05:00
|
|
|
[[maybe_unused]] void stop_timer(const char *name)
|
2025-02-16 08:58:40 -05:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(esp_timer_stop(get_handle(name)->esp_timer_));
|
|
|
|
}
|
|
|
|
|
2025-02-16 09:12:24 -05:00
|
|
|
[[maybe_unused]] void delete_timer(const char *name)
|
2025-02-16 08:58:40 -05:00
|
|
|
{
|
|
|
|
managed_timers_.erase(name);
|
|
|
|
}
|
|
|
|
|
2025-02-16 09:12:24 -05:00
|
|
|
[[maybe_unused]] void
|
2025-02-16 08:58:40 -05:00
|
|
|
start_new_timer_periodic(esp_timer_create_args_t args,
|
|
|
|
uint64_t period)
|
|
|
|
{
|
|
|
|
start_timer_periodic(create_timer(args)->args_.name, period);
|
|
|
|
}
|
|
|
|
|
2025-02-16 09:12:24 -05:00
|
|
|
[[maybe_unused]] void start_timer_periodic(const char *name,
|
|
|
|
uint64_t period)
|
2025-02-16 08:58:40 -05:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(
|
|
|
|
esp_timer_start_periodic(get_handle(name)->esp_timer_, period));
|
|
|
|
}
|
|
|
|
|
2025-02-16 09:12:24 -05:00
|
|
|
[[maybe_unused]] void
|
2025-02-16 08:58:40 -05:00
|
|
|
start_new_timer_once(esp_timer_create_args_t args,
|
|
|
|
uint64_t timeout_us)
|
|
|
|
{
|
|
|
|
start_timer_once(create_timer(args)->args_.name, timeout_us);
|
|
|
|
}
|
|
|
|
|
2025-02-16 09:12:24 -05:00
|
|
|
[[maybe_unused]] void start_timer_once(const char *name,
|
|
|
|
uint64_t timeout_us)
|
2025-02-16 08:58:40 -05:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(
|
|
|
|
esp_timer_start_once(get_handle(name)->esp_timer_, timeout_us));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Timers should only be accessed using the get_handle method.
|
|
|
|
// ~Timer() will delete the timer if called.
|
2025-02-16 09:12:24 -05:00
|
|
|
std::unordered_map<const char *, Timer> managed_timers_;
|
2025-02-16 08:58:40 -05:00
|
|
|
};
|
|
|
|
|
2025-02-16 09:12:24 -05:00
|
|
|
public:
|
|
|
|
explicit Display(IPanelDevice &device);
|
|
|
|
|
|
|
|
~Display() = default;
|
|
|
|
|
|
|
|
[[nodiscard]] inline const lv_display_t *get() const { return lv_display_; }
|
|
|
|
|
|
|
|
[[nodiscard]] inline lv_display_t *get() { return lv_display_; }
|
|
|
|
|
|
|
|
[[nodiscard]] inline const lv_display_t *operator*() const { return get(); }
|
|
|
|
|
|
|
|
[[nodiscard]] inline lv_display_t *operator*() { return get(); }
|
|
|
|
|
|
|
|
void set_text(const char *text,
|
|
|
|
const char *name,
|
|
|
|
lv_label_long_mode_t long_mode = LV_LABEL_LONG_SCROLL_CIRCULAR,
|
|
|
|
lv_align_t align = LV_ALIGN_TOP_MID);
|
|
|
|
|
|
|
|
static bool lvgl_flush_ready(esp_lcd_panel_io_handle_t panel,
|
|
|
|
esp_lcd_panel_io_event_data_t *data,
|
|
|
|
void *user_ctx);
|
|
|
|
|
|
|
|
static void lvgl_flush_cb(lv_display_t *display,
|
|
|
|
const lv_area_t *area,
|
|
|
|
uint8_t *px_map);
|
|
|
|
|
|
|
|
static void lvgl_increase_tick(void *arg);
|
|
|
|
|
|
|
|
[[noreturn]] static void lvgl_port_task(void *arg);
|
|
|
|
|
|
|
|
// Public static TimeKeeper for managing ESP timers across all displays.
|
|
|
|
static TimeKeeper timers_;
|
|
|
|
|
2025-02-14 15:02:49 -05:00
|
|
|
private:
|
2025-02-16 09:12:24 -05:00
|
|
|
void register_draw_buffer();
|
|
|
|
|
2025-02-16 07:21:16 -05:00
|
|
|
void register_lvgl_tick_timer();
|
|
|
|
|
2025-02-14 17:47:44 -05:00
|
|
|
Panel panel_;
|
2025-02-14 17:19:13 -05:00
|
|
|
|
2025-02-15 17:12:06 -05:00
|
|
|
lv_display_t *lv_display_;
|
2025-02-15 14:04:08 -05:00
|
|
|
|
|
|
|
// Draw buffer associated with the lv_display_t.
|
2025-02-15 17:12:06 -05:00
|
|
|
void *lv_buf_;
|
2025-02-14 17:47:44 -05:00
|
|
|
|
2025-02-15 14:04:08 -05:00
|
|
|
// Objects stored in the screen associated with this display.
|
|
|
|
// @sa Display::set_text
|
|
|
|
// @sa lv_display_get_screen_active
|
2025-02-15 18:26:26 -05:00
|
|
|
std::unordered_map<const char *, lv_obj_t *> lv_objects_;
|
2025-02-14 15:02:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DISPLAY_H
|