int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) { DWORD b, r; if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return 0; } if (!ReadFile(wm->dev_handle, buf, len, &b, &wm->hid_overlap)) { /* partial read */ b = GetLastError(); if ((b == ERROR_HANDLE_EOF) || (b == ERROR_DEVICE_NOT_CONNECTED)) { /* remote disconnect */ wiiuse_disconnected(wm); return 0; } r = WaitForSingleObject(wm->hid_overlap.hEvent, wm->timeout); if (r == WAIT_TIMEOUT) { /* timeout - cancel and continue */ if (*buf) { WIIUSE_WARNING("Packet ignored. This may indicate a problem (timeout is %i ms).", wm->timeout); } CancelIo(wm->dev_handle); ResetEvent(wm->hid_overlap.hEvent); return 0; } else if (r == WAIT_FAILED) { WIIUSE_WARNING("A wait error occurred on reading from wiimote %i.", wm->unid); return 0; } if (!GetOverlappedResult(wm->dev_handle, &wm->hid_overlap, &b, 0)) { return 0; } /* log the received data */ #ifdef WITH_WIIUSE_DEBUG { DWORD i; printf("[DEBUG] (id %i) RECV: (%.2x) ", wm->unid, buf[0]); for (i = 1; i < b; i++) { printf("%.2x ", buf[i]); } printf("\n"); } #endif } ResetEvent(wm->hid_overlap.hEvent); return 1; }
int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) { int rc; int i; rc = read(wm->in_sock, buf, len); if (rc == -1) { /* error reading data */ WIIUSE_ERROR("Receiving wiimote data (id %i).", wm->unid); perror("Error Details"); if (errno == ENOTCONN) { /* this can happen if the bluetooth dongle is disconnected */ WIIUSE_ERROR("Bluetooth appears to be disconnected. Wiimote unid %i will be disconnected.", wm->unid); wiiuse_os_disconnect(wm); wiiuse_disconnected(wm); } } else if (rc == 0) { /* remote disconnect */ wiiuse_disconnected(wm); } else { /* read successful */ /* on *nix we ignore the first byte */ memmove(buf, buf + 1, len - 1); /* log the received data */ #ifdef WITH_WIIUSE_DEBUG { int i; printf("[DEBUG] (id %i) RECV: (%.2x) ", wm->unid, buf[0]); for (i = 1; i < rc; i++) { printf("%.2x ", buf[i]); } printf("\n"); } #endif } return rc; }
int wiiuse_os_write(struct wiimote_t* wm, byte report_type, byte* buf, int len) { int rc; byte write_buffer[MAX_PAYLOAD]; write_buffer[0] = WM_SET_REPORT | WM_BT_OUTPUT; write_buffer[1] = report_type; memcpy(write_buffer + 2, buf, len); rc = write(wm->out_sock, write_buffer, len + 2); if (rc < 0) { wiiuse_disconnected(wm); } return rc; }
int wiiuse_io_read(struct wiimote_t* wm) { DWORD b, r; if (!wm || !WIIMOTE_IS_CONNECTED(wm)) return 0; if (!ReadFile(wm->dev_handle, wm->event_buf, sizeof(wm->event_buf), &b, &wm->hid_overlap)) { /* partial read */ b = GetLastError(); if ((b == ERROR_HANDLE_EOF) || (b == ERROR_DEVICE_NOT_CONNECTED)) { /* remote disconnect */ wiiuse_disconnected(wm); return 0; } r = WaitForSingleObject(wm->hid_overlap.hEvent, wm->timeout); if (r == WAIT_TIMEOUT) { /* timeout - cancel and continue */ if (*wm->event_buf) WIIUSE_WARNING("Packet ignored. This may indicate a problem (timeout is %i ms).", wm->timeout); CancelIo(wm->dev_handle); ResetEvent(wm->hid_overlap.hEvent); return 0; } else if (r == WAIT_FAILED) { WIIUSE_WARNING("A wait error occured on reading from wiimote %i.", wm->unid); return 0; } if (!GetOverlappedResult(wm->dev_handle, &wm->hid_overlap, &b, 0)) return 0; } ResetEvent(wm->hid_overlap.hEvent); return 1; }
/** * @brief main() * * Connect to up to two wiimotes and print any events * that occur on either device. */ ControlResult TickRemote() { // If not connected then attempt to connect if (sControlMode == ControlMode::Off) { sResult.steering_demand.x = 0; sResult.steering_demand.y = 0; sResult.steering_demand.z = 0; sResult.fire = false; sResult.exit = false; int found = wiiuse_find(sWiimotes, 1, 5); if (found > 0) { int connected = wiiuse_connect(sWiimotes, 1); if (connected > 0) { change_control_mode(ControlMode::Slaved); wiiuse_set_leds(sWiimotes[0], WIIMOTE_LED_1); wiiuse_motion_sensing(sWiimotes[0], 0); usleep(100000); wiiuse_set_ir(sWiimotes[0], 1); wiiuse_set_ir_sensitivity(sWiimotes[0], 3); } } } bool first = true; while (sControlMode != ControlMode::Off && wiiuse_poll(sWiimotes, 1)) // Returns number of events to be processed { if (first) { first = false; sResult.steering_demand.x = 0; sResult.steering_demand.y = 0; sResult.steering_demand.z = 0; sResult.fire = false; sResult.exit = false; } switch (sWiimotes[0]->event) { case WIIUSE_EVENT: /* a generic event occured */ handle_event(sWiimotes[0]); break; case WIIUSE_DISCONNECT: case WIIUSE_UNEXPECTED_DISCONNECT: /* the wiimote disconnected */ wiiuse_disconnected(sWiimotes[0]); change_control_mode(ControlMode::Off); break; default: break; } } clock_t current_time = clock(); if (sControlMode == ControlMode::Seeking) { if (current_time > sTrackAge + 0.25 * CLOCKS_PER_SEC && sTrackValid) { sResult.steering_demand.x = 0; sResult.steering_demand.y = 0; sTrackValid = false; Log("Lost track\n"); } if (current_time > sTrackAge + 1 * CLOCKS_PER_SEC) { change_control_mode(ControlMode::Searching); sSearchDirectionChangeTime = current_time + 2 * CLOCKS_PER_SEC; sSearchDirection = 1; } } if (sControlMode == ControlMode::Searching) { if (current_time > sSearchDirectionChangeTime) { sSearchDirectionChangeTime = current_time + 2 * CLOCKS_PER_SEC; Log("Turning\n"); sSearchDirection *= -1; } sResult.steering_demand.x = sSearchDirection; sResult.steering_demand.y = 0; sResult.steering_demand.z = -1; } return sResult; }
void Wiimote::disconnected() { wiiuse_disconnected(this->wm); emit this->disconnectedSignal(); }