blob: 7d8dc3e0df37419c0ef06bf803ce26d93d2813fd [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080012namespace frc971::input::driver_station {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070013
14// Represents a feature of a joystick (a button or an axis).
15// All indices are 1-based.
16class JoystickFeature {
17 public:
18 JoystickFeature(int joystick, int number)
19 : joystick_(joystick), number_(number) {}
20
Brian Silvermanc25bc892013-05-09 19:09:34 -070021 // How many joysticks there are.
Alex Perrycb7da4b2019-08-28 19:35:56 -070022 static const int kJoysticks = 6;
Brian Silvermanc25bc892013-05-09 19:09:34 -070023
Brian Silvermanba3de7e2013-05-08 16:18:15 -070024 // 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.
36class ButtonLocation : public JoystickFeature {
37 public:
38 ButtonLocation(int joystick, int number)
39 : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070040
41 // How many buttons there are available on each joystick.
Austin Schuh75c6af42018-03-09 21:16:07 -080042 static const int kButtons = 16;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070043};
44
Austin Schuhfee2e602015-03-08 18:26:05 -070045// Represents the direction of a POV on a joystick.
46// Use Data to actually get the value.
47// Safe for static initialization.
48class POVLocation : public JoystickFeature {
49 public:
James Kuszmaul7077d342021-06-09 20:23:58 -070050 POVLocation(int joystick, int number) : JoystickFeature(joystick, number) {}
Austin Schuhfee2e602015-03-08 18:26:05 -070051};
52
Brian Silvermanba3de7e2013-05-08 16:18:15 -070053// Represents various bits of control information that the DS sends.
54// Use Data to actually get the value.
James Kuszmaul7077d342021-06-09 20:23:58 -070055enum class ControlBit { kTestMode, kFmsAttached, kAutonomous, kEnabled };
Brian Silvermanba3de7e2013-05-08 16:18:15 -070056
57// Represents a single axis of a joystick.
58// Use Data to actually get the value.
59// Safe for static initialization.
60class JoystickAxis : public JoystickFeature {
61 public:
James Kuszmaul7077d342021-06-09 20:23:58 -070062 JoystickAxis(int joystick, int number) : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070063
64 // How many axes there are available on each joystick.
Alex Perrycb7da4b2019-08-28 19:35:56 -070065 static const int kAxes = 6;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070066};
67
68class 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 Kuszmaul7077d342021-06-09 20:23:58 -070075 void Update(const aos::JoystickState *new_values);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070076
Ravago Jones8c65c432023-03-25 17:35:39 -070077 virtual bool IsPressed(POVLocation location) const;
78 virtual bool PosEdge(POVLocation location) const;
79 virtual bool NegEdge(POVLocation location) const;
Austin Schuhfee2e602015-03-08 18:26:05 -070080
Brian Silverman7b9ab672015-03-14 23:41:41 -070081 // Returns the current and previous "values" for the POV.
Ravago Jones8c65c432023-03-25 17:35:39 -070082 virtual int32_t GetPOV(int joystick) const;
83 virtual int32_t GetOldPOV(int joystick) const;
Brian Silverman7b9ab672015-03-14 23:41:41 -070084
Ravago Jones8c65c432023-03-25 17:35:39 -070085 virtual bool IsPressed(ButtonLocation location) const;
86 virtual bool PosEdge(ButtonLocation location) const;
87 virtual bool NegEdge(ButtonLocation location) const;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070088
Ravago Jones8c65c432023-03-25 17:35:39 -070089 virtual bool GetControlBit(ControlBit bit) const;
90 virtual bool PosEdge(ControlBit bit) const;
91 virtual bool NegEdge(ControlBit bit) const;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070092
93 // Returns the value in the range [-1.0, 1.0].
Ravago Jones8c65c432023-03-25 17:35:39 -070094 virtual float GetAxis(JoystickAxis axis) const;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070095
96 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -070097 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 Silvermanba3de7e2013-05-08 16:18:15 -0700125};
126
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800127} // namespace frc971::input::driver_station
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700128
John Park33858a32018-09-28 23:05:48 -0700129#endif // AOS_INPUT_DRIVER_STATION_DATA_H_