blob: c5c38b48b6787e3355a1398e07825d26ad8b240c [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_INPUT_DRIVER_STATION_DATA_H_
2#define AOS_INPUT_DRIVER_STATION_DATA_H_
Brian Silvermanba3de7e2013-05-08 16:18:15 -07003
4// This file defines several types to support nicely looking at the data
5// received from the driver's station.
6
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include <array>
Brian Silvermanba3de7e2013-05-08 16:18:15 -07008#include <memory>
9
James Kuszmaul7077d342021-06-09 20:23:58 -070010#include "frc971/input/joystick_state_generated.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -070011
James Kuszmaul7077d342021-06-09 20:23:58 -070012namespace frc971 {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070013namespace input {
14namespace driver_station {
15
16// Represents a feature of a joystick (a button or an axis).
17// All indices are 1-based.
18class JoystickFeature {
19 public:
20 JoystickFeature(int joystick, int number)
21 : joystick_(joystick), number_(number) {}
22
Brian Silvermanc25bc892013-05-09 19:09:34 -070023 // How many joysticks there are.
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 static const int kJoysticks = 6;
Brian Silvermanc25bc892013-05-09 19:09:34 -070025
Brian Silvermanba3de7e2013-05-08 16:18:15 -070026 // 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.
38class ButtonLocation : public JoystickFeature {
39 public:
40 ButtonLocation(int joystick, int number)
41 : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070042
43 // How many buttons there are available on each joystick.
Austin Schuh75c6af42018-03-09 21:16:07 -080044 static const int kButtons = 16;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070045};
46
Austin Schuhfee2e602015-03-08 18:26:05 -070047// Represents the direction of a POV on a joystick.
48// Use Data to actually get the value.
49// Safe for static initialization.
50class POVLocation : public JoystickFeature {
51 public:
James Kuszmaul7077d342021-06-09 20:23:58 -070052 POVLocation(int joystick, int number) : JoystickFeature(joystick, number) {}
Austin Schuhfee2e602015-03-08 18:26:05 -070053};
54
Brian Silvermanba3de7e2013-05-08 16:18:15 -070055// Represents various bits of control information that the DS sends.
56// Use Data to actually get the value.
James Kuszmaul7077d342021-06-09 20:23:58 -070057enum class ControlBit { kTestMode, kFmsAttached, kAutonomous, kEnabled };
Brian Silvermanba3de7e2013-05-08 16:18:15 -070058
59// Represents a single axis of a joystick.
60// Use Data to actually get the value.
61// Safe for static initialization.
62class JoystickAxis : public JoystickFeature {
63 public:
James Kuszmaul7077d342021-06-09 20:23:58 -070064 JoystickAxis(int joystick, int number) : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070065
66 // How many axes there are available on each joystick.
Alex Perrycb7da4b2019-08-28 19:35:56 -070067 static const int kAxes = 6;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070068};
69
70class 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 Kuszmaul7077d342021-06-09 20:23:58 -070077 void Update(const aos::JoystickState *new_values);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070078
Austin Schuhfee2e602015-03-08 18:26:05 -070079 bool IsPressed(POVLocation location) const;
80 bool PosEdge(POVLocation location) const;
81 bool NegEdge(POVLocation location) const;
82
Brian Silverman7b9ab672015-03-14 23:41:41 -070083 // 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 Silvermanba3de7e2013-05-08 16:18:15 -070087 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 Perrycb7da4b2019-08-28 19:35:56 -070099 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 Silvermanba3de7e2013-05-08 16:18:15 -0700127};
128
129} // namespace driver_station
130} // namespace input
James Kuszmaul7077d342021-06-09 20:23:58 -0700131} // namespace frc971
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700132
John Park33858a32018-09-28 23:05:48 -0700133#endif // AOS_INPUT_DRIVER_STATION_DATA_H_