blob: 06d2d69c5072e2b76c7ebc99e2aa8cc3aa93295e [file] [log] [blame]
James Kuszmaul7077d342021-06-09 20:23:58 -07001#include "frc971/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
Stephan Pleinesf63bde82024-01-13 15:59:33 -08005namespace frc971::input::driver_station {
Brian Silvermanba3de7e2013-05-08 16:18:15 -07006
7Data::Data() : current_values_(), old_values_() {}
8
James Kuszmaul7077d342021-06-09 20:23:58 -07009void Data::Update(const aos::JoystickState *new_values) {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070010 old_values_ = current_values_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070011 CHECK(new_values->has_joysticks());
12 CHECK_EQ(new_values->joysticks()->size(), current_values_.joysticks.size());
13 for (size_t i = 0; i < current_values_.joysticks.size(); ++i) {
James Kuszmaul7077d342021-06-09 20:23:58 -070014 const aos::Joystick *joystick = new_values->joysticks()->Get(i);
15 current_values_.joysticks[i].buttons = joystick->buttons();
Alex Perrycb7da4b2019-08-28 19:35:56 -070016 current_values_.joysticks[i].pov = joystick->pov();
17 for (size_t j = 0; j < joystick->axis()->size(); ++j) {
18 current_values_.joysticks[i].axis[j] = joystick->axis()->Get(j);
19 }
20
21 current_values_.joysticks[i].pov = joystick->pov();
22 }
23 current_values_.test_mode = new_values->test_mode();
24 current_values_.fms_attached = new_values->fms_attached();
25 current_values_.enabled = new_values->enabled();
26 current_values_.autonomous = new_values->autonomous();
27 current_values_.team_id = new_values->team_id();
28 current_values_.switch_left = new_values->switch_left();
29 current_values_.scale_left = new_values->scale_left();
Brian Silvermanba3de7e2013-05-08 16:18:15 -070030}
31
Alex Perrycb7da4b2019-08-28 19:35:56 -070032bool Data::GetButton(const ButtonLocation location,
33 const Data::SavedJoystickState &values) {
Austin Schuhbfb04122019-05-22 21:16:51 -070034 if (location.joystick() < 0 ||
35 location.joystick() > static_cast<int>(values.joysticks.size())) {
36 return false;
37 }
Tyler Chatow78ffe032019-09-29 11:06:06 -070038 if (location.number() <= 0 || location.number() > 16) {
Austin Schuhbfb04122019-05-22 21:16:51 -070039 return false;
40 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070041 return values.joysticks[location.joystick() - 1].buttons &
Alex Perrycb7da4b2019-08-28 19:35:56 -070042 (1 << (location.number() - 1));
Brian Silvermanba3de7e2013-05-08 16:18:15 -070043}
44
Alex Perrycb7da4b2019-08-28 19:35:56 -070045bool Data::DoGetPOV(const POVLocation location,
46 const Data::SavedJoystickState &values) {
Austin Schuhfee2e602015-03-08 18:26:05 -070047 return values.joysticks[location.joystick() - 1].pov == location.number();
48}
49
Alex Perrycb7da4b2019-08-28 19:35:56 -070050bool Data::GetControlBitValue(const ControlBit bit,
51 const Data::SavedJoystickState &values) {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070052 switch (bit) {
53 case ControlBit::kTestMode:
Austin Schuh374fd172014-10-25 17:57:54 -070054 return values.test_mode;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070055 case ControlBit::kFmsAttached:
Austin Schuh374fd172014-10-25 17:57:54 -070056 return values.fms_attached;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070057 case ControlBit::kAutonomous:
Austin Schuh374fd172014-10-25 17:57:54 -070058 return values.autonomous;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070059 case ControlBit::kEnabled:
Austin Schuh374fd172014-10-25 17:57:54 -070060 return values.enabled;
Brian Silverman8efe23e2013-07-07 23:31:37 -070061 default:
62 __builtin_unreachable();
Brian Silvermanba3de7e2013-05-08 16:18:15 -070063 }
64}
65
Brian Silvermanba3de7e2013-05-08 16:18:15 -070066bool Data::IsPressed(const ButtonLocation location) const {
67 return GetButton(location, current_values_);
68}
69
70bool Data::PosEdge(const ButtonLocation location) const {
71 return !GetButton(location, old_values_) &&
James Kuszmaul7077d342021-06-09 20:23:58 -070072 GetButton(location, current_values_);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070073}
74
75bool Data::NegEdge(const ButtonLocation location) const {
76 return GetButton(location, old_values_) &&
James Kuszmaul7077d342021-06-09 20:23:58 -070077 !GetButton(location, current_values_);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070078}
79
Austin Schuhfee2e602015-03-08 18:26:05 -070080bool Data::IsPressed(const POVLocation location) const {
Brian Silverman7b9ab672015-03-14 23:41:41 -070081 return DoGetPOV(location, current_values_);
Austin Schuhfee2e602015-03-08 18:26:05 -070082}
83
84bool Data::PosEdge(const POVLocation location) const {
Brian Silverman7b9ab672015-03-14 23:41:41 -070085 return !DoGetPOV(location, old_values_) &&
86 DoGetPOV(location, current_values_);
Austin Schuhfee2e602015-03-08 18:26:05 -070087}
88
89bool Data::NegEdge(const POVLocation location) const {
Brian Silverman7b9ab672015-03-14 23:41:41 -070090 return DoGetPOV(location, old_values_) &&
91 !DoGetPOV(location, current_values_);
92}
93
94int32_t Data::GetPOV(int joystick) const {
95 return current_values_.joysticks[joystick - 1].pov;
96}
97
98int32_t Data::GetOldPOV(int joystick) const {
99 return old_values_.joysticks[joystick - 1].pov;
Austin Schuhfee2e602015-03-08 18:26:05 -0700100}
101
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700102bool Data::GetControlBit(const ControlBit bit) const {
103 return GetControlBitValue(bit, current_values_);
104}
105
106bool Data::PosEdge(const ControlBit bit) const {
107 return !GetControlBitValue(bit, old_values_) &&
James Kuszmaul7077d342021-06-09 20:23:58 -0700108 GetControlBitValue(bit, current_values_);
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700109}
110
111bool Data::NegEdge(const ControlBit bit) const {
112 return GetControlBitValue(bit, old_values_) &&
James Kuszmaul7077d342021-06-09 20:23:58 -0700113 !GetControlBitValue(bit, current_values_);
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700114}
115
116float Data::GetAxis(JoystickAxis axis) const {
Austin Schuh374fd172014-10-25 17:57:54 -0700117 return current_values_.joysticks[axis.joystick() - 1].axis[axis.number() - 1];
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700118}
119
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800120} // namespace frc971::input::driver_station