Skip to content

Commit ad4bda4

Browse files
committed
add ADI expander support to AdiPort class
1 parent 189f2c1 commit ad4bda4

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

include/pros/devices/port.hpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <array>
66
#include <cstdint>
7+
#include <limits>
78

89
namespace zest {
910

@@ -145,6 +146,7 @@ constexpr auto PORT_18 = SmartPort::from_number(18);
145146
constexpr auto PORT_19 = SmartPort::from_number(19);
146147
constexpr auto PORT_20 = SmartPort::from_number(20);
147148
constexpr auto PORT_21 = SmartPort::from_number(21);
149+
constexpr auto INVALID_SMART_PORT = SmartPort::from_index(std::numeric_limits<uint8_t>::max());
148150
} // namespace ports
149151

150152
/**
@@ -155,6 +157,19 @@ constexpr auto PORT_21 = SmartPort::from_number(21);
155157
*/
156158
class AdiPort {
157159
public:
160+
// the default constructor is explicitly deleted to enforce the use of from_char and from_index
161+
AdiPort(SmartPort, uint8_t) = delete;
162+
163+
/**
164+
* @brief Construct an ADI Port on an ADI expander
165+
*
166+
* @param expander_port the port of the ADI expander
167+
* @param adi_port the ADI port. Can be lowercase or lowercase
168+
*/
169+
constexpr AdiPort(SmartPort expander_port, AdiPort adi_port)
170+
: m_expander_port(expander_port),
171+
m_index(adi_port.as_index()) {}
172+
158173
/**
159174
* @brief Create an ADI Port using its character
160175
*
@@ -205,7 +220,25 @@ class AdiPort {
205220
return m_index;
206221
}
207222

223+
/**
224+
* @brief Get the mutex for the given port
225+
*
226+
* @note if the expander port is not valid, a reference to a reserved mutex is returned.
227+
*
228+
* @return pros::RecursiveMutex&
229+
*/
230+
pros::RecursiveMutex& get_mutex() const {
231+
if (m_expander_port.as_number() > 22) {
232+
return ports::INVALID_SMART_PORT.get_mutex();
233+
} else {
234+
return m_expander_port.get_mutex();
235+
}
236+
}
237+
208238
private:
239+
// the integrated ADI ports on the brain are represented by the virtual port 22
240+
static constexpr auto INTEGRATED_PORT = SmartPort::from_number(22);
241+
209242
/**
210243
* @brief construct an ADI Port from an index
211244
*
@@ -214,8 +247,10 @@ class AdiPort {
214247
* prevent bugs by abstracting the port index.
215248
*/
216249
explicit constexpr AdiPort(uint8_t index)
217-
: m_index(index) {}
250+
: m_expander_port(INTEGRATED_PORT),
251+
m_index(index) {}
218252

253+
SmartPort m_expander_port;
219254
uint8_t m_index;
220255
};
221256

0 commit comments

Comments
 (0)