Skip to content
This repository was archived by the owner on May 3, 2026. It is now read-only.

Commit 450519c

Browse files
committed
add port.cpp
1 parent 84ce58e commit 450519c

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

include/pros/devices/port.hpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class SmartPort {
8888
* @return false the smart port is physical or invalid
8989
*/
9090
constexpr bool is_virtual() const {
91-
return m_index >= 21 && m_index <= 31;
91+
return m_index >= 21 && is_valid();
9292
}
9393

9494
/**
@@ -98,7 +98,7 @@ class SmartPort {
9898
* @return false the smart port is invalid
9999
*/
100100
constexpr bool is_valid() const {
101-
return m_index <= 31;
101+
return m_index < MAX_SMART_PORTS;
102102
}
103103

104104
/**
@@ -108,13 +108,9 @@ class SmartPort {
108108
*
109109
* @return pros::RecursiveMutex&
110110
*/
111-
pros::RecursiveMutex& get_mutex() const {
112-
if (this->is_valid()) {
113-
return m_mutexes.at(m_index);
114-
} else {
115-
return m_mutexes.at(32);
116-
}
117-
}
111+
pros::RecursiveMutex& get_mutex() const;
112+
113+
static constexpr uint8_t MAX_SMART_PORTS = 32; /**< the maximum number of smart ports */
118114

119115
private:
120116
/**
@@ -132,7 +128,7 @@ class SmartPort {
132128
* This array has 33 elements instead of 32 as you may expect. The 33rd mutex is used for
133129
* invalid ports.
134130
*/
135-
static std::array<pros::RecursiveMutex, 33> m_mutexes;
131+
static std::array<pros::RecursiveMutex, MAX_SMART_PORTS + 1> m_mutexes;
136132

137133
/**
138134
* @brief construct a Smart Port from an index

src/devices/port.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "pros/devices/port.hpp"
2+
3+
namespace zest {
4+
std::array<pros::RecursiveMutex, SmartPort::MAX_SMART_PORTS + 1> SmartPort::m_mutexes;
5+
6+
pros::RecursiveMutex& SmartPort::get_mutex() const {
7+
if (this->is_valid()) {
8+
return m_mutexes.at(m_index);
9+
} else {
10+
return m_mutexes.back();
11+
}
12+
}
13+
}; // namespace zest

0 commit comments

Comments
 (0)