blob: 6439dbc3c92a371f865a841f74933c651297dd23 [file] [log] [blame]
Brian Silverman14fd0fb2014-01-14 21:42:01 -08001#include "aos/prime/input/joystick_input.h"
Brian Silvermanba3de7e2013-05-08 16:18:15 -07002
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 }
Brian Silvermana7dec802013-11-04 20:53:19 -080065
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 Silvermanc25bc892013-05-09 19:09:34 -070085 }
Brian Silvermanba3de7e2013-05-08 16:18:15 -070086
87 RunIteration(data);
88 }
89}
90
91} // namespace input
92} // namespace aos