1 /* IOTimer.cpp - IOMultiplexer v2
2 * Copyright (C) 2014 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "../IOHandler/IOTimer.h"
25 static IOTIMER_CALLBACK(c_timer_callback) {
26 CIOTimer *ciotimer = (CIOTimer *) iotimer->data;
27 ciotimer->timer_callback();
30 CIOTimer::CIOTimer() {
31 this->iotimer = iotimer_create(NULL);
32 this->iotimer->data = this;
33 iotimer_set_callback(this->iotimer, c_timer_callback);
34 iotimer_set_persistent(this->iotimer, 1);
37 CIOTimer::~CIOTimer() {
38 iotimer_destroy(this->iotimer);
41 void CIOTimer::setTimeout(timeval timeout) {
42 iotimer_set_timeout(this->iotimer, &timeout);
45 void CIOTimer::setRelativeTimeout(timeval timeout) {
47 gettimeofday(&now, NULL);
49 timeout.tv_sec += now.tv_sec;
50 timeout.tv_usec += now.tv_usec;
51 while(timeout.tv_usec > 1000000) {
52 timeout.tv_usec -= 1000000;
56 this->setTimeout(timeout);
59 void CIOTimer::setRelativeTimeout(timeval timeout, int auto_reload) {
60 this->setRelativeTimeout(timeout);
62 this->setAutoReload(timeout);
65 void CIOTimer::setRelativeTimeoutSeconds(double seconds) {
66 this->setRelativeTimeoutSeconds(seconds, 0);
69 void CIOTimer::setRelativeTimeoutSeconds(double seconds, int auto_reload) {
71 tout.tv_sec = (int) seconds;
72 tout.tv_usec = ((int) (seconds * 1000000)) % 1000000;
73 this->setRelativeTimeout(tout);
75 this->setAutoReload(tout);
78 timeval CIOTimer::getTimeout() {
79 return iotimer_get_timeout(this->iotimer);
82 timeval CIOTimer::getRelativeTimeout() {
84 gettimeofday(&now, NULL);
86 tout = iotimer_get_timeout(this->iotimer);
88 if(tout.tv_sec || tout.tv_usec) {
89 tout.tv_sec = tout.tv_sec - now.tv_sec;
90 tout.tv_usec = tout.tv_usec - now.tv_usec;
91 if(tout.tv_usec < 0) {
92 tout.tv_usec += 1000000;
100 double CIOTimer::getRelativeTimeoutSeconds() {
101 timeval tout = this->getRelativeTimeout();
102 return tout.tv_sec + (tout.tv_usec / 1000000);
106 void CIOTimer::setAutoReload(timeval timeout) {
107 iotimer_set_autoreload(this->iotimer, &timeout);
110 void CIOTimer::clearAutoReload() {
111 iotimer_set_autoreload(this->iotimer, NULL);
114 timeval CIOTimer::getAutoReloadTime() {
115 return iotimer_get_autoreload(this->iotimer);
118 int CIOTimer::getAutoReloadState() {
119 timeval timeout = this->getAutoReloadTime();
120 if(timeout.tv_sec == 0 && timeout.tv_usec == 0)
126 void CIOTimer::setActive(int active) {
128 iotimer_start(this->iotimer);
130 iotimer_stop(this->iotimer);
133 int CIOTimer::getActive() {
134 return iotimer_state(this->iotimer);
137 void CIOTimer::timer_callback() {