~aleteoryx/muditaos

d3bba9da051235226e009aecb4135106e41204ad — Maciej Gibowicz 3 years ago fda43af
[MOS-256] Turn off torch driver when not in use

We save on power consumption ~0.7 mA.
M module-bsp/board/linux/torch/torch.cpp => module-bsp/board/linux/torch/torch.cpp +4 -4
@@ 1,4 1,4 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "bsp/torch/torch.hpp"


@@ 48,9 48,9 @@ namespace bsp
            return true;
        }

        std::pair<bool, State> getState()
        State getState()
        {
            return std::make_pair(true, state_simulated);
            return state_simulated;
        }

        ColourTemperature getColorTemp()


@@ 60,7 60,7 @@ namespace bsp

        bool toggle()
        {
            return turn(getState().second == State::off ? State::on : State::off);
            return turn(getState() == State::off ? State::on : State::off);
        };

        bool setCurrent(const unsigned short mA)

M module-bsp/board/rt1051/bsp/torch/torch.cpp => module-bsp/board/rt1051/bsp/torch/torch.cpp +20 -25
@@ 1,4 1,4 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "bsp/torch/torch.hpp"


@@ 47,12 47,10 @@ namespace bsp
            gpio->WritePin(static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN), 1);
            vTaskDelay(pdMS_TO_TICKS(5));

            if (isPresent()) {
                turn(State::off);
                setCurrent(max_current_mA);
                return kStatus_Success;
            }
            return kStatus_Fail;
            auto present = isPresent();
            turn(State::off);

            return present ? kStatus_Success : kStatus_Fail;
        }

        void deinit()


@@ 95,6 93,11 @@ namespace bsp
        };
        bool turn(State state, ColourTemperature colourTemp)
        {
            if (state == State::on) {
                gpio->WritePin(static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN), 1);
                setCurrent(max_current_mA);
            }

            if (colourTemp != ColourTemperature::noChange) {
                currentColourTemp = colourTemp;
            }


@@ 114,24 117,21 @@ namespace bsp
                .tx_pin_en     = 0,
            };
            auto wrote = i2c->Write(addr, (uint8_t *)(&en_reg), 1);

            if (state == State::off) {
                gpio->WritePin(static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN), 0);
            }

            if (wrote != 1) {
                return false;
            }
            return true;
        }

        std::pair<bool, State> getState()
        State getState()
        {
            addr.subAddress = AL3644TT_ENABLE_REG;
            al3644tt_enable_reg en_reg;
            auto read = i2c->Read(addr, (uint8_t *)(&en_reg), 1);
            if (read != 1) {
                return std::make_pair(false, State::off); // invalid
            }
            return std::make_pair(true,
                                  (en_reg.led1_en == AL3644TT_LED_ENABLED || en_reg.led2_en == AL3644TT_LED_ENABLED)
                                      ? State::on
                                      : State::off);
            auto read = gpio->ReadPin(static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN));
            return read ? State::on : State::off;
        }

        ColourTemperature getColorTemp()


@@ 141,13 141,8 @@ namespace bsp

        bool toggle()
        {
            auto [success, state] = getState();
            if (success == false) {
                return false;
            }
            else {
                return turn(state == State::off ? State::on : State::off);
            }
            auto state = getState();
            return turn(state == State::off ? State::on : State::off);
        }
    } // namespace torch
} // namespace bsp

M module-bsp/bsp/torch/torch.hpp => module-bsp/bsp/torch/torch.hpp +1 -1
@@ 33,7 33,7 @@ namespace bsp::torch
    bool isPresent(void);

    bool turn(State state, ColourTemperature = ColourTemperature::noChange);
    std::pair<bool, State> getState();
    State getState();
    ColourTemperature getColorTemp();
    bool toggle();
    bool setCurrent(const unsigned short mA);

M module-services/service-evtmgr/api/torch.cpp => module-services/service-evtmgr/api/torch.cpp +2 -3
@@ 1,4 1,4 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "service-evtmgr/torch.hpp"


@@ 10,8 10,7 @@ namespace event::service::api
{
    bool isTorchOn()
    {
        auto [works, isOn] = bsp::torch::getState();
        assert(works);
        auto isOn = bsp::torch::getState();
        return isOn == bsp::torch::State::on;
    }
} // namespace event::service::api

M products/PurePhone/services/evtmgr/EventManager.cpp => products/PurePhone/services/evtmgr/EventManager.cpp +2 -2
@@ 107,14 107,14 @@ void EventManager::initProductEvents()
void EventManager::toggleTorchOnOff()
{
    auto state    = bsp::torch::getState();
    auto newState = (state.second == bsp::torch::State::off) ? bsp::torch::State::on : bsp::torch::State::off;
    auto newState = (state == bsp::torch::State::off) ? bsp::torch::State::on : bsp::torch::State::off;
    bsp::torch::turn(newState, bsp::torch::ColourTemperature::coldest);
}

void EventManager::toggleTorchColor()
{
    auto state = bsp::torch::getState();
    if (state.second == bsp::torch::State::on) {
    if (state == bsp::torch::State::on) {
        auto color    = bsp::torch::getColorTemp();
        auto newColor = (color == bsp::torch::ColourTemperature::coldest) ? bsp::torch::ColourTemperature::warmest
                                                                          : bsp::torch::ColourTemperature::coldest;