John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_INPUT_DRIVER_STATION_DATA_H_ |
| 2 | #define AOS_INPUT_DRIVER_STATION_DATA_H_ |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 3 | |
| 4 | // This file defines several types to support nicely looking at the data |
| 5 | // received from the driver's station. |
| 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include <array> |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 10 | #include "frc971/input/joystick_state_generated.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 11 | |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 12 | namespace frc971 { |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 13 | namespace input { |
| 14 | namespace driver_station { |
| 15 | |
| 16 | // Represents a feature of a joystick (a button or an axis). |
| 17 | // All indices are 1-based. |
| 18 | class JoystickFeature { |
| 19 | public: |
| 20 | JoystickFeature(int joystick, int number) |
| 21 | : joystick_(joystick), number_(number) {} |
| 22 | |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 23 | // How many joysticks there are. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | static const int kJoysticks = 6; |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 25 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 26 | // Which joystick number this is (1-based). |
| 27 | int joystick() const { return joystick_; } |
| 28 | // Which feature on joystick() this is (1-based). |
| 29 | int number() const { return number_; } |
| 30 | |
| 31 | private: |
| 32 | const int joystick_, number_; |
| 33 | }; |
| 34 | |
| 35 | // Represents the location of a button. |
| 36 | // Use Data to actually get the value. |
| 37 | // Safe for static initialization. |
| 38 | class ButtonLocation : public JoystickFeature { |
| 39 | public: |
| 40 | ButtonLocation(int joystick, int number) |
| 41 | : JoystickFeature(joystick, number) {} |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 42 | |
| 43 | // How many buttons there are available on each joystick. |
Austin Schuh | 75c6af4 | 2018-03-09 21:16:07 -0800 | [diff] [blame] | 44 | static const int kButtons = 16; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 47 | // Represents the direction of a POV on a joystick. |
| 48 | // Use Data to actually get the value. |
| 49 | // Safe for static initialization. |
| 50 | class POVLocation : public JoystickFeature { |
| 51 | public: |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 52 | POVLocation(int joystick, int number) : JoystickFeature(joystick, number) {} |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 55 | // Represents various bits of control information that the DS sends. |
| 56 | // Use Data to actually get the value. |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 57 | enum class ControlBit { kTestMode, kFmsAttached, kAutonomous, kEnabled }; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 58 | |
| 59 | // Represents a single axis of a joystick. |
| 60 | // Use Data to actually get the value. |
| 61 | // Safe for static initialization. |
| 62 | class JoystickAxis : public JoystickFeature { |
| 63 | public: |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 64 | JoystickAxis(int joystick, int number) : JoystickFeature(joystick, number) {} |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 65 | |
| 66 | // How many axes there are available on each joystick. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | static const int kAxes = 6; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | class Data { |
| 71 | public: |
| 72 | // Initializes the data to all buttons and control bits off and all joysticks |
| 73 | // at 0. |
| 74 | Data(); |
| 75 | |
| 76 | // Updates the current information with a new set of values. |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 77 | void Update(const aos::JoystickState *new_values); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 78 | |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 79 | bool IsPressed(POVLocation location) const; |
| 80 | bool PosEdge(POVLocation location) const; |
| 81 | bool NegEdge(POVLocation location) const; |
| 82 | |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame] | 83 | // Returns the current and previous "values" for the POV. |
| 84 | int32_t GetPOV(int joystick) const; |
| 85 | int32_t GetOldPOV(int joystick) const; |
| 86 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 87 | bool IsPressed(ButtonLocation location) const; |
| 88 | bool PosEdge(ButtonLocation location) const; |
| 89 | bool NegEdge(ButtonLocation location) const; |
| 90 | |
| 91 | bool GetControlBit(ControlBit bit) const; |
| 92 | bool PosEdge(ControlBit bit) const; |
| 93 | bool NegEdge(ControlBit bit) const; |
| 94 | |
| 95 | // Returns the value in the range [-1.0, 1.0]. |
| 96 | float GetAxis(JoystickAxis axis) const; |
| 97 | |
| 98 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 99 | struct SavedJoystickState { |
| 100 | struct SavedJoystick { |
| 101 | uint16_t buttons = 0; |
| 102 | std::array<double, JoystickAxis::kAxes> axis; |
| 103 | int pov = 0; |
| 104 | }; |
| 105 | |
| 106 | std::array<SavedJoystick, JoystickFeature::kJoysticks> joysticks; |
| 107 | bool test_mode = false; |
| 108 | bool fms_attached = false; |
| 109 | bool enabled = false; |
| 110 | bool autonomous = false; |
| 111 | uint16_t team_id = 0; |
| 112 | |
| 113 | // 2018 scale and switch positions. |
| 114 | bool switch_left = false; |
| 115 | bool scale_left = false; |
| 116 | }; |
| 117 | |
| 118 | static bool GetButton(const ButtonLocation location, |
| 119 | const Data::SavedJoystickState &values); |
| 120 | static bool DoGetPOV(const POVLocation location, |
| 121 | const Data::SavedJoystickState &values); |
| 122 | |
| 123 | static bool GetControlBitValue(const ControlBit bit, |
| 124 | const Data::SavedJoystickState &values); |
| 125 | |
| 126 | SavedJoystickState current_values_, old_values_; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } // namespace driver_station |
| 130 | } // namespace input |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 131 | } // namespace frc971 |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 132 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 133 | #endif // AOS_INPUT_DRIVER_STATION_DATA_H_ |