|
1 | 1 | #pragma once |
2 | 2 |
|
| 3 | +#include "pros/rtos.hpp" |
| 4 | + |
| 5 | +#include <array> |
3 | 6 | #include <cstdint> |
4 | 7 |
|
5 | 8 | namespace zest { |
@@ -55,7 +58,51 @@ class SmartPort { |
55 | 58 | return m_index; |
56 | 59 | } |
57 | 60 |
|
| 61 | + constexpr bool is_physical() const { |
| 62 | + return m_index <= 20; |
| 63 | + } |
| 64 | + |
| 65 | + constexpr bool is_virtual() const { |
| 66 | + return m_index >= 21 && m_index <= 31; |
| 67 | + } |
| 68 | + |
| 69 | + constexpr bool is_valid() const { |
| 70 | + return m_index <= 31; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @brief Get the mutex for the given port |
| 75 | + * |
| 76 | + * @todo decide whether this should go in a source file |
| 77 | + * |
| 78 | + * @return pros::RecursiveMutex& |
| 79 | + */ |
| 80 | + pros::RecursiveMutex& get_mutex() const { |
| 81 | + if (this->is_valid()) { |
| 82 | + return m_mutexes.at(m_index); |
| 83 | + } else { |
| 84 | + return *m_mutexes.end(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
58 | 88 | private: |
| 89 | + /** |
| 90 | + * @brief array of recursive mutexes, with 33 elements. |
| 91 | + * |
| 92 | + * The mutexes in this array are used to ensure thread-safety when using devices. |
| 93 | + * |
| 94 | + * The decision to use recursive mutexes was made so device drivers could be made simpler, and |
| 95 | + * performance is not a concern in this scenario. |
| 96 | + * |
| 97 | + * There are 22 physical smart ports, and 10 virtual smart ports, for a total of 32. |
| 98 | + * Virtual smart ports are used by the VEX SDK to represent devices like the battery or a |
| 99 | + * controller. |
| 100 | + * |
| 101 | + * This array has 33 elements instead of 32 as you may expect. The 33rd mutex is used for |
| 102 | + * invalid ports. |
| 103 | + */ |
| 104 | + static std::array<pros::RecursiveMutex, 33> m_mutexes; |
| 105 | + |
59 | 106 | /** |
60 | 107 | * @brief construct a Smart Port from an index |
61 | 108 | * |
|
0 commit comments