22
33#include " LovyanGFX.h"
44#include " graphics/driver/DisplayDriverConfig.h"
5+ #include " graphics/driver/ScreenSleepController.h"
56#include " graphics/driver/TFTDriver.h"
67#include " input/InputDriver.h"
8+ #include " input/ScreenSleepObserver.h"
79#include " lvgl_private.h"
810#include " util/ILog.h"
911#include < functional>
@@ -12,7 +14,7 @@ constexpr uint32_t defaultLongPressTime = 600; // ms until long press is detecte
1214constexpr uint32_t defaultGestureLimit = 10 ; // x/y diff pixel until a swipe gesture is detected (lvgl default is 50)
1315
1416constexpr uint32_t defaultScreenTimeout = 30 * 1000 ;
15- constexpr uint32_t defaultBrightness = 153 ;
17+ constexpr uint8_t defaultBrightness = 153 ;
1618
1719template <class LGFX > class LGFXDriver : public TFTDriver <LGFX>
1820{
@@ -24,15 +26,23 @@ template <class LGFX> class LGFXDriver : public TFTDriver<LGFX>
2426 bool hasTouch (void ) override ;
2527 bool hasButton (void ) override { return lgfx->hasButton (); }
2628 bool hasLight (void ) override { return lgfx->light (); }
27- bool isPowersaving (void ) override { return powerSaving ; }
29+ bool isPowersaving (void ) override { return _sleepController. isSleeping () ; }
2830 void printConfig (void ) override ;
2931 void task_handler (void ) override ;
3032
31- uint8_t getBrightness (void ) override { return lgfx->getBrightness (); }
32- void setBrightness (uint8_t brightness) override ;
33+ uint8_t getBrightness (void ) override { return lgfx->getBrightness (); }
34+ void setHardwareBrightness (uint8_t brightness) override { lgfx->setBrightness (brightness); }
35+ void setBrightness (uint8_t brightness) override ;
3336
3437 uint16_t getScreenTimeout () override { return screenTimeout / 1000 ; }
35- void setScreenTimeout (uint16_t timeout) override { screenTimeout = timeout * 1000 ; };
38+ void setScreenTimeout (uint16_t timeout) override { screenTimeout = (uint32_t )timeout * 1000 ; };
39+
40+ void panelSleep (void ) override { lgfx->sleep (); }
41+ void panelWake (void ) override { lgfx->wakeup (); }
42+ void powerSaveOn (void ) override { lgfx->powerSaveOn (); }
43+ void powerSaveOff (void ) override { lgfx->powerSaveOff (); }
44+ bool hasBacklight (void ) override { return lgfx->light () != nullptr ; }
45+ int getTouchIntPin (void ) override ;
3646
3747 protected:
3848 // lvgl callbacks have to be static cause it's a C library, not C++
@@ -41,13 +51,12 @@ template <class LGFX> class LGFXDriver : public TFTDriver<LGFX>
4151 static void touchpad_read (lv_indev_t *indev_driver, lv_indev_data_t *data);
4252
4353 uint32_t screenTimeout;
44- uint32_t lastBrightness;
45- bool powerSaving;
4654
4755 private:
4856 void init_lgfx (void );
4957
5058 static LGFX *lgfx;
59+ ScreenSleepController _sleepController;
5160 size_t bufsize;
5261 lv_color_t *buf1;
5362 lv_color_t *buf2;
@@ -59,15 +68,17 @@ template <class LGFX> LGFX *LGFXDriver<LGFX>::lgfx = nullptr;
5968template <class LGFX >
6069LGFXDriver<LGFX>::LGFXDriver(uint16_t width, uint16_t height)
6170 : TFTDriver<LGFX>(lgfx ? lgfx : new LGFX, width, height), screenTimeout(defaultScreenTimeout),
62- lastBrightness (defaultBrightness), powerSaving(false ), bufsize(0 ), buf1(nullptr ), buf2(nullptr ), calibrating(false )
71+ _sleepController (this , defaultBrightness),
72+ bufsize(0 ), buf1(nullptr ), buf2(nullptr ), calibrating(false )
6373{
6474 lgfx = this ->tft ;
6575}
6676
6777template <class LGFX >
6878LGFXDriver<LGFX>::LGFXDriver(const DisplayDriverConfig &cfg)
6979 : TFTDriver<LGFX>(lgfx ? lgfx : new LGFX(cfg), cfg.width(), cfg.height()), screenTimeout(defaultScreenTimeout),
70- lastBrightness (defaultBrightness), powerSaving(false ), bufsize(0 ), buf1(nullptr ), buf2(nullptr ), calibrating(false )
80+ _sleepController (this , defaultBrightness),
81+ bufsize(0 ), buf1(nullptr ), buf2(nullptr ), calibrating(false )
7182{
7283 lgfx = this ->tft ;
7384}
@@ -81,91 +92,18 @@ template <class LGFX> bool LGFXDriver<LGFX>::hasTouch(void)
8192#endif
8293}
8394
84- template <class LGFX > void LGFXDriver<LGFX>::task_handler (void )
95+ template <class LGFX > int LGFXDriver<LGFX>::getTouchIntPin (void )
8596{
86- // handle display timeout
87- if ((screenTimeout > 0 && lv_display_get_inactive_time (NULL ) > screenTimeout) || powerSaving ||
88- (DisplayDriver::view->isScreenLocked ())) {
89- // sleep screen only if there are means for wakeup
90- if (DisplayDriver::view->getInputDriver ()->hasPointerDevice () || hasTouch () ||
91- DisplayDriver::view->getInputDriver ()->hasKeyboardDevice () || hasButton ()) {
92- if (hasLight ()) {
93- if (!powerSaving) {
94- // dim display brightness slowly down
95- uint32_t brightness = lgfx->getBrightness ();
96- if (brightness > 0 ) {
97- lgfx->setBrightness (brightness - 1 );
98- } else {
99- ILOG_INFO (" enter powersave" );
100- DisplayDriver::view->screenSaving (true );
101- if (hasTouch () && hasButton ()) {
102- ILOG_DEBUG (" disable touch, enable button input" );
103- lv_indev_enable (DisplayDriver::touch, false );
104- lv_indev_enable (InputDriver::instance ()->getButton (), true );
105- }
106- lgfx->sleep ();
107- lgfx->powerSaveOn ();
108- powerSaving = true ;
109- }
110- }
111- if (powerSaving) {
112- int pin_int = -1 ;
113- if (hasTouch ()) {
11497#ifndef CUSTOM_TOUCH_DRIVER
115- pin_int = lgfx->touch ()->config ().pin_int ;
98+ return lgfx-> touch () ? lgfx->touch ()->config ().pin_int : - 1 ;
11699#else
117- pin_int = lgfx->getTouchInt ();
118- #endif
119- }
120- if (hasButton ()) {
121- #ifdef BUTTON_PIN // only relevant for CYD scenario
122- pin_int = BUTTON_PIN;
100+ return lgfx->getTouchInt ();
123101#endif
124- }
125- if ((pin_int >= 0 && DisplayDriver::view->sleep (pin_int)) ||
126- (screenTimeout + 50 > lv_display_get_inactive_time (NULL ) && !DisplayDriver::view->isScreenLocked ())) {
127- delay (2 ); // let the CPU finish to restore all register in case of light sleep
128- // woke up by touch or button
129- ILOG_INFO (" leaving powersave" );
130- powerSaving = false ;
131- DisplayDriver::view->triggerHeartbeat ();
132- lgfx->powerSaveOff ();
133- lgfx->wakeup ();
134- lgfx->setBrightness (lastBrightness);
135- DisplayDriver::view->screenSaving (false );
136- if (hasTouch () && hasButton ()) {
137- ILOG_DEBUG (" enable touch, disable button input" );
138- lv_indev_enable (DisplayDriver::touch, true );
139- lv_indev_enable (InputDriver::instance ()->getButton (), false );
140- }
141- lv_display_trigger_activity (NULL );
142- } else {
143- // we woke up due to e.g. serial traffic (or sleep() simply not implemented)
144- // continue with processing loop and enter sleep() again next round
145- }
146- }
147- }
148- // no BL pin defined to control brightness, so show blank screen instead
149- else {
150- if (!powerSaving) {
151- DisplayDriver::view->blankScreen (true );
152- lgfx->sleep ();
153- lgfx->powerSaveOn ();
154- powerSaving = true ;
155- }
156- if (screenTimeout > lv_display_get_inactive_time (NULL )) {
157- DisplayDriver::view->blankScreen (false );
158- lgfx->powerSaveOff ();
159- lgfx->wakeup ();
160- powerSaving = false ;
161- lv_disp_trig_activity (NULL );
162- }
163- }
164- }
165- } else if (lgfx->getBrightness () < lastBrightness) {
166- lgfx->setBrightness (lastBrightness);
167- lastBrightness = lgfx->getBrightness ();
168- }
102+ }
103+
104+ template <class LGFX > void LGFXDriver<LGFX>::task_handler(void )
105+ {
106+ _sleepController.tick (screenTimeout, hasTouch (), hasButton ());
169107
170108 if (!calibrating) {
171109 DisplayDriver::task_handler ();
@@ -337,6 +275,9 @@ template <class LGFX> void LGFXDriver<LGFX>::init(DeviceGUI *gui)
337275 lv_timer_set_period (timer, 10 ); // 100Hz as I2C touch controllers support
338276#endif
339277 }
278+
279+ _sleepController.setContext (DisplayDriver::view, DisplayDriver::touch);
280+ DisplayDriver::_sleepController = &_sleepController;
340281}
341282
342283template <class LGFX > void LGFXDriver<LGFX>::init_lgfx(void )
@@ -413,8 +354,8 @@ template <class LGFX> bool LGFXDriver<LGFX>::calibrate(uint16_t parameters[8])
413354
414355template <class LGFX > void LGFXDriver<LGFX>::setBrightness(uint8_t brightness)
415356{
416- lgfx-> setBrightness (brightness);
417- lastBrightness = brightness;
357+ setHardwareBrightness (brightness);
358+ _sleepController. setWakeBrightness ( brightness) ;
418359}
419360
420361template <class LGFX > void LGFXDriver<LGFX>::printConfig(void )
0 commit comments