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 | |
| 9 | void Data::Update(const NetworkRobotJoysticks &new_values) { |
| 10 | old_values_ = current_values_; |
| 11 | current_values_ = new_values; |
| 12 | } |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | bool GetButton(const ButtonLocation location, |
| 17 | const NetworkRobotJoysticks &values) { |
| 18 | return values.joysticks[location.joystick() - 1].buttons & |
| 19 | (1 << (location.number() - 1)); |
| 20 | } |
| 21 | |
| 22 | bool GetControlBitValue(const ControlBit bit, |
| 23 | const NetworkRobotJoysticks &values) { |
| 24 | switch (bit) { |
| 25 | case ControlBit::kTestMode: |
| 26 | return values.control.test_mode(); |
| 27 | case ControlBit::kFmsAttached: |
| 28 | return values.control.fms_attached(); |
| 29 | case ControlBit::kAutonomous: |
| 30 | return values.control.autonomous(); |
| 31 | case ControlBit::kEnabled: |
| 32 | return values.control.enabled(); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | bool Data::IsPressed(const ButtonLocation location) const { |
| 39 | return GetButton(location, current_values_); |
| 40 | } |
| 41 | |
| 42 | bool Data::PosEdge(const ButtonLocation location) const { |
| 43 | return !GetButton(location, old_values_) && |
| 44 | GetButton(location, current_values_); |
| 45 | } |
| 46 | |
| 47 | bool Data::NegEdge(const ButtonLocation location) const { |
| 48 | return GetButton(location, old_values_) && |
| 49 | !GetButton(location, current_values_); |
| 50 | } |
| 51 | |
| 52 | bool Data::GetControlBit(const ControlBit bit) const { |
| 53 | return GetControlBitValue(bit, current_values_); |
| 54 | } |
| 55 | |
| 56 | bool Data::PosEdge(const ControlBit bit) const { |
| 57 | return !GetControlBitValue(bit, old_values_) && |
| 58 | GetControlBitValue(bit, current_values_); |
| 59 | } |
| 60 | |
| 61 | bool Data::NegEdge(const ControlBit bit) const { |
| 62 | return GetControlBitValue(bit, old_values_) && |
| 63 | !GetControlBitValue(bit, current_values_); |
| 64 | } |
| 65 | |
| 66 | float Data::GetAxis(JoystickAxis axis) const { |
| 67 | // TODO(brians): check this math against what our joysticks report as their |
| 68 | // logical minimums and maximums |
| 69 | return current_values_.joysticks[axis.joystick()].axes[axis.number()] / 127.0; |
| 70 | } |
| 71 | |
| 72 | } // namespace driver_station |
| 73 | } // namespace input |
| 74 | } // namespace aos |