FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
timer::RTT< NTIMER_ > Class Template Reference

API to handle a real-time timer. More...

#include <fastarduino/realtime_timer.h>

Inheritance diagram for timer::RTT< NTIMER_ >:
Collaboration diagram for timer::RTT< NTIMER_ >:

Public Types

using RAW_TIME = RTTRawTime< TYPE >
 The adequate RTTRawTime type for this RTT. More...
 

Public Member Functions

 RTT (const RTT< NTIMER_ > &)=delete
 
RTT< NTIMER_ > & operator= (const RTT< NTIMER_ > &)=delete
 
 RTT ()
 Construct a new real-time timer handler and initializes its current time to 0ms. More...
 
uint32_t millis () const
 Elapsed time, in milliseconds, since this timer has started. More...
 
uint32_t millis_ () const
 Elapsed time, in milliseconds, since this timer has started. More...
 
void delay (uint32_t ms) const
 Delay program execution for the given amount of milliseconds. More...
 
uint16_t micros () const
 Compute the microseconds part (from 0 to 999) of the time that has elapsed, since this timer has started. More...
 
uint16_t micros_ () const
 Compute the microseconds part (from 0 to 999) of the time that has elapsed, since this timer has started. More...
 
time::RTTTime time () const
 Elapsed time, in milliseconds and microseconds, since this timer has started. More...
 
time::RTTTime time_ () const
 Elapsed time, in milliseconds and microseconds, since this timer has started. More...
 
RAW_TIME raw_time () const
 Elapsed time, in raw representation, since this timer has started. More...
 
RAW_TIME raw_time_ () const
 Elapsed time, in raw representation, since this timer has started. More...
 
void millis (uint32_t ms)
 Reset the current milliseconds count of this RTT to the given value. More...
 
void begin ()
 Start this real-time timer, hence elapsed time starts getting counted from then. More...
 
void begin_ ()
 Start this real-time timer, hence elapsed time starts getting counted from then. More...
 
void end ()
 Stop this real-time timer, hence time gets not counted anymore. More...
 
void end_ ()
 Stop this real-time timer, hence time gets not counted anymore. More...
 
Timer< NTIMER > & timer ()
 Get a reference to the underlying Timer of this RTT.
 

Static Public Attributes

static constexpr const board::Timer NTIMER = NTIMER_
 The AVR timer used by this RTT.
 

Friends

struct isr_handler_rtt
 

Detailed Description

template<board::Timer NTIMER_>
class timer::RTT< NTIMER_ >

API to handle a real-time timer.

A real-time timer keeps track of time with micro-second precision. In order to perform properly, an appropriate ISR must be registered for it.

A real-time timer can be used to:

  • capture the duration of some event with good accuracy
  • implement timeouts in programs waiting for an event to occur
  • delay program execution for some us or ms
  • generate periodic events
Template Parameters
NTIMER_the AVR timer used by this RTT
See also
board::Timer
REGISTER_RTT_ISR()

Definition at line 203 of file realtime_timer.h.

Member Typedef Documentation

◆ RAW_TIME

template<board::Timer NTIMER_>
using timer::RTT< NTIMER_ >::RAW_TIME = RTTRawTime<TYPE>

The adequate RTTRawTime type for this RTT.

See also
RTTRawTime

Definition at line 222 of file realtime_timer.h.

Constructor & Destructor Documentation

◆ RTT()

template<board::Timer NTIMER_>
timer::RTT< NTIMER_ >::RTT ( )
inline

Construct a new real-time timer handler and initializes its current time to 0ms.

Note that this constructor does not start the timer.

See also
begin()
millis()

Definition at line 231 of file realtime_timer.h.

Member Function Documentation

◆ millis() [1/2]

template<board::Timer NTIMER_>
uint32_t timer::RTT< NTIMER_ >::millis ( ) const
inline

Elapsed time, in milliseconds, since this timer has started.

If millis(uint32_t) is called, this sets a new time reference point to count elapsed time from. If you want to get more precision about the elpased time, you can get the number of microsecond elapsed, in addition to millis(), by calling micros(). Note that this method is synchronized, i.e. it disables interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use millis_() instead.

Returns
elapsed time in ms
See also
millis(uint32_t)
micros()
millis_()

Definition at line 252 of file realtime_timer.h.

◆ millis_()

template<board::Timer NTIMER_>
uint32_t timer::RTT< NTIMER_ >::millis_ ( ) const
inline

Elapsed time, in milliseconds, since this timer has started.

If millis(uint32_t) is called, this sets a new time reference point to count elapsed time from. If you want to get more precision about the elpased time, you can get the number of microsecond elapsed, in addition to millis(), by calling micros(). Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use millis() instead.

Returns
elapsed time in ms
See also
millis(uint32_t)
micros()
millis()

Definition at line 273 of file realtime_timer.h.

◆ delay()

template<board::Timer NTIMER_>
void timer::RTT< NTIMER_ >::delay ( uint32_t  ms) const
inline

Delay program execution for the given amount of milliseconds.

Contrarily to time::delay_ms(), this method does not perform a busy loop, but it calls time::yield() which will put the MCU in sleep mode but will be awakened every ms (by a timer interrupt) and check if required delay has elapsed already.

Parameters
msthe number of milliseconds to hold program execution
See also
time::yield()

Definition at line 288 of file realtime_timer.h.

◆ micros()

template<board::Timer NTIMER_>
uint16_t timer::RTT< NTIMER_ >::micros ( ) const
inline

Compute the microseconds part (from 0 to 999) of the time that has elapsed, since this timer has started.

The milliseconds part is provided by millis(). In general, you will not call this method unless you are sure the elapsed time is strictly less than 1ms. If you need the elapsed time with microsecond precision, then you should call time() which returns an time::RTTTime structure that contains both milliseconds and microseconds. Note that this method is synchronized, i.e. it disables interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use micros_() instead.

Returns
the us part of elapsed time
See also
millis()
time()
micros_()

Definition at line 312 of file realtime_timer.h.

◆ micros_()

template<board::Timer NTIMER_>
uint16_t timer::RTT< NTIMER_ >::micros_ ( ) const
inline

Compute the microseconds part (from 0 to 999) of the time that has elapsed, since this timer has started.

The milliseconds part is provided by millis(). In general, you will not call this method unless you are sure the elapsed time is strictly less than 1ms. If you need the elapsed time with microsecond precision, then you should call time() which returns an time::RTTTime structure that contains both milliseconds and microseconds. Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use micros() instead.

Returns
the us part of elapsed time
See also
millis()
time()
micros()

Definition at line 335 of file realtime_timer.h.

◆ time()

template<board::Timer NTIMER_>
time::RTTTime timer::RTT< NTIMER_ >::time ( ) const
inline

Elapsed time, in milliseconds and microseconds, since this timer has started.

If you do not need microsecond precision, you should instead use millis(). Note that this method is synchronized, i.e. it disables interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use time_() instead.

Returns
elapsed time in ms and us
See also
millis()
micros()
time_()

Definition at line 354 of file realtime_timer.h.

◆ time_()

template<board::Timer NTIMER_>
time::RTTTime timer::RTT< NTIMER_ >::time_ ( ) const
inline

Elapsed time, in milliseconds and microseconds, since this timer has started.

If you do not need microsecond precision, you should instead use millis(). Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use time() instead.

Returns
elapsed time in ms and us
See also
millis()
micros()
time()

Definition at line 373 of file realtime_timer.h.

◆ raw_time()

template<board::Timer NTIMER_>
RAW_TIME timer::RTT< NTIMER_ >::raw_time ( ) const
inline

Elapsed time, in raw representation, since this timer has started.

This method is a fast substitute for time(); instead of returning a time::RTTTime which takes longer to instantiate, it returns a simpler, faster RTTRawTime which contains the same information but unprocessed yet. Note that this method is synchronized, i.e. it disables interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use raw_time_() instead.

Returns
elapsed time in ms and us
See also
time()
raw_time_()
RTTRawTime

Definition at line 394 of file realtime_timer.h.

◆ raw_time_()

template<board::Timer NTIMER_>
RAW_TIME timer::RTT< NTIMER_ >::raw_time_ ( ) const
inline

Elapsed time, in raw representation, since this timer has started.

This method is a fast substitute for time_(); instead of returning a time::RTTTime which takes longer to instantiate, it returns a simpler, faster RTTRawTime which contains the same information but unprocessed yet. Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use raw_time() instead.

Returns
elapsed time in ms and us
See also
time_()
raw_time()
RTTRawTime

Definition at line 415 of file realtime_timer.h.

◆ millis() [2/2]

template<board::Timer NTIMER_>
void timer::RTT< NTIMER_ >::millis ( uint32_t  ms)
inline

Reset the current milliseconds count of this RTT to the given value.

Evey elapsed millisecond will then be added to this new value.

Parameters
msthe new millisecond start

Definition at line 425 of file realtime_timer.h.

◆ begin()

template<board::Timer NTIMER_>
void timer::RTT< NTIMER_ >::begin ( )
inline

Start this real-time timer, hence elapsed time starts getting counted from then.

Note that this method is synchronized, i.e. it disables interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use begin_() instead.

See also
end()
begin_()

Definition at line 445 of file realtime_timer.h.

◆ begin_()

template<board::Timer NTIMER_>
void timer::RTT< NTIMER_ >::begin_ ( )
inline

Start this real-time timer, hence elapsed time starts getting counted from then.

Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use begin() instead.

See also
end_()
begin()

Definition at line 460 of file realtime_timer.h.

◆ end()

template<board::Timer NTIMER_>
void timer::RTT< NTIMER_ >::end ( )
inline

Stop this real-time timer, hence time gets not counted anymore.

Note that this method is synchronized, i.e. it disables interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use end_() instead.

See also
begin()
end_()

Definition at line 475 of file realtime_timer.h.

◆ end_()

template<board::Timer NTIMER_>
void timer::RTT< NTIMER_ >::end_ ( )
inline

Stop this real-time timer, hence time gets not counted anymore.

Note that this method is synchronized, i.e. it disables interrupts during its call and restores interrupts on return. Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use end() instead.

See also
begin_()
end()

Definition at line 491 of file realtime_timer.h.


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