diff --git a/Platformio/HAL/Targets/ESP32/display/display.cpp b/Platformio/HAL/Targets/ESP32/display/display.cpp index 422bd2c..c27ba7e 100644 --- a/Platformio/HAL/Targets/ESP32/display/display.cpp +++ b/Platformio/HAL/Targets/ESP32/display/display.cpp @@ -58,9 +58,15 @@ void Display::setupTouchScreen(){ touch.begin(128); // Initialize touchscreen and set sensitivity threshold } -void Display::setBrightness(uint8_t brigthness) +void Display::setBrightness(uint8_t brightness) { - ledcWrite(LCD_BACKLIGHT_LEDC_CHANNEL, brigthness); + mAwakeBrightness = brightness; + startFade(); +} + +void Display::setCurrentBrightness(uint8_t brightness){ + mBrightness = brightness; + ledcWrite(LCD_BACKLIGHT_LEDC_CHANNEL, mBrightness); } void Display::turnOff() @@ -102,6 +108,40 @@ void Display::screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data){ } } +void Display::fadeImpl(void* ){ + bool fadeDone = false; + while(!fadeDone){ + fadeDone = getInstance()->fade(); + vTaskDelay(3 / portTICK_PERIOD_MS); // 3 miliseconds between steps + // 0 - 255 will take about .75 seconds to fade up. + } + vTaskDelete(getInstance()->mDisplayFadeTask); + getInstance()->mDisplayFadeTask = nullptr; +} + +bool Display::fade(){ + //Early return no fade needed. + if (mBrightness == mAwakeBrightness || + isAsleep && mBrightness == 0){return true;} + + bool fadeDown = isAsleep || mBrightness > mAwakeBrightness; + if (fadeDown){ + setCurrentBrightness(mBrightness - 1); + auto setPoint = isAsleep ? 0 : mAwakeBrightness; + return mBrightness == setPoint; + }else{ + setCurrentBrightness(mBrightness + 1); + return mBrightness == mAwakeBrightness; + } +} + +void Display::startFade(){ + if(mDisplayFadeTask != nullptr){// Already have fade task no need to start another. + xTaskCreate(&Display::fadeImpl, "Display Fade Task", + 1024, nullptr, 5, &mDisplayFadeTask); + } +} + void Display::flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) { uint32_t w = (area->x2 - area->x1 + 1); uint32_t h = (area->y2 - area->y1 + 1); diff --git a/Platformio/HAL/Targets/ESP32/display/display.hpp b/Platformio/HAL/Targets/ESP32/display/display.hpp index 8dd7791..e801e94 100644 --- a/Platformio/HAL/Targets/ESP32/display/display.hpp +++ b/Platformio/HAL/Targets/ESP32/display/display.hpp @@ -22,15 +22,31 @@ class Display: public DisplayAbstract public: static std::shared_ptr getInstance(); + /// @brief Set brightness setting and fade to it + /// @param brightness virtual void setBrightness(uint8_t brightness) override; virtual void turnOff() override; void onTouch(Notification::HandlerTy aTouchHandler); + void wake() {isAsleep = false; startFade();} + void sleep() {isAsleep = true; startFade();} + protected: virtual void flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p); virtual void screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) override; + /// @brief Fade toward brightness based on isAwake + /// @return True - Fade complete + /// False - Fade set point not reached + bool fade(); + /// @brief Start the Fade task + void startFade(); + + /// @brief Set the actual display brightness right now + /// @param brightness + void setCurrentBrightness(uint8_t brightness); + private: Display(int backlight_pin, int enable_pin); void setupTFT(); @@ -44,4 +60,11 @@ class Display: public DisplayAbstract TS_Point touchPoint; TS_Point oldPoint; Notification mTouchEvent; + + TaskHandle_t mDisplayFadeTask = nullptr; + static void fadeImpl(void* aBrightness); + + uint8_t mBrightness = 0; // Current display brightness + uint8_t mAwakeBrightness = 100; // Current setting for brightness when awake + bool isAsleep = false; }; \ No newline at end of file