Alicat BASIS-2 Driver 1.0.0
API reference
Loading...
Searching...
No Matches
alicat_basis2_uart_interface.hpp
Go to the documentation of this file.
1
33#pragma once
34
35#include <cstdint>
36#include <cstddef>
37#include <type_traits>
38
39namespace alicat_basis2 {
40
50template <typename Derived>
52public:
57 void write(const uint8_t* data, std::size_t length) noexcept {
58 static_cast<Derived*>(this)->write(data, length);
59 }
60
65 std::size_t read(uint8_t* out, std::size_t max, uint32_t timeout_ms) noexcept {
66 return static_cast<Derived*>(this)->read(out, max, timeout_ms);
67 }
68
70 void flush_rx() noexcept {
71 static_cast<Derived*>(this)->flush_rx();
72 }
73
82 void delay_ms(uint32_t ms) noexcept {
83 if constexpr (HasDelay<Derived>::value) {
84 static_cast<Derived*>(this)->delay_ms_impl(ms);
85 } else {
86 (void)ms;
87 }
88 }
89
98 template <typename... Args>
99 void log(const char* tag, const char* fmt, Args... args) noexcept {
100 if constexpr (HasLog<Derived, Args...>::value) {
101 static_cast<Derived*>(this)->log_impl(tag, fmt, args...);
102 } else {
103 (void)tag;
104 (void)fmt;
105 ((void)args, ...);
106 }
107 }
108
109private:
110 template <typename, typename = void>
111 struct HasDelay : std::false_type {};
112 template <typename T>
113 struct HasDelay<T, std::void_t<decltype(std::declval<T&>().delay_ms_impl(uint32_t{0}))>>
114 : std::true_type {};
115
116 template <typename T, typename Void, typename... Args>
117 struct HasLogImpl : std::false_type {};
118 template <typename T, typename... Args>
119 struct HasLogImpl<T,
120 std::void_t<decltype(std::declval<T&>().log_impl(
121 static_cast<const char*>(nullptr), static_cast<const char*>(nullptr),
122 std::declval<Args>()...))>,
123 Args...> : std::true_type {};
124 template <typename T, typename... Args>
125 using HasLog = HasLogImpl<T, void, Args...>;
126};
127
128} // namespace alicat_basis2
CRTP base for the BASIS-2 serial transport.
Definition alicat_basis2_uart_interface.hpp:51
std::size_t read(uint8_t *out, std::size_t max, uint32_t timeout_ms) noexcept
Read up to max bytes into out with a per-call timeout.
Definition alicat_basis2_uart_interface.hpp:65
void delay_ms(uint32_t ms) noexcept
Sleep at least ms milliseconds.
Definition alicat_basis2_uart_interface.hpp:82
void log(const char *tag, const char *fmt, Args... args) noexcept
Optional logging hook, printf-style.
Definition alicat_basis2_uart_interface.hpp:99
void flush_rx() noexcept
Drop any unread bytes from the RX path.
Definition alicat_basis2_uart_interface.hpp:70
void write(const uint8_t *data, std::size_t length) noexcept
Send length bytes synchronously.
Definition alicat_basis2_uart_interface.hpp:57
Definition alicat_basis2.hpp:52