display.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. inline int width() const { return width_; }
  24. inline int height() const { return height_; }
  25. protected:
  26. int width_ = 0;
  27. int height_ = 0;
  28. esp_pm_lock_handle_t pm_lock_ = nullptr;
  29. lv_display_t *display_ = nullptr;
  30. lv_obj_t *emotion_label_ = nullptr;
  31. lv_obj_t *network_label_ = nullptr;
  32. lv_obj_t *status_label_ = nullptr;
  33. lv_obj_t *notification_label_ = nullptr;
  34. lv_obj_t *mute_label_ = nullptr;
  35. lv_obj_t *battery_label_ = nullptr;
  36. lv_obj_t* chat_message_label_ = nullptr;
  37. lv_obj_t* low_battery_popup_ = nullptr;
  38. const char* battery_icon_ = nullptr;
  39. const char* network_icon_ = nullptr;
  40. bool muted_ = false;
  41. esp_timer_handle_t notification_timer_ = nullptr;
  42. esp_timer_handle_t update_timer_ = nullptr;
  43. friend class DisplayLockGuard;
  44. virtual bool Lock(int timeout_ms = 0) = 0;
  45. virtual void Unlock() = 0;
  46. virtual void Update();
  47. };
  48. class DisplayLockGuard {
  49. public:
  50. DisplayLockGuard(Display *display) : display_(display) {
  51. if (!display_->Lock(3000)) {
  52. ESP_LOGE("Display", "Failed to lock display");
  53. }
  54. }
  55. ~DisplayLockGuard() {
  56. display_->Unlock();
  57. }
  58. private:
  59. Display *display_;
  60. };
  61. class NoDisplay : public Display {
  62. private:
  63. virtual bool Lock(int timeout_ms = 0) override {
  64. return true;
  65. }
  66. virtual void Unlock() override {}
  67. };
  68. #endif