PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType > Class Template Reference

Template class to implement a polled timer. More...

#include <PolledTimer.h>

Inheritance diagram for PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >:
Collaboration diagram for PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >:

Public Types

using TickType = typename Clock::TickType
 
using Margin = NanoTime::TimeConst< Clock, NanoTime::Microseconds, POLLED_TIMER_MARGIN_US >
 
- Public Types inherited from NanoTime::TimeSource< Clock, unit_, TimeType >
using Clock = Clock
 
using TimeType = TimeType
 
using TicksPerUnit = typename Clock::template TicksPerUnit< unit_ >
 
using TimeConst = TimeConst< Clock, unit_, time >
 
using TicksConst = TicksConst< Clock, ticks >
 
using MaxClockTime = typename Clock::template MaxTime< unit_ >
 Get the time corresponding to the maximum clock tick value. More...
 
- Public Types inherited from NanoTime::Clock< ClockDef, frequency_, TickType_, maxTicks_ >
using TickType = TickType_
 
template<uint64_t ticks>
using TicksConst = TicksConst< Clock, ticks >
 
template<Unit unit, uint64_t time>
using TimeConst = TimeConst< Clock, unit, time >
 
template<Unit unit>
using TicksPerUnit = std::ratio_divide< std::ratio< frequency_ >, UnitTickRatio< unit > >
 
template<Unit unit, typename TimeType >
using TimeSource = TimeSource< Clock, unit, TimeType >
 
template<typename T >
using Ticks = Ticks< Clock, T >
 
using MaxTicks = TicksConst< maxTicks_ >
 
template<Unit unit>
using MaxTime = typename MaxTicks::template TimeConst< unit >
 

Public Member Functions

 Timer (const TimeType &timeInterval=0)
 Create a Timer with optional expiry time. More...
 
void start ()
 Start the timer. More...
 
template<uint64_t timeInterval>
void reset ()
 Start the timer with a new expiry interval. More...
 
template<uint64_t timeInterval>
constexpr uint32_t checkTime ()
 Check the given time interval is valid and return the corresponding tick count. More...
 
bool reset (const TimeType &timeInterval)
 Start the timer with a new expiry interval. More...
 
bool resetTicks (const TimeType &interval)
 Start the timer with a new expiry interval. More...
 
void cancel ()
 Cancelling a timer means it will never expire. More...
 
TickType elapsedTicks () const
 Get elapsed ticks since start() was last called. More...
 
NanoTime::Time< TimeTypeelapsedTime () const
 Get elapsed time since start() was last called. More...
 
TickType remainingTicks () const
 Get ticks remaining until expiry. More...
 
NanoTime::Time< TimeTyperemainingTime () const
 Get time remaining until expiry. More...
 
bool canExpire () const
 
bool canWait () const
 
bool expired ()
 Determine if timer has expired. More...
 

Static Public Member Functions

static constexpr NanoTime::Unit unit ()
 
static constexpr TickType maxInterval ()
 
static constexpr Margin margin ()
 
- Static Public Member Functions inherited from NanoTime::TimeSource< Clock, unit_, TimeType >
static constexpr Unit unit ()
 
static constexpr BasicRatio32 ticksPerUnit ()
 Number of clock ticks per unit of time. More...
 
static constexpr MaxClockTime maxClockTime ()
 
static constexpr TimeConst< time > timeConst ()
 Obtain a TimeConst type representing the given time quantity. More...
 
static constexpr TicksConst< ticks > ticksConst ()
 Class template defining a fixed tick quantity. More...
 
static constexpr Time< TimeTypemaxCalcTime ()
 The maximum time value supported by timeToTicks without overflowing. More...
 
static constexpr Ticks< Clock, TimeTypemaxCalcTicks ()
 The maximum tick value supported by ticksToTime without overflowing. More...
 
static Ticks< Clock, TimeTypetimeToTicks (TimeType time)
 Get the number of ticks for a given time. More...
 
static constexpr uint64_t timeToTicks ()
 Get the number of ticks for a given time. More...
 
static constexpr uint64_t ticksToTime ()
 Get the time for a given number of clock ticks. More...
 
static Time< TimeTypeticksToTime (TimeType ticks)
 Get the time for a given number of clock ticks. More...
 
static String toString ()
 
- Static Public Member Functions inherited from NanoTime::Clock< ClockDef, frequency_, TickType_, maxTicks_ >
static constexpr const char * typeName ()
 
static constexpr uint32_t frequency ()
 
static constexpr MaxTicks maxTicks ()
 
template<Unit unit>
static constexpr MaxTime< unit > maxTime ()
 
static Ratio32 ticksPerUnit (Unit unit)
 Get ticks per unit as a Ratio object. More...
 
template<Unit unit, uint64_t time>
static constexpr TimeConst< unit, timetimeConst ()
 Class template defining a fixed time quantity. More...
 
template<uint64_t ticks>
static constexpr TicksConst< ticks > ticksConst ()
 Class template defining a fixed tick quantity. More...
 
template<Unit unit, typename TimeType >
static constexpr TimeSource< unit, TimeType > timeSource ()
 Create a Time Source for this Clock. More...
 
template<Unit unit, typename TimeType >
static Ticks< TimeType > timeToTicks (TimeType time)
 Get the number of ticks for a given time. More...
 
template<Unit unit, typename TimeType >
static Time< TimeType > ticksToTime (TimeType ticks)
 Get the time for a given number of clock ticks. More...
 
static String toString ()
 

Detailed Description

template<class Clock, NanoTime::Unit unit_, bool IsPeriodic, typename TimeType>
class PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >

Template class to implement a polled timer.

Template Parameters
ClockClock source
unitTime unit for tick/time conversions
IsPeriodic
TimeTypeVariable type to use (uint32_t or uint64_t)
Note
Intervals and expiry use 'tick' values which are very efficient, whereas 'time' values must be computed so are very slow (~100+ cycles for uint32_t, considerably more for uint64_t). The class keeps a note of a 'start' time (in ticks) used to determine the elapsed() return value. An 'interval' value may be given is specified, then the expired() method returns true.

If the interval is set to 0, the timer will expire immediately, and remain so: canWait() will return false. Call cancel() to prevent the timer from ever expiring: canExpire() will return false. Call start() to set the timer going with the previously set interval. Call reset() to set the timer with a new interval.

A periodic timer will automatically restart when expired. A one-shot timer will remain expired until reset.

Member Typedef Documentation

◆ Margin

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
using PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::Margin = NanoTime::TimeConst<Clock, NanoTime::Microseconds, POLLED_TIMER_MARGIN_US>

◆ TickType

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
using PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::TickType = typename Clock::TickType

Constructor & Destructor Documentation

◆ Timer()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::Timer ( const TimeType timeInterval = 0)
inline

Create a Timer with optional expiry time.

Parameters
timeIntervalRelative time until expiry

Member Function Documentation

◆ cancel()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
void PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::cancel ( )
inline

Cancelling a timer means it will never expire.

◆ canExpire()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
bool PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::canExpire ( ) const
inline

◆ canWait()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
bool PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::canWait ( ) const
inline

◆ checkTime()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
template<uint64_t timeInterval>
constexpr uint32_t PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::checkTime ( )
inlineconstexpr

Check the given time interval is valid and return the corresponding tick count.

Template Parameters
timeInterval
Return values
uint32_t
Note
If time interval is invalid fails compilation

◆ elapsedTicks()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
TickType PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::elapsedTicks ( ) const
inline

Get elapsed ticks since start() was last called.

◆ elapsedTime()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
NanoTime::Time<TimeType> PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::elapsedTime ( ) const
inline

Get elapsed time since start() was last called.

◆ expired()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
bool PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::expired ( )
inline

Determine if timer has expired.

Return values
bool

◆ margin()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
static constexpr Margin PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::margin ( )
inlinestaticconstexpr

◆ maxInterval()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
static constexpr TickType PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::maxInterval ( )
inlinestaticconstexpr

◆ remainingTicks()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
TickType PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::remainingTicks ( ) const
inline

Get ticks remaining until expiry.

◆ remainingTime()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
NanoTime::Time<TimeType> PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::remainingTime ( ) const
inline

Get time remaining until expiry.

◆ reset() [1/2]

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
template<uint64_t timeInterval>
void PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::reset ( )
inline

Start the timer with a new expiry interval.

Template Parameters
timeIntervalTime to expire after last call to start()

◆ reset() [2/2]

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
bool PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::reset ( const TimeType timeInterval)
inline

Start the timer with a new expiry interval.

Parameters
timeIntervalTime to expire after last call to start()
Return values
booltrue on success, false on failure
See also
See resetTicks()

◆ resetTicks()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
bool PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::resetTicks ( const TimeType interval)
inline

Start the timer with a new expiry interval.

Parameters
intervalClock ticks to expire after last call to start()
Return values
booltrue on success, false if interval is out of range
Note
If time interval is 0, timer will expire immediately, and if it exceeds the maximum interval the timer will never expire.

◆ start()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
void PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::start ( )
inline

Start the timer.

◆ unit()

template<class Clock , NanoTime::Unit unit_, bool IsPeriodic, typename TimeType >
static constexpr NanoTime::Unit PolledTimer::Timer< Clock, unit_, IsPeriodic, TimeType >::unit ( )
inlinestaticconstexpr

The documentation for this class was generated from the following file: