~aleteoryx/muditaos

muditaos/module-apps/apps-common/GuiTimer.cpp -rw-r--r-- 3.6 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
// 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 "GuiTimer.hpp"
#include "Item.hpp" // for Item

#include <apps-common/ApplicationCommon.hpp> // for Application

#include "Timers/SystemTimer.hpp" // for Timer, Timer::Type, Timer::Ty...
#include "Timers/TimerFactory.hpp"

#include <functional>

namespace app
{
    namespace
    {
        auto guiTypeToSysType(GuiTimer::Type type) noexcept
        {
            if (type == GuiTimer::Type::Periodic) {
                return sys::timer::Type::Periodic;
            }
            return sys::timer::Type::SingleShot;
        }
    } // namespace

    class GuiTimer::Impl
    {
      public:
        Impl(ApplicationCommon *parent,
             gui::Item *item,
             const std::string &name,
             std::chrono::milliseconds timeout,
             Type type);
        std::unique_ptr<sys::timer::SystemTimer> timer;
    };

    GuiTimer::Impl::Impl(ApplicationCommon *parent,
                         gui::Item *item,
                         const std::string &name,
                         std::chrono::milliseconds timeout,
                         Type type)
        : timer{std::make_unique<sys::timer::SystemTimer>(parent, name, timeout, guiTypeToSysType(type))}
    {
        timer->connect([item](sys::Timer &self) { item->onTimer(self); });
    }

    GuiTimer::GuiTimer(ApplicationCommon *parent,
                       gui::Item *item,
                       const std::string &name,
                       std::chrono::milliseconds timeout,
                       Type type)
        : pimpl(std::make_unique<GuiTimer::Impl>(parent, item, name, timeout, type)), item{item}
    {}

    GuiTimer::~GuiTimer() noexcept
    {
        item->detachTimer(*(pimpl->timer));
    }

    void GuiTimer::start()
    {
        pimpl->timer->start();
    }

    void GuiTimer::stop()
    {
        pimpl->timer->stop();
    }

    void GuiTimer::restart(std::chrono::milliseconds newInterval)
    {
        pimpl->timer->restart(newInterval);
    }

    bool GuiTimer::isActive() const noexcept
    {
        return pimpl->timer->isActive();
    }

    sys::TimerHandle GuiTimerFactory::createSingleShotTimer(ApplicationCommon *parent,
                                                            gui::Item *item,
                                                            const std::string &name,
                                                            std::chrono::milliseconds interval)
    {
        return createGuiTimer(parent, item, name, interval, GuiTimer::Type::SingleShot);
    }

    sys::TimerHandle GuiTimerFactory::createPeriodicTimer(ApplicationCommon *parent,
                                                          gui::Item *item,
                                                          const std::string &name,
                                                          std::chrono::milliseconds interval)
    {
        return createGuiTimer(parent, item, name, interval, GuiTimer::Type::Periodic);
    }

    sys::TimerHandle GuiTimerFactory::createGuiTimer(ApplicationCommon *parent,
                                                     gui::Item *item,
                                                     const std::string &name,
                                                     std::chrono::milliseconds interval,
                                                     GuiTimer::Type type)
    {
        auto timer = new GuiTimer{parent, item, name, interval, type};
        parent->connect(timer, item);
        return sys::TimerHandle{timer};
    }
} // namespace app