Brian Silverman | c206573 | 2015-11-28 22:55:30 +0000 | [diff] [blame] | 1 | #include "aos/input/joystick_input.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 2 | |
| 3 | #include <string.h> |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 4 | #include <atomic> |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 6 | #include "aos/robot_state/robot_state.q.h" |
| 7 | #include "aos/logging/logging.h" |
| 8 | #include "aos/logging/queue_logging.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 9 | |
| 10 | namespace aos { |
| 11 | namespace input { |
| 12 | |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 13 | ::std::atomic<bool> JoystickInput::run_; |
| 14 | |
| 15 | void JoystickInput::Quit(int /*signum*/) { run_ = false; } |
| 16 | |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 17 | void JoystickInput::Run() { |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 18 | 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 Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 28 | driver_station::Data data; |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 29 | while (run_) { |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 30 | joystick_state.FetchAnother(); |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 31 | |
Brian Silverman | 699f0cb | 2015-02-05 19:45:01 -0500 | [diff] [blame] | 32 | data.Update(*joystick_state); |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 33 | |
| 34 | { |
| 35 | using driver_station::JoystickFeature; |
| 36 | using driver_station::ButtonLocation; |
Brian Silverman | 3d4b897 | 2013-05-15 20:35:33 -0700 | [diff] [blame] | 37 | for (int joystick = 1; joystick <= JoystickFeature::kJoysticks; |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 38 | ++joystick) { |
Brian Silverman | 3d4b897 | 2013-05-15 20:35:33 -0700 | [diff] [blame] | 39 | for (int button = 1; button <= ButtonLocation::kButtons; ++button) { |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 40 | 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 Silverman | 7b9ab67 | 2015-03-14 23:41:41 -0700 | [diff] [blame] | 48 | 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 Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 52 | } |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 53 | } |
| 54 | { |
Brian Silverman | a7dec80 | 2013-11-04 20:53:19 -0800 | [diff] [blame] | 55 | 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 Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 74 | } |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 75 | |
| 76 | RunIteration(data); |
| 77 | } |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 78 | LOG(INFO, "Shutting down\n"); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } // namespace input |
| 82 | } // namespace aos |