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 | |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame^] | 7 | #include <assert.h> |
| 8 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
| 11 | #include "aos/externals/WPILib/WPILib/NetworkRobot/NetworkRobotValues.h" |
| 12 | |
| 13 | namespace aos { |
| 14 | namespace input { |
| 15 | namespace driver_station { |
| 16 | |
| 17 | // Represents a feature of a joystick (a button or an axis). |
| 18 | // All indices are 1-based. |
| 19 | class JoystickFeature { |
| 20 | public: |
| 21 | JoystickFeature(int joystick, int number) |
| 22 | : joystick_(joystick), number_(number) {} |
| 23 | |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame^] | 24 | // How many joysticks there are. |
| 25 | static const int kJoysticks = sizeof(NetworkRobotJoysticks::joysticks) / |
| 26 | sizeof(NetworkRobotJoysticks::joysticks[0]); |
| 27 | |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 28 | // Which joystick number this is (1-based). |
| 29 | int joystick() const { return joystick_; } |
| 30 | // Which feature on joystick() this is (1-based). |
| 31 | int number() const { return number_; } |
| 32 | |
| 33 | private: |
| 34 | const int joystick_, number_; |
| 35 | }; |
| 36 | |
| 37 | // Represents the location of a button. |
| 38 | // Use Data to actually get the value. |
| 39 | // Safe for static initialization. |
| 40 | class ButtonLocation : public JoystickFeature { |
| 41 | public: |
| 42 | ButtonLocation(int joystick, int number) |
| 43 | : JoystickFeature(joystick, number) {} |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame^] | 44 | |
| 45 | // How many buttons there are available on each joystick. |
| 46 | static const int kButtons = 12; |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | // Represents various bits of control information that the DS sends. |
| 50 | // Use Data to actually get the value. |
| 51 | enum class ControlBit { |
| 52 | kTestMode, kFmsAttached, kAutonomous, kEnabled |
| 53 | }; |
| 54 | |
| 55 | // Represents a single axis of a joystick. |
| 56 | // Use Data to actually get the value. |
| 57 | // Safe for static initialization. |
| 58 | class JoystickAxis : public JoystickFeature { |
| 59 | public: |
| 60 | JoystickAxis(int joystick, int number) |
| 61 | : JoystickFeature(joystick, number) {} |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame^] | 62 | |
| 63 | // How many axes there are available on each joystick. |
| 64 | static const int kAxes = sizeof(NetworkRobotJoysticks::Joystick::axes) / |
| 65 | sizeof(NetworkRobotJoysticks::Joystick::axes[0]); |
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. |
| 75 | void Update(const NetworkRobotJoysticks &new_values); |
| 76 | |
| 77 | bool IsPressed(ButtonLocation location) const; |
| 78 | bool PosEdge(ButtonLocation location) const; |
| 79 | bool NegEdge(ButtonLocation location) const; |
| 80 | |
| 81 | bool GetControlBit(ControlBit bit) const; |
| 82 | bool PosEdge(ControlBit bit) const; |
| 83 | bool NegEdge(ControlBit bit) const; |
| 84 | |
| 85 | // Returns the value in the range [-1.0, 1.0]. |
| 86 | float GetAxis(JoystickAxis axis) const; |
| 87 | |
| 88 | private: |
| 89 | NetworkRobotJoysticks current_values_, old_values_; |
| 90 | }; |
| 91 | |
| 92 | } // namespace driver_station |
| 93 | } // namespace input |
| 94 | } // namespace aos |
| 95 | |
| 96 | #endif // AOS_COMMON_INPUT_DRIVER_STATION_DATA_H_ |