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