blob: 32748564807239213eb9de643e620fdd88151e8e [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++) {
12 joystick_map_.at(i) = i;
13 }
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 {
35 return joystick_map_.at(joystick);
36}
37
38bool RedundantData::IsPressed(POVLocation location) const {
39 POVLocation mapped_location(MapRedundantJoystick(location.joystick()),
40 location.number());
41 return data_.IsPressed(mapped_location);
42}
43
44bool RedundantData::PosEdge(POVLocation location) const {
45 POVLocation mapped_location(MapRedundantJoystick(location.joystick()),
46 location.number());
47 return data_.PosEdge(mapped_location);
48}
49
50bool RedundantData::NegEdge(POVLocation location) const {
51 POVLocation mapped_location(MapRedundantJoystick(location.joystick()),
52 location.number());
53 return data_.NegEdge(mapped_location);
54}
55
56// Returns the current and previous "values" for the POV.
57int32_t RedundantData::GetPOV(int joystick) const {
58 return data_.GetPOV(MapRedundantJoystick(joystick));
59}
60
61int32_t RedundantData::GetOldPOV(int joystick) const {
62 return data_.GetOldPOV(MapRedundantJoystick(joystick));
63}
64
65bool RedundantData::IsPressed(ButtonLocation location) const {
66 ButtonLocation mapped_location(MapRedundantJoystick(location.joystick()),
67 location.number());
68 return data_.IsPressed(mapped_location);
69}
70
71bool RedundantData::PosEdge(ButtonLocation location) const {
72 ButtonLocation mapped_location(MapRedundantJoystick(location.joystick()),
73 location.number());
74 return data_.PosEdge(mapped_location);
75}
76
77bool RedundantData::NegEdge(ButtonLocation location) const {
78 ButtonLocation mapped_location(MapRedundantJoystick(location.joystick()),
79 location.number());
80 return data_.NegEdge(mapped_location);
81}
82
83bool RedundantData::GetControlBit(ControlBit bit) const {
84 return data_.GetControlBit(bit);
85}
86
87bool RedundantData::PosEdge(ControlBit bit) const { return data_.PosEdge(bit); }
88
89bool RedundantData::NegEdge(ControlBit bit) const { return data_.NegEdge(bit); }
90
91// Returns the value in the range [-1.0, 1.0].
92float RedundantData::GetAxis(JoystickAxis axis) const {
93 JoystickAxis mapped_location(MapRedundantJoystick(axis.joystick()),
94 axis.number());
95 return data_.GetAxis(mapped_location);
96}
97
98} // namespace driver_station
99} // namespace input
100} // namespace frc971