Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 1 | #include "aos/common/input/driver_station_data.h" |
| 2 | |
| 3 | namespace aos { |
| 4 | namespace input { |
| 5 | namespace driver_station { |
| 6 | |
| 7 | Data::Data() : current_values_(), old_values_() {} |
| 8 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 9 | void Data::Update(const JoystickState &new_values) { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 10 | old_values_ = current_values_; |
| 11 | current_values_ = new_values; |
| 12 | } |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | bool GetButton(const ButtonLocation location, |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 17 | const JoystickState &values) { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 18 | return values.joysticks[location.joystick() - 1].buttons & |
| 19 | (1 << (location.number() - 1)); |
| 20 | } |
| 21 | |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame^] | 22 | bool DoGetPOV(const POVLocation location, const JoystickState &values) { |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 23 | return values.joysticks[location.joystick() - 1].pov == location.number(); |
| 24 | } |
| 25 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 26 | bool GetControlBitValue(const ControlBit bit, |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 27 | const JoystickState &values) { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 28 | switch (bit) { |
| 29 | case ControlBit::kTestMode: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 30 | return values.test_mode; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 31 | case ControlBit::kFmsAttached: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 32 | return values.fms_attached; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 33 | case ControlBit::kAutonomous: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 34 | return values.autonomous; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 35 | case ControlBit::kEnabled: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 36 | return values.enabled; |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame] | 37 | default: |
| 38 | __builtin_unreachable(); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
| 44 | bool Data::IsPressed(const ButtonLocation location) const { |
| 45 | return GetButton(location, current_values_); |
| 46 | } |
| 47 | |
| 48 | bool Data::PosEdge(const ButtonLocation location) const { |
| 49 | return !GetButton(location, old_values_) && |
| 50 | GetButton(location, current_values_); |
| 51 | } |
| 52 | |
| 53 | bool Data::NegEdge(const ButtonLocation location) const { |
| 54 | return GetButton(location, old_values_) && |
| 55 | !GetButton(location, current_values_); |
| 56 | } |
| 57 | |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 58 | bool Data::IsPressed(const POVLocation location) const { |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame^] | 59 | return DoGetPOV(location, current_values_); |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool Data::PosEdge(const POVLocation location) const { |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame^] | 63 | return !DoGetPOV(location, old_values_) && |
| 64 | DoGetPOV(location, current_values_); |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool Data::NegEdge(const POVLocation location) const { |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame^] | 68 | return DoGetPOV(location, old_values_) && |
| 69 | !DoGetPOV(location, current_values_); |
| 70 | } |
| 71 | |
| 72 | int32_t Data::GetPOV(int joystick) const { |
| 73 | return current_values_.joysticks[joystick - 1].pov; |
| 74 | } |
| 75 | |
| 76 | int32_t Data::GetOldPOV(int joystick) const { |
| 77 | return old_values_.joysticks[joystick - 1].pov; |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 80 | bool Data::GetControlBit(const ControlBit bit) const { |
| 81 | return GetControlBitValue(bit, current_values_); |
| 82 | } |
| 83 | |
| 84 | bool Data::PosEdge(const ControlBit bit) const { |
| 85 | return !GetControlBitValue(bit, old_values_) && |
| 86 | GetControlBitValue(bit, current_values_); |
| 87 | } |
| 88 | |
| 89 | bool Data::NegEdge(const ControlBit bit) const { |
| 90 | return GetControlBitValue(bit, old_values_) && |
| 91 | !GetControlBitValue(bit, current_values_); |
| 92 | } |
| 93 | |
| 94 | float Data::GetAxis(JoystickAxis axis) const { |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 95 | return current_values_.joysticks[axis.joystick() - 1].axis[axis.number() - 1]; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namespace driver_station |
| 99 | } // namespace input |
| 100 | } // namespace aos |