Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame^] | 1 | #include "aos/prime/input/joystick_input.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 2 | |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "aos/externals/WPILib/WPILib/NetworkRobot/NetworkRobotValues.h" |
| 6 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 7 | #include "aos/common/network_port.h" |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 8 | #include "aos/common/network/ReceiveSocket.h" |
| 9 | #include "aos/common/messages/RobotState.q.h" |
| 10 | #include "aos/common/logging/logging.h" |
| 11 | |
| 12 | namespace aos { |
| 13 | namespace input { |
| 14 | |
| 15 | void JoystickInput::Run() { |
| 16 | ReceiveSocket sock(NetworkPort::kDS); |
| 17 | |
| 18 | NetworkRobotJoysticks joysticks; |
| 19 | char buffer[sizeof(joysticks) + ::buffers::kOverhead]; |
| 20 | driver_station::Data data; |
| 21 | |
| 22 | while (true) { |
| 23 | int received = sock.Receive(buffer, sizeof(buffer)); |
| 24 | if (received == -1) { |
| 25 | LOG(WARNING, "socket receive failed with %d: %s\n", |
| 26 | errno, strerror(errno)); |
| 27 | continue; |
| 28 | } |
| 29 | |
| 30 | if (!joysticks.DeserializeFrom(buffer, received)) { |
| 31 | LOG(WARNING, "deserializing data from %zd bytes failed\n", received); |
| 32 | continue; |
| 33 | } |
| 34 | |
| 35 | if (!robot_state.MakeWithBuilder() |
| 36 | .enabled(joysticks.control.enabled()) |
| 37 | .autonomous(joysticks.control.autonomous()) |
| 38 | .team_id(joysticks.team_number) |
| 39 | .Send()) { |
| 40 | LOG(WARNING, "sending robot_state failed\n"); |
| 41 | } else { |
| 42 | LOG(DEBUG, "sent robot_state{%s, %s, %hu}\n", |
| 43 | joysticks.control.enabled() ? "enabled" : "disabled", |
| 44 | joysticks.control.autonomous() ? "auto" : "not auto", |
| 45 | joysticks.team_number); |
| 46 | } |
| 47 | |
| 48 | data.Update(joysticks); |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 49 | |
| 50 | { |
| 51 | using driver_station::JoystickFeature; |
| 52 | using driver_station::ButtonLocation; |
Brian Silverman | 3d4b897 | 2013-05-15 20:35:33 -0700 | [diff] [blame] | 53 | for (int joystick = 1; joystick <= JoystickFeature::kJoysticks; |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 54 | ++joystick) { |
Brian Silverman | 3d4b897 | 2013-05-15 20:35:33 -0700 | [diff] [blame] | 55 | for (int button = 1; button <= ButtonLocation::kButtons; ++button) { |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 56 | ButtonLocation location(joystick, button); |
| 57 | if (data.PosEdge(location)) { |
| 58 | LOG(INFO, "PosEdge(%d, %d)\n", joystick, button); |
| 59 | } |
| 60 | if (data.NegEdge(location)) { |
| 61 | LOG(INFO, "NegEdge(%d, %d)\n", joystick, button); |
| 62 | } |
| 63 | } |
| 64 | } |
Brian Silverman | a7dec80 | 2013-11-04 20:53:19 -0800 | [diff] [blame] | 65 | |
| 66 | using driver_station::ControlBit; |
| 67 | if (data.PosEdge(ControlBit::kFmsAttached)) { |
| 68 | LOG(INFO, "PosEdge(kFmsAttached)\n"); |
| 69 | } |
| 70 | if (data.NegEdge(ControlBit::kFmsAttached)) { |
| 71 | LOG(INFO, "NegEdge(kFmsAttached)\n"); |
| 72 | } |
| 73 | if (data.PosEdge(ControlBit::kAutonomous)) { |
| 74 | LOG(INFO, "PosEdge(kAutonomous)\n"); |
| 75 | } |
| 76 | if (data.NegEdge(ControlBit::kAutonomous)) { |
| 77 | LOG(INFO, "NegEdge(kAutonomous)\n"); |
| 78 | } |
| 79 | if (data.PosEdge(ControlBit::kEnabled)) { |
| 80 | LOG(INFO, "PosEdge(kEnabled)\n"); |
| 81 | } |
| 82 | if (data.NegEdge(ControlBit::kEnabled)) { |
| 83 | LOG(INFO, "NegEdge(kEnabled)\n"); |
| 84 | } |
Brian Silverman | c25bc89 | 2013-05-09 19:09:34 -0700 | [diff] [blame] | 85 | } |
Brian Silverman | ba3de7e | 2013-05-08 16:18:15 -0700 | [diff] [blame] | 86 | |
| 87 | RunIteration(data); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace input |
| 92 | } // namespace aos |