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#include <ove/error.hpp>
19
20#ifdef CONFIG_OVE_TIMER
21
22namespace ove
23{
24
33template <typename F>
34concept TimerCallback = std::convertible_to<F, ove_timer_fn>;
35
48class Timer
49{
50 public:
65 template <typename F>
66 Timer(F callback, void *user_data, uint32_t period_ms, bool one_shot = false)
67 requires TimerCallback<F>
68 {
69#ifdef CONFIG_OVE_ZERO_HEAP
70 int err = ove_timer_init(&handle_, &storage_, callback, user_data, period_ms,
71 one_shot ? 1 : 0);
72#else
73 int err = ove_timer_create(&handle_, callback, user_data, period_ms,
74 one_shot ? 1 : 0);
75#endif
76 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
77 }
78
82 ~Timer() noexcept
83 {
84 if (!handle_)
85 return;
86#ifdef CONFIG_OVE_ZERO_HEAP
87 ove_timer_deinit(handle_);
88#else
89 ove_timer_destroy(handle_);
90#endif
91 }
92
93 Timer(const Timer &) = delete;
94 Timer &operator=(const Timer &) = delete;
95
96#ifdef CONFIG_OVE_ZERO_HEAP
97 Timer(Timer &&) = delete;
98 Timer &operator=(Timer &&) = delete;
99#else
104 Timer(Timer &&other) noexcept : handle_(other.handle_)
105 {
106 other.handle_ = nullptr;
107 }
108
114 Timer &operator=(Timer &&other) noexcept
115 {
116 if (this != &other) {
117 if (handle_)
118 ove_timer_destroy(handle_);
119 handle_ = other.handle_;
120 other.handle_ = nullptr;
121 }
122 return *this;
123 }
124#endif
125
131 [[nodiscard]] Result<void> start() noexcept
132 {
133 return from_rc(ove_timer_start(handle_));
134 }
135
141 [[nodiscard]] Result<void> stop() noexcept
142 {
143 return from_rc(ove_timer_stop(handle_));
144 }
145
151 [[nodiscard]] Result<void> reset() noexcept
152 {
153 return from_rc(ove_timer_reset(handle_));
154 }
155
160 bool valid() const
161 {
162 return handle_ != nullptr;
163 }
164
169 ove_timer_t handle() const
170 {
171 return handle_;
172 }
173
174 private:
175 ove_timer_t handle_ = nullptr;
176#ifdef CONFIG_OVE_ZERO_HEAP
177 ove_timer_storage_t storage_ = {};
178#endif
179};
180
181} /* namespace ove */
182
183#endif /* CONFIG_OVE_TIMER */
RAII wrapper around an oveRTOS software timer.
Definition timer.hpp:49
Result< void > reset() noexcept
Restarts the timer, resetting the period countdown.
Definition timer.hpp:151
Timer & operator=(Timer &&other) noexcept
Move-assignment operator — transfers ownership of the kernel handle.
Definition timer.hpp:114
Result< void > start() noexcept
Starts the timer.
Definition timer.hpp:131
Result< void > stop() noexcept
Stops the timer without resetting its period.
Definition timer.hpp:141
Timer(F callback, void *user_data, uint32_t period_ms, bool one_shot=false)
Constructs and initialises the timer.
Definition timer.hpp:66
ove_timer_t handle() const
Returns the raw oveRTOS timer handle.
Definition timer.hpp:169
Timer(Timer &&other) noexcept
Move constructor — transfers ownership of the kernel handle.
Definition timer.hpp:104
bool valid() const
Returns true if the underlying kernel handle is non-null.
Definition timer.hpp:160
~Timer() noexcept
Destroys the timer, stopping it if running and releasing the kernel resource.
Definition timer.hpp:82
Concept satisfied by any callable convertible to ove_timer_fn.
Definition timer.hpp:34
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:20
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
Common type definitions and concepts for the C++ wrapper layer.