brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/atom_code/input/JoystickInput.h" |
| 2 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | #include "aos/common/Configuration.h" |
| 4 | #include "aos/common/network/ReceiveSocket.h" |
| 5 | #include "aos/common/messages/RobotState.q.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | |
| 9 | void 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 | |
brians | b642ffe | 2013-03-13 00:42:30 +0000 | [diff] [blame] | 18 | // 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. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 23 | buttons[0] |= (control_data_.enabled << (ENABLED - 9)) | |
| 24 | (control_data_.autonomous << (AUTONOMOUS - 9)) | |
Brian Silverman | 6d29de8 | 2013-03-09 22:34:46 -0800 | [diff] [blame] | 25 | (control_data_.fmsAttached << (FMS_ATTACHED - 9)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | |
| 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 | |
| 45 | void JoystickInput::Run() { |
| 46 | ReceiveSocket sock(NetworkPort::kDS); |
| 47 | while (true) { |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 48 | if (sock.Receive(&control_data_, sizeof(control_data_)) != |
| 49 | sizeof(control_data_)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 50 | LOG(WARNING, "socket receive failed\n"); |
| 51 | continue; |
| 52 | } |
| 53 | SetupButtons(); |
brians | 6591d58 | 2013-03-03 05:31:53 +0000 | [diff] [blame] | 54 | if (!robot_state.MakeWithBuilder() |
| 55 | .enabled(Pressed(0, ENABLED)) |
| 56 | .autonomous(Pressed(0, AUTONOMOUS)) |
brians | 6591d58 | 2013-03-03 05:31:53 +0000 | [diff] [blame] | 57 | .team_id(ntohs(control_data_.teamID)) |
| 58 | .Send()) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 59 | 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 | |