blob: 7a946eb070e4fdd9be0dd6c03d4411270302f4ee [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
47// Represents various bits of control information that the DS sends.
48// Use Data to actually get the value.
49enum class ControlBit {
50 kTestMode, kFmsAttached, kAutonomous, kEnabled
51};
52
53// Represents a single axis of a joystick.
54// Use Data to actually get the value.
55// Safe for static initialization.
56class JoystickAxis : public JoystickFeature {
57 public:
58 JoystickAxis(int joystick, int number)
59 : JoystickFeature(joystick, number) {}
Brian Silvermanc25bc892013-05-09 19:09:34 -070060
61 // How many axes there are available on each joystick.
Austin Schuh374fd172014-10-25 17:57:54 -070062 static const int kAxes = sizeof(Joystick::axis) /
63 sizeof(Joystick::axis[0]);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070064};
65
66class Data {
67 public:
68 // Initializes the data to all buttons and control bits off and all joysticks
69 // at 0.
70 Data();
71
72 // Updates the current information with a new set of values.
Brian Silverman699f0cb2015-02-05 19:45:01 -050073 void Update(const JoystickState &new_values);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070074
75 bool IsPressed(ButtonLocation location) const;
76 bool PosEdge(ButtonLocation location) const;
77 bool NegEdge(ButtonLocation location) const;
78
79 bool GetControlBit(ControlBit bit) const;
80 bool PosEdge(ControlBit bit) const;
81 bool NegEdge(ControlBit bit) const;
82
83 // Returns the value in the range [-1.0, 1.0].
84 float GetAxis(JoystickAxis axis) const;
85
86 private:
Brian Silverman699f0cb2015-02-05 19:45:01 -050087 JoystickState current_values_, old_values_;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070088};
89
90} // namespace driver_station
91} // namespace input
92} // namespace aos
93
94#endif // AOS_COMMON_INPUT_DRIVER_STATION_DATA_H_