|
3 | 3 | #include <cstdint> |
4 | 4 |
|
5 | 5 | namespace zest { |
| 6 | + |
| 7 | +/** |
| 8 | + * @brief Smart Port class. Represents a Smart Port on the V5 brain. |
| 9 | + * |
| 10 | + * @details Smart Ports may be represented as a 1-indexed number or a 0-indexed number. While the |
| 11 | + * user expects a 1-indexed number (matching the labels on the brain), treating it as an index makes |
| 12 | + * development easier. This class abstracts that away so we don't have to worry about it. |
| 13 | + * |
| 14 | + */ |
6 | 15 | class SmartPort { |
7 | 16 | public: |
| 17 | + /** |
| 18 | + * @brief Construct a Smart Port from its number |
| 19 | + * |
| 20 | + * @note the smart port labelled "1" on the brain has a port number of 1 |
| 21 | + * |
| 22 | + * @param port_number the port number, 1-indexed |
| 23 | + * @return constexpr SmartPort |
| 24 | + */ |
8 | 25 | static constexpr SmartPort from_number(uint8_t port_number) { |
9 | 26 | return SmartPort(port_number - 1); |
10 | 27 | } |
11 | 28 |
|
| 29 | + /** |
| 30 | + * @brief Construct a Smart Port from its index |
| 31 | + * |
| 32 | + * @note the smart port labelled "1" on the brain has a port index of 0 |
| 33 | + * |
| 34 | + * @param port_index the port index, 0-indexed |
| 35 | + * @return constexpr SmartPort |
| 36 | + */ |
12 | 37 | static constexpr SmartPort from_index(uint8_t port_index) { |
13 | 38 | return SmartPort(port_index); |
14 | 39 | } |
15 | 40 |
|
| 41 | + /** |
| 42 | + * @brief Get the Smart Port as a number (1-indexed) |
| 43 | + * |
| 44 | + * @return constexpr uint8_t |
| 45 | + */ |
16 | 46 | constexpr uint8_t as_number() const { |
17 | 47 | return m_index + 1; |
18 | 48 | } |
19 | 49 |
|
| 50 | + /** |
| 51 | + * @brief Get the Smart Port as an index (0-indexed) |
| 52 | + * |
| 53 | + * @return constexpr uint8_t |
| 54 | + */ |
20 | 55 | constexpr uint8_t as_index() const { |
21 | 56 | return m_index; |
22 | 57 | } |
23 | 58 |
|
24 | 59 | private: |
25 | | - constexpr SmartPort(int port_index) |
| 60 | + /** |
| 61 | + * @brief construct a Smart Port from an index |
| 62 | + * |
| 63 | + * This constructor is private to enforce the use of the `from_number` and `from_index` member |
| 64 | + * functions. Having this construct be public defeats the purpose of this class, which is to |
| 65 | + * prevent bugs by abstracting the port index. |
| 66 | + */ |
| 67 | + explicit constexpr SmartPort(uint8_t port_index) |
26 | 68 | : m_index(port_index) {} |
27 | 69 |
|
28 | 70 | uint8_t m_index; |
|
0 commit comments