Instrument Neutral Distributed Interface INDI  1.9.2
inditimer.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 by Pawel Soja <kernel32.pl@gmail.com>
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 
25 namespace INDI
26 {
27 
29  : p(p)
30 { }
31 
33 {
34  stop();
35 }
36 
38 {
39  if (singleShot)
40  {
41  timerId = addTimer(interval, [](void *arg){
42  TimerPrivate *d = static_cast<TimerPrivate*>(arg);
43  d->timerId = -1;
44  d->p->timeout();
45  }, this);
46  } else {
47  timerId = addPeriodicTimer(interval, [](void *arg){
48  TimerPrivate *d = static_cast<TimerPrivate*>(arg);
49  d->p->timeout();
50  }, this);
51  }
52 }
53 
55 {
56  int id = timerId.exchange(-1);
57  if (id != -1)
58  rmTimer(id);
59 }
60 
62  : d_ptr(new TimerPrivate(this))
63 { }
64 
66  : d_ptr(&dd)
67 { }
68 
70 { }
71 
72 void Timer::callOnTimeout(const std::function<void()> &callback)
73 {
74  D_PTR(Timer);
75  d->callback = callback;
76 }
77 
79 {
80  D_PTR(Timer);
81  d->stop();
82  d->start();
83 }
84 
85 void Timer::start(int msec)
86 {
87  D_PTR(Timer);
88  d->stop();
89  d->interval = msec;
90  d->start();
91 }
92 
94 {
95  D_PTR(Timer);
96  d->stop();
97 }
98 
99 void Timer::setInterval(int msec)
100 {
101  D_PTR(Timer);
102  d->interval = msec;
103 }
104 
105 void Timer::setSingleShot(bool singleShot)
106 {
107  D_PTR(Timer);
108  d->singleShot = singleShot;
109 }
110 
111 bool Timer::isActive() const
112 {
113  D_PTR(const Timer);
114  return d->timerId != -1;
115 }
116 
118 {
119  D_PTR(const Timer);
120  return d->singleShot;
121 }
122 
124 {
125  D_PTR(const Timer);
126  return d->timerId != -1 ? std::max(remainingTimer(d->timerId), 0) : 0;
127 }
128 
129 int Timer::interval() const
130 {
131  D_PTR(const Timer);
132  return d->interval;
133 }
134 
136 {
137  D_PTR(Timer);
138  if (d->callback != nullptr)
139  d->callback();
140 }
141 
142 void Timer::singleShot(int msec, const std::function<void()> &callback)
143 {
144  Timer *timer = new Timer();
145  timer->setSingleShot(true);
146  timer->setInterval(msec);
147  timer->callOnTimeout([callback, timer](){
148  callback();
149  delete timer;
150  });
151  timer->start();
152 }
153 
154 }
INDI::Timer::isActive
bool isActive() const
Returns true if the timer is running (pending); otherwise returns false.
Definition: inditimer.cpp:111
INDI::Timer::remainingTime
int remainingTime() const
Returns the timer's remaining value in milliseconds left until the timeout. If the timer not exists,...
Definition: inditimer.cpp:123
inditimer.h
INDI::TimerPrivate::timerId
std::atomic< int > timerId
Definition: inditimer_p.h:42
INDI::Timer
The Timer class provides repetitive and single-shot timers.
Definition: inditimer.h:40
inditimer_p.h
INDI::Timer::Timer
Timer()
Definition: inditimer.cpp:61
INDI::TimerPrivate::stop
void stop()
Definition: inditimer.cpp:54
max
double max(void)
eventloop.h
Public interface to INDI's eventloop mechanism.
INDI::TimerPrivate::TimerPrivate
TimerPrivate(Timer *p)
Definition: inditimer.cpp:28
INDI::Timer::start
void start()
Starts or restarts the timer with the timeout specified in interval.
Definition: inditimer.cpp:78
INDI::Timer::setSingleShot
void setSingleShot(bool singleShot)
Set whether the timer is a single-shot timer.
Definition: inditimer.cpp:105
INDI::Timer::setInterval
void setInterval(int msec)
Set the timeout interval in milliseconds.
Definition: inditimer.cpp:99
INDI::Timer::~Timer
virtual ~Timer()
Definition: inditimer.cpp:69
INDI::Timer::isSingleShot
bool isSingleShot() const
Returns whether the timer is a single-shot timer.
Definition: inditimer.cpp:117
INDI::TimerPrivate::~TimerPrivate
virtual ~TimerPrivate()
Definition: inditimer.cpp:32
INDI::TimerPrivate::interval
int interval
Definition: inditimer_p.h:40
addPeriodicTimer
int addPeriodicTimer(int ms, TCF *fp, void *ud)
Definition: eventloop.c:253
INDI::TimerPrivate::start
void start()
Definition: inditimer.cpp:37
INDI::Timer::callOnTimeout
void callOnTimeout(const std::function< void()> &callback)
Definition: inditimer.cpp:72
INDI::TimerPrivate::p
Timer * p
Definition: inditimer_p.h:39
INDI::Timer::stop
void stop()
Stops the timer.
Definition: inditimer.cpp:93
INDI::TimerPrivate
Definition: inditimer_p.h:28
INDI
Namespace to encapsulate INDI client, drivers, and mediator classes.
Definition: AlignmentSubsystemForClients.cpp:11
INDI::Timer::singleShot
static void singleShot(int msec, const std::function< void()> &callback)
This static function calls a the given function after a given time interval.
Definition: inditimer.cpp:142
INDI::Timer::timeout
virtual void timeout()
This function is called when the timer times out.
Definition: inditimer.cpp:135
INDI::Timer::interval
int interval() const
Returns the timeout interval in milliseconds.
Definition: inditimer.cpp:129
remainingTimer
int remainingTimer(int timer_id)
Definition: eventloop.c:311
rmTimer
void rmTimer(int timer_id)
Definition: eventloop.c:286
addTimer
int addTimer(int ms, TCF *fp, void *ud)
Definition: eventloop.c:248
INDI::TimerPrivate::singleShot
bool singleShot
Definition: inditimer_p.h:43