~aleteoryx/muditaos

ref: 2cbadc6a00dc51fee7f2257bb57f756dee3ebc57 muditaos/module-services/service-evtmgr/battery/BatteryBrownoutDetector.hpp -rw-r--r-- 1.3 KiB
2cbadc6a — Dawid Wojtas [BH-1630] Turn off the device for low voltage 2 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <hal/battery_charger/AbstractBatteryCharger.hpp>
#include <Service/Service.hpp>
#include <Timers/TimerHandle.hpp>

namespace sys
{
    class Service;
} // namespace sys

class BatteryBrownoutDetector
{
  public:
    struct Thresholds
    {
        units::Voltage shutdown;
        std::uint8_t measurementMaxCount;
    };

    enum class DetectionResult : std::uint8_t
    {
        eventDetected,
        detectionNegative
    };

    using Callback = std::function<void(DetectionResult result)>;

    BatteryBrownoutDetector(sys::Service *service,
                            hal::battery::AbstractBatteryCharger &charger,
                            Thresholds voltage,
                            Callback callback);

    void poll();

  private:
    void tick();
    bool isTimerExpired();
    bool isVoltageThresholdExceeded();
    void brownoutDetected();
    void brownoutNotDetected();
    void clearDetection();
    void triggerDetection();
    hal::battery::AbstractBatteryCharger &charger;

    bool eventDetectionOngoing        = false;
    unsigned measurementBrownoutCount = 0;
    Thresholds voltage;
    Callback callback;
    sys::TimerHandle measurementTick;
};