4.06.2013 - Version 0.3.4 - PIOBJECT() macro, ethernet improvement, documentation based on Doxygen
This commit is contained in:
222
pitimer.cpp
222
pitimer.cpp
@@ -20,6 +20,73 @@
|
||||
#include "pitimer.h"
|
||||
#include "pisystemtests.h"
|
||||
|
||||
bool operator ==(const PITime & t0, const PITime & t1) {
|
||||
return (t0.hours == t1.hours && t0.minutes == t1.minutes && t0.seconds == t1.seconds);
|
||||
}
|
||||
bool operator <(const PITime & t0, const PITime & t1) {
|
||||
if (t0.hours == t1.hours) {
|
||||
if (t0.minutes == t1.minutes) {
|
||||
return t0.seconds < t1.seconds;
|
||||
} else return t0.minutes < t1.minutes;
|
||||
} else return t0.hours < t1.hours;
|
||||
}
|
||||
bool operator >(const PITime & t0, const PITime & t1) {
|
||||
if (t0.hours == t1.hours) {
|
||||
if (t0.minutes == t1.minutes) {
|
||||
return t0.seconds > t1.seconds;
|
||||
} else return t0.minutes > t1.minutes;
|
||||
} else return t0.hours > t1.hours;
|
||||
}
|
||||
|
||||
bool operator ==(const PIDate & t0, const PIDate & t1) {
|
||||
return (t0.year == t1.year && t0.month == t1.month && t0.day == t1.day);
|
||||
}
|
||||
bool operator <(const PIDate & t0, const PIDate & t1) {
|
||||
if (t0.year == t1.year) {
|
||||
if (t0.month == t1.month) {
|
||||
return t0.day < t1.day;
|
||||
} else return t0.month < t1.month;
|
||||
} else return t0.year < t1.year;
|
||||
}
|
||||
bool operator >(const PIDate & t0, const PIDate & t1) {
|
||||
if (t0.year == t1.year) {
|
||||
if (t0.month == t1.month) {
|
||||
return t0.day > t1.day;
|
||||
} else return t0.month > t1.month;
|
||||
} else return t0.year > t1.year;
|
||||
}
|
||||
|
||||
bool operator ==(const PIDateTime & t0, const PIDateTime & t1) {
|
||||
return (t0.year == t1.year && t0.month == t1.month && t0.day == t1.day &&
|
||||
t0.hours == t1.hours && t0.minutes == t1.minutes && t0.seconds == t1.seconds);
|
||||
}
|
||||
bool operator <(const PIDateTime & t0, const PIDateTime & t1) {
|
||||
if (t0.year == t1.year) {
|
||||
if (t0.month == t1.month) {
|
||||
if (t0.day == t1.day) {
|
||||
if (t0.hours == t1.hours) {
|
||||
if (t0.minutes == t1.minutes) {
|
||||
return t0.seconds < t1.seconds;
|
||||
} else return t0.minutes < t1.minutes;
|
||||
} else return t0.hours < t1.hours;
|
||||
} else return t0.day < t1.day;
|
||||
} else return t0.month < t1.month;
|
||||
} else return t0.year < t1.year;
|
||||
}
|
||||
bool operator >(const PIDateTime & t0, const PIDateTime & t1) {
|
||||
if (t0.year == t1.year) {
|
||||
if (t0.month == t1.month) {
|
||||
if (t0.day == t1.day) {
|
||||
if (t0.hours == t1.hours) {
|
||||
if (t0.minutes == t1.minutes) {
|
||||
return t0.seconds > t1.seconds;
|
||||
} else return t0.minutes > t1.minutes;
|
||||
} else return t0.hours > t1.hours;
|
||||
} else return t0.day > t1.day;
|
||||
} else return t0.month > t1.month;
|
||||
} else return t0.year > t1.year;
|
||||
}
|
||||
|
||||
|
||||
#ifdef PIP_TIMER_RT
|
||||
PITimer::TimerPool * pool = 0;
|
||||
@@ -34,17 +101,20 @@ PITimer::PITimer(TimerEvent slot, void * data_, bool threaded_)
|
||||
#endif
|
||||
ret_func = slot;
|
||||
data = data_;
|
||||
running_ = false;
|
||||
interval_ = 0.;
|
||||
#ifdef PIP_TIMER_RT
|
||||
piMonitor.timers++;
|
||||
ti = -1;
|
||||
threaded = threaded_;
|
||||
running = false;
|
||||
memset(&se, 0, sizeof(se));
|
||||
se.sigev_notify = SIGEV_THREAD;
|
||||
se.sigev_value.sival_ptr = this;
|
||||
se.sigev_notify_function = PITimer::timer_event;
|
||||
se.sigev_notify_attributes = 0;
|
||||
lockRun = false;
|
||||
#else
|
||||
deferred_ = false;
|
||||
#endif
|
||||
reset();
|
||||
}
|
||||
@@ -58,17 +128,20 @@ PITimer::PITimer(bool threaded_)
|
||||
#endif
|
||||
ret_func = 0;
|
||||
data = 0;
|
||||
running_ = false;
|
||||
interval_ = 0.;
|
||||
#ifdef PIP_TIMER_RT
|
||||
piMonitor.timers++;
|
||||
ti = -1;
|
||||
threaded = threaded_;
|
||||
running = false;
|
||||
memset(&se, 0, sizeof(se));
|
||||
se.sigev_notify = SIGEV_THREAD;
|
||||
se.sigev_value.sival_ptr = this;
|
||||
se.sigev_notify_function = PITimer::timer_event;
|
||||
se.sigev_notify_attributes = 0;
|
||||
lockRun = false;
|
||||
#else
|
||||
deferred_ = false;
|
||||
#endif
|
||||
reset();
|
||||
}
|
||||
@@ -84,14 +157,15 @@ PITimer::~PITimer() {
|
||||
|
||||
#ifdef PIP_TIMER_RT
|
||||
void PITimer::start(double msecs) {
|
||||
if (ti != -1 || msecs < 0 || running) return;
|
||||
if (ti != -1 || msecs < 0 || running_) return;
|
||||
interval_ = msecs;
|
||||
if (!threaded) {
|
||||
ticks = int(msecs);
|
||||
if (pool == 0) pool = new TimerPool();
|
||||
pool->add(this);
|
||||
//cout << "not threaded timer start " << msecs << " msecs\n";
|
||||
if (!pool->isRunning()) pool->start();
|
||||
running = true;
|
||||
running_ = true;
|
||||
return;
|
||||
}
|
||||
spec.it_interval.tv_nsec = ((int)(msecs * 1000) % 1000000) * 1000;
|
||||
@@ -100,16 +174,17 @@ void PITimer::start(double msecs) {
|
||||
ti = timer_create(CLOCK_REALTIME, &se, &timer);
|
||||
//cout << "***create timer " << msecs << " msecs\n";
|
||||
if (ti == -1) {
|
||||
piCout << "[PITimer] Can`t create timer for " << msecs << " msecs: " << errorString() << endl;
|
||||
piCout << "[PITimer] Can`t create timer for " << msecs << " msecs: " << errorString();
|
||||
return;
|
||||
}
|
||||
timer_settime(timer, 0, &spec, 0);
|
||||
running = true;
|
||||
running_ = true;
|
||||
}
|
||||
|
||||
|
||||
void PITimer::deferredStart(double interval_msecs, double delay_msecs) {
|
||||
if (ti != -1 || interval_msecs < 0 || running) return;
|
||||
if (ti != -1 || interval_msecs < 0 || running_) return;
|
||||
interval_ = interval_msecs;
|
||||
spec.it_interval.tv_nsec = ((int)(interval_msecs * 1000) % 1000000) * 1000;
|
||||
spec.it_interval.tv_sec = (time_t)(interval_msecs / 1000);
|
||||
spec.it_value.tv_nsec = ((int)(delay_msecs * 1000) % 1000000) * 1000;
|
||||
@@ -117,16 +192,17 @@ void PITimer::deferredStart(double interval_msecs, double delay_msecs) {
|
||||
ti = timer_create(CLOCK_REALTIME, &se, &timer);
|
||||
//cout << "***create timer\n";
|
||||
if (ti == -1) {
|
||||
piCout << "[PITimer] Can`t create timer for " << interval_msecs << " msecs: " << errorString() << endl;
|
||||
piCout << "[PITimer] Can`t create timer for " << interval_msecs << " msecs: " << errorString();
|
||||
return;
|
||||
}
|
||||
timer_settime(timer, 0, &spec, 0);
|
||||
running = true;
|
||||
running_ = true;
|
||||
}
|
||||
|
||||
|
||||
void PITimer::deferredStart(double interval_msecs, const PIDateTime & start_datetime) {
|
||||
if (ti != -1 || interval_msecs < 0 || running) return;
|
||||
if (ti != -1 || interval_msecs < 0 || running_) return;
|
||||
interval_ = interval_msecs;
|
||||
spec.it_interval.tv_nsec = ((int)(interval_msecs * 1000) % 1000000) * 1000;
|
||||
spec.it_interval.tv_sec = (time_t)(interval_msecs / 1000);
|
||||
struct tm dtm;
|
||||
@@ -142,11 +218,11 @@ void PITimer::deferredStart(double interval_msecs, const PIDateTime & start_date
|
||||
ti = timer_create(CLOCK_REALTIME, &se, &timer);
|
||||
//cout << "***create timer\n";
|
||||
if (ti == -1) {
|
||||
piCout << "[PITimer] Can`t create timer for " << interval_msecs << " msecs: " << errorString() << endl;
|
||||
piCout << "[PITimer] Can`t create timer for " << interval_msecs << " msecs: " << errorString();
|
||||
return;
|
||||
}
|
||||
timer_settime(timer, TIMER_ABSTIME, &spec, 0);
|
||||
running = true;
|
||||
running_ = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -170,7 +246,7 @@ void PITimer::TimerPool::begin() {
|
||||
sa.sa_handler = empty_handler;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
if (sigaction(SIGALRM, &sa, 0) == -1) {
|
||||
piCout << "[PITimer] sigaction error: " << errorString() << endl;
|
||||
piCout << "[PITimer] sigaction error: " << errorString();
|
||||
stop();
|
||||
return;
|
||||
}*/
|
||||
@@ -184,12 +260,12 @@ void PITimer::TimerPool::begin() {
|
||||
spec.it_value = spec.it_interval;
|
||||
//cout << "***create pool timer\n";
|
||||
if (timer_create(CLOCK_REALTIME, &se, &timer) == -1) {
|
||||
piCout << "[PITimer] Can`t create timer for pool: " << errorString() << endl;
|
||||
piCout << "[PITimer] Can`t create timer for pool: " << errorString();
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
if (timer_settime(timer, 0, &spec, 0) == -1) {
|
||||
piCout << "[PITimer] Can`t set timer for pool: " << errorString() << endl;
|
||||
piCout << "[PITimer] Can`t set timer for pool: " << errorString();
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
@@ -221,8 +297,8 @@ void PITimer::TimerPool::run() {
|
||||
|
||||
|
||||
void PITimer::stop() {
|
||||
if (!running) return;
|
||||
running = false;
|
||||
if (!running_) return;
|
||||
running_ = false;
|
||||
if (!threaded) {
|
||||
if (pool != 0) {
|
||||
pool->remove(this);
|
||||
@@ -231,12 +307,13 @@ void PITimer::stop() {
|
||||
}
|
||||
if (ti != -1) timer_delete(timer);
|
||||
ti = -1;
|
||||
interval_ = 0.;
|
||||
}
|
||||
|
||||
|
||||
void PITimer::timer_event(sigval e) {
|
||||
PITimer * ct = (PITimer * )e.sival_ptr;
|
||||
if (!ct->running) return;
|
||||
if (!ct->running_) return;
|
||||
if (ct->lockRun) ct->lock();
|
||||
if (ct->ret_func != 0) ct->ret_func(ct->data, 1);
|
||||
ct->timeout(ct->data, 1);
|
||||
@@ -255,12 +332,12 @@ void PITimer::timer_event(sigval e) {
|
||||
|
||||
bool PITimer::waitForFinish(int timeout_msecs) {
|
||||
if (timeout_msecs < 0) {
|
||||
while (running)
|
||||
while (running_)
|
||||
msleep(1);
|
||||
return true;
|
||||
}
|
||||
int cnt = 0;
|
||||
while (running && cnt < timeout_msecs) {
|
||||
while (running_ && cnt < timeout_msecs) {
|
||||
msleep(1);
|
||||
++cnt;
|
||||
}
|
||||
@@ -270,18 +347,65 @@ bool PITimer::waitForFinish(int timeout_msecs) {
|
||||
#else
|
||||
|
||||
void PITimer::start(double msecs) {
|
||||
if (msecs < 0 || running) return;
|
||||
if (msecs < 0 || running_) return;
|
||||
interval_ = msecs;
|
||||
inc_time = PISystemTime::fromMilliseconds(msecs);
|
||||
st_time = currentSystemTime() + inc_time;
|
||||
deferred_ = false;
|
||||
running_ = true;
|
||||
PIThread::start();
|
||||
}
|
||||
|
||||
|
||||
void PITimer::deferredStart(double interval_msecs, double delay_msecs) {
|
||||
//piCout << "defStart exec with" << delay_msecs << interval_msecs;
|
||||
if (interval_msecs < 0 || running_) return;
|
||||
interval_ = interval_msecs;
|
||||
PISystemTime cst = currentSystemTime();
|
||||
inc_time = PISystemTime::fromMilliseconds(interval_msecs);
|
||||
st_time = currentSystemTime() + PISystemTime::fromMilliseconds(delay_msecs);
|
||||
if (st_time < cst) st_time = cst;
|
||||
running_ = deferred_ = true;
|
||||
PIThread::start();
|
||||
//piCout << "timer start def";
|
||||
}
|
||||
|
||||
|
||||
void PITimer::deferredStart(double interval_msecs, const PIDateTime & start_datetime) {
|
||||
//piCout << "defStart exec to" << start_datetime.toString() << interval_msecs;
|
||||
if (interval_msecs < 0 || running_) return;
|
||||
interval_ = interval_msecs;
|
||||
PISystemTime cst = currentSystemTime();
|
||||
inc_time = PISystemTime::fromMilliseconds(interval_msecs);
|
||||
st_time = start_datetime.toSystemTime();
|
||||
if (st_time < cst) st_time = cst;
|
||||
running_ = deferred_ = true;
|
||||
PIThread::start();
|
||||
//piCout << "timer start def";
|
||||
}
|
||||
|
||||
|
||||
void PITimer::run() {
|
||||
if (!running) return;
|
||||
if (!running_) return;
|
||||
while (deferred_) {
|
||||
PISystemTime tst = st_time - currentSystemTime();
|
||||
if (tst.seconds > 0) {
|
||||
piMSleep(100);
|
||||
if (!running_) return;
|
||||
continue;
|
||||
}
|
||||
if (tst.nanoseconds > 100000000) {
|
||||
piMSleep(100);
|
||||
if (!running_) return;
|
||||
continue;
|
||||
}
|
||||
tst.sleep();
|
||||
deferred_ = false;
|
||||
if (!running_) return;
|
||||
}
|
||||
(st_time - currentSystemTime()).sleep();
|
||||
st_time += inc_time;
|
||||
if (lockRun) lock();
|
||||
//if (lockRun) lock();
|
||||
if (ret_func != 0) ret_func(data, 1);
|
||||
timeout(data, 1);
|
||||
tick(data, 1);
|
||||
@@ -293,7 +417,7 @@ void PITimer::run() {
|
||||
timeout(data, i.delim);
|
||||
tick(data, i.delim);
|
||||
}
|
||||
if (lockRun) unlock();
|
||||
//if (lockRun) unlock();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -524,7 +648,7 @@ PISystemTime currentSystemTime() {
|
||||
}
|
||||
|
||||
|
||||
PIString PITime::toString(const PIString & format) {
|
||||
PIString PITime::toString(const PIString & format) const {
|
||||
PIString ts = format;
|
||||
ts.replace("hh", PIString::fromNumber(hours).expandLeftTo(2, '0'));
|
||||
ts.replace("h", PIString::fromNumber(hours));
|
||||
@@ -532,11 +656,14 @@ PIString PITime::toString(const PIString & format) {
|
||||
ts.replace("m", PIString::fromNumber(minutes));
|
||||
ts.replace("ss", PIString::fromNumber(seconds).expandLeftTo(2, '0'));
|
||||
ts.replace("s", PIString::fromNumber(seconds));
|
||||
ts.replace("zzz", PIString::fromNumber(milliseconds).expandLeftTo(3, '0'));
|
||||
ts.replace("zz", PIString::fromNumber(milliseconds).expandLeftTo(2, '0'));
|
||||
ts.replace("z", PIString::fromNumber(milliseconds));
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
PIString PIDate::toString(const PIString & format) {
|
||||
PIString PIDate::toString(const PIString & format) const {
|
||||
PIString ts = format;
|
||||
ts.replace("yyyy", PIString::fromNumber(year).expandLeftTo(4, '0'));
|
||||
ts.replace("yy", PIString::fromNumber(year).right(2));
|
||||
@@ -549,14 +676,8 @@ PIString PIDate::toString(const PIString & format) {
|
||||
}
|
||||
|
||||
|
||||
PIString PIDateTime::toString(const PIString & format) {
|
||||
PIString PIDateTime::toString(const PIString & format) const {
|
||||
PIString ts = format;
|
||||
ts.replace("hh", PIString::fromNumber(hours).expandLeftTo(2, '0'));
|
||||
ts.replace("h", PIString::fromNumber(hours));
|
||||
ts.replace("mm", PIString::fromNumber(minutes).expandLeftTo(2, '0'));
|
||||
ts.replace("m", PIString::fromNumber(minutes));
|
||||
ts.replace("ss", PIString::fromNumber(seconds).expandLeftTo(2, '0'));
|
||||
ts.replace("s", PIString::fromNumber(seconds));
|
||||
ts.replace("yyyy", PIString::fromNumber(year).expandLeftTo(4, '0'));
|
||||
ts.replace("yy", PIString::fromNumber(year).right(2));
|
||||
ts.replace("y", PIString::fromNumber(year).right(1));
|
||||
@@ -564,10 +685,45 @@ PIString PIDateTime::toString(const PIString & format) {
|
||||
ts.replace("M", PIString::fromNumber(month));
|
||||
ts.replace("dd", PIString::fromNumber(day).expandLeftTo(2, '0'));
|
||||
ts.replace("d", PIString::fromNumber(day));
|
||||
ts.replace("hh", PIString::fromNumber(hours).expandLeftTo(2, '0'));
|
||||
ts.replace("h", PIString::fromNumber(hours));
|
||||
ts.replace("mm", PIString::fromNumber(minutes).expandLeftTo(2, '0'));
|
||||
ts.replace("m", PIString::fromNumber(minutes));
|
||||
ts.replace("ss", PIString::fromNumber(seconds).expandLeftTo(2, '0'));
|
||||
ts.replace("s", PIString::fromNumber(seconds));
|
||||
ts.replace("zzz", PIString::fromNumber(milliseconds).expandLeftTo(3, '0'));
|
||||
ts.replace("zz", PIString::fromNumber(milliseconds).expandLeftTo(2, '0'));
|
||||
ts.replace("z", PIString::fromNumber(milliseconds));
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
time_t PIDateTime::toSecondSinceEpoch() const {
|
||||
tm pt;
|
||||
pt.tm_sec = seconds;
|
||||
pt.tm_min = minutes;
|
||||
pt.tm_hour = hours;
|
||||
pt.tm_mday = day;
|
||||
pt.tm_mon = month - 1;
|
||||
pt.tm_year = year - 1900;
|
||||
return mktime(&pt);
|
||||
}
|
||||
|
||||
|
||||
PIDateTime PIDateTime::fromSecondSinceEpoch(const time_t sec) {
|
||||
tm * pt = localtime(&sec);
|
||||
PIDateTime dt;
|
||||
dt.seconds = pt->tm_sec;
|
||||
dt.minutes = pt->tm_min;
|
||||
dt.hours = pt->tm_hour;
|
||||
dt.day = pt->tm_mday;
|
||||
dt.month = pt->tm_mon + 1;
|
||||
dt.year = pt->tm_year + 1900;
|
||||
return dt;
|
||||
|
||||
}
|
||||
|
||||
|
||||
PIString time2string(const PITime & time, const PIString & format) {
|
||||
PIString ts = format;
|
||||
ts.replace("hh", PIString::fromNumber(time.hours).expandLeftTo(2, '0'));
|
||||
|
||||
Reference in New Issue
Block a user