blob: 9e3bc52b4ea16c87803ba25ab47b61f6206380cf [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/input/driver_station_data.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -07002
Alex Perrycb7da4b2019-08-28 19:35:56 -07003#include "glog/logging.h"
4
Brian Silvermanba3de7e2013-05-08 16:18:15 -07005namespace aos {
6namespace input {
7namespace driver_station {
8
9Data::Data() : current_values_(), old_values_() {}
10
Alex Perrycb7da4b2019-08-28 19:35:56 -070011void Data::Update(const JoystickState *new_values) {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070012 old_values_ = current_values_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070013 CHECK(new_values->has_joysticks());
14 CHECK_EQ(new_values->joysticks()->size(), current_values_.joysticks.size());
15 for (size_t i = 0; i < current_values_.joysticks.size(); ++i) {
16 const Joystick *joystick = new_values->joysticks()->Get(i);
17 current_values_.joysticks[i].buttons =
18 joystick->buttons();
19 current_values_.joysticks[i].pov = joystick->pov();
20 for (size_t j = 0; j < joystick->axis()->size(); ++j) {
21 current_values_.joysticks[i].axis[j] = joystick->axis()->Get(j);
22 }
23
24 current_values_.joysticks[i].pov = joystick->pov();
25 }
26 current_values_.test_mode = new_values->test_mode();
27 current_values_.fms_attached = new_values->fms_attached();
28 current_values_.enabled = new_values->enabled();
29 current_values_.autonomous = new_values->autonomous();
30 current_values_.team_id = new_values->team_id();
31 current_values_.switch_left = new_values->switch_left();
32 current_values_.scale_left = new_values->scale_left();
Brian Silvermanba3de7e2013-05-08 16:18:15 -070033}
34
Alex Perrycb7da4b2019-08-28 19:35:56 -070035bool Data::GetButton(const ButtonLocation location,
36 const Data::SavedJoystickState &values) {
Austin Schuhbfb04122019-05-22 21:16:51 -070037 if (location.joystick() < 0 ||
38 location.joystick() > static_cast<int>(values.joysticks.size())) {
39 return false;
40 }
Tyler Chatow78ffe032019-09-29 11:06:06 -070041 if (location.number() <= 0 || location.number() > 16) {
Austin Schuhbfb04122019-05-22 21:16:51 -070042 return false;
43 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070044 return values.joysticks[location.joystick() - 1].buttons &
Alex Perrycb7da4b2019-08-28 19:35:56 -070045 (1 << (location.number() - 1));
Brian Silvermanba3de7e2013-05-08 16:18:15 -070046}
47
Alex Perrycb7da4b2019-08-28 19:35:56 -070048bool Data::DoGetPOV(const POVLocation location,
49 const Data::SavedJoystickState &values) {
Austin Schuhfee2e602015-03-08 18:26:05 -070050 return values.joysticks[location.joystick() - 1].pov == location.number();
51}
52
Alex Perrycb7da4b2019-08-28 19:35:56 -070053bool Data::GetControlBitValue(const ControlBit bit,
54 const Data::SavedJoystickState &values) {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070055 switch (bit) {
56 case ControlBit::kTestMode:
Austin Schuh374fd172014-10-25 17:57:54 -070057 return values.test_mode;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070058 case ControlBit::kFmsAttached:
Austin Schuh374fd172014-10-25 17:57:54 -070059 return values.fms_attached;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070060 case ControlBit::kAutonomous:
Austin Schuh374fd172014-10-25 17:57:54 -070061 return values.autonomous;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070062 case ControlBit::kEnabled:
Austin Schuh374fd172014-10-25 17:57:54 -070063 return values.enabled;
Brian Silverman8efe23e2013-07-07 23:31:37 -070064 default:
65 __builtin_unreachable();
Brian Silvermanba3de7e2013-05-08 16:18:15 -070066 }
67}
68
Brian Silvermanba3de7e2013-05-08 16:18:15 -070069bool Data::IsPressed(const ButtonLocation location) const {
70 return GetButton(location, current_values_);
71}
72
73bool Data::PosEdge(const ButtonLocation location) const {
74 return !GetButton(location, old_values_) &&
75 GetButton(location, current_values_);
76}
77
78bool Data::NegEdge(const ButtonLocation location) const {
79 return GetButton(location, old_values_) &&
80 !GetButton(location, current_values_);
81}
82
Austin Schuhfee2e602015-03-08 18:26:05 -070083bool Data::IsPressed(const POVLocation location) const {
Brian Silverman7b9ab672015-03-14 23:41:41 -070084 return DoGetPOV(location, current_values_);
Austin Schuhfee2e602015-03-08 18:26:05 -070085}
86
87bool Data::PosEdge(const POVLocation location) const {
Brian Silverman7b9ab672015-03-14 23:41:41 -070088 return !DoGetPOV(location, old_values_) &&
89 DoGetPOV(location, current_values_);
Austin Schuhfee2e602015-03-08 18:26:05 -070090}
91
92bool Data::NegEdge(const POVLocation location) const {
Brian Silverman7b9ab672015-03-14 23:41:41 -070093 return DoGetPOV(location, old_values_) &&
94 !DoGetPOV(location, current_values_);
95}
96
97int32_t Data::GetPOV(int joystick) const {
98 return current_values_.joysticks[joystick - 1].pov;
99}
100
101int32_t Data::GetOldPOV(int joystick) const {
102 return old_values_.joysticks[joystick - 1].pov;
Austin Schuhfee2e602015-03-08 18:26:05 -0700103}
104
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700105bool Data::GetControlBit(const ControlBit bit) const {
106 return GetControlBitValue(bit, current_values_);
107}
108
109bool Data::PosEdge(const ControlBit bit) const {
110 return !GetControlBitValue(bit, old_values_) &&
111 GetControlBitValue(bit, current_values_);
112}
113
114bool Data::NegEdge(const ControlBit bit) const {
115 return GetControlBitValue(bit, old_values_) &&
116 !GetControlBitValue(bit, current_values_);
117}
118
119float Data::GetAxis(JoystickAxis axis) const {
Austin Schuh374fd172014-10-25 17:57:54 -0700120 return current_values_.joysticks[axis.joystick() - 1].axis[axis.number() - 1];
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700121}
122
123} // namespace driver_station
124} // namespace input
125} // namespace aos