same interface for all wiiuse_os_read's

This commit is contained in:
Lysann Schlegel
2012-12-08 21:13:15 +01:00
parent 97513647de
commit f2bf07c18c
10 changed files with 63 additions and 60 deletions

View File

@@ -122,19 +122,13 @@ void wiiuse_disconnect(struct wiimote_t* wm) {
*/ */
void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength) void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength)
{ {
byte readReport;
for(;;) for(;;)
{ {
if(wiiuse_os_read(wm, buffer, bufferLength) > 0) { if(wiiuse_os_read(wm, buffer, bufferLength) > 0) {
#ifdef WIIUSE_WIN32 if(buffer[0] == report) {
readReport = buffer[0];
#else
readReport = buffer[1];
#endif
if(readReport == report) {
break; break;
} else { } else {
WIIUSE_WARNING("(id %i) dropping report 0x%x, waiting for 0x%x", wm->unid, readReport, report); WIIUSE_WARNING("(id %i) dropping report 0x%x, waiting for 0x%x", wm->unid, buffer[0], report);
} }
} }
} }
@@ -153,7 +147,7 @@ void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int buff
* amount of data from the Wiimote. * amount of data from the Wiimote.
* *
*/ */
void wiiuse_read(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data) void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data)
{ {
byte pkt[6]; byte pkt[6];
byte buf[MAX_PAYLOAD]; byte buf[MAX_PAYLOAD];
@@ -185,7 +179,7 @@ void wiiuse_read(struct wiimote_t *wm, byte memory, unsigned addr, unsigned shor
for(i = 0; i < n_full_reports; ++i) for(i = 0; i < n_full_reports; ++i)
{ {
wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD); wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD);
memmove(output, buf + 7, 16); memmove(output, buf + 6, 16);
output += 16; output += 16;
} }
@@ -193,7 +187,7 @@ void wiiuse_read(struct wiimote_t *wm, byte memory, unsigned addr, unsigned shor
if(last_report) if(last_report)
{ {
wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD); wiiuse_wait_report(wm, WM_RPT_READ, buf, MAX_PAYLOAD);
memmove(output, buf + 7, last_report); memmove(output, buf + 6, last_report);
} }
} }
@@ -240,7 +234,7 @@ void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len)
{ {
struct accel_t* accel = &wm->accel_calib; struct accel_t* accel = &wm->accel_calib;
wiiuse_read(wm, 1, WM_MEM_OFFSET_CALIBRATION, 8, buf); wiiuse_read_data_sync(wm, 1, WM_MEM_OFFSET_CALIBRATION, 8, buf);
/* received read data */ /* received read data */
accel->cal_zero.x = buf[0]; accel->cal_zero.x = buf[0];

View File

@@ -45,7 +45,7 @@ extern "C" {
void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len); void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len);
void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength); void wiiuse_wait_report(struct wiimote_t *wm, int report, byte *buffer, int bufferLength);
void wiiuse_read(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data); void wiiuse_read_data_sync(struct wiimote_t *wm, byte memory, unsigned addr, unsigned short size, byte *data);
/** @} */ /** @} */
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -47,23 +47,19 @@ void wiiuse_probe_motion_plus(struct wiimote_t *wm)
{ {
byte buf[MAX_PAYLOAD]; byte buf[MAX_PAYLOAD];
unsigned id; unsigned id;
unsigned short offset = 0;
#ifdef WIIUSE_WIN32
offset = 1;
#endif
wiiuse_read(wm, 0, WM_EXP_MOTION_PLUS_IDENT, 6, buf); wiiuse_read_data_sync(wm, 0, WM_EXP_MOTION_PLUS_IDENT, 6, buf);
/* check error code */ /* check error code */
if(buf[4-offset] & 0x0f) if(buf[4] & 0x0f)
{ {
WIIUSE_DEBUG("No Motion+ available, stopping probe."); WIIUSE_DEBUG("No Motion+ available, stopping probe.");
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_MPLUS_PRESENT); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_MPLUS_PRESENT);
return; return;
} }
/* decode the id*/ /* decode the id */
id = from_big_endian_uint32_t(buf + 2 - offset); id = from_big_endian_uint32_t(buf + 2);
if(id != EXP_ID_CODE_INACTIVE_MOTION_PLUS && if(id != EXP_ID_CODE_INACTIVE_MOTION_PLUS &&
id != EXP_ID_CODE_NLA_MOTION_PLUS && id != EXP_ID_CODE_NLA_MOTION_PLUS &&

View File

@@ -52,6 +52,7 @@ int wiiuse_os_connect(struct wiimote_t** wm, int wiimotes);
void wiiuse_os_disconnect(struct wiimote_t* wm); void wiiuse_os_disconnect(struct wiimote_t* wm);
int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes); int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes);
/* buf[0] will be the report type, buf+1 the rest of the report */
int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len); int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len);
int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len); int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len);
/** @} */ /** @} */

View File

@@ -253,7 +253,8 @@
} }
// copy the data into the buffer // copy the data into the buffer
WiiuseReceivedData* newData = [[WiiuseReceivedData alloc] initWithBytes: data length: length]; // on Mac, we ignore the first byte
WiiuseReceivedData* newData = [[WiiuseReceivedData alloc] initWithBytes: data+1 length: length-1];
[receivedDataLock lock]; [receivedDataLock lock];
[receivedData addObject: newData]; [receivedData addObject: newData];
[receivedDataLock unlock]; [receivedDataLock unlock];
@@ -316,9 +317,9 @@
// log the received data // log the received data
#ifdef WITH_WIIUSE_DEBUG #ifdef WITH_WIIUSE_DEBUG
{ {
printf("[DEBUG] (id %i) RECV: (%x) ", wm->unid, bytes[1]); printf("[DEBUG] (id %i) RECV: (%.2x) ", wm->unid, bytes[0]);
int x; int x;
for (x = 2; x < length; ++x) for (x = 1; x < length; ++x)
printf("%.2x ", bytes[x]); printf("%.2x ", bytes[x]);
printf("\n"); printf("\n");
} }

View File

@@ -152,8 +152,8 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) {
memset(read_buffer, 0, sizeof(read_buffer)); memset(read_buffer, 0, sizeof(read_buffer));
/* read */ /* read */
if (wiiuse_os_read(wm[i], read_buffer, sizeof(read_buffer))) { if (wiiuse_os_read(wm[i], read_buffer, sizeof(read_buffer))) {
/* propagate the event, messages should be read as in linux, starting from the second element */ /* propagate the event */
propagate_event(wm[i], read_buffer[1], read_buffer+2); propagate_event(wm[i], read_buffer[0], read_buffer+1);
} else { } else {
/* send out any waiting writes */ /* send out any waiting writes */
wiiuse_send_next_pending_write_request(wm[i]); wiiuse_send_next_pending_write_request(wm[i]);

View File

@@ -286,32 +286,12 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) {
clear_dirty_reads(wm[i]); clear_dirty_reads(wm[i]);
/* read the pending message into the buffer */ /* read the pending message into the buffer */
r = read(wm[i]->in_sock, read_buffer, sizeof(read_buffer)); r = wiiuse_os_read(wm, read_buffer, sizeof(read_buffer));
if (r == -1) { if (r > 0) {
/* error reading data */
WIIUSE_ERROR("Receiving wiimote data (id %i).", wm[i]->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[i]->unid);
wiiuse_os_disconnect(wm[i]);
wiiuse_disconnected(wm[i]);
wm[i]->event = WIIUSE_UNEXPECTED_DISCONNECT;
}
continue;
}
if (!r) {
/* remote disconnect */
wiiuse_disconnected(wm[i]);
evnt = 1;
continue;
}
/* propagate the event */ /* propagate the event */
propagate_event(wm[i], read_buffer[1], read_buffer+2); propagate_event(wm[i], read_buffer[0], read_buffer+1);
evnt += (wm[i]->event != WIIUSE_NONE); evnt += (wm[i]->event != WIIUSE_NONE);
}
} else { } else {
/* send out any waiting writes */ /* send out any waiting writes */
wiiuse_send_next_pending_write_request(wm[i]); wiiuse_send_next_pending_write_request(wm[i]);
@@ -324,11 +304,41 @@ int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes) {
int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) { int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) {
int rc; int rc;
int i;
rc = read(wm->in_sock, buf, len); rc = read(wm->in_sock, buf, len);
if(rc <= 0) if (r == -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); 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; return rc;
} }

View File

@@ -240,12 +240,13 @@ int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) {
if (!GetOverlappedResult(wm->dev_handle, &wm->hid_overlap, &b, 0)) if (!GetOverlappedResult(wm->dev_handle, &wm->hid_overlap, &b, 0))
return 0; return 0;
/* log the received data */
#ifdef WITH_WIIUSE_DEBUG #ifdef WITH_WIIUSE_DEBUG
{ {
int i; int i;
printf("[DEBUG] (id %i) RECV: (%x) ", wm->unid, buf[0]); printf("[DEBUG] (id %i) RECV: (%.2x) ", wm->unid, buf[0]);
for(i = 1; i < b; i++) { for(i = 1; i < b; i++) {
printf("%x ", buf[i]); printf("%.2x ", buf[i]);
} }
printf("\n"); printf("\n");
} }

View File

@@ -307,12 +307,6 @@ typedef enum ir_position_t {
#define WIIUSE_IS_LED_SET(wm, num) ((wm->leds & WIIMOTE_LED_##num) == WIIMOTE_LED_##num) #define WIIUSE_IS_LED_SET(wm, num) ((wm->leds & WIIMOTE_LED_##num) == WIIMOTE_LED_##num)
/** @} */ /** @} */
/*
* Largest known payload is 21 bytes.
* Add 2 for the prefix and round up to a power of 2.
*/
#define MAX_PAYLOAD 32
/* /*
* This is left over from an old hack, but it may actually * This is left over from an old hack, but it may actually
* be a useful feature to keep so it wasn't removed. * be a useful feature to keep so it wasn't removed.

View File

@@ -243,6 +243,12 @@
#define NUNCHUK_IS_FLAG_SET(wm, s) ((*(wm->flags) & (s)) == (s)) #define NUNCHUK_IS_FLAG_SET(wm, s) ((*(wm->flags) & (s)) == (s))
/*
* Largest known payload is 21 bytes.
* Add 1 (Win32) or 2 (Mac, *nix) for the prefix and round up to a power of 2.
*/
#define MAX_PAYLOAD 32
/* /*
* Smooth tilt calculations are computed with the * Smooth tilt calculations are computed with the
* exponential moving average formula: * exponential moving average formula: