blob: f825bd444de574daade0fe17fd68a9b05e2f42b3 [file] [log] [blame]
Brian Silvermanba3de7e2013-05-08 16:18:15 -07001#include "aos/common/input/driver_station_data.h"
2
3namespace aos {
4namespace input {
5namespace driver_station {
6
7Data::Data() : current_values_(), old_values_() {}
8
Brian Silverman699f0cb2015-02-05 19:45:01 -05009void Data::Update(const JoystickState &new_values) {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070010 old_values_ = current_values_;
11 current_values_ = new_values;
12}
13
14namespace {
15
16bool GetButton(const ButtonLocation location,
Brian Silverman699f0cb2015-02-05 19:45:01 -050017 const JoystickState &values) {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070018 return values.joysticks[location.joystick() - 1].buttons &
19 (1 << (location.number() - 1));
20}
21
22bool GetControlBitValue(const ControlBit bit,
Brian Silverman699f0cb2015-02-05 19:45:01 -050023 const JoystickState &values) {
Brian Silvermanba3de7e2013-05-08 16:18:15 -070024 switch (bit) {
25 case ControlBit::kTestMode:
Austin Schuh374fd172014-10-25 17:57:54 -070026 return values.test_mode;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070027 case ControlBit::kFmsAttached:
Austin Schuh374fd172014-10-25 17:57:54 -070028 return values.fms_attached;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070029 case ControlBit::kAutonomous:
Austin Schuh374fd172014-10-25 17:57:54 -070030 return values.autonomous;
Brian Silvermanba3de7e2013-05-08 16:18:15 -070031 case ControlBit::kEnabled:
Austin Schuh374fd172014-10-25 17:57:54 -070032 return values.enabled;
Brian Silverman8efe23e2013-07-07 23:31:37 -070033 default:
34 __builtin_unreachable();
Brian Silvermanba3de7e2013-05-08 16:18:15 -070035 }
36}
37
38} // namespace
39
40bool Data::IsPressed(const ButtonLocation location) const {
41 return GetButton(location, current_values_);
42}
43
44bool Data::PosEdge(const ButtonLocation location) const {
45 return !GetButton(location, old_values_) &&
46 GetButton(location, current_values_);
47}
48
49bool Data::NegEdge(const ButtonLocation location) const {
50 return GetButton(location, old_values_) &&
51 !GetButton(location, current_values_);
52}
53
54bool Data::GetControlBit(const ControlBit bit) const {
55 return GetControlBitValue(bit, current_values_);
56}
57
58bool Data::PosEdge(const ControlBit bit) const {
59 return !GetControlBitValue(bit, old_values_) &&
60 GetControlBitValue(bit, current_values_);
61}
62
63bool Data::NegEdge(const ControlBit bit) const {
64 return GetControlBitValue(bit, old_values_) &&
65 !GetControlBitValue(bit, current_values_);
66}
67
68float Data::GetAxis(JoystickAxis axis) const {
Austin Schuh374fd172014-10-25 17:57:54 -070069 return current_values_.joysticks[axis.joystick() - 1].axis[axis.number() - 1];
Brian Silvermanba3de7e2013-05-08 16:18:15 -070070}
71
72} // namespace driver_station
73} // namespace input
74} // namespace aos