display.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. struct DisplayFonts {
  9. const lv_font_t* text_font = nullptr;
  10. const lv_font_t* icon_font = nullptr;
  11. const lv_font_t* emoji_font = nullptr;
  12. };
  13. class Display {
  14. public:
  15. Display();
  16. virtual ~Display();
  17. virtual void SetStatus(const char* status);
  18. virtual void ShowNotification(const char* notification, int duration_ms = 3000);
  19. virtual void ShowNotification(const std::string &notification, int duration_ms = 3000);
  20. virtual void SetEmotion(const char* emotion);
  21. virtual void SetChatMessage(const char* role, const char* content);
  22. virtual void SetIcon(const char* icon);
  23. virtual void SetTheme(const std::string& theme_name);
  24. virtual std::string GetTheme() { return current_theme_name_; }
  25. inline int width() const { return width_; }
  26. inline int height() const { return height_; }
  27. protected:
  28. int width_ = 0;
  29. int height_ = 0;
  30. esp_pm_lock_handle_t pm_lock_ = nullptr;
  31. lv_display_t *display_ = nullptr;
  32. lv_obj_t *emotion_label_ = nullptr;
  33. lv_obj_t *network_label_ = nullptr;
  34. lv_obj_t *status_label_ = nullptr;
  35. lv_obj_t *notification_label_ = nullptr;
  36. lv_obj_t *mute_label_ = nullptr;
  37. lv_obj_t *battery_label_ = nullptr;
  38. lv_obj_t* chat_message_label_ = nullptr;
  39. lv_obj_t* low_battery_popup_ = nullptr;
  40. const char* battery_icon_ = nullptr;
  41. const char* network_icon_ = nullptr;
  42. bool muted_ = false;
  43. std::string current_theme_name_;
  44. esp_timer_handle_t notification_timer_ = nullptr;
  45. esp_timer_handle_t update_timer_ = nullptr;
  46. friend class DisplayLockGuard;
  47. virtual bool Lock(int timeout_ms = 0) = 0;
  48. virtual void Unlock() = 0;
  49. virtual void Update();
  50. };
  51. class DisplayLockGuard {
  52. public:
  53. DisplayLockGuard(Display *display) : display_(display) {
  54. if (!display_->Lock(3000)) {
  55. ESP_LOGE("Display", "Failed to lock display");
  56. }
  57. }
  58. ~DisplayLockGuard() {
  59. display_->Unlock();
  60. }
  61. private:
  62. Display *display_;
  63. };
  64. class NoDisplay : public Display {
  65. private:
  66. virtual bool Lock(int timeout_ms = 0) override {
  67. return true;
  68. }
  69. virtual void Unlock() override {}
  70. };
  71. #endif