Skip to content

Commit dfe13ff

Browse files
committed
add mutex array to SmartPort class
1 parent 9f613f5 commit dfe13ff

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

include/pros/devices/port.hpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include "pros/rtos.hpp"
4+
5+
#include <array>
36
#include <cstdint>
47

58
namespace zest {
@@ -55,7 +58,51 @@ class SmartPort {
5558
return m_index;
5659
}
5760

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+
5888
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+
59106
/**
60107
* @brief construct a Smart Port from an index
61108
*

0 commit comments

Comments
 (0)