blob: 507ad6110e525784c8bcee074b9d1cd32e21ea10 [file] [log] [blame]
Brian Silvermanba3de7e2013-05-08 16:18:15 -07001#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 Schuh374fd172014-10-25 17:57:54 -07009#include "aos/common/messages/robot_state.q.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -070010
11namespace aos {
12namespace input {
13namespace driver_station {
14
15// Represents a feature of a joystick (a button or an axis).
16// All indices are 1-based.
17class JoystickFeature {
18 public:
19 JoystickFeature(int joystick, int number)
20 : joystick_(joystick), number_(number) {}
21
Brian Silvermanc25bc892013-05-09 19:09:34 -070022 // How many joysticks there are.
Brian Silverman699f0cb2015-02-05 19:45:01 -050023 static const int kJoysticks = sizeof(JoystickState::joysticks) /
24 sizeof(JoystickState::joysticks[0]);
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.
44 static const int kButtons = 12;
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:
52 POVLocation(int joystick, int number)
53 : JoystickFeature(joystick, number) {}
54};
55
Brian Silvermanba3de7e2013-05-08 16:18:15 -070056// Represents various bits of control information that the DS sends.
57// Use Data to actually get the value.
58enum 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.
65class JoystickAxis : public JoystickFeature {
66 public:
67 JoystickAxis(int joystick, int number)
68 : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070069
70 // How many axes there are available on each joystick.
Austin Schuh374fd172014-10-25 17:57:54 -070071 static const int kAxes = sizeof(Joystick::axis) /
72 sizeof(Joystick::axis[0]);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070073};
74
75class 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 Silverman699f0cb2015-02-05 19:45:01 -050082 void Update(const JoystickState &new_values);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070083
Austin Schuhfee2e602015-03-08 18:26:05 -070084 bool IsPressed(POVLocation location) const;
85 bool PosEdge(POVLocation location) const;
86 bool NegEdge(POVLocation location) const;
87
Brian Silverman7b9ab672015-03-14 23:41:41 -070088 // 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 Silvermanba3de7e2013-05-08 16:18:15 -070092 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 Silverman699f0cb2015-02-05 19:45:01 -0500104 JoystickState current_values_, old_values_;
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700105};
106
107} // namespace driver_station
108} // namespace input
109} // namespace aos
110
111#endif // AOS_COMMON_INPUT_DRIVER_STATION_DATA_H_