/** * @brief Send the next pending data read request to the wiimote. * * @param wm Pointer to a wiimote_t structure. * * @see wiiuse_read_data() * * This function is not part of the wiiuse API. */ void wiiuse_send_next_pending_read_request(struct wiimote_t* wm) { byte buf[6]; struct read_req_t* req; if (!wm || !WIIMOTE_IS_CONNECTED(wm)) { return; } if (!wm->read_req) { return; } /* skip over dirty ones since they have already been read */ req = wm->read_req; while (req && req->dirty) { req = req->next; } if (!req) { return; } /* the offset is in big endian */ to_big_endian_uint32_t(buf, req->addr); /* the length is in big endian */ to_big_endian_uint16_t(buf + 4, req->size); WIIUSE_DEBUG("Request read at address: 0x%x length: %i", req->addr, req->size); wiiuse_send(wm, WM_CMD_READ_DATA, buf, 6); }
/** * @brief Read memory/register data synchronously * * @param wm Pointer to a wiimote_t structure. * @param memory If set to non-zero, reads EEPROM, otherwise registers * @param addr Address offset to read from * @param size How many bytes to read * @param data Pre-allocated memory to store the received data * * Synchronous/blocking read, this function will not return until it receives the specified * amount of data from the Wiimote. * */ void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data) { byte pkt[6]; byte buf[MAX_PAYLOAD]; unsigned n_full_reports; unsigned last_report; byte *output; unsigned int i; /* * address in big endian first, the leading byte will * be overwritten (only 3 bytes are sent) */ to_big_endian_uint32_t(pkt, addr); /* read from registers or memory */ pkt[0] = (memory != 0) ? 0x00 : 0x04; /* length in big endian */ to_big_endian_uint16_t(pkt + 4, size); /* send */ wiiuse_send(wm, WM_CMD_READ_DATA, pkt, sizeof(pkt)); /* calculate how many 16B packets we have to get back */ n_full_reports = size / 16; last_report = size % 16; output = data; for (i = 0; i < n_full_reports; ++i) { wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD); memmove(output, buf + 6, 16); output += 16; } /* read the last incomplete packet */ if (last_report) { wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD); memmove(output, buf + 6, last_report); } }