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 | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame^] | 17 | void 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 Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 72 | void JoystickInput::Run() { |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame^] | 73 | // TODO(austin): We need a better sigint story for event loops in general. |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 74 | 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 Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame^] | 84 | event_loop_->Run(); |
Austin Schuh | 374fd17 | 2014-10-25 17:57:54 -0700 | [diff] [blame] | 85 | |
Austin Schuh | b58ceb6 | 2017-02-05 14:21:57 -0800 | [diff] [blame] | 86 | LOG(INFO, "Shutting down\n"); |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace input |
| 90 | } // namespace aos |