Ravago Jones | 8c65c43 | 2023-03-25 17:35:39 -0700 | [diff] [blame] | 1 | #include "frc971/input/redundant_joystick_data.h" |
| 2 | |
| 3 | #include "aos/logging/logging.h" |
| 4 | |
| 5 | namespace frc971 { |
| 6 | namespace input { |
| 7 | namespace driver_station { |
| 8 | |
| 9 | RedundantData::RedundantData(const Data &data) : joystick_map_(), data_(data) { |
| 10 | // Start with a naive map. |
| 11 | for (int i = 0; i < JoystickFeature::kJoysticks; i++) { |
Austin Schuh | da19207 | 2023-04-09 17:35:35 -0700 | [diff] [blame] | 12 | joystick_map_.at(i) = -1; |
Ravago Jones | 8c65c43 | 2023-03-25 17:35:39 -0700 | [diff] [blame] | 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 | |
| 34 | int RedundantData::MapRedundantJoystick(int joystick) const { |
Austin Schuh | da19207 | 2023-04-09 17:35:35 -0700 | [diff] [blame] | 35 | if (joystick < 0 || joystick >= static_cast<int>(joystick_map_.size())) { |
| 36 | return -1; |
| 37 | } |
Ravago Jones | 8c65c43 | 2023-03-25 17:35:39 -0700 | [diff] [blame] | 38 | return joystick_map_.at(joystick); |
| 39 | } |
| 40 | |
| 41 | bool RedundantData::IsPressed(POVLocation location) const { |
| 42 | POVLocation mapped_location(MapRedundantJoystick(location.joystick()), |
| 43 | location.number()); |
| 44 | return data_.IsPressed(mapped_location); |
| 45 | } |
| 46 | |
| 47 | bool RedundantData::PosEdge(POVLocation location) const { |
| 48 | POVLocation mapped_location(MapRedundantJoystick(location.joystick()), |
| 49 | location.number()); |
| 50 | return data_.PosEdge(mapped_location); |
| 51 | } |
| 52 | |
| 53 | bool 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. |
| 60 | int32_t RedundantData::GetPOV(int joystick) const { |
| 61 | return data_.GetPOV(MapRedundantJoystick(joystick)); |
| 62 | } |
| 63 | |
| 64 | int32_t RedundantData::GetOldPOV(int joystick) const { |
| 65 | return data_.GetOldPOV(MapRedundantJoystick(joystick)); |
| 66 | } |
| 67 | |
| 68 | bool RedundantData::IsPressed(ButtonLocation location) const { |
| 69 | ButtonLocation mapped_location(MapRedundantJoystick(location.joystick()), |
| 70 | location.number()); |
| 71 | return data_.IsPressed(mapped_location); |
| 72 | } |
| 73 | |
| 74 | bool RedundantData::PosEdge(ButtonLocation location) const { |
| 75 | ButtonLocation mapped_location(MapRedundantJoystick(location.joystick()), |
| 76 | location.number()); |
| 77 | return data_.PosEdge(mapped_location); |
| 78 | } |
| 79 | |
| 80 | bool RedundantData::NegEdge(ButtonLocation location) const { |
| 81 | ButtonLocation mapped_location(MapRedundantJoystick(location.joystick()), |
| 82 | location.number()); |
| 83 | return data_.NegEdge(mapped_location); |
| 84 | } |
| 85 | |
| 86 | bool RedundantData::GetControlBit(ControlBit bit) const { |
| 87 | return data_.GetControlBit(bit); |
| 88 | } |
| 89 | |
| 90 | bool RedundantData::PosEdge(ControlBit bit) const { return data_.PosEdge(bit); } |
| 91 | |
| 92 | bool RedundantData::NegEdge(ControlBit bit) const { return data_.NegEdge(bit); } |
| 93 | |
| 94 | // Returns the value in the range [-1.0, 1.0]. |
| 95 | float 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 |