我为我的项目编写了一个计时器类。我使用了标准的C ++函数调用。
我想创建一个计时器(启动,停止,暂停,继续,重置)并传递新的计时。
如果计时器在我停止之前触发,我想在给定的通道上获得一个qnx脉冲信息。
这是标题:
#ifndef TIMER_H_
#define TIMER_H_
#include <time.h>
#include <sys/neutrino.h>
#include "HWaccess.h"
#include "address.h"
#define MILLISECONDS_NANOSECONDS_CONV 1000000
#define SECONDS_MILLISECONDS_CONV 1000
class Timer {
public:
/**
* Constructor creates a new timer, attaches to the given
* channel.
* Does not start timer!
*
* @param chid
* @param seconds
* @param milliseconds
* @param msg
*/
Timer(int chid, int sec, int msec, int msg);
/**
* Deletes timer after detach from channel
*/
virtual ~Timer();
/**
* Starts the timer
*/
void start();
/**
* Stops timer and resets it to initial values
*/
void stop();
/**
* Pauses the timer
*/
void pause();
/**
* Continues the timer
*/
void cont();
/**
* Resets the timer to initial values
*/
void reset();
/**
* Changes timer values to new given values and
* resets it
* Does not start the timer!
*
* @param seconds
* @param milliseconds
*/
void changeTime(int sec, int msec);
private:
/**
* Timer ID
*/
timer_t timerid;
/**
* Timerstruct for running timer
*/
struct itimerspec timer;
/**
* Timerstruct for backing up the running timer
*/
struct itimerspec backupTimer;
/**
* timer value: seconds
*/
int seconds;
/**
* timer value: milliseconds
*/
int miliSeconds;
/**
* Connection ID for timeout pulse
*/
int coid;
/**
* Event structure for timer if it fires
*/
struct sigevent event;
};
#endif /* TIMER_H_ */
......和实施:
#include "Timer.h"
Timer::Timer(int chid, int sec, int msec, int msg) {
if ((coid = ConnectAttach(0, 0, chid, _NTO_SIDE_CHANNEL, 0)) == -1) {
printf("Timer: Error in ConnectAttach\n");
}
SIGEV_PULSE_INIT(&event, coid, SIGEV_PULSE_PRIO_INHERIT, PULSE_FROM_TIMER, msg/*Timer abgelaufen*/);
if (timer_create(CLOCK_REALTIME, &event, &timerid) == -1) {
printf("Timer: Error in timer_create()\n");
}
seconds = sec;
miliSeconds = msec;
reset();
}
Timer::~Timer() {
if (ConnectDetach(coid) == -1) {
printf("Timer: Error in ConnectDetach\n");
}
if (timer_delete(timerid) == -1) {
printf("Timer: Error in timer_delete()\n");
}
}
void Timer::start() {
if(timer_settime(timerid, 0, &timer, NULL) == -1){
printf("Timer: Error in timer_settime()\n");
}
}
void Timer::stop() {
// stop timer
timer.it_value.tv_sec = 0;
timer.it_value.tv_nsec = 0;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_nsec = 0;
if(timer_settime(timerid, 0, &timer, NULL) == -1){
printf("Timer: Error in timer_settime()\n");
}
// reset it
reset();
}
void Timer::pause() {
timer.it_value.tv_sec = 0;
timer.it_value.tv_nsec = 0;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_nsec = 0;
// backup running timer values
if(timer_gettime(timerid, &backupTimer) == -1){
printf("Timer: Error in timer_gettime()\n");
}
// disarm
if(timer_settime(timerid, 0, &timer, NULL) == -1){
printf("Timer: Error in timer_settime()\n");
}
}
void Timer::cont() {
// recover old values
timer = backupTimer;
// Arm timer
if(timer_settime(timerid, 0, &timer, NULL) == -1) {
printf("Timer: Error in timer_settime()\n");
}
}
void Timer::reset(){
timer.it_value.tv_sec = seconds;
timer.it_value.tv_nsec = miliSeconds * MILLISECONDS_NANOSECONDS_CONV;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_nsec = 0;
}
void Timer::changeTime(int sec, int msec){
seconds = sec;
miliSeconds = msec;
reset();
}
1)我可以将timer struct的值设置为零(停止计时器)并以我的方式备份另一个后结构中的值吗?或者,计时器结构中的运行计时器是否递减?
2)我可以使用简单的timer = backupTimer
轻松恢复旧计时器值吗?
3)最后,如果我在一个函数中创建并启动一个计时器,就像这样:
void coolClass::interestingFunction() {
//do other time consuming stuff here...
Timer timer(chid, 10, 0);
timer.start();
}
...然后在堆栈上创建计时器,当我退出此函数(及其变量等)不再有效时。计时器是否仍会倒计时并触发我的脉冲?或者我必须在我的头文件中使用分类器来进行此计时器吗?
答案 0 :(得分:0)
好的,我修复了所有问题;&gt; 如果some1对如何感兴趣,继承修改后的代码:
#include "Timer.h"
Timer::Timer(int chid, int sec, int msec, int msg) {
if ((coid = ConnectAttach(0, 0, chid, _NTO_SIDE_CHANNEL, 0)) == -1) {
printf("Timer: Error in ConnectAttach\n");
}
SIGEV_PULSE_INIT(&event, coid, SIGEV_PULSE_PRIO_INHERIT, PULSE_FROM_TIMER, msg/*Timer abgelaufen*/);
if (timer_create(CLOCK_REALTIME, &event, &timerid) == -1) {
printf("Timer: Error in timer_create()\n");
}
seconds = sec;
miliSeconds = msec;
reset();
}
Timer::~Timer() {
if (ConnectDetach(coid) == -1) {
printf("Timer: Error in ConnectDetach\n");
}
if (timer_delete(timerid) == -1) {
printf("Timer: Error in timer_delete()\n");
}
}
void Timer::start() {
//TODO running flag, wg doppelt pause / continue
if(timer_settime(timerid, 0, &timer, NULL) == -1){
printf("Timer: Error in timer_settime()\n");
}
}
void Timer::stop() {
// Stoppe den Timer
if(timer_settime(timerid, 0, NULL, NULL) == -1){
printf("Timer: Error in timer_settime()\n");
}
// Zuruecksetzen
reset();
}
void Timer::pause() {
// disarm (da erster Wert NULL)
if(timer_settime(timerid, 0, NULL, &backupTimer) == -1){
printf("Timer: Error in timer_settime()\n");
}
}
void Timer::cont() {
// Arm, da Werte im struct wieder != 0
if(timer_settime(timerid, 0, &backupTimer, NULL) == -1) {
printf("Timer: Error in timer_settime()\n");
}
}
void Timer::reset(){
timer.it_value.tv_sec = seconds;
timer.it_value.tv_nsec = miliSeconds * MILLISECONDS_NANOSECONDS_CONV;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_nsec = 0;
}
void Timer::changeTime(int sec, int msec){
seconds = sec;
miliSeconds = msec;
reset();
}