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