display.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef DISPLAY_H
  2. #define DISPLAY_H
  3. #include <lvgl.h>
  4. #include <esp_timer.h>
  5. #include <esp_log.h>
  6. #include <esp_pm.h>
  7. #include <string>
  8. #include <chrono>
  9. struct DisplayFonts {
  10. const lv_font_t* text_font = nullptr;
  11. const lv_font_t* icon_font = nullptr;
  12. const lv_font_t* emoji_font = nullptr;
  13. };
  14. class Display {
  15. public:
  16. Display();
  17. virtual ~Display();
  18. virtual void SetStatus(const char* status);
  19. virtual void ShowNotification(const char* notification, int duration_ms = 3000);
  20. virtual void ShowNotification(const std::string &notification, int duration_ms = 3000);
  21. virtual void SetEmotion(const char* emotion);
  22. virtual void SetChatMessage(const char* role, const char* content);
  23. virtual void SetIcon(const char* icon);
  24. virtual void SetPreviewImage(const lv_img_dsc_t* image);
  25. virtual void SetTheme(const std::string& theme_name);
  26. virtual std::string GetTheme() { return current_theme_name_; }
  27. virtual void UpdateStatusBar(bool update_all = false);
  28. virtual void ShowStandbyScreen(bool show);
  29. inline int width() const { return width_; }
  30. inline int height() const { return height_; }
  31. protected:
  32. int width_ = 0;
  33. int height_ = 0;
  34. esp_pm_lock_handle_t pm_lock_ = nullptr;
  35. lv_display_t *display_ = nullptr;
  36. lv_obj_t *emotion_label_ = nullptr;
  37. lv_obj_t *network_label_ = nullptr;
  38. lv_obj_t *status_label_ = nullptr;
  39. lv_obj_t *notification_label_ = nullptr;
  40. lv_obj_t *mute_label_ = nullptr;
  41. lv_obj_t *battery_label_ = nullptr;
  42. lv_obj_t* chat_message_label_ = nullptr;
  43. lv_obj_t* low_battery_popup_ = nullptr;
  44. lv_obj_t* low_battery_label_ = nullptr;
  45. const char* battery_icon_ = nullptr;
  46. const char* network_icon_ = nullptr;
  47. bool muted_ = false;
  48. std::string current_theme_name_;
  49. std::chrono::system_clock::time_point last_status_update_time_;
  50. esp_timer_handle_t notification_timer_ = nullptr;
  51. friend class DisplayLockGuard;
  52. virtual bool Lock(int timeout_ms = 0) = 0;
  53. virtual void Unlock() = 0;
  54. };
  55. class DisplayLockGuard {
  56. public:
  57. DisplayLockGuard(Display *display) : display_(display) {
  58. if (!display_->Lock(30000)) {
  59. ESP_LOGE("Display", "Failed to lock display");
  60. }
  61. }
  62. ~DisplayLockGuard() {
  63. display_->Unlock();
  64. }
  65. private:
  66. Display *display_;
  67. };
  68. class NoDisplay : public Display {
  69. private:
  70. virtual bool Lock(int timeout_ms = 0) override {
  71. return true;
  72. }
  73. virtual void Unlock() override {}
  74. };
  75. #endif