James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 1 | #include "frc971/input/driver_station_data.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #include "glog/logging.h" |
| 4 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 5 | namespace frc971 { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 6 | namespace input { |
| 7 | namespace driver_station { |
| 8 | |
| 9 | Data::Data() : current_values_(), old_values_() {} |
| 10 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 11 | void Data::Update(const aos::JoystickState *new_values) { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 12 | old_values_ = current_values_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | CHECK(new_values->has_joysticks()); |
| 14 | CHECK_EQ(new_values->joysticks()->size(), current_values_.joysticks.size()); |
| 15 | for (size_t i = 0; i < current_values_.joysticks.size(); ++i) { |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 16 | const aos::Joystick *joystick = new_values->joysticks()->Get(i); |
| 17 | current_values_.joysticks[i].buttons = joystick->buttons(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | current_values_.joysticks[i].pov = joystick->pov(); |
| 19 | for (size_t j = 0; j < joystick->axis()->size(); ++j) { |
| 20 | current_values_.joysticks[i].axis[j] = joystick->axis()->Get(j); |
| 21 | } |
| 22 | |
| 23 | current_values_.joysticks[i].pov = joystick->pov(); |
| 24 | } |
| 25 | current_values_.test_mode = new_values->test_mode(); |
| 26 | current_values_.fms_attached = new_values->fms_attached(); |
| 27 | current_values_.enabled = new_values->enabled(); |
| 28 | current_values_.autonomous = new_values->autonomous(); |
| 29 | current_values_.team_id = new_values->team_id(); |
| 30 | current_values_.switch_left = new_values->switch_left(); |
| 31 | current_values_.scale_left = new_values->scale_left(); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | bool Data::GetButton(const ButtonLocation location, |
| 35 | const Data::SavedJoystickState &values) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 36 | if (location.joystick() < 0 || |
| 37 | location.joystick() > static_cast<int>(values.joysticks.size())) { |
| 38 | return false; |
| 39 | } |
Tyler Chatow | 78ffe03 | 2019-09-29 11:06:06 -0700 | [diff] [blame] | 40 | if (location.number() <= 0 || location.number() > 16) { |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 41 | return false; |
| 42 | } |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 43 | return values.joysticks[location.joystick() - 1].buttons & |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | (1 << (location.number() - 1)); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 47 | bool Data::DoGetPOV(const POVLocation location, |
| 48 | const Data::SavedJoystickState &values) { |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 49 | return values.joysticks[location.joystick() - 1].pov == location.number(); |
| 50 | } |
| 51 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 52 | bool Data::GetControlBitValue(const ControlBit bit, |
| 53 | const Data::SavedJoystickState &values) { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 54 | switch (bit) { |
| 55 | case ControlBit::kTestMode: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 56 | return values.test_mode; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 57 | case ControlBit::kFmsAttached: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 58 | return values.fms_attached; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 59 | case ControlBit::kAutonomous: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 60 | return values.autonomous; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 61 | case ControlBit::kEnabled: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 62 | return values.enabled; |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame] | 63 | default: |
| 64 | __builtin_unreachable(); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 68 | bool Data::IsPressed(const ButtonLocation location) const { |
| 69 | return GetButton(location, current_values_); |
| 70 | } |
| 71 | |
| 72 | bool Data::PosEdge(const ButtonLocation location) const { |
| 73 | return !GetButton(location, old_values_) && |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 74 | GetButton(location, current_values_); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool Data::NegEdge(const ButtonLocation location) const { |
| 78 | return GetButton(location, old_values_) && |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 79 | !GetButton(location, current_values_); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 82 | bool Data::IsPressed(const POVLocation location) const { |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame] | 83 | return DoGetPOV(location, current_values_); |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool Data::PosEdge(const POVLocation location) const { |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame] | 87 | return !DoGetPOV(location, old_values_) && |
| 88 | DoGetPOV(location, current_values_); |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool Data::NegEdge(const POVLocation location) const { |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame] | 92 | return DoGetPOV(location, old_values_) && |
| 93 | !DoGetPOV(location, current_values_); |
| 94 | } |
| 95 | |
| 96 | int32_t Data::GetPOV(int joystick) const { |
| 97 | return current_values_.joysticks[joystick - 1].pov; |
| 98 | } |
| 99 | |
| 100 | int32_t Data::GetOldPOV(int joystick) const { |
| 101 | return old_values_.joysticks[joystick - 1].pov; |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 104 | bool Data::GetControlBit(const ControlBit bit) const { |
| 105 | return GetControlBitValue(bit, current_values_); |
| 106 | } |
| 107 | |
| 108 | bool Data::PosEdge(const ControlBit bit) const { |
| 109 | return !GetControlBitValue(bit, old_values_) && |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 110 | GetControlBitValue(bit, current_values_); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool Data::NegEdge(const ControlBit bit) const { |
| 114 | return GetControlBitValue(bit, old_values_) && |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 115 | !GetControlBitValue(bit, current_values_); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | float Data::GetAxis(JoystickAxis axis) const { |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 119 | return current_values_.joysticks[axis.joystick() - 1].axis[axis.number() - 1]; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | } // namespace driver_station |
| 123 | } // namespace input |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 124 | } // namespace frc971 |