Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
board_requirements.hpp
Go to the documentation of this file.
1/**
2 * @file board_requirements.hpp
3 * @brief Board role contracts: what a generated Board calls on each role
4 * @ingroup validation
5 *
6 * A board role is a component type a hardware manifest can declare (pot,
7 * cv_input, button, encoder, rgb_led). The generated Board reads the HAL and
8 * hands each role a sample; what the role does with it (smoothing, debounce,
9 * calibration) is the implementation's. SBL's implementations are the
10 * default bindings; a device, app or target can bind any type that meets the
11 * role's contract.
12 *
13 * ## Role Contracts
14 *
15 * - pot: `update(uint16_t raw)`
16 * - cv_input: `update(uint16_t raw)`; `set_electrical(const sbl::hal::adc::Electrical&)`,
17 * called once from Board::init() with the jack's manifest electrics
18 * - button: `update(bool level, uint32_t elapsed_ticks)`, the ticks the app's control clock counted
19 * since the update before, as the app handed them to the Board's update
20 * - encoder: `update(bool a, bool b)`
21 * - rgb_led: `state()` returning a value with `r`, `g`, `b` duty as `uint8_t`
22 *
23 * ## Validation Usage
24 *
25 * The generated Board asserts each role it uses:
26 * ```cpp
27 * static_assert(sbl::validation::board_button_valid<MyButton>, "button role");
28 * ```
29 */
30
31#ifndef SBL_HW_VALIDATION_BOARD_REQUIREMENTS_HPP_
32#define SBL_HW_VALIDATION_BOARD_REQUIREMENTS_HPP_
33
34#include <cstdint>
35#include <type_traits>
36#include <utility>
37
39
40namespace sbl {
41namespace validation {
42
43namespace detail {
44
45template<typename T, typename = void>
46struct updates_from_counts : std::false_type {};
47template<typename T>
48struct updates_from_counts<T, std::void_t<decltype(std::declval<T&>().update(std::declval<uint16_t>()))>>
49 : std::true_type {};
50
51template<typename T, typename = void>
52struct takes_electrical : std::false_type {};
53template<typename T>
54struct takes_electrical<
55 T, std::void_t<decltype(std::declval<T&>().set_electrical(std::declval<const sbl::hal::adc::Electrical&>()))>>
56 : std::true_type {};
57
58template<typename T, typename = void>
59struct updates_from_level : std::false_type {};
60template<typename T>
61struct updates_from_level<
62 T, std::void_t<decltype(std::declval<T&>().update(std::declval<bool>(), std::declval<uint32_t>()))>>
63 : std::true_type {};
64
65template<typename T, typename = void>
66struct updates_from_quadrature : std::false_type {};
67template<typename T>
68struct updates_from_quadrature<
69 T, std::void_t<decltype(std::declval<T&>().update(std::declval<bool>(), std::declval<bool>()))>> : std::true_type {
70};
71
72template<typename T, typename = void>
73struct has_rgb_state : std::false_type {};
74template<typename T>
75struct has_rgb_state<
76 T, std::void_t<decltype(std::declval<const T&>().state().r), decltype(std::declval<const T&>().state().g),
77 decltype(std::declval<const T&>().state().b)>>
78 : std::integral_constant<bool, std::is_convertible_v<decltype(std::declval<const T&>().state().r), uint8_t> &&
79 std::is_convertible_v<decltype(std::declval<const T&>().state().g), uint8_t> &&
80 std::is_convertible_v<decltype(std::declval<const T&>().state().b), uint8_t>> {};
81
82} // namespace detail
83
84/** @brief pot role: `update(uint16_t raw)` */
85template<typename T>
87public:
88 static constexpr bool validate() {
89 static_assert(detail::updates_from_counts<T>::value, "SBL ERROR: pot role type missing update().\n"
90 "Required: void update(uint16_t raw)");
91
92 return true;
93 }
94};
95
96template<typename T>
97constexpr bool board_pot_valid = PotRoleRequirements<T>::validate();
98
99/** @brief cv_input role: `update(uint16_t raw)`, `set_electrical(const Electrical&)` */
100template<typename T>
102public:
103 static constexpr bool validate() {
104 static_assert(detail::updates_from_counts<T>::value, "SBL ERROR: cv_input role type missing update().\n"
105 "Required: void update(uint16_t raw)");
106
107 static_assert(detail::takes_electrical<T>::value,
108 "SBL ERROR: cv_input role type missing set_electrical().\n"
109 "Required: void set_electrical(const sbl::hal::adc::Electrical&)");
110
111 return true;
112 }
113};
114
115template<typename T>
116constexpr bool board_cv_input_valid = CvInputRoleRequirements<T>::validate();
117
118/** @brief button role: `update(bool level, uint32_t elapsed_ticks)` */
119template<typename T>
121public:
122 static constexpr bool validate() {
123 static_assert(detail::updates_from_level<T>::value,
124 "SBL ERROR: button role type missing update().\n"
125 "Required: void update(bool level, uint32_t elapsed_ticks)");
126
127 return true;
128 }
129};
130
131template<typename T>
132constexpr bool board_button_valid = ButtonRoleRequirements<T>::validate();
133
134/** @brief encoder role: `update(bool a, bool b)` */
135template<typename T>
137public:
138 static constexpr bool validate() {
139 static_assert(detail::updates_from_quadrature<T>::value, "SBL ERROR: encoder role type missing update().\n"
140 "Required: void update(bool a, bool b)");
141
142 return true;
143 }
144};
145
146template<typename T>
147constexpr bool board_encoder_valid = EncoderRoleRequirements<T>::validate();
148
149/** @brief rgb_led role: `state()` with `r`, `g`, `b` duty */
150template<typename T>
152public:
153 static constexpr bool validate() {
154 static_assert(detail::has_rgb_state<T>::value, "SBL ERROR: rgb_led role type missing state().\n"
155 "Required: state() returning r, g, b convertible to uint8_t");
156
157 return true;
158 }
159};
160
161template<typename T>
162constexpr bool board_rgb_led_valid = RgbLedRoleRequirements<T>::validate();
163
164} // namespace validation
165} // namespace sbl
166
167#endif // SBL_HW_VALIDATION_BOARD_REQUIREMENTS_HPP_
button role: update(bool level, uint32_t elapsed_ticks)
cv_input role: update(uint16_t raw), set_electrical(const Electrical&)
encoder role: update(bool a, bool b)
pot role: update(uint16_t raw)
rgb_led role: state() with r, g, b duty
Electrical description of an analog port - the jack-to-converter circuit.
Root namespace for all Sound Byte Libs code.