blob: dca33f9b6eac9a815e11d616d4f12f5b033e5081 [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
Brian Silvermanc25bc892013-05-09 19:09:34 -07007#include <assert.h>
8
Brian Silvermanba3de7e2013-05-08 16:18:15 -07009#include <memory>
10
11#include "aos/externals/WPILib/WPILib/NetworkRobot/NetworkRobotValues.h"
12
13namespace aos {
14namespace input {
15namespace driver_station {
16
17// Represents a feature of a joystick (a button or an axis).
18// All indices are 1-based.
19class JoystickFeature {
20 public:
21 JoystickFeature(int joystick, int number)
22 : joystick_(joystick), number_(number) {}
23
Brian Silvermanc25bc892013-05-09 19:09:34 -070024 // How many joysticks there are.
25 static const int kJoysticks = sizeof(NetworkRobotJoysticks::joysticks) /
26 sizeof(NetworkRobotJoysticks::joysticks[0]);
27
Brian Silvermanba3de7e2013-05-08 16:18:15 -070028 // 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.
40class ButtonLocation : public JoystickFeature {
41 public:
42 ButtonLocation(int joystick, int number)
43 : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070044
45 // How many buttons there are available on each joystick.
46 static const int kButtons = 12;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070047};
48
49// Represents various bits of control information that the DS sends.
50// Use Data to actually get the value.
51enum 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.
58class JoystickAxis : public JoystickFeature {
59 public:
60 JoystickAxis(int joystick, int number)
61 : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070062
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 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.
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_