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

Commit 300d787

Browse files
committed
try using constinit
1 parent 2e16c0b commit 300d787

4 files changed

Lines changed: 70 additions & 61 deletions

File tree

include/pros/devices/adi_expander.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AdiExpander {
1616
*
1717
* @param port the smart port the ADI expander is connected to
1818
*/
19-
AdiExpander(SmartPort& port)
19+
consteval AdiExpander(SmartPort& port)
2020
: port_a(port, 'A'),
2121
port_b(port, 'B'),
2222
port_c(port, 'C'),
@@ -36,6 +36,7 @@ class AdiExpander {
3636
AdiPort port_f;
3737
AdiPort port_g;
3838
AdiPort port_h;
39+
// users may use port_invalid as a placeholder or temporary port
3940
AdiPort port_invalid;
4041

4142
SmartPort& smart_port;

include/pros/devices/brain.hpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,46 @@
33
#include "pros/devices/adi_expander.hpp"
44

55
namespace zest {
6+
/**
7+
* @brief
8+
*
9+
*/
610
class Brain {
711
public:
812
// port collections
9-
static std::array<SmartPort, 33> ports;
10-
static AdiExpander adi;
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+
static constinit AdiExpander adi;
1117

1218
// physical smart ports
13-
static SmartPort& port_1;
14-
static SmartPort& port_2;
15-
static SmartPort& port_3;
16-
static SmartPort& port_4;
17-
static SmartPort& port_5;
18-
static SmartPort& port_6;
19-
static SmartPort& port_7;
20-
static SmartPort& port_8;
21-
static SmartPort& port_9;
22-
static SmartPort& port_10;
23-
static SmartPort& port_11;
24-
static SmartPort& port_12;
25-
static SmartPort& port_13;
26-
static SmartPort& port_14;
27-
static SmartPort& port_15;
28-
static SmartPort& port_16;
29-
static SmartPort& port_17;
30-
static SmartPort& port_18;
31-
static SmartPort& port_19;
32-
static SmartPort& port_20;
33-
static SmartPort& port_21;
19+
// these references are simply a QoL feature
20+
static constinit SmartPort& port_1;
21+
static constinit SmartPort& port_2;
22+
static constinit SmartPort& port_3;
23+
static constinit SmartPort& port_4;
24+
static constinit SmartPort& port_5;
25+
static constinit SmartPort& port_6;
26+
static constinit SmartPort& port_7;
27+
static constinit SmartPort& port_8;
28+
static constinit SmartPort& port_9;
29+
static constinit SmartPort& port_10;
30+
static constinit SmartPort& port_11;
31+
static constinit SmartPort& port_12;
32+
static constinit SmartPort& port_13;
33+
static constinit SmartPort& port_14;
34+
static constinit SmartPort& port_15;
35+
static constinit SmartPort& port_16;
36+
static constinit SmartPort& port_17;
37+
static constinit SmartPort& port_18;
38+
static constinit SmartPort& port_19;
39+
static constinit SmartPort& port_20;
40+
static constinit SmartPort& port_21;
3441

3542
// virtual smart ports
36-
static SmartPort& integrated_adi_port;
37-
static SmartPort& battery_port;
38-
static SmartPort& invalid_port;
43+
static constinit SmartPort& integrated_adi_port;
44+
static constinit SmartPort& battery_port;
45+
// users may use port_invalid as a placeholder or temporary port
46+
static constinit SmartPort& invalid_port;
3947
};
4048
}; // namespace zest

include/pros/devices/port.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SmartPort {
2424
*
2525
* @return constexpr uint8_t
2626
*/
27-
uint8_t as_number() const {
27+
constexpr uint8_t as_number() const {
2828
return m_number;
2929
}
3030

@@ -33,7 +33,7 @@ class SmartPort {
3333
*
3434
* @return constexpr uint8_t
3535
*/
36-
uint8_t as_index() const {
36+
constexpr uint8_t as_index() const {
3737
return m_number - 1;
3838
}
3939

@@ -47,7 +47,7 @@ class SmartPort {
4747
* @brief construct a Smart Port from an index
4848
*
4949
*/
50-
SmartPort(uint8_t port_number)
50+
constexpr SmartPort(uint8_t port_number)
5151
: m_number(port_number) {}
5252

5353
uint8_t m_number;
@@ -71,7 +71,7 @@ class AdiPort {
7171
*
7272
* @return constexpr char
7373
*/
74-
char as_char() const {
74+
constexpr char as_char() const {
7575
// convert index to an uppercase letter
7676
return m_index + 'A';
7777
}
@@ -81,7 +81,7 @@ class AdiPort {
8181
*
8282
* @return constexpr uint8_t
8383
*/
84-
uint8_t as_index() const {
84+
constexpr uint8_t as_index() const {
8585
return m_index;
8686
}
8787

@@ -98,7 +98,7 @@ class AdiPort {
9898
* @param port must be uppercase, 'A' - 'H'
9999
* @return constexpr AdiPort
100100
*/
101-
AdiPort(SmartPort& host_port, char port)
101+
constexpr AdiPort(SmartPort& host_port, char port)
102102
: host_port(host_port),
103103
m_index(port - 'A') {}
104104

src/devices/brain.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22

33
namespace zest {
44
// collection of smart ports
5-
std::array<SmartPort, 33> Brain::ports = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
6-
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
7-
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
5+
constinit std::array<SmartPort, 33> Brain::ports = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
6+
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
7+
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
88

99
// adi ports integrated in the brain
10-
AdiExpander Brain::adi = AdiExpander(battery_port);
10+
constinit AdiExpander Brain::adi = AdiExpander(battery_port);
1111
// physical smart ports
12-
SmartPort& Brain::port_1 = ports.at(0);
13-
SmartPort& Brain::port_2 = ports.at(1);
14-
SmartPort& Brain::port_3 = ports.at(2);
15-
SmartPort& Brain::port_4 = ports.at(3);
16-
SmartPort& Brain::port_5 = ports.at(4);
17-
SmartPort& Brain::port_6 = ports.at(5);
18-
SmartPort& Brain::port_7 = ports.at(6);
19-
SmartPort& Brain::port_8 = ports.at(7);
20-
SmartPort& Brain::port_9 = ports.at(8);
21-
SmartPort& Brain::port_10 = ports.at(9);
22-
SmartPort& Brain::port_11 = ports.at(10);
23-
SmartPort& Brain::port_12 = ports.at(11);
24-
SmartPort& Brain::port_13 = ports.at(12);
25-
SmartPort& Brain::port_14 = ports.at(13);
26-
SmartPort& Brain::port_15 = ports.at(14);
27-
SmartPort& Brain::port_16 = ports.at(15);
28-
SmartPort& Brain::port_17 = ports.at(16);
29-
SmartPort& Brain::port_18 = ports.at(17);
30-
SmartPort& Brain::port_19 = ports.at(18);
31-
SmartPort& Brain::port_20 = ports.at(19);
32-
SmartPort& Brain::port_21 = ports.at(20);
12+
constinit SmartPort& Brain::port_1 = ports.at(0);
13+
constinit SmartPort& Brain::port_2 = ports.at(1);
14+
constinit SmartPort& Brain::port_3 = ports.at(2);
15+
constinit SmartPort& Brain::port_4 = ports.at(3);
16+
constinit SmartPort& Brain::port_5 = ports.at(4);
17+
constinit SmartPort& Brain::port_6 = ports.at(5);
18+
constinit SmartPort& Brain::port_7 = ports.at(6);
19+
constinit SmartPort& Brain::port_8 = ports.at(7);
20+
constinit SmartPort& Brain::port_9 = ports.at(8);
21+
constinit SmartPort& Brain::port_10 = ports.at(9);
22+
constinit SmartPort& Brain::port_11 = ports.at(10);
23+
constinit SmartPort& Brain::port_12 = ports.at(11);
24+
constinit SmartPort& Brain::port_13 = ports.at(12);
25+
constinit SmartPort& Brain::port_14 = ports.at(13);
26+
constinit SmartPort& Brain::port_15 = ports.at(14);
27+
constinit SmartPort& Brain::port_16 = ports.at(15);
28+
constinit SmartPort& Brain::port_17 = ports.at(16);
29+
constinit SmartPort& Brain::port_18 = ports.at(17);
30+
constinit SmartPort& Brain::port_19 = ports.at(18);
31+
constinit SmartPort& Brain::port_20 = ports.at(19);
32+
constinit SmartPort& Brain::port_21 = ports.at(20);
3333

3434
// virtual smart ports
35-
SmartPort& Brain::integrated_adi_port = ports.at(21);
36-
SmartPort& Brain::battery_port = ports.at(24);
37-
SmartPort& Brain::invalid_port = ports.at(32);
35+
constinit SmartPort& Brain::integrated_adi_port = ports.at(21);
36+
constinit SmartPort& Brain::battery_port = ports.at(24);
37+
constinit SmartPort& Brain::invalid_port = ports.at(32);
3838
} // namespace zest

0 commit comments

Comments
 (0)