7 #ifndef ZPP_INCLUDE_ZPP_TIMER_HPP
8 #define ZPP_INCLUDE_ZPP_TIMER_HPP
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/__assert.h>
15 #include <type_traits>
44 template<
class T_Rep1,
class T_Period1,
class T_Rep2,
class T_Period2>
45 void start(
const std::chrono::duration<T_Rep1, T_Period1>& duration,
46 const std::chrono::duration<T_Rep2, T_Period2>& period) noexcept
48 using namespace std::chrono;
58 template<
class T_Rep,
class T_Period>
59 void start(
const std::chrono::duration<T_Rep, T_Period>& duration) noexcept
61 using namespace std::chrono;
63 k_timer_start(&m_timer,
to_timeout(duration), K_NO_WAIT);
71 k_timer_stop(&m_timer);
81 return k_timer_status_get(&m_timer);
89 return k_timer_status_sync(&m_timer);
99 auto t = k_timer_remaining_ticks(&m_timer);
100 return std::chrono::nanoseconds(k_ticks_to_ns_floor64(t));
113 struct k_timer m_timer { };
127 template<
class T_ExpireCallback,
class T_StopCallback>
139 explicit timer(T_ExpireCallback ecb, T_StopCallback scb) noexcept
141 , m_expire_callback(ecb)
142 , m_stop_callback(scb)
144 k_timer_expiry_t ecb_func = [](
struct k_timer* t) noexcept {
145 auto self = get_user_data(t);
146 if (
self !=
nullptr) {
147 std::invoke(self->m_expire_callback,
self);
151 k_timer_stop_t scb_func = [](
struct k_timer* t) noexcept {
152 auto self = get_user_data(t);
153 if (
self !=
nullptr) {
154 std::invoke(self->m_stop_callback,
self);
162 static timer* get_user_data(
struct k_timer* t) noexcept
164 return static_cast<timer*
>(k_timer_user_data_get(t));
167 T_ExpireCallback m_expire_callback;
168 T_StopCallback m_stop_callback;
177 template<
class T_ExpireCallback>
190 , m_expire_callback(ecb)
192 k_timer_expiry_t ecb_func = [](
struct k_timer* t) noexcept {
193 auto self = get_user_data(t);
194 if (
self !=
nullptr) {
195 std::invoke(self->m_expire_callback,
self);
203 static basic_timer* get_user_data(
struct k_timer* t) noexcept
205 return static_cast<basic_timer*
>(k_timer_user_data_get(t));
208 T_ExpireCallback m_expire_callback;
245 template<
class T_ExpireCallback>
248 return basic_timer(std::forward<T_ExpireCallback>(ecb));
259 template<
class T_ExpireCallback,
class T_StopCallback>
260 inline auto make_timer(T_ExpireCallback&& ecb, T_StopCallback&& scb) noexcept
262 return timer(std::forward<T_ExpireCallback>(ecb), std::forward<T_StopCallback>(scb));
timer class with only an expire callback
basic_timer(T_ExpireCallback ecb) noexcept
construct timer with an expire callback
timer class with no callbacks used for syncing only
sync_timer() noexcept
constuctor for the sync timer
base class for the timer class
timer_base(timer_base &&)=delete
std::chrono::nanoseconds remaining_time() noexcept
Get remaining time.
timer_base & operator=(const timer_base &)=delete
void start(const std::chrono::duration< T_Rep, T_Period > &duration) noexcept
Start a single shot timer with duration.
void start(const std::chrono::duration< T_Rep1, T_Period1 > &duration, const std::chrono::duration< T_Rep2, T_Period2 > &period) noexcept
Start a timer with duration and period.
void stop() noexcept
Stop the timer.
timer_base & operator=(timer_base &&)=delete
auto sync() noexcept
sync with the timer
~timer_base()
Destructor that stops the timer.
auto status() noexcept
get the timer status
auto native_handle() noexcept
Zephyr native handle.
timer_base(const timer_base &)=delete
timer class with expire and stop callbacks
timer(T_ExpireCallback ecb, T_StopCallback scb) noexcept
construct timer with expire and stop callback
constexpr k_timeout_t to_timeout(const std::chrono::duration< T_Rep, T_Period > &d) noexcept
convert a duration to tick
auto make_timer() noexcept
create sync_timer object