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 | |
| 22 | bool GetControlBitValue(const ControlBit bit, |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame^] | 23 | const JoystickState &values) { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 24 | switch (bit) { |
| 25 | case ControlBit::kTestMode: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 26 | return values.test_mode; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 27 | case ControlBit::kFmsAttached: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 28 | return values.fms_attached; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 29 | case ControlBit::kAutonomous: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 30 | return values.autonomous; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 31 | case ControlBit::kEnabled: |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 32 | return values.enabled; |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame] | 33 | default: |
| 34 | __builtin_unreachable(); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | } // namespace |
| 39 | |
| 40 | bool Data::IsPressed(const ButtonLocation location) const { |
| 41 | return GetButton(location, current_values_); |
| 42 | } |
| 43 | |
| 44 | bool Data::PosEdge(const ButtonLocation location) const { |
| 45 | return !GetButton(location, old_values_) && |
| 46 | GetButton(location, current_values_); |
| 47 | } |
| 48 | |
| 49 | bool Data::NegEdge(const ButtonLocation location) const { |
| 50 | return GetButton(location, old_values_) && |
| 51 | !GetButton(location, current_values_); |
| 52 | } |
| 53 | |
| 54 | bool Data::GetControlBit(const ControlBit bit) const { |
| 55 | return GetControlBitValue(bit, current_values_); |
| 56 | } |
| 57 | |
| 58 | bool Data::PosEdge(const ControlBit bit) const { |
| 59 | return !GetControlBitValue(bit, old_values_) && |
| 60 | GetControlBitValue(bit, current_values_); |
| 61 | } |
| 62 | |
| 63 | bool Data::NegEdge(const ControlBit bit) const { |
| 64 | return GetControlBitValue(bit, old_values_) && |
| 65 | !GetControlBitValue(bit, current_values_); |
| 66 | } |
| 67 | |
| 68 | float Data::GetAxis(JoystickAxis axis) const { |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 69 | return current_values_.joysticks[axis.joystick() - 1].axis[axis.number() - 1]; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } // namespace driver_station |
| 73 | } // namespace input |
| 74 | } // namespace aos |