RBCX
Library for the RB3204-RBCX board with the ESP32 by RoboticsBrno.
Loading...
Searching...
No Matches
RBCXTimers.h
Go to the documentation of this file.
1#pragma once
2
3#include <esp_timer.h>
4
5#include <memory>
6#include <mutex>
7#include <vector>
8
9namespace rb {
10
11class Manager;
12class Timers;
13
14class Timers {
15public:
16 static constexpr uint16_t INVALID_ID = 0;
17
21 static void deleteFreeRtOsTimerTask();
22
23 static Timers& get();
24
34 uint16_t schedule(uint32_t period_ms, std::function<bool()> callback);
35
41 bool reset(uint16_t id, uint32_t period_ms);
42
48 bool cancel(uint16_t id);
49
55 bool stop(uint16_t id);
56
57 // returns true if the calling code is running in the timer task.
58 bool isOnTimerTask() const;
59
60private:
61 struct timer_t {
62 std::function<bool()> callback;
63 esp_timer_handle_t handle;
64 uint16_t id;
65
66 void swap(timer_t& o) {
67 callback.swap(o.callback);
68 std::swap(handle, o.handle);
69 std::swap(id, o.id);
70 }
71 };
72
73 static void timerCallback(void* handleVoid);
74
75 Timers();
76 ~Timers();
77
78 void cancelByIdxLocked(size_t idx);
79 uint16_t getFreeIdLocked();
80
81 std::vector<timer_t> m_timers;
82 std::recursive_mutex m_mutex;
83 uint16_t m_id_counter;
84};
85
86};
Definition: RBCXTimers.h:14
bool reset(uint16_t id, uint32_t period_ms)
Reset the timer with the given ID.
Definition: RBCXTimers.cpp:70
bool cancel(uint16_t id)
Cancel the timer with the given ID.
Definition: RBCXTimers.cpp:84
static void deleteFreeRtOsTimerTask()
If you don't plan to use FreeRTOS SW timers, call this to free up 2KB of heap.
Definition: RBCXTimers.cpp:14
uint16_t schedule(uint32_t period_ms, std::function< bool()> callback)
Schedule callback to fire after period (in millisecond).
Definition: RBCXTimers.cpp:44
static Timers & get()
Definition: RBCXTimers.cpp:19
static constexpr uint16_t INVALID_ID
Definition: RBCXTimers.h:16
bool isOnTimerTask() const
Definition: RBCXTimers.cpp:145
bool stop(uint16_t id)
Stop the timer with the given ID.
Definition: RBCXTimers.cpp:97
Definition: RBCXAngle.cpp:3