11#pragma once
22
33#include " pros/devices/adi_expander.hpp"
4+ #include " pros/rtos.hpp"
45
56namespace zest {
67/* *
@@ -9,42 +10,65 @@ namespace zest {
910 */
1011class Brain {
1112 public:
12- // port collections
13- // there are 21 physical smart ports, 11 virtual smart ports, and 1 reserved virtual smart port
14- // for invalid ports. Totals 33.
15- static constinit std::array<SmartPort, 33 > ports;
16-
1713 // physical smart ports
18- // these references are simply a QoL feature
19- static constinit SmartPort& port_1;
20- static constinit SmartPort& port_2;
21- static constinit SmartPort& port_3;
22- static constinit SmartPort& port_4;
23- static constinit SmartPort& port_5;
24- static constinit SmartPort& port_6;
25- static constinit SmartPort& port_7;
26- static constinit SmartPort& port_8;
27- static constinit SmartPort& port_9;
28- static constinit SmartPort& port_10;
29- static constinit SmartPort& port_11;
30- static constinit SmartPort& port_12;
31- static constinit SmartPort& port_13;
32- static constinit SmartPort& port_14;
33- static constinit SmartPort& port_15;
34- static constinit SmartPort& port_16;
35- static constinit SmartPort& port_17;
36- static constinit SmartPort& port_18;
37- static constinit SmartPort& port_19;
38- static constinit SmartPort& port_20;
39- static constinit SmartPort& port_21;
14+ static constexpr SmartPort PORT_1 = SmartPort(1 );
15+ static constexpr SmartPort PORT_2 = SmartPort(2 );
16+ static constexpr SmartPort PORT_3 = SmartPort(3 );
17+ static constexpr SmartPort PORT_4 = SmartPort(4 );
18+ static constexpr SmartPort PORT_5 = SmartPort(5 );
19+ static constexpr SmartPort PORT_6 = SmartPort(6 );
20+ static constexpr SmartPort PORT_7 = SmartPort(7 );
21+ static constexpr SmartPort PORT_8 = SmartPort(8 );
22+ static constexpr SmartPort PORT_9 = SmartPort(9 );
23+ static constexpr SmartPort PORT_10 = SmartPort(10 );
24+ static constexpr SmartPort PORT_11 = SmartPort(11 );
25+ static constexpr SmartPort PORT_12 = SmartPort(12 );
26+ static constexpr SmartPort PORT_13 = SmartPort(13 );
27+ static constexpr SmartPort PORT_14 = SmartPort(14 );
28+ static constexpr SmartPort PORT_15 = SmartPort(15 );
29+ static constexpr SmartPort PORT_16 = SmartPort(16 );
30+ static constexpr SmartPort PORT_17 = SmartPort(17 );
31+ static constexpr SmartPort PORT_18 = SmartPort(18 );
32+ static constexpr SmartPort PORT_19 = SmartPort(19 );
33+ static constexpr SmartPort PORT_20 = SmartPort(20 );
34+ static constexpr SmartPort PORT_21 = SmartPort(21 );
4035
4136 // virtual smart ports
42- static constinit SmartPort& integrated_adi_port ;
43- static constinit SmartPort& battery_port ;
37+ static constexpr SmartPort INTEGRATED_ADI_PORT = SmartPort( 22 ) ;
38+ static constexpr SmartPort BATTERY_PORT = SmartPort( 25 ) ;
4439 // users may use port_invalid as a placeholder or temporary port
45- static constinit SmartPort& invalid_port ;
40+ static constexpr SmartPort INVALID_PORT = SmartPort( 33 ) ;
4641
4742 // all adi ports on the brain
48- static constinit AdiExpander adi;
43+ static constexpr AdiExpander adi = AdiExpander(INTEGRATED_ADI_PORT);
44+
45+ // TODO: conditionally declare functions below, to prevent users accidentally calling them
46+ // TODO: replace lock_all and unlock_all with a single function that returns an std::scoped_lock
47+
48+ /* *
49+ * @brief Get the mutex of a smart port
50+ *
51+ * @warning this function should not be used by typical users
52+ *
53+ * @param smart_port the smart port to get the mutex of
54+ * @return pros::RecursiveMutex&
55+ */
56+ static pros::RecursiveMutex& get_smart_port_mutex (SmartPort smart_port);
57+
58+ /* *
59+ * @brief lock all smart port mutexes
60+ *
61+ * uses std::lock to prevent deadlocks
62+ */
63+ static void smart_port_mutex_lock_all ();
64+
65+ /* *
66+ * @brief unlock all smart port mutexes
67+ *
68+ */
69+ static void smart_port_mutex_unlock_all ();
70+
71+ private:
72+ static constinit std::array<pros::RecursiveMutex, 33 > m_mutexes;
4973};
5074}; // namespace zest
0 commit comments