Embedded Artistry Framework
Embedded Systems C++ Framework
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
embvm::timer::Timer Class Referenceabstract

This class provides generic Timer interface definitions. More...

#include <timer.hpp>

Inheritance diagram for embvm::timer::Timer:
Inheritance graph

Public Member Functions

timer_period_t period () const noexcept
 Get the current timer period. More...
 
timer_period_t period (timer_period_t period) noexcept
 Set the timer period. More...
 
virtual void registerCallback (const timer::cb_t &cb) noexcept=0
 Register a timer callback. More...
 
virtual void registerCallback (timer::cb_t &&cb) noexcept=0
 Register a timer callback. More...
 
virtual void restart (const timer_period_t period) noexcept
 Restart the timer with a specific period. More...
 
void restart (const timer_period_t::rep period) noexcept
 Restart the timer with a specific period. More...
 
void restart () noexcept
 Restart the timer with the currently configured period. More...
 
virtual timer::state state () const noexcept
 Check the state of the timer. More...
 
virtual timer::config config () const noexcept
 Check the configuration of the timer. More...
 
virtual timer::config config (timer::config c) noexcept
 Set the timer configuration. More...
 
virtual timer_period_t count () const noexcept=0
 Read the current timer count. More...
 
void start () noexcept
 Start the driver. More...
 
void stop () noexcept
 Stop the driver. More...
 
type_safe::boolean started () const noexcept
 Check if the driver has been started. More...
 
constexpr embvm::DriverType_t DriverType () const noexcept
 Returns the registered type ID of the driver. More...
 
constexpr const std::string_view & name () const noexcept
 Provides a string_view reference of the driver name. More...
 
constexpr const char * name_cstr () const noexcept
 Provides a c-string version of the driver name. More...
 
virtual DriverBaseoperator++ () noexcept
 Increment operator is a no-op, but is used for iterator compatibility. More...
 

Static Public Member Functions

static constexpr embvm::DriverType type () noexcept
 Timer Driver Type ID. More...
 

Protected Member Functions

 Timer () noexcept
 Default constructor. More...
 
 Timer (const char *name) noexcept
 Construct an Timer with a C-string name. More...
 
 Timer (const std::string &name) noexcept
 Construct an Timer with a std::string name. More...
 
 Timer (const std::string_view &name) noexcept
 Construct an Timer with a std::string_view name. More...
 
 ~Timer () noexcept override
 Default destructor. More...
 
void start_ () noexcept override=0
 Derived classes override the start_ method to control driver-specific startup behavior. More...
 
void stop_ () noexcept override=0
 Derived classes override the start_ method to control driver-specific startup behavior. More...
 

Protected Attributes

timer_period_t period_ {0}
 The configured timer period. More...
 
timer::state state_ = timer::state::stopped
 The current state of the timer. More...
 
timer::config config_ = timer::config::oneshot
 The timer configuratoin. More...
 
type_safe::boolean started_ = false
 Tracks the driver state. More...
 
const std::string_view name_
 Name of the driver instance. More...
 
const embvm::DriverType_t type_
 Type ID of the driver instance. More...
 

Detailed Description

This class provides generic Timer interface definitions.

The Timer class defines the common interfaces for Timer drivers. This class can be inherited to provide specific Timer implementations.

Timers can be used with the embvm::TimerManager<> class to provide software timer support.

Define a Timer driver

To create a specialized Timer driver, derive from the Timer object:

class SimulatorTimer final : public timer::Timer
{ ... };

Derived classes must implement the following interfaces:

Derived classes must also implement pure virtual embvm::DriverBase functions:

The start_() function must implement the steps for starting the timer hardware. The stop_() function is responsible for stopping the timer hardware.

Using a Timer driver

Timers should be configured with a period(). If the default one-shot configuration is not desired, the config() can be used to change the timer operational mode.

A callback should be registered with the registerCallback() function. When the timer expires, the callback will be invoked. If the timer is configured as a one-shot, the timer will remain expired until start() or restart() is called. If a timer is configured as periodic, the timer will reload the period and re-arm itself.

A timer can be restarted (even while running) using the existing period or with a new period.

Timers can be stopped while they are running using the stop() function.

Software Timers with embvm::TimerManager<>

Timers can be used with the embvm::TimerManager<> class to provide software timer support.

SimulatorTimer timer;

The embvm::TimerManager will use the timer hardware driver as a master timer, enabling multiple software timers to be created and used while consuming only a single hardware resource.

Constructor & Destructor Documentation

◆ Timer() [1/4]

embvm::timer::Timer::Timer ( )
inlineprotectednoexcept

Default constructor.

Initializes the Timer instance with a generic name.

◆ Timer() [2/4]

embvm::timer::Timer::Timer ( const char *  name)
inlineexplicitprotectednoexcept

Construct an Timer with a C-string name.

Initializes the Timer instance with a name.

Parameters
nameThe name of the Timer instance.

◆ Timer() [3/4]

embvm::timer::Timer::Timer ( const std::string &  name)
inlineexplicitprotectednoexcept

Construct an Timer with a std::string name.

Initializes the Timer instance with a name.

Parameters
nameThe name of the Timer instance. Timer() uses a std::string_view, so the std::string must remain valid

◆ Timer() [4/4]

embvm::timer::Timer::Timer ( const std::string_view &  name)
inlineexplicitprotectednoexcept

Construct an Timer with a std::string_view name.

Initializes the Timer instance with a name.

Parameters
nameThe name of the Timer instance.

◆ ~Timer()

Timer::~Timer ( )
overrideprotecteddefaultnoexcept

Default destructor.

Member Function Documentation

◆ config() [1/2]

virtual timer::config embvm::timer::Timer::config ( ) const
inlinevirtualnoexcept

Check the configuration of the timer.

Returns
the current timer configuration.

References config_.

Referenced by embdrv::SimulatorTimer::SimulatorTimer().

◆ config() [2/2]

virtual timer::config embvm::timer::Timer::config ( timer::config  c)
inlinevirtualnoexcept

Set the timer configuration.

Parameters
cThe desired timer configuration (periodic or oneshot).
Returns
the current timer configuration.

References config_.

◆ count()

virtual timer_period_t embvm::timer::Timer::count ( ) const
pure virtualnoexcept

Read the current timer count.

Returns
the current count of the timer

Implemented in embdrv::SimulatorTimer.

◆ DriverType()

constexpr embvm::DriverType_t embvm::DriverBase::DriverType ( ) const
inlinenoexceptinherited

Returns the registered type ID of the driver.

When using DriverBase interfaces, clients can retrieve the registered driver type in order to up-cast to the correct interface.

The type is returned as a embvm::DriverType_t rather than a embvm::DriverType enum to work with custom user-defined types. Enforcing a embvm::DriverType return value would prevent clients from defining and using their own custom types.

Returns
the registered driver type as a embvm::DriverType_t value

References embvm::DriverBase::type_.

◆ name()

constexpr const std::string_view& embvm::DriverBase::name ( ) const
inlinenoexceptinherited

Provides a string_view reference of the driver name.

Returns
std::string_view ref containing the driver name.

References embvm::DriverBase::name_.

◆ name_cstr()

constexpr const char* embvm::DriverBase::name_cstr ( ) const
inlinenoexceptinherited

Provides a c-string version of the driver name.

Returns
c-string containing the driver name

References embvm::DriverBase::name_.

◆ operator++()

virtual DriverBase& embvm::DriverBase::operator++ ( )
inlinevirtualnoexceptinherited

Increment operator is a no-op, but is used for iterator compatibility.

◆ period() [1/2]

timer_period_t embvm::timer::Timer::period ( ) const
inlinenoexcept

Get the current timer period.

Returns
the currently configured timer period.

References period_.

Referenced by period(), restart(), and embdrv::SimulatorTimer::SimulatorTimer().

◆ period() [2/2]

timer_period_t embvm::timer::Timer::period ( timer_period_t  period)
inlinenoexcept

Set the timer period.

If the timer is currently running, this call has no effect. Use restart() instead.

Parameters
periodthe desired timer period.
Returns
the currently configured timer period.

References period(), and period_.

◆ registerCallback() [1/2]

virtual void embvm::timer::Timer::registerCallback ( const timer::cb_t cb)
pure virtualnoexcept

Register a timer callback.

The timer callback function is called whenever the timer expires.

Derived classes must implement this function. Derived classes can choose to support either a single callback or multiple callbacks.

Parameters
cbA callback to invoke when the timer expires.

Implemented in embdrv::SimulatorTimer.

◆ registerCallback() [2/2]

virtual void embvm::timer::Timer::registerCallback ( timer::cb_t &&  cb)
pure virtualnoexcept

Register a timer callback.

The timer callback function is called whenever the timer expires.

Derived classes must implement this function. Derived classes can choose to support either a single callback or multiple callbacks.

Parameters
cbA callback to invoke when the timer expires.

Implemented in embdrv::SimulatorTimer.

◆ restart() [1/3]

virtual void embvm::timer::Timer::restart ( const timer_period_t  period)
inlinevirtualnoexcept

Restart the timer with a specific period.

Parameters
periodThe desired timer period to reset the timer to use. Accepts a std::chrono value.

References period(), period_, embvm::DriverBase::start(), and embvm::DriverBase::stop().

◆ restart() [2/3]

void embvm::timer::Timer::restart ( const timer_period_t::rep  period)
inlinenoexcept

Restart the timer with a specific period.

Parameters
periodThe desired timer period to reset the timer to use. Accepts an integral count representing the period in microseonds.

References period(), and restart().

◆ restart() [3/3]

void embvm::timer::Timer::restart ( )
inlinenoexcept

Restart the timer with the currently configured period.

References period_.

Referenced by restart().

◆ start()

void embvm::DriverBase::start ( )
inlinenoexceptinherited

◆ start_()

void embvm::timer::Timer::start_ ( )
overrideprotectedpure virtualnoexcept

Derived classes override the start_ method to control driver-specific startup behavior.

(Template Method Pattern)

Implements embvm::DriverBase.

Implemented in embdrv::SimulatorTimer.

◆ started()

type_safe::boolean embvm::DriverBase::started ( ) const
inlinenoexceptinherited

Check if the driver has been started.

Returns
true if the driver is running (started), false if not running (stopped).

References embvm::DriverBase::started_.

Referenced by embdrv::aardvarkSPIMaster::configure_(), embdrv::aardvarkGPIOInput< 4 >::get(), embdrv::aardvarkGPIOOutput< 5 >::set(), embdrv::vl53l1x::start_(), embdrv::aardvarkAdapter::start_(), and embdrv::aardvarkAdapter::stop_().

◆ state()

virtual timer::state embvm::timer::Timer::state ( ) const
inlinevirtualnoexcept

Check the state of the timer.

Returns
the current timer state.

References state_.

◆ stop()

void embvm::DriverBase::stop ( )
inlinenoexceptinherited

◆ stop_()

void embvm::timer::Timer::stop_ ( )
overrideprotectedpure virtualnoexcept

Derived classes override the start_ method to control driver-specific startup behavior.

(Template Method Pattern)

Implements embvm::DriverBase.

Implemented in embdrv::SimulatorTimer.

◆ type()

static constexpr embvm::DriverType embvm::timer::Timer::type ( )
inlinestaticnoexcept

Timer Driver Type ID.

Returns
Timer type ID.

References embvm::TIMER.

Member Data Documentation

◆ config_

timer::config embvm::timer::Timer::config_ = timer::config::oneshot
protected

◆ name_

const std::string_view embvm::DriverBase::name_
protectedinherited

Name of the driver instance.

Referenced by embvm::DriverBase::name(), and embvm::DriverBase::name_cstr().

◆ period_

timer_period_t embvm::timer::Timer::period_ {0}
protected

◆ started_

type_safe::boolean embvm::DriverBase::started_ = false
protectedinherited

Tracks the driver state.

True if the driver has been started, false if it has been stopped or not yet started.

Referenced by embvm::DriverBase::start(), embvm::DriverBase::started(), and embvm::DriverBase::stop().

◆ state_

timer::state embvm::timer::Timer::state_ = timer::state::stopped
protected

◆ type_

const embvm::DriverType_t embvm::DriverBase::type_
protectedinherited

Type ID of the driver instance.

Referenced by embvm::DriverBase::DriverType().


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