~aleteoryx/muditaos

ref: 55bd0f3ae8ebb5cb0b93da0098949f7c2a0bef76 muditaos/module-services/service-evtmgr/battery/BatteryBrownoutDetector.cpp -rw-r--r-- 1.7 KiB
55bd0f3a — Marcin Zieliński [MOS-838] Unable to unlock SIM card (corner case) 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BatteryBrownoutDetector.hpp"

#include <service-evtmgr/BatteryMessages.hpp>
#include <system/Constants.hpp>
#include <Timers/TimerFactory.hpp>
#include <log/log.hpp>

namespace
{
    constexpr std::chrono::milliseconds measurementTickTime{1000};
    constexpr auto measurementTickName     = "BrtownoutDetectorTick";
    constexpr unsigned measurementMaxCount = 5;
    constexpr auto brownoutLevelVoltage    = 3600; // mV
} // namespace

BatteryBrownoutDetector::BatteryBrownoutDetector(sys::Service *service, hal::battery::AbstractBatteryCharger &charger)
    : parentService(service), charger{charger},
      measurementTick{sys::TimerFactory::createSingleShotTimer(
          service, measurementTickName, measurementTickTime, [this](sys::Timer &) { checkBrownout(); })}
{}

void BatteryBrownoutDetector::startDetection()
{
    if (detectionOngoing) {
        return;
    }
    LOG_DEBUG("Battery Brownout detection window start");
    detectionOngoing = true;
    measurementCount = 0;
    checkBrownout();
}

void BatteryBrownoutDetector::checkBrownout()
{
    if (charger.getBatteryVoltage() < brownoutLevelVoltage) {
        LOG_DEBUG("Battery Brownout detected");

        auto messageBrownout = std::make_shared<sevm::BatteryBrownoutMessage>();
        parentService->bus.sendUnicast(std::move(messageBrownout), service::name::system_manager);

        return;
    }

    measurementCount++;
    if (measurementCount <= measurementMaxCount) {
        measurementTick.start();
    }
    else {
        LOG_DEBUG("Battery Brownout detection window finish with negative result");
        detectionOngoing = false;
    }
}