Improve balance board, use stdint.h

This commit is contained in:
Ryan Pavlik
2010-07-22 18:42:04 -05:00
parent db3c0e1e7b
commit dde5bb965a
9 changed files with 804 additions and 815 deletions

View File

@@ -389,10 +389,10 @@ static void propagate_event(struct wiimote_t* wm, byte event, byte* msg) {
* @param msg The message specified in the event packet.
*/
void wiiuse_pressed_buttons(struct wiimote_t* wm, byte* msg) {
short now;
int16_t now;
/* convert to big endian */
now = BIG_ENDIAN_SHORT(*(short*)msg) & WIIMOTE_BUTTON_ALL;
now = BIG_ENDIAN_SHORT(*(int16_t*)msg) & WIIMOTE_BUTTON_ALL;
/* pressed now & were pressed, then held */
wm->btns_held = (now & wm->btns);
@@ -421,7 +421,7 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) {
/* we must always assume the packet received is from the most recent request */
byte err;
byte len;
unsigned short offset;
uint16_t offset;
struct read_req_t* req = wm->read_req;
wiiuse_pressed_buttons(wm, msg);
@@ -460,7 +460,7 @@ static void event_data_read(struct wiimote_t* wm, byte* msg) {
}
len = ((msg[2] & 0xF0) >> 4) + 1;
offset = BIG_ENDIAN_SHORT(*(unsigned short*)(msg + 3));
offset = BIG_ENDIAN_SHORT(*(uint16_t*)(msg + 3));
req->addr = (req->addr & 0xFFFF);
req->wait -= len;
@@ -634,7 +634,7 @@ static void handle_expansion(struct wiimote_t* wm, byte* msg) {
* If the data is NULL then this function will try to start
* a handshake with the expansion.
*/
void handshake_expansion(struct wiimote_t* wm, byte* data, unsigned short len) {
void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) {
int id;
if (!data) {
@@ -731,7 +731,7 @@ void disable_expansion(struct wiimote_t* wm) {
wm->event = WIIUSE_GUITAR_HERO_3_CTRL_REMOVED;
break;
case EXP_WII_BOARD:
guitar_hero_3_disconnected(&wm->exp.gh3);
wii_board_disconnected(&wm->exp.wb);
wm->event = WIIUSE_WII_BOARD_CTRL_REMOVED;
break;
default:
@@ -785,6 +785,13 @@ static void save_state(struct wiimote_t* wm) {
wm->lstate.exp_btns = wm->exp.gh3.btns;
break;
case EXP_WII_BOARD:
wm->lstate.exp_wb_rtr = wm->exp.wb.rtr;
wm->lstate.exp_wb_rtl = wm->exp.wb.rtl;
wm->lstate.exp_wb_rbr = wm->exp.wb.rbr;
wm->lstate.exp_wb_rbl = wm->exp.wb.rbl;
break;
case EXP_NONE:
break;
}
@@ -882,10 +889,10 @@ static int state_changed(struct wiimote_t* wm) {
}
case EXP_WII_BOARD:
{
STATE_CHANGED(wm->exp.wb.ltr,wm->exp.wb.tr);
STATE_CHANGED(wm->exp.wb.ltl,wm->exp.wb.tl);
STATE_CHANGED(wm->exp.wb.lbr,wm->exp.wb.br);
STATE_CHANGED(wm->exp.wb.lbl,wm->exp.wb.bl);
STATE_CHANGED(wm->lstate.exp_wb_rtr,wm->exp.wb.tr);
STATE_CHANGED(wm->lstate.exp_wb_rtl,wm->exp.wb.tl);
STATE_CHANGED(wm->lstate.exp_wb_rbr,wm->exp.wb.br);
STATE_CHANGED(wm->lstate.exp_wb_rbl,wm->exp.wb.bl);
break;
}
case EXP_NONE:

View File

@@ -40,10 +40,11 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
void wiiuse_pressed_buttons(struct wiimote_t* wm, byte* msg);
void handshake_expansion(struct wiimote_t* wm, byte* data, unsigned short len);
void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len);
void disable_expansion(struct wiimote_t* wm);
#ifdef __cplusplus

View File

@@ -52,7 +52,7 @@
* The handshake will be concluded when the wiimote responds
* with this data.
*/
void wiiuse_handshake(struct wiimote_t* wm, byte* data, unsigned short len) {
void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len) {
if (!wm) return;
switch (wm->handshake_state) {

View File

@@ -44,7 +44,7 @@
extern "C" {
#endif
void wiiuse_handshake(struct wiimote_t* wm, byte* data, unsigned short len);
void wiiuse_handshake(struct wiimote_t* wm, byte* data, uint16_t len);
int wiiuse_io_read(struct wiimote_t* wm);
int wiiuse_io_write(struct wiimote_t* wm, byte* buf, int len);

View File

@@ -33,6 +33,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#ifdef WIN32
@@ -45,34 +46,34 @@
#include "events.h"
#include "wiiboard.h"
static short big_to_lil(unsigned short num)
static uint16_t big_to_lil(uint16_t num)
{
short ret = num;
char *bret = (char*)&ret;
char tmp = bret[1];
uint16_t ret = num;
uint8_t *bret = (uint8_t*)&ret;
uint8_t tmp = bret[1];
bret[1] = bret[0];
bret[0] = tmp;
return ret;
}
/**
* @brief Handle the handshake data from the guitar.
* @brief Handle the handshake data from the wiiboard.
*
* @param cc A pointer to a classic_ctrl_t structure.
* @param wb A pointer to a wii_board_t structure.
* @param data The data read in from the device.
* @param len The length of the data block, in bytes.
*
* @return Returns 1 if handshake was successful, 0 if not.
*/
int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data, unsigned short len) {
int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data, uint16_t len) {
int i;
/* decrypt data */
printf("DECRYPTED DATA WIIBOARD\n");
for (i = 0; i < len; ++i)
{
if(i%16==0)
{
{
if(i!=0)
printf("\n");
@@ -81,9 +82,9 @@ int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data
printf("%02X ", data[i]);
}
printf("\n");
short *handshake_short = (short*)data;
uint16_t *handshake_short = (uint16_t*)data;
wb->ctr[0] = big_to_lil(handshake_short[2]);
wb->cbr[0] = big_to_lil(handshake_short[3]);
wb->ctl[0] = big_to_lil(handshake_short[4]);
@@ -100,6 +101,7 @@ int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data
wb->cbl[2] = big_to_lil(handshake_short[13]);
/* handshake done */
wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED;
wm->exp.type = EXP_WII_BOARD;
#ifdef WIN32
@@ -119,63 +121,35 @@ void wii_board_disconnected(struct wii_board_t* wb) {
memset(wb, 0, sizeof(struct wii_board_t));
}
static float do_interpolate(uint16_t raw, uint16_t cal[3]) {
if (raw < cal[1]) {
return ((raw-cal[0]) * 14.0f)/(float)(cal[1] - cal[0]);
} else if (raw > cal[1]) {
return ((raw-cal[1]) * 14.0f)/(float)(cal[2] - cal[1]) + 14.0f;
}
}
/**
* @brief Handle guitar event.
* @brief Handle wii board event.
*
* @param cc A pointer to a classic_ctrl_t structure.
* @param wb A pointer to a wii_board_t structure.
* @param msg The message specified in the event packet.
*/
void wii_board_event(struct wii_board_t* wb, byte* msg) {
short *shmsg = (short*)(msg);
wb->rtr = big_to_lil(shmsg[0]);
if(wb->rtr<0) wb->rtr = 0;
wb->rbr = big_to_lil(shmsg[1]);
if(wb->rbr<0) wb->rbr = 0;
wb->rtl = big_to_lil(shmsg[2]);
if(wb->rtl<0) wb->rtl = 0;
wb->rbl = big_to_lil(shmsg[3]);
if(wb->rbl<0) wb->rbl = 0;
uint16_t *shmsg = (uint16_t*)(msg);
wb->rtr = (msg[0] << 8) + msg[1];// big_to_lil(shmsg[0]);
wb->rbr = (msg[2] << 8) + msg[3];// big_to_lil(shmsg[1]);
wb->rtl = (msg[4] << 8) + msg[5];// big_to_lil(shmsg[2]);
wb->rbl = (msg[6] << 8) + msg[7];// big_to_lil(shmsg[3]);
/*
Interpolate values
/*
Interpolate values
Calculations borrowed from wiili.org - No names to mention sadly :( http://www.wiili.org/index.php/Wii_Balance_Board_PC_Drivers page however!
*/
if(wb->rtr<wb->ctr[1])
{
wb->tr = 68*(wb->rtr-wb->ctr[0])/(wb->ctr[1]-wb->ctr[0]);
}
else if(wb->rtr >= wb->ctr[1])
{
wb->tr = 68*(wb->rtr-wb->ctr[1])/(wb->ctr[2]-wb->ctr[1]) + 68;
}
if(wb->rtl<wb->ctl[1])
{
wb->tl = 68*(wb->rtl-wb->ctl[0])/(wb->ctl[1]-wb->ctl[0]);
}
else if(wb->rtl >= wb->ctl[1])
{
wb->tl = 68*(wb->rtl-wb->ctl[1])/(wb->ctl[2]-wb->ctl[1]) + 68;
}
if(wb->rbr<wb->cbr[1])
{
wb->br = 68*(wb->rbr-wb->cbr[0])/(wb->cbr[1]-wb->cbr[0]);
}
else if(wb->rbr >= wb->cbr[1])
{
wb->br = 68*(wb->rbr-wb->cbr[1])/(wb->cbr[2]-wb->cbr[1]) + 68;
}
if(wb->rbl<wb->cbl[1])
{
wb->bl = 68*(wb->rbl-wb->cbl[0])/(wb->cbl[1]-wb->cbl[0]);
}
else if(wb->rbl >= wb->cbl[1])
{
wb->bl = 68*(wb->rbl-wb->cbl[1])/(wb->cbl[2]-wb->cbl[1]) + 68;
}
wb->tr = do_interpolate(wb->rtr, wb->ctr);
wb->tl = do_interpolate(wb->rtl, wb->ctl);
wb->br = do_interpolate(wb->rbr, wb->cbr);
wb->bl = do_interpolate(wb->rbl, wb->cbl);
}
void wiiuse_set_wii_board_calib(struct wiimote_t *wm)

View File

@@ -28,7 +28,7 @@
/**
* @file
* @brief Guitar Hero 3 expansion device.
* @brief Wii board expansion device.
*/
#ifndef WII_BOARD_H_INCLUDED
@@ -40,7 +40,7 @@
extern "C" {
#endif
int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data, unsigned short len);
int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data, uint16_t len);
void wii_board_disconnected(struct wii_board_t* wb);

File diff suppressed because it is too large Load Diff

View File

@@ -53,6 +53,8 @@
#define WCONST const
#endif
#include <stdint.h>
/* led bit masks */
#define WIIMOTE_LED_NONE 0x00
#define WIIMOTE_LED_1 0x10
@@ -231,7 +233,7 @@ struct gforce_t;
* library when the wiimote has returned the full data requested by a previous
* call to wiiuse_read_data().
*/
typedef void (*wiiuse_read_cb)(struct wiimote_t* wm, byte* data, unsigned short len);
typedef void (*wiiuse_read_cb)(struct wiimote_t* wm, byte* data, uint16_t len);
/**
@@ -241,9 +243,9 @@ typedef void (*wiiuse_read_cb)(struct wiimote_t* wm, byte* data, unsigned short
struct read_req_t {
wiiuse_read_cb cb; /**< read data callback */
byte* buf; /**< buffer where read data is written */
unsigned int addr; /**< the offset that the read started at */
unsigned short size; /**< the length of the data read */
unsigned short wait; /**< num bytes still needed to finish read */
uint32_t addr; /**< the offset that the read started at */
uint16_t size; /**< the length of the data read */
uint16_t wait; /**< num bytes still needed to finish read */
byte dirty; /**< set to 1 if not using callback and needs to be cleaned up */
struct read_req_t* next; /**< next read request in the queue */
@@ -326,8 +328,8 @@ typedef struct ir_dot_t {
unsigned int x; /**< interpolated X coordinate */
unsigned int y; /**< interpolated Y coordinate */
short rx; /**< raw X coordinate (0-1023) */
short ry; /**< raw Y coordinate (0-767) */
int16_t rx; /**< raw X coordinate (0-1023) */
int16_t ry; /**< raw Y coordinate (0-767) */
byte order; /**< increasing order by x-axis value */
@@ -425,9 +427,9 @@ typedef struct nunchuk_t {
* @brief Classic controller expansion device.
*/
typedef struct classic_ctrl_t {
short btns; /**< what buttons have just been pressed */
short btns_held; /**< what buttons are being held down */
short btns_released; /**< what buttons were just released this */
int16_t btns; /**< what buttons have just been pressed */
int16_t btns_held; /**< what buttons are being held down */
int16_t btns_released; /**< what buttons were just released this */
float r_shoulder; /**< right shoulder button (range 0-1) */
float l_shoulder; /**< left shoulder button (range 0-1) */
@@ -442,9 +444,9 @@ typedef struct classic_ctrl_t {
* @brief Guitar Hero 3 expansion device.
*/
typedef struct guitar_hero_3_t {
short btns; /**< what buttons have just been pressed */
short btns_held; /**< what buttons are being held down */
short btns_released; /**< what buttons were just released this */
int16_t btns; /**< what buttons have just been pressed */
int16_t btns_held; /**< what buttons are being held down */
int16_t btns_released; /**< what buttons were just released this */
float whammy_bar; /**< whammy bar (range 0-1) */
@@ -455,22 +457,19 @@ typedef struct guitar_hero_3_t {
Wii board
*/
typedef struct wii_board_t {
short tl; /* Interpolated */
short tr;
short bl;
short br; /* End interp */
short rtl; /* RAW */
short rtr;
short rbl;
short rbr; /* /RAW */
short ltl;
short ltr;
short lbl;
short lbr;
short ctl[3]; /* Calibration */
short ctr[3];
short cbl[3];
short cbr[3]; /* /Calibration */
float tl; /* Interpolated */
float tr;
float bl;
float br; /* End interp */
uint16_t rtl; /* RAW */
uint16_t rtr;
uint16_t rbl;
uint16_t rbr; /* /RAW */
uint16_t ctl[3]; /* Calibration */
uint16_t ctr[3];
uint16_t cbl[3];
uint16_t cbr[3]; /* /Calibration */
uint8_t update_calib;
} wii_board_t;
@@ -511,19 +510,25 @@ typedef struct wiimote_state_t {
float exp_rjs_ang;
float exp_ljs_mag;
float exp_rjs_mag;
unsigned short exp_btns;
uint16_t exp_btns;
struct orient_t exp_orient;
struct vec3b_t exp_accel;
float exp_r_shoulder;
float exp_l_shoulder;
/* wiiboard */
uint16_t exp_wb_rtr;
uint16_t exp_wb_rtl;
uint16_t exp_wb_rbr;
uint16_t exp_wb_rbl;
/* ir_t */
int ir_ax;
int ir_ay;
float ir_distance;
struct orient_t orient;
unsigned short btns;
uint16_t btns;
struct vec3b_t accel;
} wiimote_state_t;
@@ -590,12 +595,12 @@ typedef struct wiimote_t {
WCONST struct ir_t ir; /**< IR data */
WCONST unsigned short btns; /**< what buttons have just been pressed */
WCONST unsigned short btns_held; /**< what buttons are being held down */
WCONST unsigned short btns_released; /**< what buttons were just released this */
WCONST uint16_t btns; /**< what buttons have just been pressed */
WCONST uint16_t btns_held; /**< what buttons are being held down */
WCONST uint16_t btns_released; /**< what buttons were just released this */
WCONST float orient_threshold; /**< threshold for orient to generate an event */
WCONST int accel_threshold; /**< threshold for accel to generate an event */
WCONST int32_t accel_threshold; /**< threshold for accel to generate an event */
WCONST struct wiimote_state_t lstate; /**< last saved state */
@@ -638,7 +643,7 @@ WIIUSE_EXPORT extern void wiiuse_rumble(struct wiimote_t* wm, int status);
WIIUSE_EXPORT extern void wiiuse_toggle_rumble(struct wiimote_t* wm);
WIIUSE_EXPORT extern void wiiuse_set_leds(struct wiimote_t* wm, int leds);
WIIUSE_EXPORT extern void wiiuse_motion_sensing(struct wiimote_t* wm, int status);
WIIUSE_EXPORT extern int wiiuse_read_data(struct wiimote_t* wm, byte* buffer, unsigned int offset, unsigned short len);
WIIUSE_EXPORT extern int wiiuse_read_data(struct wiimote_t* wm, byte* buffer, unsigned int offset, uint16_t len);
WIIUSE_EXPORT extern int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, byte* data, byte len);
WIIUSE_EXPORT extern void wiiuse_status(struct wiimote_t* wm);
WIIUSE_EXPORT extern struct wiimote_t* wiiuse_get_by_id(struct wiimote_t** wm, int wiimotes, int unid);

View File

@@ -51,6 +51,8 @@
#include "definitions.h"
#include <stdint.h>
/* wiiuse version */
#define WIIUSE_VERSION "0.12"
@@ -218,7 +220,7 @@ extern "C" {
int wiiuse_set_report_type(struct wiimote_t* wm);
void wiiuse_send_next_pending_read_request(struct wiimote_t* wm);
int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len);
int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buffer, unsigned int offset, unsigned short len);
int wiiuse_read_data_cb(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buffer, unsigned int offset, uint16_t len);
#ifdef __cplusplus
}