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