Instrument Neutral Distributed Interface INDI 2.2.5
inditimer.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 by Pawel Soja <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "inditimer.h"
20#include "inditimer_p.h"
21#include <algorithm>
22
23#include "eventloop.h"
24
25namespace INDI
26{
27
31
36
38{
39 if (singleShot)
40 {
41 timerId = addTimer(interval, [](void *arg)
42 {
43 TimerPrivate *d = static_cast<TimerPrivate*>(arg);
44 d->timerId = -1;
45 d->p->timeout();
46 }, this);
47 }
48 else
49 {
50 timerId = addPeriodicTimer(interval, [](void *arg)
51 {
52 TimerPrivate *d = static_cast<TimerPrivate*>(arg);
53 d->p->timeout();
54 }, this);
55 }
56}
57
59{
60 int id = timerId.exchange(-1);
61 if (id != -1)
62 rmTimer(id);
63}
64
66 : d_ptr(new TimerPrivate(this))
67{ }
68
70 : d_ptr(&dd)
71{ }
72
75
76void Timer::callOnTimeout(const std::function<void()> &callback)
77{
78 D_PTR(Timer);
79 d->callback = callback;
80}
81
83{
84 D_PTR(Timer);
85 d->stop();
86 d->start();
87}
88
89void Timer::start(int msec)
90{
91 D_PTR(Timer);
92 d->stop();
93 d->interval = msec;
94 d->start();
95}
96
98{
99 D_PTR(Timer);
100 d->stop();
101}
102
103void Timer::setInterval(int msec)
104{
105 D_PTR(Timer);
106 d->interval = msec;
107}
108
109void Timer::setSingleShot(bool singleShot)
110{
111 D_PTR(Timer);
112 d->singleShot = singleShot;
113}
114
115bool Timer::isActive() const
116{
117 D_PTR(const Timer);
118 return d->timerId != -1;
119}
120
122{
123 D_PTR(const Timer);
124 return d->singleShot;
125}
126
128{
129 D_PTR(const Timer);
130 return d->timerId != -1 ? std::max(remainingTimer(d->timerId), 0) : 0;
131}
132
134{
135 D_PTR(const Timer);
136 return d->interval;
137}
138
140{
141 D_PTR(Timer);
142 if (d->callback != nullptr)
143 d->callback();
144}
145
146void Timer::singleShot(int msec, const std::function<void()> &callback)
147{
148 Timer *timer = new Timer();
149 timer->setSingleShot(true);
150 timer->setInterval(msec);
151 timer->callOnTimeout([callback, timer]()
152 {
153 callback();
154 delete timer;
155 });
156 timer->start();
157}
158
159}
std::atomic< int > timerId
Definition inditimer_p.h:42
virtual ~TimerPrivate()
Definition inditimer.cpp:32
TimerPrivate(Timer *p)
Definition inditimer.cpp:28
The Timer class provides repetitive and single-shot timers.
Definition inditimer.h:41
int interval() const
Returns the timeout interval in milliseconds.
void setSingleShot(bool singleShot)
Set whether the timer is a single-shot timer.
void callOnTimeout(const std::function< void()> &callback)
Definition inditimer.cpp:76
void start()
Starts or restarts the timer with the timeout specified in interval.
Definition inditimer.cpp:82
static void singleShot(int msec, const std::function< void()> &callback)
This static function calls a the given function after a given time interval.
bool isActive() const
Returns true if the timer is running (pending); otherwise returns false.
int remainingTime() const
Returns the timer's remaining value in milliseconds left until the timeout. If the timer not exists,...
void setInterval(int msec)
Set the timeout interval in milliseconds.
virtual ~Timer()
Definition inditimer.cpp:73
void stop()
Stops the timer.
Definition inditimer.cpp:97
virtual void timeout()
This function is called when the timer times out.
bool isSingleShot() const
Returns whether the timer is a single-shot timer.
void rmTimer(int timer_id)
Definition eventloop.c:290
int addTimer(int ms, TCF *fp, void *ud)
Definition eventloop.c:252
int addPeriodicTimer(int ms, TCF *fp, void *ud)
Definition eventloop.c:257
int remainingTimer(int timer_id)
Definition eventloop.c:315
Public interface to INDI's eventloop mechanism.
Namespace to encapsulate INDI client, drivers, and mediator classes.