~aleteoryx/muditaos

ref: 0e8b4c848e3f87f3bbb1f64ef7460cf56017b87f muditaos/module-services/service-eink/ServiceEink.hpp -rw-r--r-- 3.7 KiB
0e8b4c84 — Lefucjusz [BH-2108] Fix misaligned charging symbol 3 months 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include <module-gui/gui/core/Context.hpp>
#include <system/Common.hpp>
#include <Service/Message.hpp>
#include <Service/Service.hpp>
#include <Timers/TimerHandle.hpp>

#include "EinkSentinel.hpp"

#include <service-db/DBServiceName.hpp>
#include <service-db/Settings.hpp>

#include <chrono>
#include <cstdint>
#include <string>
#include <module-services/service-eink/messages/EinkModeMessage.hpp>
#include <hal/eink/AbstractEinkDisplay.hpp>
#include "service-eink/ServiceEinkDependencies.hpp"

namespace service::eink
{
    enum ExitAction
    {
        None,
        WipeOut
    };

    class ServiceEink : public sys::Service
    {
      public:
        explicit ServiceEink(ExitAction exitAction   = WipeOut,
                             const std::string &name = service::name::eink,
                             std::string parent      = {});

        sys::MessagePointer DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *response) override;
        sys::ReturnCodes InitHandler() override;
        sys::ReturnCodes DeinitHandler() override;
        void ProcessCloseReason(sys::CloseReason closeReason) override;
        void ProcessCloseReasonHandler(sys::CloseReason closeReason) override;
        sys::ReturnCodes SwitchPowerModeHandler(const sys::ServicePowerMode mode) override;

      private:
        enum class State
        {
            Running,
            Suspended
        };

        enum class EinkDisplayState
        {
            Idle,
            NeedRefresh,
            Canceled
        };

        enum class RefreshStatus
        {
            Success,
            Failed
        };

        void setState(State state) noexcept;
        bool isInState(State state) const noexcept;
        void restartDisplayPowerOffTimer();

        void enterActiveMode();
        void suspend();

        void setDisplayMode(EinkModeMessage::Mode mode);

        sys::MessagePointer handleEinkModeChangedMessage(sys::Message *message);
        sys::MessagePointer handleImageMessage(sys::Message *message);
        sys::MessagePointer handleRefreshMessage(sys::Message *message);
        sys::MessagePointer handleCancelRefreshMessage(sys::Message *message);
        sys::MessagePointer handlePrepareEarlyRequest(sys::Message *message);

        void initStaticData();

        ExitAction exitAction;
        State currentState;
        std::unique_ptr<hal::eink::AbstractEinkDisplay> display;
        sys::TimerHandle displayPowerOffTimer;
        std::shared_ptr<EinkSentinel> eInkSentinel;
        std::unique_ptr<settings::Settings> settings;

        std::unique_ptr<::gui::Context> previousContext;
        hal::eink::EinkRefreshMode previousRefreshMode = hal::eink::EinkRefreshMode::REFRESH_NONE;
        EinkDisplayState einkDisplayState              = EinkDisplayState::Idle;
        hal::eink::EinkFrame refreshFramesSum;
        hal::eink::EinkRefreshMode refreshModeSum = hal::eink::EinkRefreshMode::REFRESH_NONE;
        bool isRefreshFramesSumValid              = false;
        RefreshStatus previousRefreshStatus       = RefreshStatus::Success;

        sys::CloseReason systemCloseReason = sys::CloseReason::RegularPowerDown;
    };
} // namespace service::eink

namespace sys
{
    template <>
    struct ManifestTraits<service::eink::ServiceEink>
    {
        static auto GetManifest() -> ServiceManifest
        {
            ServiceManifest manifest;
            manifest.name         = service::name::eink;
            manifest.dependencies = sys::dependencies::getDependenciesTo<service::eink::ServiceEink>();
            return manifest;
        }
    };
} // namespace sys