cwIo.h/cpp : Add timerSetNextTime().
This commit is contained in:
parent
5b28630b6d
commit
92df31e591
35
cwIo.cpp
35
cwIo.cpp
@ -58,6 +58,7 @@ namespace cw
|
|||||||
unsigned index;
|
unsigned index;
|
||||||
unsigned periodMicroSec;
|
unsigned periodMicroSec;
|
||||||
bool asyncFl;
|
bool asyncFl;
|
||||||
|
time::spec_t nextTime;
|
||||||
} timer_t;
|
} timer_t;
|
||||||
|
|
||||||
typedef struct serialPort_str
|
typedef struct serialPort_str
|
||||||
@ -282,6 +283,7 @@ namespace cw
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// Timer
|
// Timer
|
||||||
@ -290,7 +292,16 @@ namespace cw
|
|||||||
{
|
{
|
||||||
timer_t* t = (timer_t*)arg;
|
timer_t* t = (timer_t*)arg;
|
||||||
|
|
||||||
sleepUs( t->periodMicroSec );
|
time::spec_t t0;
|
||||||
|
time::get(t0);
|
||||||
|
|
||||||
|
int usec = time::diffMicros(t0,t->nextTime);
|
||||||
|
|
||||||
|
if( usec > 0 )
|
||||||
|
sleepUs(usec);
|
||||||
|
|
||||||
|
time::advanceMicros(t->nextTime,t->periodMicroSec);
|
||||||
|
|
||||||
|
|
||||||
if( t->startedFl && !t->deletedFl )
|
if( t->startedFl && !t->deletedFl )
|
||||||
{
|
{
|
||||||
@ -352,6 +363,9 @@ namespace cw
|
|||||||
t->asyncFl = asyncFl;
|
t->asyncFl = asyncFl;
|
||||||
t->periodMicroSec = periodMicroSec;
|
t->periodMicroSec = periodMicroSec;
|
||||||
|
|
||||||
|
|
||||||
|
time::get(t->nextTime);
|
||||||
|
|
||||||
if((rc = thread_mach::add(p->threadMachH,_timerThreadCb,t)) != kOkRC )
|
if((rc = thread_mach::add(p->threadMachH,_timerThreadCb,t)) != kOkRC )
|
||||||
{
|
{
|
||||||
rc = cwLogError(rc,"Timer thread assignment failed.");
|
rc = cwLogError(rc,"Timer thread assignment failed.");
|
||||||
@ -380,7 +394,6 @@ namespace cw
|
|||||||
rc = kInvalidIdRC;
|
rc = kInvalidIdRC;
|
||||||
else
|
else
|
||||||
p->timerA[ timerIdx ].startedFl = startFl;
|
p->timerA[ timerIdx ].startedFl = startFl;
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2516,7 +2529,25 @@ cw::rc_t cw::io::timerSetPeriodMicroSec( handle_t h, unsigned timerIdx, unsig
|
|||||||
if( t == nullptr )
|
if( t == nullptr )
|
||||||
rc = kInvalidIdRC;
|
rc = kInvalidIdRC;
|
||||||
else
|
else
|
||||||
|
{
|
||||||
p->timerA[ timerIdx ].periodMicroSec = periodMicroSec;
|
p->timerA[ timerIdx ].periodMicroSec = periodMicroSec;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
cw::rc_t cw::io::timerSetNextTime( handle_t h, unsigned timerIdx, const time::spec_t& time )
|
||||||
|
{
|
||||||
|
rc_t rc = kOkRC;
|
||||||
|
io_t* p = _handleToPtr(h);
|
||||||
|
timer_t* t = _timerIndexToPtr(p, timerIdx);
|
||||||
|
|
||||||
|
if( t == nullptr )
|
||||||
|
rc = kInvalidIdRC;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
p->timerA[ timerIdx ].nextTime = time;
|
||||||
|
}
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
6
cwIo.h
6
cwIo.h
@ -190,6 +190,12 @@ namespace cw
|
|||||||
unsigned timerId( handle_t h, unsigned timerIdx );
|
unsigned timerId( handle_t h, unsigned timerIdx );
|
||||||
unsigned timerPeriodMicroSec( handle_t h, unsigned timerIdx );
|
unsigned timerPeriodMicroSec( handle_t h, unsigned timerIdx );
|
||||||
rc_t timerSetPeriodMicroSec( handle_t h, unsigned timerIdx, unsigned periodMicroSec );
|
rc_t timerSetPeriodMicroSec( handle_t h, unsigned timerIdx, unsigned periodMicroSec );
|
||||||
|
|
||||||
|
// Set an explicit next time to trigger. The 'time' argument will over ride the next periodic callback.
|
||||||
|
// Note that the most reliable way to use this function is by calling it in the timer callback
|
||||||
|
// so that it will deterine the time of the following callback.
|
||||||
|
rc_t timerSetNextTime( handle_t h, unsigned timerIdx, const time::spec_t& time );
|
||||||
|
|
||||||
rc_t timerStart( handle_t h, unsigned timerIdx );
|
rc_t timerStart( handle_t h, unsigned timerIdx );
|
||||||
rc_t timerStop( handle_t h, unsigned timerIdx );
|
rc_t timerStop( handle_t h, unsigned timerIdx );
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user