~aleteoryx/muditaos

8513eaaef56abc210056f951fd496dffe0d118ac — wojtekrzepecki 4 years ago bd4db9e
[EGD-7900] Fix backlight behavior

Cumulated fixes for both screen and keypad light
M module-services/service-evtmgr/screen-light-control/ControlFunctions.cpp => module-services/service-evtmgr/screen-light-control/ControlFunctions.cpp +5 -0
@@ 108,6 108,11 @@ namespace screen_light_control::functions
        rampTarget = value;
    }

    void resetRampToTarget()
    {
        rampState = rampTarget;
    }

    bool isRampTargetReached()
    {
        return rampTargetReached;

M module-services/service-evtmgr/screen-light-control/ControlFunctions.hpp => module-services/service-evtmgr/screen-light-control/ControlFunctions.hpp +2 -0
@@ 29,6 29,8 @@ namespace screen_light_control::functions

    void setRampStep(float step);

    void resetRampToTarget();

    void setHysteresis(float hyst);

    void setFunctionFromPoints(const BrightnessFunction &points);

M module-services/service-evtmgr/screen-light-control/ScreenLightControl.hpp => module-services/service-evtmgr/screen-light-control/ScreenLightControl.hpp +1 -0
@@ 31,5 31,6 @@ namespace screen_light_control
        [[nodiscard]] virtual auto isAutoModeOn() const noexcept -> bool     = 0;
        [[nodiscard]] virtual auto getBrightnessValue() const noexcept
            -> bsp::eink_frontlight::BrightnessPercentage = 0;
        [[nodiscard]] virtual auto isFadeOutOngoing() -> bool = 0;
    };
} // namespace screen_light_control

M module-services/service-evtmgr/screen-light-control/ScreenLightControlParameters.hpp => module-services/service-evtmgr/screen-light-control/ScreenLightControlParameters.hpp +9 -8
@@ 18,12 18,13 @@ namespace screen_light_control
    /// Set of actions to control the screen light
    enum class Action
    {
        turnOff,                   ///< Turn off screen frontlight
        turnOn,                    ///< Turn on screen frontlight
        enableAutomaticMode,       ///< Enable automatic mode of screen frontlight
        disableAutomaticMode,      ///< Disable automatic mode of screen frontlight
        setManualModeBrightness,   ///< Set screen brightness in manual mode control
        setAutomaticModeParameters ///< Set parameters for automatic mode of screen frontlight
        turnOff,                    ///< Turn off screen frontlight
        turnOn,                     ///< Turn on screen frontlight
        enableAutomaticMode,        ///< Enable automatic mode of screen frontlight
        disableAutomaticMode,       ///< Disable automatic mode of screen frontlight
        setManualModeBrightness,    ///< Set screen brightness in manual mode control
        setAutomaticModeParameters, ///< Set parameters for automatic mode of screen frontlight
        fadeOut                     ///< Set light fade out in automatic mode
    };

    struct ManualModeParameters


@@ 37,8 38,8 @@ namespace screen_light_control
    {
        /// Vector of points for screen brightness [%] in relation to ambient light [Lux] function. Points have to be in
        /// ascending order of ambient light values.
        functions::BrightnessFunction functionPoints =
            functions::BrightnessFunction({{0.0f, 70.0f}, {250.0f, 70.0f}, {450.0f, 40.0f}, {500.0f, 0.0f}});
        functions::BrightnessFunction functionPoints = functions::BrightnessFunction(
            {{0.0f, 35.0f}, {10.0f, 75.0f}, {85.0f, 100.0f}, {500.0f, 100.0f}, {520.0f, 0.0f}});
        /// Ramp time of screen brightness in milliseconds per 0-100% change
        unsigned int rampTimeMS = 1500;
        /// Hysteresis value of screen brightness control

M products/BellHybrid/services/evtmgr/screen-light-control/ScreenLightControl.cpp => products/BellHybrid/services/evtmgr/screen-light-control/ScreenLightControl.cpp +6 -0
@@ 196,4 196,10 @@ namespace bell::screen_light_control
    {
        return std::max<bsp::eink_frontlight::BrightnessPercentage>(target, MINIMAL_TARGET);
    }

    bool ScreenLightController::isFadeOutOngoing()
    {
        return false;
    }

} // namespace bell::screen_light_control

M products/BellHybrid/services/evtmgr/screen-light-control/ScreenLightControl.hpp => products/BellHybrid/services/evtmgr/screen-light-control/ScreenLightControl.hpp +1 -0
@@ 42,6 42,7 @@ namespace bell::screen_light_control
        [[nodiscard]] auto isLightOn() const noexcept -> bool override;
        [[nodiscard]] bool isAutoModeOn() const noexcept override;
        [[nodiscard]] auto getBrightnessValue() const noexcept -> bsp::eink_frontlight::BrightnessPercentage override;
        [[nodiscard]] auto isFadeOutOngoing() -> bool override;

      private:
        void controlTimerCallback();

M products/PurePhone/services/evtmgr/backlight-handler/BacklightHandler.cpp => products/PurePhone/services/evtmgr/backlight-handler/BacklightHandler.cpp +30 -13
@@ 14,21 14,17 @@ namespace backlight
    namespace timers
    {
        constexpr auto keypadLightTimerName    = "KeypadLightTimer";
        constexpr auto keypadLightTimerTimeout = std::chrono::seconds(5);
        constexpr auto lightTimerTimeout        = std::chrono::seconds(20);
        constexpr auto lightFadeoutTimerTimeout = std::chrono::seconds(10);
    } // namespace timers

    Handler::Handler(std::shared_ptr<settings::Settings> settings, sys::Service *parent)
        : HandlerCommon(std::move(settings),
                        std::make_shared<pure::screen_light_control::ScreenLightController>(parent),
                        parent,
                        [this](sys::Timer &t) {
                            if (getScreenAutoModeState() == screen_light_control::ScreenLightMode::Automatic &&
                                this->screenLightController->isLightOn()) {
                                this->screenLightController->processRequest(screen_light_control::Action::turnOff);
                            }
                        }),
                        [this](sys::Timer &t) { handleScreenLightTimeout(); }),
          keypadLightTimer{sys::TimerFactory::createSingleShotTimer(
              parent, timers::keypadLightTimerName, timers::keypadLightTimerTimeout, [this](sys::Timer &) {
              parent, timers::keypadLightTimerName, timers::lightTimerTimeout, [this](sys::Timer &) {
                  bsp::keypad_backlight::shutdown();
              })}
    {}


@@ 67,7 63,7 @@ namespace backlight
                                       const screen_light_control::Parameters &params)
    {
        if (action == screen_light_control::Action::enableAutomaticMode) {
            startScreenLightTimer();
            getScreenLightTimer()->restart(timers::lightTimerTimeout);
        }
        handleScreenLightSettings(action, params);
        getScreenLightControl()->processRequest(action, params);


@@ 157,16 153,37 @@ namespace backlight

    void Handler::handleScreenLightRefresh([[maybe_unused]] const int key)
    {
        if (getScreenLightState() && getScreenAutoModeState() == screen_light_control::ScreenLightMode::Automatic) {
            if (!getScreenLightControl()->isLightOn()) {
        if (getScreenLightState()) {
            if (!getScreenLightControl()->isLightOn() || getScreenLightControl()->isFadeOutOngoing()) {
                getScreenLightControl()->processRequest(screen_light_control::Action::turnOn);
            }
            startScreenLightTimer();
            getScreenLightTimer()->restart(timers::lightTimerTimeout);
        }
    }

    void Handler::handleScreenLightTimeout()
    {
        if (getScreenAutoModeState() == screen_light_control::ScreenLightMode::Automatic) {
            if (screenLightController->isLightOn()) {
                if (screenLightController->isFadeOutOngoing()) {
                    screenLightController->processRequest(screen_light_control::Action::turnOff);
                }
                else {
                    screenLightController->processRequest(screen_light_control::Action::fadeOut);
                    getScreenLightTimer()->restart(timers::lightFadeoutTimerTimeout);
                }
            }
        }
        else if (getScreenAutoModeState() == screen_light_control::ScreenLightMode::Manual) {
            if (screenLightController->isLightOn()) {
                screenLightController->processRequest(screen_light_control::Action::turnOff);
            }
        }
    }

    void Handler::onScreenLightTurnedOn()
    {
        startScreenLightTimer();
        getScreenLightTimer()->restart(timers::lightTimerTimeout);
    }

} // namespace backlight

M products/PurePhone/services/evtmgr/include/evtmgr/BacklightHandler.hpp => products/PurePhone/services/evtmgr/include/evtmgr/BacklightHandler.hpp +1 -0
@@ 48,5 48,6 @@ namespace backlight
        void handleKeypadLightRefresh();
        void handleScreenLightRefresh(int key = 0);
        void onScreenLightTurnedOn() override;
        void handleScreenLightTimeout();
    };
} // namespace backlight

M products/PurePhone/services/evtmgr/screen-light-control/ScreenLightControl.cpp => products/PurePhone/services/evtmgr/screen-light-control/ScreenLightControl.cpp +40 -1
@@ 8,6 8,10 @@

namespace pure::screen_light_control
{
    namespace
    {
        constexpr bsp::eink_frontlight::BrightnessPercentage fadeOutBrigthnessMax = 35.0f;
    }

    ScreenLightController::ScreenLightController(sys::Service *parent)
    {


@@ 58,6 62,9 @@ namespace pure::screen_light_control
                setParameters(params.getAutoModeParams());
            }
            break;
        case Action::fadeOut:
            handleFadeOut();
            break;
        }
    }



@@ 68,7 75,9 @@ namespace pure::screen_light_control

    void ScreenLightController::readoutTimerCallback()
    {
        ::screen_light_control::functions::calculateBrightness(bsp::light_sensor::readout());
        if (!fadeOut) {
            ::screen_light_control::functions::calculateBrightness(bsp::light_sensor::readout());
        }
    }

    auto ScreenLightController::isLightOn() const noexcept -> bool


@@ 137,9 146,21 @@ namespace pure::screen_light_control

    void ScreenLightController::turnOn()
    {
        fadeOut = false;

        bsp::eink_frontlight::turnOn();
        bsp::light_sensor::wakeup();

        if (automaticMode == ScreenLightMode::Automatic) {
            if (lightOn) {
                ::screen_light_control::functions::calculateBrightness(bsp::light_sensor::readout());
            }
            else {
                // It takes some time to get initial readout -> using last saved measurement
                ::screen_light_control::functions::calculateBrightness(stashedReadout);
            }
            ::screen_light_control::functions::resetRampToTarget();
            bsp::eink_frontlight::setBrightness(::screen_light_control::functions::getRampState());
            enableTimers();
        }
        lightOn = true;


@@ 153,9 174,27 @@ namespace pure::screen_light_control
    void ScreenLightController::turnOff()
    {
        bsp::eink_frontlight::turnOff();
        stashedReadout = bsp::light_sensor::readout();
        bsp::light_sensor::standby();
        disableTimers();
        lightOn = false;
        fadeOut = false;
    }

    void ScreenLightController::handleFadeOut()
    {
        if (automaticMode == ScreenLightMode::Automatic) {
            fadeOut = true;
            // Set fadeout brightess as maximum or current ramp state if lower
            auto rampState         = ::screen_light_control::functions::getRampState();
            auto fadeOutBrigthness = std::clamp(rampState, 0.0f, fadeOutBrigthnessMax);
            ::screen_light_control::functions::setRampTarget(fadeOutBrigthness);
        }
    }

    bool ScreenLightController::isFadeOutOngoing()
    {
        return fadeOut;
    }

} // namespace screen_light_control

M products/PurePhone/services/evtmgr/screen-light-control/ScreenLightControl.hpp => products/PurePhone/services/evtmgr/screen-light-control/ScreenLightControl.hpp +7 -1
@@ 40,6 40,8 @@ namespace pure::screen_light_control
        [[nodiscard]] bool isAutoModeOn() const noexcept override;
        [[nodiscard]] auto getBrightnessValue() const noexcept -> bsp::eink_frontlight::BrightnessPercentage override;

        [[nodiscard]] auto isFadeOutOngoing() -> bool override;

      private:
        void controlTimerCallback();
        void readoutTimerCallback();


@@ 57,6 59,8 @@ namespace pure::screen_light_control
        void enableAutomaticMode();
        void disableAutomaticMode();

        void handleFadeOut();

        static constexpr inline auto CONTROL_TIMER_MS = 25;
        static constexpr inline auto READOUT_TIMER_MS = 500;



@@ 65,6 69,8 @@ namespace pure::screen_light_control

        bool lightOn                                               = false;
        ScreenLightMode automaticMode                              = ScreenLightMode::Manual;
        bsp::eink_frontlight::BrightnessPercentage brightnessValue = 0.0;
        bsp::eink_frontlight::BrightnessPercentage brightnessValue = 0.0f;
        bool fadeOut                                               = false;
        bsp::light_sensor::IlluminanceLux stashedReadout           = 0.0f;
    };
} // namespace pure::screen_light_control