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