ESP-LiveControl  1.99.1
HTTP server, AJAX API backend and Vue.js web application implementing self-contained, zero-install WiFi remote control of hardware modules attached to the Espressif ESP32 SoC
Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
MultiTimer Class Reference

Ticker timer derivative allowing for a fixed number of repeated calls. More...

#include <multi_timer.hpp>

Inheritance diagram for MultiTimer:
Inheritance graph
[legend]
Collaboration diagram for MultiTimer:
Collaboration graph
[legend]

Classes

struct  type_identity
 

Public Types

template<class T >
using type_identity_t = typename type_identity< T >::type
 
using callback_with_arg_and_count_t = void(*)(void *, uint32_t)
 

Public Member Functions

template<typename TArg >
esp_err_t attach_static_ms (uint32_t milliseconds, uint32_t total_repeat_count, void(*callback)(type_identity_t< TArg >, uint32_t), TArg arg, bool first_tick_nodelay=false)
 Attach a free function, static member function or a non-capturing lambda function to the timer. More...
 
template<typename TArg >
esp_err_t attach_static_ms (uint32_t milliseconds, uint32_t total_repeat_count, void(*callback)(type_identity_t< TArg >), TArg arg, bool first_tick_nodelay=false)
 
esp_err_t attach_static_ms (uint32_t milliseconds, uint32_t total_repeat_count, callback_t callback, bool first_tick_nodelay=false)
 
void start ()
 
void start (uint32_t interval_ms)
 
void stop ()
 
esp_err_t stop_return_errors ()
 
void pause ()
 
esp_err_t pause_return_errors ()
 
void resume ()
 
esp_err_t resume_return_errors ()
 

Protected Member Functions

esp_err_t _attach_ms (uint32_t arg)
 
void _mutex_take ()
 
void _mutex_give ()
 

Protected Attributes

uint32_t _interval_ms
 
uint32_t _repeat_count_requested {1}
 
uint32_t _repeat_count {0}
 
callback_t _callback
 
uint32_t _orig_arg {0}
 
callback_with_arg_t _cb_lambda {nullptr}
 
bool _first_tick_nodelay {false}
 
SemaphoreHandle_t _reentry_mutex = NULL
 

Detailed Description

Ticker timer derivative allowing for a fixed number of repeated calls.

This also allows unlimited on-demand restarting of the already attached callback without deleting the existing timer first.

Like for the original Ticker.h version, all callbacks are invoked from "esp_timer" task, which is a high-priority task. For this reason, the callbacks should only perform a minimum amount of work and refer to other tasks via message passing to do any blocking action.

Member Function Documentation

◆ attach_static_ms()

template<typename TArg >
esp_err_t MultiTimer::attach_static_ms ( uint32_t  milliseconds,
uint32_t  total_repeat_count,
void(*)(type_identity_t< TArg >, uint32_t)  callback,
TArg  arg,
bool  first_tick_nodelay = false 
)
inline

Attach a free function, static member function or a non-capturing lambda function to the timer.

This timer is created without activating it.

  • It is activated by calling start().
  • It is stopped without detaching the callback by calling stop(), which also resets the number of repeats to its original value.
  • It can be paused by calling pause(), which does not reset the repeats.
  • After calling resume(), continues until total repeat count is reached.

The callback can receive zero, one or two arguments:

  • For no arguments, it is just called the preset number of times.
  • For one argument, this is a fixed value for all calls, typically the pointer to the object of a user class for accessing any member of it.
  • In case of two arguments, the second is an additional uint32_t value containing the current number of times the callback was called. (This wraps around after UINT32_MAX)

The macros TICKER_MEMBER_CALL() and TICKER_MEMBER_CALL_WITH_COUNT() are a short-cut for calling non-static member functions from a calling class object by inserting a lambda in place of the member function name:

class PrintFoo {
public:
MultiTimer timer;
const char *foo_str = "This is Foo Number: %d";
void print_foo(uint32_t i) {
printf(foo_str, i);
}
PrintFoo(uint32_t millisecs, uint32_t reps) {
timer.attach_static_ms(millisecs,
reps,
TICKER_MEMBER_CALL_WITH_COUNT(print_foo)
);
}
};
PrintFoo print_6x_foo{500, 6};
print_6x_foo.timer.start();
Ticker timer derivative allowing for a fixed number of repeated calls.
Definition: multi_timer.hpp:35
esp_err_t attach_static_ms(uint32_t milliseconds, uint32_t total_repeat_count, void(*callback)(type_identity_t< TArg >, uint32_t), TArg arg, bool first_tick_nodelay=false)
Attach a free function, static member function or a non-capturing lambda function to the timer.
Definition: multi_timer.hpp:123

==> Please note: This software timer is only relatively accurate. That menas, for each repeat, the timer is stopped and started again immediately if the total number of repeats is not yet reached. This means that for multiple repeats, each small timing error will sum up to a larger value. If you need accurate timing for a large (or infinite) number of repeats, please use the Ticker class which features the periodic attach_ms() which has better long-term accuracy.

Parameters
millisecondsTimer interval in milliseconds
total_repeat_countTimer is stopped after this many repeats
callbackCallback function to register into this timer
argNumeric arg or pointer to object, e.g. calling class instance
first_tick_nodelayIf set to true, call callback immediately when the start() function is invoked, the first tick counts as a normal repeat and is repeated until total repeat count is reached

◆ start()

void MultiTimer::start ( )
inline

Without return value, we don't get this out of the lambda without another class member..


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