1#ifndef Callback_Watchdog_h
2#define Callback_Watchdog_h
8#if THINGSBOARD_USE_ESP_TIMER
11#include <arduino-timer.h>
15#if THINGSBOARD_USE_ESP_TIMER
16constexpr char WATCHDOG_TIMER_NAME[] =
"watchdog_timer";
41 , m_oneshot_timer(nullptr)
49#if THINGSBOARD_USE_ESP_TIMER
53 (void)esp_timer_delete(m_oneshot_timer);
54 m_oneshot_timer =
nullptr;
64 , m_oneshot_timer(nullptr)
78 Callback::operator=(other);
80 (void)esp_timer_delete(m_oneshot_timer);
81 m_oneshot_timer =
nullptr;
89 void once(uint64_t
const & timeout_microseconds) {
90#if THINGSBOARD_USE_ESP_TIMER
92 (void)esp_timer_start_once(m_oneshot_timer, timeout_microseconds);
94 m_oneshot_timer.in(timeout_microseconds, &Callback_Watchdog::oneshot_timer_callback,
this);
100#if THINGSBOARD_USE_ESP_TIMER
101 (void)esp_timer_stop(m_oneshot_timer);
103 m_oneshot_timer.cancel();
107#if !THINGSBOARD_USE_ESP_TIMER
115 m_oneshot_timer.tick<
void>();
120#if THINGSBOARD_USE_ESP_TIMER
124 void create_timer() {
126 if (m_oneshot_timer !=
nullptr) {
130 const esp_timer_create_args_t oneshot_timer_args = {
131 .callback = &oneshot_timer_callback,
133 .dispatch_method = esp_timer_dispatch_t::ESP_TIMER_TASK,
134 .name = WATCHDOG_TIMER_NAME,
135 .skip_unhandled_events =
false
138 const esp_err_t error = esp_timer_create(&oneshot_timer_args, &m_oneshot_timer);
139 if (error != ESP_OK) {
145#if THINGSBOARD_USE_ESP_TIMER
150 oneshot_timer_callback(
void *arg) {
151 if (arg ==
nullptr) {
152#if THINGSBOARD_USE_ESP_TIMER
161#if !THINGSBOARD_USE_ESP_TIMER
166#if THINGSBOARD_USE_ESP_TIMER
167 esp_timer_handle_t m_oneshot_timer = {};
169 Timer<1, micros> m_oneshot_timer = {};
#define THINGSBOARD_USE_ESP_TIMER
Definition: Configuration.h:69
Wrapper class which allows to start a timer and if it is not stopped in the given time then the inter...
Definition: Callback_Watchdog.h:31
void once(uint64_t const &timeout_microseconds)
Starts the watchdog timer once for the given timeout.
Definition: Callback_Watchdog.h:89
void detach()
Stops the currently ongoing watchdog timer and ensures the callback is not called....
Definition: Callback_Watchdog.h:99
Callback_Watchdog()=default
Constructs empty timeout timer callback, will result in never being called. Internals are simply defa...
void update()
Internally checks if the time already passed, has to be done because we are using a simple software t...
Definition: Callback_Watchdog.h:114
Callback_Watchdog(function callback)
Constructs callback, will be called if the timeout time passes without detach() being called beforeha...
Definition: Callback_Watchdog.h:38
General purpose safe callback wrapper. Expects either c-style or c++ style function pointer,...
Definition: Callback.h:30
std::function< void(argument_types... arguments)> function
Callback signature.
Definition: Callback.h:34
return_type Call_Callback(argument_types const &... arguments) const
Calls the callback that was subscribed, when this class instance was initally created.
Definition: Callback.h:62