int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { int i; byte read_buffer[MAX_PAYLOAD]; int evnt = 0; if (!wm) { return 0; } for (i = 0; i < wiimotes; ++i) { wm[i]->event = WIIUSE_NONE; /* clear out the buffer */ memset(read_buffer, 0, sizeof(read_buffer)); /* read */ if (wiiuse_os_read(wm[i], read_buffer, sizeof(read_buffer))) { /* propagate the event */ propagate_event(wm[i], read_buffer[0], read_buffer + 1); evnt += (wm[i]->event != WIIUSE_NONE); } else { /* send out any waiting writes */ wiiuse_send_next_pending_write_request(wm[i]); idle_cycle(wm[i]); } } return evnt; }
/** * @brief Wait until specified report arrives and return it * * @param wm Pointer to a wiimote_t structure. * @param buffer Pre-allocated memory to store the received data * @param bufferLength size of buffer in bytes * * Synchronous/blocking, this function will not return until it receives the specified * report from the Wiimote. * */ void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength) { for (;;) { if (wiiuse_os_read(wm, buffer, bufferLength) > 0) { if (buffer[0] == report) { break; } else { WIIUSE_DEBUG("(id %i) dropping report 0x%x, waiting for 0x%x", wm->unid, buffer[0], report); } } } }
int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) { int evnt; struct timeval tv; fd_set fds; int r; int i; byte read_buffer[MAX_PAYLOAD]; int highest_fd = -1; evnt = 0; if (!wm) { return 0; } /* block select() for 1/2000th of a second */ tv.tv_sec = 0; tv.tv_usec = 500; FD_ZERO(&fds); for (i = 0; i < wiimotes; ++i) { /* only poll it if it is connected */ if (WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_CONNECTED)) { FD_SET(wm[i]->in_sock, &fds); /* find the highest fd of the connected wiimotes */ if (wm[i]->in_sock > highest_fd) { highest_fd = wm[i]->in_sock; } } wm[i]->event = WIIUSE_NONE; } if (highest_fd == -1) /* nothing to poll */ { return 0; } if (select(highest_fd + 1, &fds, NULL, NULL, &tv) == -1) { WIIUSE_ERROR("Unable to select() the wiimote interrupt socket(s)."); perror("Error Details"); return 0; } /* check each socket for an event */ for (i = 0; i < wiimotes; ++i) { /* if this wiimote is not connected, skip it */ if (!WIIMOTE_IS_CONNECTED(wm[i])) { continue; } if (FD_ISSET(wm[i]->in_sock, &fds)) { /* clear out the event buffer */ memset(read_buffer, 0, sizeof(read_buffer)); /* clear out any old read data */ clear_dirty_reads(wm[i]); /* read the pending message into the buffer */ r = wiiuse_os_read(wm[i], read_buffer, sizeof(read_buffer)); if (r > 0) { /* propagate the event */ propagate_event(wm[i], read_buffer[0], read_buffer + 1); evnt += (wm[i]->event != WIIUSE_NONE); } } else { /* send out any waiting writes */ wiiuse_send_next_pending_write_request(wm[i]); idle_cycle(wm[i]); } } return evnt; }