~aleteoryx/muditaos

ref: 240e9eb112ccf63711c572b32ab419add6d624d8 muditaos/module-sys/Service/SystemTimer.cpp -rw-r--r-- 3.3 KiB
240e9eb1 — Lefucjusz [BH-2068] Remove pre-1.9.0 What's New entries 1 year, 2 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
116
117
118
119
// 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 <projdefs.h>
#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,
                             timer::Type type)
        : cpp_freertos::Timer(name.c_str(), pdMS_TO_TICKS(interval.count()), type == timer::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 %ld ms!", name.c_str(), static_cast<long int>(value.count()));
        interval = value;
        cpp_freertos::Timer::SetPeriod(pdMS_TO_TICKS(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 == timer::Type::SingleShot) {
            stop();
        }
        callback(*this);
    }

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

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