blob: 8f8c4fa2ad5b041c518a0fe319c5ca2ab0280b8e [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)) |
briansb642ffe2013-03-13 00:42:30 +000026 (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) {
49 if (sock.Recv(&control_data_, sizeof(control_data_)) == -1) {
50 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