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 Write data to the wiimote (callback version). * * @param wm Pointer to a wiimote_t structure. * @param addr The address to write to. * @param data The data to be written to the memory location. * @param len The length of the block to be written. * @param cb Function pointer to call when the data arrives from the wiimote. * * The library can only handle one data read request at a time * because it must keep track of the buffer and other * events that are specific to that request. So if a request * has already been made, subsequent requests will be added * to a pending list and be sent out when the previous * finishes. */ int wiiuse_write_data_cb(struct wiimote_t *wm, unsigned int addr, byte *data, byte len, wiiuse_write_cb write_cb) { struct data_req_t* req; if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return 0; } if (!data || !len) { return 0; } req = (struct data_req_t*)malloc(sizeof(struct data_req_t)); req->cb = write_cb; req->len = len; memcpy(req->data, data, req->len); req->state = REQ_READY; req->addr = addr;/* BIG_ENDIAN_LONG(addr); */ req->next = NULL; /* add this to the request list */ if (!wm->data_req) { /* root node */ wm->data_req = req; WIIUSE_DEBUG("Data write request can be sent out immediately."); /* send the request out immediately */ wiiuse_send_next_pending_write_request(wm); } else { struct data_req_t* nptr = wm->data_req; WIIUSE_DEBUG("chaud2fois"); for (; nptr->next; nptr = nptr->next) { ; } nptr->next = req; WIIUSE_DEBUG("Added pending data write request."); } return 1; }
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; }