blob: 28618ed059e6c3eba8fad7fa2fface4aeea7b0e7 [file] [log] [blame]
Brian Silvermanba3de7e2013-05-08 16:18:15 -07001#include "aos/atom_code/input/joystick_input.h"
2
3#include <string.h>
4
5#include "aos/externals/WPILib/WPILib/NetworkRobot/NetworkRobotValues.h"
6
Brian Silverman66f079a2013-08-26 16:24:30 -07007#include "aos/common/network_port.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -07008#include "aos/common/network/ReceiveSocket.h"
9#include "aos/common/messages/RobotState.q.h"
10#include "aos/common/logging/logging.h"
11
12namespace aos {
13namespace input {
14
15void 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 Silvermanc25bc892013-05-09 19:09:34 -070049
50 {
51 using driver_station::JoystickFeature;
52 using driver_station::ButtonLocation;
Brian Silverman3d4b8972013-05-15 20:35:33 -070053 for (int joystick = 1; joystick <= JoystickFeature::kJoysticks;
Brian Silvermanc25bc892013-05-09 19:09:34 -070054 ++joystick) {
Brian Silverman3d4b8972013-05-15 20:35:33 -070055 for (int button = 1; button <= ButtonLocation::kButtons; ++button) {
Brian Silvermanc25bc892013-05-09 19:09:34 -070056 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 }
65 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070066
67 RunIteration(data);
68 }
69}
70
71} // namespace input
72} // namespace aos