blob: 752239d4407c39cd0ed1e4e1bfaa24992f56973d [file] [log] [blame]
Ravago Jones8c65c432023-03-25 17:35:39 -07001#include "frc971/input/redundant_joystick_data.h"
2
3#include "aos/logging/logging.h"
4
Stephan Pleinesf63bde82024-01-13 15:59:33 -08005namespace frc971::input::driver_station {
Ravago Jones8c65c432023-03-25 17:35:39 -07006
7RedundantData::RedundantData(const Data &data) : joystick_map_(), data_(data) {
8 // Start with a naive map.
9 for (int i = 0; i < JoystickFeature::kJoysticks; i++) {
Austin Schuhda192072023-04-09 17:35:35 -070010 joystick_map_.at(i) = -1;
Ravago Jones8c65c432023-03-25 17:35:39 -070011 }
12
13 for (int i = 0; i < JoystickFeature::kJoysticks; i++) {
14 ButtonLocation id_bit0_location(i + 1, kIdBit0Button);
15 ButtonLocation id_bit1_location(i + 1, kIdBit1Button);
16 ButtonLocation redundant_bit_location(i + 1, kRedundantBitButton);
17
18 int id_bit0 = data_.IsPressed(id_bit0_location);
19 int id_bit1 = data_.IsPressed(id_bit1_location);
20 int is_redundant = data_.IsPressed(redundant_bit_location);
21
22 // We don't care if this is the redundant or primary one. Pick the second
23 // one.
24 (void)is_redundant;
25
26 int packed_joystick_number = (id_bit1 << 1u) | (id_bit0 << 0u);
27
28 joystick_map_.at(packed_joystick_number) = i + 1;
29 }
30};
31
32int RedundantData::MapRedundantJoystick(int joystick) const {
Austin Schuhda192072023-04-09 17:35:35 -070033 if (joystick < 0 || joystick >= static_cast<int>(joystick_map_.size())) {
34 return -1;
35 }
Ravago Jones8c65c432023-03-25 17:35:39 -070036 return joystick_map_.at(joystick);
37}
38
39bool RedundantData::IsPressed(POVLocation location) const {
40 POVLocation mapped_location(MapRedundantJoystick(location.joystick()),
41 location.number());
42 return data_.IsPressed(mapped_location);
43}
44
45bool RedundantData::PosEdge(POVLocation location) const {
46 POVLocation mapped_location(MapRedundantJoystick(location.joystick()),
47 location.number());
48 return data_.PosEdge(mapped_location);
49}
50
51bool RedundantData::NegEdge(POVLocation location) const {
52 POVLocation mapped_location(MapRedundantJoystick(location.joystick()),
53 location.number());
54 return data_.NegEdge(mapped_location);
55}
56
57// Returns the current and previous "values" for the POV.
58int32_t RedundantData::GetPOV(int joystick) const {
59 return data_.GetPOV(MapRedundantJoystick(joystick));
60}
61
62int32_t RedundantData::GetOldPOV(int joystick) const {
63 return data_.GetOldPOV(MapRedundantJoystick(joystick));
64}
65
66bool RedundantData::IsPressed(ButtonLocation location) const {
67 ButtonLocation mapped_location(MapRedundantJoystick(location.joystick()),
68 location.number());
69 return data_.IsPressed(mapped_location);
70}
71
72bool RedundantData::PosEdge(ButtonLocation location) const {
73 ButtonLocation mapped_location(MapRedundantJoystick(location.joystick()),
74 location.number());
75 return data_.PosEdge(mapped_location);
76}
77
78bool RedundantData::NegEdge(ButtonLocation location) const {
79 ButtonLocation mapped_location(MapRedundantJoystick(location.joystick()),
80 location.number());
81 return data_.NegEdge(mapped_location);
82}
83
84bool RedundantData::GetControlBit(ControlBit bit) const {
85 return data_.GetControlBit(bit);
86}
87
88bool RedundantData::PosEdge(ControlBit bit) const { return data_.PosEdge(bit); }
89
90bool RedundantData::NegEdge(ControlBit bit) const { return data_.NegEdge(bit); }
91
92// Returns the value in the range [-1.0, 1.0].
93float RedundantData::GetAxis(JoystickAxis axis) const {
94 JoystickAxis mapped_location(MapRedundantJoystick(axis.joystick()),
95 axis.number());
96 return data_.GetAxis(mapped_location);
97}
98
Stephan Pleinesf63bde82024-01-13 15:59:33 -080099} // namespace frc971::input::driver_station