blob: 18d96fd82aad1cd3610c6aad71419235599d0ea1 [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
James Kuszmaul7077d342021-06-09 20:23:58 -07005namespace frc971 {
Brian Silvermanba3de7e2013-05-08 16:18:15 -07006namespace input {
7namespace driver_station {
8
9Data::Data() : current_values_(), old_values_() {}
10
James Kuszmaul7077d342021-06-09 20:23:58 -070011void Data::Update(const aos::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) {
James Kuszmaul7077d342021-06-09 20:23:58 -070016 const aos::Joystick *joystick = new_values->joysticks()->Get(i);
17 current_values_.joysticks[i].buttons = joystick->buttons();
Alex Perrycb7da4b2019-08-28 19:35:56 -070018 current_values_.joysticks[i].pov = joystick->pov();
19 for (size_t j = 0; j < joystick->axis()->size(); ++j) {
20 current_values_.joysticks[i].axis[j] = joystick->axis()->Get(j);
21 }
22
23 current_values_.joysticks[i].pov = joystick->pov();
24 }
25 current_values_.test_mode = new_values->test_mode();
26 current_values_.fms_attached = new_values->fms_attached();
27 current_values_.enabled = new_values->enabled();
28 current_values_.autonomous = new_values->autonomous();
29 current_values_.team_id = new_values->team_id();
30 current_values_.switch_left = new_values->switch_left();
31 current_values_.scale_left = new_values->scale_left();
Brian Silvermanba3de7e2013-05-08 16:18:15 -070032}
33
Alex Perrycb7da4b2019-08-28 19:35:56 -070034bool Data::GetButton(const ButtonLocation location,
35 const Data::SavedJoystickState &values) {
Austin Schuhbfb04122019-05-22 21:16:51 -070036 if (location.joystick() < 0 ||
37 location.joystick() > static_cast<int>(values.joysticks.size())) {
38 return false;
39 }
Tyler Chatow78ffe032019-09-29 11:06:06 -070040 if (location.number() <= 0 || location.number() > 16) {
Austin Schuhbfb04122019-05-22 21:16:51 -070041 return false;
42 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070043 return values.joysticks[location.joystick() - 1].buttons &
Alex Perrycb7da4b2019-08-28 19:35:56 -070044 (1 << (location.number() - 1));
Brian Silvermanba3de7e2013-05-08 16:18:15 -070045}
46
Alex Perrycb7da4b2019-08-28 19:35:56 -070047bool Data::DoGetPOV(const POVLocation location,
48 const Data::SavedJoystickState &values) {
Austin Schuhfee2e602015-03-08 18:26:05 -070049 return values.joysticks[location.joystick() - 1].pov == location.number();
50}
51
Alex Perrycb7da4b2019-08-28 19:35:56 -070052bool Data::GetControlBitValue(const ControlBit bit,
53 const Data::SavedJoystickState &values) {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070054 switch (bit) {
55 case ControlBit::kTestMode:
Austin Schuh374fd172014-10-25 17:57:54 -070056 return values.test_mode;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070057 case ControlBit::kFmsAttached:
Austin Schuh374fd172014-10-25 17:57:54 -070058 return values.fms_attached;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070059 case ControlBit::kAutonomous:
Austin Schuh374fd172014-10-25 17:57:54 -070060 return values.autonomous;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070061 case ControlBit::kEnabled:
Austin Schuh374fd172014-10-25 17:57:54 -070062 return values.enabled;
Brian Silverman8efe23e2013-07-07 23:31:37 -070063 default:
64 __builtin_unreachable();
Brian Silvermanba3de7e2013-05-08 16:18:15 -070065 }
66}
67
Brian Silvermanba3de7e2013-05-08 16:18:15 -070068bool Data::IsPressed(const ButtonLocation location) const {
69 return GetButton(location, current_values_);
70}
71
72bool Data::PosEdge(const ButtonLocation location) const {
73 return !GetButton(location, old_values_) &&
James Kuszmaul7077d342021-06-09 20:23:58 -070074 GetButton(location, current_values_);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070075}
76
77bool Data::NegEdge(const ButtonLocation location) const {
78 return GetButton(location, old_values_) &&
James Kuszmaul7077d342021-06-09 20:23:58 -070079 !GetButton(location, current_values_);
Brian Silvermanba3de7e2013-05-08 16:18:15 -070080}
81
Austin Schuhfee2e602015-03-08 18:26:05 -070082bool Data::IsPressed(const POVLocation location) const {
Brian Silverman7b9ab672015-03-14 23:41:41 -070083 return DoGetPOV(location, current_values_);
Austin Schuhfee2e602015-03-08 18:26:05 -070084}
85
86bool Data::PosEdge(const POVLocation location) const {
Brian Silverman7b9ab672015-03-14 23:41:41 -070087 return !DoGetPOV(location, old_values_) &&
88 DoGetPOV(location, current_values_);
Austin Schuhfee2e602015-03-08 18:26:05 -070089}
90
91bool Data::NegEdge(const POVLocation location) const {
Brian Silverman7b9ab672015-03-14 23:41:41 -070092 return DoGetPOV(location, old_values_) &&
93 !DoGetPOV(location, current_values_);
94}
95
96int32_t Data::GetPOV(int joystick) const {
97 return current_values_.joysticks[joystick - 1].pov;
98}
99
100int32_t Data::GetOldPOV(int joystick) const {
101 return old_values_.joysticks[joystick - 1].pov;
Austin Schuhfee2e602015-03-08 18:26:05 -0700102}
103
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700104bool Data::GetControlBit(const ControlBit bit) const {
105 return GetControlBitValue(bit, current_values_);
106}
107
108bool Data::PosEdge(const ControlBit bit) const {
109 return !GetControlBitValue(bit, old_values_) &&
James Kuszmaul7077d342021-06-09 20:23:58 -0700110 GetControlBitValue(bit, current_values_);
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700111}
112
113bool Data::NegEdge(const ControlBit bit) const {
114 return GetControlBitValue(bit, old_values_) &&
James Kuszmaul7077d342021-06-09 20:23:58 -0700115 !GetControlBitValue(bit, current_values_);
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700116}
117
118float Data::GetAxis(JoystickAxis axis) const {
Austin Schuh374fd172014-10-25 17:57:54 -0700119 return current_values_.joysticks[axis.joystick() - 1].axis[axis.number() - 1];
Brian Silvermanba3de7e2013-05-08 16:18:15 -0700120}
121
122} // namespace driver_station
123} // namespace input
James Kuszmaul7077d342021-06-09 20:23:58 -0700124} // namespace frc971