32#ifndef __STATEOS_TMR_H
33#define __STATEOS_TMR_H
37#include "oscriticalsection.h"
62typedef struct __tmr tmr_id [];
80#define _TMR_INIT( _proc ) \
81 { _OBJ_INIT(), _HDR_INIT(), _proc, NULL, 0, 0, 0, 0 }
91#define OS_TMR( tmr, proc ) \
92 tmr_t tmr[] = { _TMR_INIT( proc ) }
95#define static_TMR( tmr, proc ) \
96 static tmr_t tmr[] = { _TMR_INIT( proc ) }
106#define OS_TMR_DEF( tmr ) \
107 static void tmr##__fun( void ); \
108 OS_TMR( tmr, tmr##__fun ); \
109 static void tmr##__fun( void )
112#define static_TMR_DEF( tmr ) \
113 static void tmr##__fun( void ); \
114 static_TMR( tmr, tmr##__fun ); \
115 static void tmr##__fun( void )
134#define OS_TMR_START( tmr, delay, period ) \
135 static void tmr##__fun( void ); \
136 OS_TMR( tmr, tmr##__fun ); \
137 __CONSTRUCTOR static void tmr##__run( void ) { sys_init(); tmr_start(tmr, delay, period); } \
138 static void tmr##__fun( void )
143#define static_TMR_START( tmr, delay, period ) \
144 static void tmr##__fun( void ); \
145 static_TMR( tmr, tmr##__fun ); \
146 __CONSTRUCTOR static void tmr##__run( void ) { sys_init(); tmr_start(tmr, delay, period); } \
147 static void tmr##__fun( void )
162#define OS_TMR_UNTIL( tmr, time ) \
163 static void tmr##__fun( void ); \
164 OS_TMR( tmr, tmr##__fun ); \
165 __CONSTRUCTOR static void tmr##__run( void ) { sys_init(); tmr_startUntil(tmr, time); } \
166 static void tmr##__fun( void )
171#define static_TMR_UNTIL( tmr, time ) \
172 static void tmr##__fun( void ); \
173 static_TMR( tmr, tmr##__fun ); \
174 __CONSTRUCTOR static void tmr##__run( void ) { sys_init(); tmr_startUntil(tmr, time); } \
175 static void tmr##__fun( void )
190#define TMR_INIT( proc ) \
206#define TMR_CREATE( proc ) \
207 { TMR_INIT ( proc ) }
226tmr_t *
tmr_thisISR(
void ) {
return (tmr_t *) WAIT.hdr.next; }
304void tmr_start( tmr_t *tmr, cnt_t delay, cnt_t period );
545#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
562struct baseTimer :
public __tmr
564 baseTimer(): __tmr _TMR_INIT(nullptr) {}
565#if __cplusplus >= 201402L
567 baseTimer( F&& _proc ): __tmr _TMR_INIT(fun_), fun{_proc} {}
569 baseTimer(
fun_t *_proc ): __tmr _TMR_INIT(_proc) {}
585#if __cplusplus >= 201402L
588 template<
typename T,
class F>
589 void startFrom (
const T& _delay,
const T& _period, F&& _proc )
603 int take () {
return tmr_take (
this); }
613 int wait () {
return tmr_wait (
this); }
615 operator bool ()
const {
return __tmr::hdr.id != ID_STOPPED; }
617 template<
class T = baseTimer>
static
618 T * current () {
return static_cast<T *
>(
tmr_thisISR()); }
620#if __cplusplus >= 201402L
624 auto tmr = current();
643#if __cplusplus >= 201402L
645 void flipISR ( std::nullptr_t ) {
tmr_flipISR (
nullptr); }
646 template<
class F>
static
647 void flipISR ( F&& _proc ) { current()->fun = Fun_t(_proc);
649 template<
typename F,
typename... A>
static
650 void flipISR ( F&& _proc, A&&... _args ) { flipISR(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...)); }
655 template<
typename T>
static
662using thisTimer = baseTimer::Current;
663using this_timer = baseTimer::Current;
673struct Timer :
public baseTimer
676 Timer(): baseTimer{} {}
679 Timer( F&& _proc ): baseTimer{_proc} {}
680#if __cplusplus >= 201402L
682 Timer( std::nullptr_t ): baseTimer{} {}
684 template<
typename F,
typename... A>
685 Timer( F&& _proc, A&&... _args ): baseTimer{std::bind(std::forward<F>(_proc), std::forward<A>(_args)...)} {}
688 ~Timer() { assert(__tmr::hdr.
id == ID_STOPPED); }
691 Timer(
Timer&& _src ): baseTimer(static_cast<baseTimer&&>(_src))
693 assert(_src.hdr.id == ID_STOPPED && _src.obj.queue ==
nullptr);
702 template<
class S,
typename... A>
703 Timer( Starter, S&& _start, A&&... _args ):
Timer{static_cast<A&&>(_args)...} { _start(*
this); }
724 template<
class F>
static
730#if __cplusplus >= 201402L
739 template<
typename F,
typename... A>
static
742 return Make(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
768 template<
typename T>
static
769 Timer Start(
const T& _delay,
const T& _period )
771 return { Starter{}, [&](
Timer& _tmr ){ _tmr.start(_delay, _period); } };
775 template<
typename T,
class F>
static
776 Timer Start(
const T& _delay,
const T& _period, F&& _proc )
778 return { Starter{}, [&](
Timer& _tmr ){ _tmr.start(_delay, _period); }, _proc };
781#if __cplusplus >= 201402L
783 template<
typename T>
static
784 Timer Start(
const T& _delay,
const T& _period, std::nullptr_t )
786 return Start(_delay, _period);
790 template<
typename T,
typename F,
typename... A>
static
791 Timer Start(
const T& _delay,
const T& _period, F&& _proc, A&&... _args )
793 return Start(_delay, _period, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
814 template<
typename T>
static
817 return { Starter{}, [&](
Timer& _tmr ){ _tmr.startFor(_delay); } };
821 template<
typename T,
class F>
static
824 return { Starter{}, [&](
Timer& _tmr ){ _tmr.startFor(_delay); }, _proc };
827#if __cplusplus >= 201402L
829 template<
typename T>
static
836 template<
typename T,
typename F,
typename... A>
static
839 return StartFor(_delay, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
862 template<
typename T>
static
865 return { Starter{}, [&](
Timer& _tmr ){ _tmr.startPeriodic(_period); } };
869 template<
typename T,
class F>
static
872 return { Starter{}, [&](
Timer& _tmr ){ _tmr.startPeriodic(_period); }, _proc };
875#if __cplusplus >= 201402L
877 template<
typename T>
static
884 template<
typename T,
typename F,
typename... A>
static
887 return StartPeriodic(_period, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
906 template<
typename T>
static
909 return { Starter{}, [&](
Timer& _tmr ){ _tmr.startUntil(_time); } };
913 template<
typename T,
class F>
static
916 return { Starter{}, [&](
Timer& _tmr ){ _tmr.startUntil(_time); }, _proc };
919#if __cplusplus >= 201402L
921 template<
typename T>
static
928 template<
typename T,
typename F,
typename... A>
static
931 return StartUntil(_time, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
935#if __cplusplus >= 201402L
936 using Ptr = std::unique_ptr<Timer>;
957 auto tmr =
new (std::nothrow)
Timer();
959 tmr->__tmr::obj.res = tmr;
964 template<
class F>
static
967 auto tmr =
new (std::nothrow)
Timer(_proc);
969 tmr->__tmr::obj.res = tmr;
973#if __cplusplus >= 201402L
982 template<
typename F,
typename... A>
static
985 return Create(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
#define INFINITE
infinite waiting
Definition osbase.h:261
void fun_t(void)
timer/task procedure
Definition osbase.h:137
void tmr_delete(tmr_t *tmr)
alias of tmr_destroy()
Definition ostimer.h:283
int tmr_wait(tmr_t *tmr)
wait indefinitely until the timer finishes countdown
Definition ostimer.h:498
int tmr_takeISR(tmr_t *tmr)
ISR alias of tmr_take().
Definition ostimer.h:425
tmr_t * tmr_new(fun_t *proc)
alias of tmr_create()
Definition ostimer.h:255
void tmr_startPeriodic(tmr_t *tmr, cnt_t period)
start/restart periodic timer for given duration of time
Definition ostimer.h:337
void tmr_destroy(tmr_t *tmr)
reset the timer object, wake up all waiting tasks with 'E_DELETED' event value and free allocated res...
void tmr_reset(tmr_t *tmr)
reset the timer object and wake up all waiting tasks with 'E_STOPPED' event value
void tmr_stop(tmr_t *tmr)
stop timer
Definition ostimer.h:398
int tmr_tryWait(tmr_t *tmr)
alias of tmr_take()
Definition ostimer.h:421
tmr_t * tmr_thisISR(void)
return current timer object
Definition ostimer.h:225
void tmr_delayISR(cnt_t delay)
change delay time for current timer
Definition ostimer.h:523
int tmr_waitUntil(tmr_t *tmr, cnt_t time)
wait until given timepoint until the timer finishes countdown
void tmr_startUntil(tmr_t *tmr, cnt_t time)
start/restart one-shot timer until given timepoint
void tmr_init(tmr_t *tmr, fun_t *proc)
initialize a timer object
tmr_t * tmr_create(fun_t *proc)
create and initialize a new timer object
int tmr_take(tmr_t *tmr)
check if the timer finishes countdown
cnt_t tmr_overrunISR(void)
return the number of periods skipped by the current periodic timer
Definition ostimer.h:536
void tmr_kill(tmr_t *tmr)
alias of tmr_reset()
Definition ostimer.h:269
void tmr_startNext(tmr_t *tmr, cnt_t delay)
restart the timer for given duration of time from the end of the previous countdown
void tmr_flipISR(fun_t *proc)
change callback procedure for current periodic timer (available in next period)
Definition ostimer.h:510
int tmr_waitFor(tmr_t *tmr, cnt_t delay)
wait for given duration of time until the timer finishes countdown
int tmr_expired(tmr_t *tmr)
alias of tmr_take()
Definition ostimer.h:417
void tmr_startFor(tmr_t *tmr, cnt_t delay)
start/restart one-shot timer for given duration of time
Definition ostimer.h:319
void tmr_start(tmr_t *tmr, cnt_t delay, cnt_t period)
start/restart periodic timer for given duration of time
void tmr_startFrom(tmr_t *tmr, cnt_t delay, cnt_t period, fun_t *proc)
start/restart periodic timer for given duration of time
int tmr_waitNext(tmr_t *tmr, cnt_t delay)
wait for given duration of time from the end of the previous countdown
static constexpr rep count(const std::chrono::duration< _Rep, _Period > &_delay)
delay in ticks, rounded up to whole ticks; negative delay: IMMEDIATE, delay out of range: INFINITE
Definition osclock.h:109
static constexpr rep until(const std::chrono::time_point< Clock, _Duration > &_time)
time point in ticks, rounded up to whole ticks (only a time point of this clock)
Definition osclock.h:116
create and initialize a timer object
Definition ostimer.h:673
Timer()
constructor, see the constructor parameters of the class
Definition ostimer.h:675
static Timer StartFor(const T &_delay)
create and initialize static timer object and start one-shot timer for given duration of time
Definition ostimer.h:814
std::unique_ptr< Timer > Ptr
result type of Create()
Definition ostimer.h:935
static Timer StartPeriodic(const T &_period)
create and initialize static timer object and start periodic timer for given duration of time
Definition ostimer.h:862
static Timer Start(const T &_delay, const T &_period)
create and initialize static timer object and start periodic timer for given duration of time
Definition ostimer.h:768
static Ptr Create()
create and initialize dynamic timer with manageable resources
Definition ostimer.h:954
static Timer StartUntil(const T &_time)
create and initialize static timer object and start one-shot timer until given timepoint
Definition ostimer.h:906
static Timer Make()
create and initialize static timer object
Definition ostimer.h:717