oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#include <ove/timer.h>
17#include <ove/types.hpp>
18
19#ifdef CONFIG_OVE_TIMER
20
21namespace ove {
22
31template <typename F>
32concept TimerCallback = std::convertible_to<F, ove_timer_fn>;
33
46class Timer {
47public:
62 template <typename F>
63 Timer(F callback, void *user_data, uint32_t period_ms,
64 bool one_shot = false)
65 requires TimerCallback<F>
66 {
67#ifdef CONFIG_OVE_ZERO_HEAP
68 int err = ove_timer_init(&handle_, &storage_, callback,
69 user_data, period_ms,
70 one_shot ? 1 : 0);
71#else
72 int err = ove_timer_create(&handle_, callback, user_data,
73 period_ms,
74 one_shot ? 1 : 0);
75#endif
76 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
77 }
78
83 if (!handle_) return;
84#ifdef CONFIG_OVE_ZERO_HEAP
85 ove_timer_deinit(handle_);
86#else
87 ove_timer_destroy(handle_);
88#endif
89 }
90
91 Timer(const Timer &) = delete;
92 Timer &operator=(const Timer &) = delete;
93
94#ifdef CONFIG_OVE_ZERO_HEAP
95 Timer(Timer &&) = delete;
96 Timer &operator=(Timer &&) = delete;
97#else
102 Timer(Timer &&other) noexcept : handle_(other.handle_) {
103 other.handle_ = nullptr;
104 }
105
111 Timer &operator=(Timer &&other) noexcept {
112 if (this != &other) {
113 if (handle_) ove_timer_destroy(handle_);
114 handle_ = other.handle_;
115 other.handle_ = nullptr;
116 }
117 return *this;
118 }
119#endif
120
125 [[nodiscard]] int start() {
126 return ove_timer_start(handle_);
127 }
128
133 [[nodiscard]] int stop() {
134 return ove_timer_stop(handle_);
135 }
136
141 [[nodiscard]] int reset() {
142 return ove_timer_reset(handle_);
143 }
144
149 bool valid() const { return handle_ != nullptr; }
150
155 ove_timer_t handle() const { return handle_; }
156
157private:
158 ove_timer_t handle_ = nullptr;
159#ifdef CONFIG_OVE_ZERO_HEAP
160 ove_timer_storage_t storage_ = {};
161#endif
162};
163
164} /* namespace ove */
165
166#endif /* CONFIG_OVE_TIMER */
RAII wrapper around an oveRTOS software timer.
Definition timer.hpp:46
int stop()
Stops the timer without resetting its period.
Definition timer.hpp:133
int start()
Starts the timer.
Definition timer.hpp:125
~Timer()
Destroys the timer, stopping it if running and releasing the kernel resource.
Definition timer.hpp:82
Timer & operator=(Timer &&other) noexcept
Move-assignment operator — transfers ownership of the kernel handle.
Definition timer.hpp:111
Timer(F callback, void *user_data, uint32_t period_ms, bool one_shot=false)
Constructs and initialises the timer.
Definition timer.hpp:63
ove_timer_t handle() const
Returns the raw oveRTOS timer handle.
Definition timer.hpp:155
int reset()
Restarts the timer, resetting the period countdown.
Definition timer.hpp:141
Timer(Timer &&other) noexcept
Move constructor — transfers ownership of the kernel handle.
Definition timer.hpp:102
bool valid() const
Returns true if the underlying kernel handle is non-null.
Definition timer.hpp:149
Concept satisfied by any callable convertible to ove_timer_fn.
Definition timer.hpp:32
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.