blob: 1620fdb2771a8923ccbbdd50cb5fa599b2bedcae [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 Schuh374fd172014-10-25 17:57:54 -070017void JoystickInput::Run() {
Austin Schuhb58ceb62017-02-05 14:21:57 -080018 run_ = true;
19 struct sigaction action;
20 action.sa_handler = &JoystickInput::Quit;
21 sigemptyset(&action.sa_mask);
22 action.sa_flags = SA_RESETHAND;
23
24 PCHECK(sigaction(SIGTERM, &action, nullptr));
25 PCHECK(sigaction(SIGQUIT, &action, nullptr));
26 PCHECK(sigaction(SIGINT, &action, nullptr));
27
Austin Schuh374fd172014-10-25 17:57:54 -070028 driver_station::Data data;
Austin Schuhb58ceb62017-02-05 14:21:57 -080029 while (run_) {
Brian Silverman699f0cb2015-02-05 19:45:01 -050030 joystick_state.FetchAnother();
Austin Schuh374fd172014-10-25 17:57:54 -070031
Brian Silverman699f0cb2015-02-05 19:45:01 -050032 data.Update(*joystick_state);
Brian Silvermanc25bc892013-05-09 19:09:34 -070033
34 {
35 using driver_station::JoystickFeature;
36 using driver_station::ButtonLocation;
Brian Silverman3d4b8972013-05-15 20:35:33 -070037 for (int joystick = 1; joystick <= JoystickFeature::kJoysticks;
Brian Silvermanc25bc892013-05-09 19:09:34 -070038 ++joystick) {
Brian Silverman3d4b8972013-05-15 20:35:33 -070039 for (int button = 1; button <= ButtonLocation::kButtons; ++button) {
Brian Silvermanc25bc892013-05-09 19:09:34 -070040 ButtonLocation location(joystick, button);
41 if (data.PosEdge(location)) {
42 LOG(INFO, "PosEdge(%d, %d)\n", joystick, button);
43 }
44 if (data.NegEdge(location)) {
45 LOG(INFO, "NegEdge(%d, %d)\n", joystick, button);
46 }
47 }
Brian Silverman7b9ab672015-03-14 23:41:41 -070048 if (data.GetPOV(joystick) != data.GetOldPOV(joystick)) {
49 LOG(INFO, "POV %d %d->%d\n", joystick, data.GetOldPOV(joystick),
50 data.GetPOV(joystick));
51 }
Brian Silvermanc25bc892013-05-09 19:09:34 -070052 }
Brian Silverman6da04272014-05-18 18:47:48 -070053 }
54 {
Brian Silvermana7dec802013-11-04 20:53:19 -080055 using driver_station::ControlBit;
56 if (data.PosEdge(ControlBit::kFmsAttached)) {
57 LOG(INFO, "PosEdge(kFmsAttached)\n");
58 }
59 if (data.NegEdge(ControlBit::kFmsAttached)) {
60 LOG(INFO, "NegEdge(kFmsAttached)\n");
61 }
62 if (data.PosEdge(ControlBit::kAutonomous)) {
63 LOG(INFO, "PosEdge(kAutonomous)\n");
64 }
65 if (data.NegEdge(ControlBit::kAutonomous)) {
66 LOG(INFO, "NegEdge(kAutonomous)\n");
67 }
68 if (data.PosEdge(ControlBit::kEnabled)) {
69 LOG(INFO, "PosEdge(kEnabled)\n");
70 }
71 if (data.NegEdge(ControlBit::kEnabled)) {
72 LOG(INFO, "NegEdge(kEnabled)\n");
73 }
Brian Silvermanc25bc892013-05-09 19:09:34 -070074 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070075
76 RunIteration(data);
77 }
Austin Schuhb58ceb62017-02-05 14:21:57 -080078 LOG(INFO, "Shutting down\n");
Brian Silvermanba3de7e2013-05-08 16:18:15 -070079}
80
81} // namespace input
82} // namespace aos