~aleteoryx/muditaos

muditaos/module-sys/Service/SystemTimer.cpp -rw-r--r-- 3.2 KiB
a405cad6Aleteoryx trim readme 6 days 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
116
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <Timers/SystemTimer.hpp>
#include <Service/Service.hpp>
#include <Timers/TimerMessage.hpp>
#include <log/log.hpp>
#include <ticks.hpp>
#include <memory>

#if DEBUG_TIMER == 1
#define log_debug(...) LOG_DEBUG(__VA_ARGS__)
#else
#define log_debug(...)
#endif

namespace sys::timer
{
    SystemTimer::SystemTimer(Service *parent, const std::string &name, std::chrono::milliseconds interval, Type type)
        : cpp_freertos::Timer(name.c_str(), cpp_freertos::Ticks::MsToTicks(interval.count()), type == Type::Periodic),
          name{name}, interval{interval}, type{type}, parent{parent}
    {
        attachToService();
        log_debug("%s %s timer created", name.c_str(), type == Type::Periodic ? "periodic" : "single-shot");
    }

    void SystemTimer::attachToService()
    {
        assert(parent != nullptr);
        parent->getTimers().attach(this);
    }

    SystemTimer::~SystemTimer() noexcept
    {
        parent->getTimers().detach(this);
    }

    void SystemTimer::Run()
    {
        auto msg = std::make_shared<TimerMessage>(this);
        if (const auto ret = parent->bus.sendUnicast(std::move(msg), parent->GetName()); !ret) {
            LOG_ERROR("Timer %s error: bus error", name.c_str());
        }
    }

    void SystemTimer::start()
    {
        log_debug("Timer %s start", name.c_str());
        stopTimerIfActive();
        startTimer();
    }

    void SystemTimer::restart(std::chrono::milliseconds newInterval)
    {
        log_debug("Timer %s restart", name.c_str());
        stopTimerIfActive();
        setInterval(newInterval);
        startTimer();
    }

    void SystemTimer::stopTimerIfActive()
    {
        if (active) {
            cpp_freertos::Timer::Stop(0);
        }
    }

    void SystemTimer::startTimer()
    {
        active = true;
        cpp_freertos::Timer::Start(0);
    }

    void SystemTimer::stop()
    {
        log_debug("Timer %s stop!", name.c_str());
        // make sure callback is not called even if it is already in the queue
        active = false;
        cpp_freertos::Timer::Stop(0);
    }

    void SystemTimer::setInterval(std::chrono::milliseconds value)
    {
        log_debug("Timer %s set interval to %" PRIi64 " ms!", name.c_str(), value.count());
        interval = value;
        cpp_freertos::Timer::SetPeriod(cpp_freertos::Ticks::MsToTicks(interval.count()), 0);
    }

    void SystemTimer::onTimeout()
    {
        log_debug("Timer %s tick", name.c_str());
        if (!callback) {
            LOG_WARN("Timer %s error: Callback is not valid.", name.c_str());
            return;
        }
        if (!isActive()) {
            LOG_WARN("Timer %s error: Timer is not active.", name.c_str());
            return;
        }
        log_debug("Timer %s runs callback", name.c_str());
        if (type == Type::SingleShot) {
            stop();
        }
        callback(*this);
    }

    bool SystemTimer::isActive() const noexcept
    {
        return active;
    }

    void SystemTimer::connect(TimerCallback &&newCallback) noexcept
    {
        callback = std::move(newCallback);
    }
} // namespace sys::timer