HF-TMC51x0 Driver (TMC5130 & TMC5160) 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC51x0 (TMC5130 & TMC5160)
Loading...
Searching...
No Matches
tmc51x0_result.hpp
Go to the documentation of this file.
1
6#pragma once
7#include <cstddef>
8#include <cstdint>
9#include <tuple>
10#include <type_traits>
11#include <utility>
12
13namespace tmc51x0 {
14
33
34#ifndef TMC51X0_NO_ERROR_STRINGS
40inline const char *ErrorMessage(ErrorCode code) {
41 switch (code) {
42 case ErrorCode::OK:
43 return "OK";
45 return "Driver not initialized";
47 return "Communication error";
49 return "Invalid parameter value";
51 return "Invalid state for operation";
53 return "Operation timed out";
55 return "Operation cancelled";
57 return "Hardware fault detected";
59 return "Short circuit detected";
61 return "Open load detected";
63 return "Overtemperature warning";
65 return "Overtemperature shutdown";
67 return "Feature not supported";
68 default:
69 return "Unknown error";
70 }
71}
72#endif
73
90template <typename T> class Result {
91private:
94
95public:
99 explicit Result(T &&value) noexcept
100 : error_(ErrorCode::OK), value_(std::move(value)) {}
101
105 explicit Result(const T &value) noexcept
106 : error_(ErrorCode::OK), value_(value) {}
107
111 explicit Result(ErrorCode error) noexcept : error_(error), value_{} {}
112
126 [[nodiscard]] explicit operator bool() const noexcept {
127 return error_ == ErrorCode::OK;
128 }
129
134 [[nodiscard]] bool IsOk() const noexcept { return error_ == ErrorCode::OK; }
135
140 [[nodiscard]] bool IsErr() const noexcept { return error_ != ErrorCode::OK; }
141
146 [[nodiscard]] ErrorCode Error() const noexcept { return error_; }
147
148#ifndef TMC51X0_NO_ERROR_STRINGS
153 [[nodiscard]] const char *ErrorMessage() const noexcept {
155 }
156#endif
157
163 [[nodiscard]] T &Value() noexcept { return value_; }
164
170 [[nodiscard]] const T &Value() const noexcept { return value_; }
171
177 [[nodiscard]] T ValueOr(const T &default_value) const noexcept {
178 return IsOk() ? value_ : default_value;
179 }
180
184 template <std::size_t N>
185 std::enable_if_t<N == 0, ErrorCode> get() const & noexcept {
186 return error_;
187 }
188
189 template <std::size_t N>
190 std::enable_if_t<N == 1, const T &> get() const & noexcept {
191 return value_;
192 }
193
194 template <std::size_t N>
195 std::enable_if_t<N == 0, ErrorCode> get() & noexcept {
196 return error_;
197 }
198
199 template <std::size_t N> std::enable_if_t<N == 1, T &> get() & noexcept {
200 return value_;
201 }
202
203 template <std::size_t N>
204 std::enable_if_t<N == 0, ErrorCode> get() && noexcept {
205 return error_;
206 }
207
208 template <std::size_t N> std::enable_if_t<N == 1, T &&> get() && noexcept {
209 return std::move(value_);
210 }
211};
212
225template <> class Result<void> {
226private:
228
229public:
233 Result() noexcept : error_(ErrorCode::OK) {}
234
238 explicit Result(ErrorCode error) noexcept : error_(error) {}
239
254 [[nodiscard]] explicit operator bool() const noexcept {
255 return error_ == ErrorCode::OK;
256 }
257
262 [[nodiscard]] bool IsOk() const noexcept { return error_ == ErrorCode::OK; }
263
268 [[nodiscard]] bool IsErr() const noexcept { return error_ != ErrorCode::OK; }
269
274 [[nodiscard]] ErrorCode Error() const noexcept { return error_; }
275
276#ifndef TMC51X0_NO_ERROR_STRINGS
281 [[nodiscard]] const char *ErrorMessage() const noexcept {
283 }
284#endif
285};
286
287// Type alias for Result<void> to allow Result<> syntax
289
296template <std::size_t N, typename T>
297decltype(auto) get(Result<T> &r) noexcept {
298 return r.template get<N>();
299}
300
301template <std::size_t N, typename T>
302decltype(auto) get(const Result<T> &r) noexcept {
303 return r.template get<N>();
304}
305
306template <std::size_t N, typename T>
307decltype(auto) get(Result<T> &&r) noexcept {
308 return std::move(r).template get<N>();
309}
310
311// Result<void> is tuple-like with a single element: the ErrorCode.
312template <std::size_t N>
313std::enable_if_t<N == 0, ErrorCode> get(Result<void> &r) noexcept {
314 return r.Error();
315}
316
317template <std::size_t N>
318std::enable_if_t<N == 0, ErrorCode> get(const Result<void> &r) noexcept {
319 return r.Error();
320}
321
322template <std::size_t N>
323std::enable_if_t<N == 0, ErrorCode> get(Result<void> &&r) noexcept {
324 return r.Error();
325}
326
327} // namespace tmc51x0
328
329// Support for structured bindings
330namespace std {
331template <typename T>
332struct tuple_size<tmc51x0::Result<T>> : integral_constant<size_t, 2> {};
333
334template <>
335struct tuple_size<tmc51x0::Result<void>> : integral_constant<size_t, 1> {};
336
337template <typename T> struct tuple_element<0, tmc51x0::Result<T>> {
339};
340
341template <typename T> struct tuple_element<1, tmc51x0::Result<T>> {
342 using type = T;
343};
344
345template <> struct tuple_element<0, tmc51x0::Result<void>> {
347};
348} // namespace std
Result type for operations that don't return a value.
Definition tmc51x0_result.hpp:225
Result(ErrorCode error) noexcept
Construct an error result.
Definition tmc51x0_result.hpp:238
ErrorCode error_
Definition tmc51x0_result.hpp:227
Result() noexcept
Construct a successful result.
Definition tmc51x0_result.hpp:233
bool IsOk() const noexcept
Check if result is OK.
Definition tmc51x0_result.hpp:262
const char * ErrorMessage() const noexcept
Get human-readable error message.
Definition tmc51x0_result.hpp:281
bool IsErr() const noexcept
Check if result is an error.
Definition tmc51x0_result.hpp:268
ErrorCode Error() const noexcept
Get the error code.
Definition tmc51x0_result.hpp:274
Result type for operations that return a value.
Definition tmc51x0_result.hpp:90
std::enable_if_t< N==1, T && > get() &&noexcept
Definition tmc51x0_result.hpp:208
ErrorCode Error() const noexcept
Get the error code.
Definition tmc51x0_result.hpp:146
const char * ErrorMessage() const noexcept
Get human-readable error message.
Definition tmc51x0_result.hpp:153
const T & Value() const noexcept
Get the result value (const reference)
Definition tmc51x0_result.hpp:170
std::enable_if_t< N==0, ErrorCode > get() &noexcept
Definition tmc51x0_result.hpp:195
bool IsOk() const noexcept
Check if result is OK.
Definition tmc51x0_result.hpp:134
T & Value() noexcept
Get the result value (mutable reference)
Definition tmc51x0_result.hpp:163
bool IsErr() const noexcept
Check if result is an error.
Definition tmc51x0_result.hpp:140
std::enable_if_t< N==0, ErrorCode > get() &&noexcept
Definition tmc51x0_result.hpp:204
ErrorCode error_
Definition tmc51x0_result.hpp:92
std::enable_if_t< N==1, const T & > get() const &noexcept
Definition tmc51x0_result.hpp:190
Result(ErrorCode error) noexcept
Construct an error result.
Definition tmc51x0_result.hpp:111
Result(T &&value) noexcept
Construct a successful result with value.
Definition tmc51x0_result.hpp:99
std::enable_if_t< N==0, ErrorCode > get() const &noexcept
Support structured bindings: auto [err, value] = result;.
Definition tmc51x0_result.hpp:185
T ValueOr(const T &default_value) const noexcept
Get the result value or a default.
Definition tmc51x0_result.hpp:177
Result(const T &value) noexcept
Construct a successful result with value (copy)
Definition tmc51x0_result.hpp:105
std::enable_if_t< N==1, T & > get() &noexcept
Definition tmc51x0_result.hpp:199
T value_
Definition tmc51x0_result.hpp:93
Definition tmc51x0_result.hpp:330
Definition tmc51x0_register_defs.cpp:10
const char * ErrorMessage(ErrorCode code)
Get human-readable error message.
Definition tmc51x0_result.hpp:40
ErrorCode
Error codes for TMC51x0 operations.
Definition tmc51x0_result.hpp:18
@ SHORT_CIRCUIT
Short circuit detected.
@ NOT_INITIALIZED
Driver not initialized.
@ TIMEOUT
Operation timed out.
@ UNSUPPORTED
Feature not supported by this chip variant.
@ INVALID_STATE
Operation not valid in current state.
@ HARDWARE_ERROR
Hardware fault detected.
@ CANCELLED
Operation cancelled by user/request.
@ COMM_ERROR
Communication interface error (SPI/UART)
@ OVERTEMP_WARNING
Overtemperature warning threshold.
@ INVALID_VALUE
Invalid parameter value.
@ OK
Operation succeeded.
@ OVERTEMP_SHUTDOWN
Overtemperature shutdown.
@ OPEN_LOAD
Open load detected.
decltype(auto) get(Result< T > &r) noexcept
Tuple-like access for structured bindings (C++17)
Definition tmc51x0_result.hpp:297
T type
Definition tmc51x0_result.hpp:342