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