blob: a208f1ebeb202952d06de8bc4a2972fe459f0b8b [file] [log] [blame]
Brian Silvermanc2065732015-11-28 22:55:30 +00001#include "aos/input/joystick_input.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -07002
3#include <string.h>
Austin Schuhb58ceb62017-02-05 14:21:57 -08004#include <atomic>
Brian Silvermanba3de7e2013-05-08 16:18:15 -07005
John Park33858a32018-09-28 23:05:48 -07006#include "aos/robot_state/robot_state.q.h"
7#include "aos/logging/logging.h"
8#include "aos/logging/queue_logging.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -07009
10namespace aos {
11namespace input {
12
Austin Schuhb58ceb62017-02-05 14:21:57 -080013::std::atomic<bool> JoystickInput::run_;
14
15void JoystickInput::Quit(int /*signum*/) { run_ = false; }
16
Austin Schuh3e45c752019-02-02 12:19:11 -080017void JoystickInput::HandleData(const ::aos::JoystickState &joystick_state) {
18 data_.Update(joystick_state);
19
20 mode_ = static_cast<int>(joystick_state.switch_left) |
21 (static_cast<int>(joystick_state.scale_left) << 1);
22
23 {
24 using driver_station::JoystickFeature;
25 using driver_station::ButtonLocation;
26 for (int joystick = 1; joystick <= JoystickFeature::kJoysticks;
27 ++joystick) {
28 for (int button = 1; button <= ButtonLocation::kButtons; ++button) {
29 ButtonLocation location(joystick, button);
30 if (data_.PosEdge(location)) {
31 LOG(INFO, "PosEdge(%d, %d)\n", joystick, button);
32 }
33 if (data_.NegEdge(location)) {
34 LOG(INFO, "NegEdge(%d, %d)\n", joystick, button);
35 }
36 }
37 if (data_.GetPOV(joystick) != data_.GetOldPOV(joystick)) {
38 LOG(INFO, "POV %d %d->%d\n", joystick, data_.GetOldPOV(joystick),
39 data_.GetPOV(joystick));
40 }
41 }
42 }
43 {
44 using driver_station::ControlBit;
45 if (data_.PosEdge(ControlBit::kFmsAttached)) {
46 LOG(INFO, "PosEdge(kFmsAttached)\n");
47 }
48 if (data_.NegEdge(ControlBit::kFmsAttached)) {
49 LOG(INFO, "NegEdge(kFmsAttached)\n");
50 }
51 if (data_.PosEdge(ControlBit::kAutonomous)) {
52 LOG(INFO, "PosEdge(kAutonomous)\n");
53 }
54 if (data_.NegEdge(ControlBit::kAutonomous)) {
55 LOG(INFO, "NegEdge(kAutonomous)\n");
56 }
57 if (data_.PosEdge(ControlBit::kEnabled)) {
58 LOG(INFO, "PosEdge(kEnabled)\n");
59 }
60 if (data_.NegEdge(ControlBit::kEnabled)) {
61 LOG(INFO, "NegEdge(kEnabled)\n");
62 }
63 }
64
65 RunIteration(data_);
66
67 if (!run_) {
68 event_loop_->Exit();
69 }
70}
71
Austin Schuh374fd172014-10-25 17:57:54 -070072void JoystickInput::Run() {
Austin Schuh3e45c752019-02-02 12:19:11 -080073 // TODO(austin): We need a better sigint story for event loops in general.
Austin Schuhb58ceb62017-02-05 14:21:57 -080074 run_ = true;
75 struct sigaction action;
76 action.sa_handler = &JoystickInput::Quit;
77 sigemptyset(&action.sa_mask);
78 action.sa_flags = SA_RESETHAND;
79
80 PCHECK(sigaction(SIGTERM, &action, nullptr));
81 PCHECK(sigaction(SIGQUIT, &action, nullptr));
82 PCHECK(sigaction(SIGINT, &action, nullptr));
83
Austin Schuh3e45c752019-02-02 12:19:11 -080084 event_loop_->Run();
Austin Schuh374fd172014-10-25 17:57:54 -070085
Austin Schuhb58ceb62017-02-05 14:21:57 -080086 LOG(INFO, "Shutting down\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070087}
88
89} // namespace input
90} // namespace aos