Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 1 | #ifndef AOS_COMMON_INPUT_DRIVER_STATION_DATA_H_ |
| 2 | #define AOS_COMMON_INPUT_DRIVER_STATION_DATA_H_ |
| 3 | |
| 4 | // This file defines several types to support nicely looking at the data |
| 5 | // received from the driver's station. |
| 6 | |
| 7 | #include <memory> |
| 8 | |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 9 | #include "aos/common/messages/robot_state.q.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
| 12 | namespace input { |
| 13 | namespace driver_station { |
| 14 | |
| 15 | // Represents a feature of a joystick (a button or an axis). |
| 16 | // All indices are 1-based. |
| 17 | class JoystickFeature { |
| 18 | public: |
| 19 | JoystickFeature(int joystick, int number) |
| 20 | : joystick_(joystick), number_(number) {} |
| 21 | |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 22 | // How many joysticks there are. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 23 | static const int kJoysticks = sizeof(JoystickState::joysticks) / |
| 24 | sizeof(JoystickState::joysticks[0]); |
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. |
| 44 | static const int kButtons = 12; |
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: |
| 52 | POVLocation(int joystick, int number) |
| 53 | : JoystickFeature(joystick, number) {} |
| 54 | }; |
| 55 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 56 | // Represents various bits of control information that the DS sends. |
| 57 | // Use Data to actually get the value. |
| 58 | enum class ControlBit { |
| 59 | kTestMode, kFmsAttached, kAutonomous, kEnabled |
| 60 | }; |
| 61 | |
| 62 | // Represents a single axis of a joystick. |
| 63 | // Use Data to actually get the value. |
| 64 | // Safe for static initialization. |
| 65 | class JoystickAxis : public JoystickFeature { |
| 66 | public: |
| 67 | JoystickAxis(int joystick, int number) |
| 68 | : JoystickFeature(joystick, number) {} |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 69 | |
| 70 | // How many axes there are available on each joystick. |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 71 | static const int kAxes = sizeof(Joystick::axis) / |
| 72 | sizeof(Joystick::axis[0]); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | class Data { |
| 76 | public: |
| 77 | // Initializes the data to all buttons and control bits off and all joysticks |
| 78 | // at 0. |
| 79 | Data(); |
| 80 | |
| 81 | // Updates the current information with a new set of values. |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 82 | void Update(const JoystickState &new_values); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 83 | |
Austin Schuh | fee2e60 | 2015-03-08 18:26:05 -0700 | [diff] [blame] | 84 | bool IsPressed(POVLocation location) const; |
| 85 | bool PosEdge(POVLocation location) const; |
| 86 | bool NegEdge(POVLocation location) const; |
| 87 | |
Brian Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame^] | 88 | // Returns the current and previous "values" for the POV. |
| 89 | int32_t GetPOV(int joystick) const; |
| 90 | int32_t GetOldPOV(int joystick) const; |
| 91 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 92 | bool IsPressed(ButtonLocation location) const; |
| 93 | bool PosEdge(ButtonLocation location) const; |
| 94 | bool NegEdge(ButtonLocation location) const; |
| 95 | |
| 96 | bool GetControlBit(ControlBit bit) const; |
| 97 | bool PosEdge(ControlBit bit) const; |
| 98 | bool NegEdge(ControlBit bit) const; |
| 99 | |
| 100 | // Returns the value in the range [-1.0, 1.0]. |
| 101 | float GetAxis(JoystickAxis axis) const; |
| 102 | |
| 103 | private: |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 104 | JoystickState current_values_, old_values_; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace driver_station |
| 108 | } // namespace input |
| 109 | } // namespace aos |
| 110 | |
| 111 | #endif // AOS_COMMON_INPUT_DRIVER_STATION_DATA_H_ |