blob: 2c3b3b96f9e490c9c507a57ccf6343299452d39d [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/atom_code/input/JoystickInput.h"
2
brians343bc112013-02-10 01:53:46 +00003#include "aos/common/Configuration.h"
4#include "aos/common/network/ReceiveSocket.h"
5#include "aos/common/messages/RobotState.q.h"
6
7namespace aos {
8
9void JoystickInput::SetupButtons() {
10 for (int i = 0; i < 4; ++i) {
11 old_buttons[i] = buttons[i];
12 }
13 buttons[0] = control_data_.stick0Buttons;
14 buttons[1] = control_data_.stick1Buttons;
15 buttons[2] = control_data_.stick2Buttons;
16 buttons[3] = control_data_.stick3Buttons;
17
briansb642ffe2013-03-13 00:42:30 +000018 // Put the ENABLED, AUTONOMOUS, and FMS_ATTACHED values into unused bits in
19 // the values for joystick 0 so that PosEdge and NegEdge can be used with
20 // them.
21 // Windows only supports 12 buttons, so we know there will never be any more.
22 // Not using MASK because it doesn't make it any cleaner.
brians343bc112013-02-10 01:53:46 +000023 buttons[0] |= (control_data_.enabled << (ENABLED - 9)) |
24 (control_data_.autonomous << (AUTONOMOUS - 9)) |
Brian Silverman6d29de82013-03-09 22:34:46 -080025 (control_data_.fmsAttached << (FMS_ATTACHED - 9));
brians343bc112013-02-10 01:53:46 +000026
27 for (int j = 0; j < 4; ++j) {
28 for (int k = 1; k <= 12; ++k) {
29 if (PosEdge(j, k)) {
30 LOG(INFO, "PosEdge(%d, %d)\n", j, k);
31 }
32 if (NegEdge(j, k)) {
33 LOG(INFO, "NegEdge(%d, %d)\n", j, k);
34 }
35 }
36 }
37 if (PosEdge(0, ENABLED)) LOG(INFO, "PosEdge(ENABLED)\n");
38 if (NegEdge(0, ENABLED)) LOG(INFO, "NegEdge(ENABLED)\n");
39 if (PosEdge(0, AUTONOMOUS)) LOG(INFO, "PosEdge(AUTONOMOUS)\n");
40 if (NegEdge(0, AUTONOMOUS)) LOG(INFO, "NegEdge(AUTONOMOUS)\n");
41 if (PosEdge(0, FMS_ATTACHED)) LOG(INFO, "PosEdge(FMS_ATTACHED)\n");
42 if (NegEdge(0, FMS_ATTACHED)) LOG(INFO, "NegEdge(FMS_ATTACHED)\n");
43}
44
45void JoystickInput::Run() {
46 ReceiveSocket sock(NetworkPort::kDS);
47 while (true) {
Brian Silverman3204dd82013-03-12 18:42:01 -070048 if (sock.Receive(&control_data_, sizeof(control_data_)) !=
49 sizeof(control_data_)) {
brians343bc112013-02-10 01:53:46 +000050 LOG(WARNING, "socket receive failed\n");
51 continue;
52 }
53 SetupButtons();
brians6591d582013-03-03 05:31:53 +000054 if (!robot_state.MakeWithBuilder()
55 .enabled(Pressed(0, ENABLED))
56 .autonomous(Pressed(0, AUTONOMOUS))
brians6591d582013-03-03 05:31:53 +000057 .team_id(ntohs(control_data_.teamID))
58 .Send()) {
brians343bc112013-02-10 01:53:46 +000059 LOG(WARNING, "sending robot_state failed\n");
60 }
61 if (robot_state.FetchLatest()) {
62 char state[1024];
63 robot_state->Print(state, sizeof(state));
64 LOG(DEBUG, "robot_state={%s}\n", state);
65 } else {
66 LOG(WARNING, "fetching robot_state failed\n");
67 }
68 RunIteration();
69 }
70}
71
72} // namespace aos
73