mirror of
https://github.com/libretro/RetroArch
synced 2025-04-01 04:20:27 +00:00
(input/wiimote) Style nits/cleanups
This commit is contained in:
parent
09e27f523d
commit
d2367b9342
279
input/wiimote.c
279
input/wiimote.c
@ -66,13 +66,12 @@ void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg);
|
|||||||
/* TODO - Get rid of Apple-specific functions. */
|
/* TODO - Get rid of Apple-specific functions. */
|
||||||
void apple_pad_send_control(void *data, uint8_t* data_buf, size_t size);
|
void apple_pad_send_control(void *data, uint8_t* data_buf, size_t size);
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @brief Request the wiimote controller status.
|
* Request the wiimote controller status.
|
||||||
*
|
*
|
||||||
* @param wm Pointer to a wiimote_t structure.
|
* Controller status includes: battery level, LED status, expansions.
|
||||||
*
|
|
||||||
* Controller status includes: battery level, LED status, expansions
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void wiimote_status(struct wiimote_t* wm)
|
void wiimote_status(struct wiimote_t* wm)
|
||||||
{
|
{
|
||||||
byte buf = 0;
|
byte buf = 0;
|
||||||
@ -95,20 +94,22 @@ void wiimote_data_report(struct wiimote_t* wm, byte type)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
buf[1] = type;
|
buf[1] = type;
|
||||||
//CUIDADO es un &buf?
|
|
||||||
|
/* CUIDADO es un &buf? */
|
||||||
wiimote_send(wm, WM_CMD_REPORT_TYPE, buf, 2);
|
wiimote_send(wm, WM_CMD_REPORT_TYPE, buf, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @brief Set the enabled LEDs.
|
* Set the enabled LEDs.
|
||||||
*
|
*
|
||||||
* @param wm Pointer to a wiimote_t structure.
|
* leds is a bitwise OR of:
|
||||||
* @param leds What LEDs to enable.
|
* - WIIMOTE_LED_1
|
||||||
*
|
* - WIIMOTE_LED_2
|
||||||
* \a leds is a bitwise or of WIIMOTE_LED_1, WIIMOTE_LED_2,
|
* - WIIMOTE_LED_3
|
||||||
* WIIMOTE_LED_3, or WIIMOTE_LED_4.
|
* - WIIMOTE_LED_4
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void wiimote_set_leds(struct wiimote_t* wm, int leds)
|
void wiimote_set_leds(struct wiimote_t* wm, int leds)
|
||||||
{
|
{
|
||||||
byte buf;
|
byte buf;
|
||||||
@ -116,7 +117,7 @@ void wiimote_set_leds(struct wiimote_t* wm, int leds)
|
|||||||
if (!wm || !WIIMOTE_IS_CONNECTED(wm))
|
if (!wm || !WIIMOTE_IS_CONNECTED(wm))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* remove the lower 4 bits because they control rumble */
|
/* Remove the lower 4 bits because they control rumble. */
|
||||||
wm->leds = (leds & 0xF0);
|
wm->leds = (leds & 0xF0);
|
||||||
|
|
||||||
buf = wm->leds;
|
buf = wm->leds;
|
||||||
@ -124,29 +125,23 @@ void wiimote_set_leds(struct wiimote_t* wm, int leds)
|
|||||||
wiimote_send(wm, WM_CMD_LED, &buf, 1);
|
wiimote_send(wm, WM_CMD_LED, &buf, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @brief Find what buttons are pressed.
|
* Find what buttons are pressed.
|
||||||
*
|
|
||||||
* @param wm Pointer to a wiimote_t structure.
|
|
||||||
* @param msg The message specified in the event packet.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void wiimote_pressed_buttons(struct wiimote_t* wm, byte* msg)
|
void wiimote_pressed_buttons(struct wiimote_t* wm, byte* msg)
|
||||||
{
|
{
|
||||||
short now;
|
/* Convert to big endian. */
|
||||||
|
short now = BIG_ENDIAN_SHORT(*(short*)msg) & WIIMOTE_BUTTON_ALL;
|
||||||
|
|
||||||
/* convert to big endian */
|
/* buttons pressed now. */
|
||||||
now = BIG_ENDIAN_SHORT(*(short*)msg) & WIIMOTE_BUTTON_ALL;
|
|
||||||
|
|
||||||
/* buttons pressed now */
|
|
||||||
wm->btns = now;
|
wm->btns = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @brief Handle data from the expansion.
|
* Handle data from the expansion.
|
||||||
*
|
|
||||||
* @param wm A pointer to a wiimote_t structure.
|
|
||||||
* @param msg The message specified in the event packet for the expansion.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void wiimote_handle_expansion(struct wiimote_t* wm, byte* msg)
|
void wiimote_handle_expansion(struct wiimote_t* wm, byte* msg)
|
||||||
{
|
{
|
||||||
switch (wm->exp.type)
|
switch (wm->exp.type)
|
||||||
@ -159,46 +154,40 @@ void wiimote_handle_expansion(struct wiimote_t* wm, byte* msg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @brief Get initialization data from the wiimote.
|
* Get initialization data from the Wiimote.
|
||||||
*
|
*
|
||||||
* @param wm Pointer to a wiimote_t structure.
|
* When first called for a wiimote_t structure, a request
|
||||||
* @param data unused
|
* is sent to the wiimote for initialization information.
|
||||||
* @param len unused
|
* This includes factory set accelerometer data.
|
||||||
*
|
* The handshake will be concluded when the wiimote responds
|
||||||
* When first called for a wiimote_t structure, a request
|
* with this data.
|
||||||
* is sent to the wiimote for initialization information.
|
*/
|
||||||
* This includes factory set accelerometer data.
|
|
||||||
* The handshake will be concluded when the wiimote responds
|
|
||||||
* with this data.
|
|
||||||
*/
|
|
||||||
int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data,
|
int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data,
|
||||||
unsigned short len)
|
unsigned short len)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!wm) return 0;
|
if (!wm)
|
||||||
|
return 0;
|
||||||
|
|
||||||
while(1)
|
do
|
||||||
{
|
{
|
||||||
#ifdef WIIMOTE_DBG
|
|
||||||
printf("Handshake %d\n",wm->handshake_state);
|
|
||||||
#endif
|
|
||||||
switch (wm->handshake_state)
|
switch (wm->handshake_state)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
{
|
/* no ha habido nunca handshake, debemos forzar un
|
||||||
/* no ha habido nunca handshake, debemos forzar un
|
* mensaje de staus para ver que pasa. */
|
||||||
* mensaje de staus para ver que pasa. */
|
|
||||||
|
|
||||||
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
|
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
|
||||||
wiimote_set_leds(wm, WIIMOTE_LED_NONE);
|
wiimote_set_leds(wm, WIIMOTE_LED_NONE);
|
||||||
|
|
||||||
/* request the status of the wiimote to see if there is an expansion */
|
/* Request the status of the Wiimote to
|
||||||
wiimote_status(wm);
|
* see if there is an expansion */
|
||||||
|
wiimote_status(wm);
|
||||||
|
|
||||||
wm->handshake_state=1;
|
wm->handshake_state=1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
case 1:
|
case 1:
|
||||||
{
|
{
|
||||||
/* estamos haciendo handshake o bien se necesita iniciar un
|
/* estamos haciendo handshake o bien se necesita iniciar un
|
||||||
@ -208,7 +197,8 @@ int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data,
|
|||||||
if(event != WM_RPT_CTRL_STATUS)
|
if(event != WM_RPT_CTRL_STATUS)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* is an attachment connected to the expansion port? */
|
/* Is an attachment connected to
|
||||||
|
* the expansion port? */
|
||||||
if ((data[2] & WM_CTRL_STATUS_BYTE1_ATTACHMENT) ==
|
if ((data[2] & WM_CTRL_STATUS_BYTE1_ATTACHMENT) ==
|
||||||
WM_CTRL_STATUS_BYTE1_ATTACHMENT)
|
WM_CTRL_STATUS_BYTE1_ATTACHMENT)
|
||||||
attachment = 1;
|
attachment = 1;
|
||||||
@ -218,21 +208,18 @@ int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data,
|
|||||||
WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP));
|
WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* expansion port */
|
|
||||||
if (attachment && !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP))
|
if (attachment && !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP))
|
||||||
{
|
{
|
||||||
|
/* Expansion port */
|
||||||
|
|
||||||
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP);
|
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP);
|
||||||
|
|
||||||
/* send the initialization code for the attachment */
|
/* Send the initialization code for the attachment */
|
||||||
#ifdef WIIMOTE_DBG
|
|
||||||
printf("haciendo el handshake de la expansion\n");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(WIIMOTE_IS_SET(wm,WIIMOTE_STATE_HANDSHAKE_COMPLETE))
|
if(WIIMOTE_IS_SET(wm,WIIMOTE_STATE_HANDSHAKE_COMPLETE))
|
||||||
{
|
{
|
||||||
#ifdef WIIMOTE_DBG
|
/* Rehandshake. */
|
||||||
printf("rehandshake\n");
|
|
||||||
#endif
|
|
||||||
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE);
|
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE);
|
||||||
/* forzamos un handshake por si venimos
|
/* forzamos un handshake por si venimos
|
||||||
* de un hanshake completo. */
|
* de un hanshake completo. */
|
||||||
@ -240,6 +227,7 @@ int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data,
|
|||||||
}
|
}
|
||||||
|
|
||||||
byte buf;
|
byte buf;
|
||||||
|
|
||||||
/*Old way. initialize the extension was by writing the
|
/*Old way. initialize the extension was by writing the
|
||||||
* single encryption byte 0x00 to 0x(4)A40040. */
|
* single encryption byte 0x00 to 0x(4)A40040. */
|
||||||
#if 0
|
#if 0
|
||||||
@ -264,11 +252,10 @@ int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data,
|
|||||||
|
|
||||||
wm->handshake_state = 4;
|
wm->handshake_state = 4;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (!attachment && WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP))
|
else if (!attachment && WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP))
|
||||||
{
|
{
|
||||||
/* attachment removed */
|
/* Attachment removed */
|
||||||
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP);
|
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP);
|
||||||
wm->exp.type = EXP_NONE;
|
wm->exp.type = EXP_NONE;
|
||||||
|
|
||||||
@ -293,98 +280,82 @@ int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
case 2:
|
case 2:
|
||||||
{
|
/* Find handshake no expansion. */
|
||||||
/* find handshake no expansion. */
|
|
||||||
#ifdef WIIMOTE_DBG
|
#ifdef WIIMOTE_DBG
|
||||||
printf("Finalizado HANDSHAKE SIN EXPANSION\n");
|
printf("Finalizado HANDSHAKE SIN EXPANSION\n");
|
||||||
#endif
|
#endif
|
||||||
wiimote_data_report(wm,WM_RPT_BTN);
|
wiimote_data_report(wm,WM_RPT_BTN);
|
||||||
wm->handshake_state = 6;
|
wm->handshake_state = 6;
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
case 3:
|
case 3:
|
||||||
{
|
/* Find handshake expansion. */
|
||||||
/* Find handshake expansion. */
|
|
||||||
#ifdef WIIMOTE_DBG
|
#ifdef WIIMOTE_DBG
|
||||||
printf("Finalizado HANDSHAKE CON EXPANSION\n");
|
printf("Finalizado HANDSHAKE CON EXPANSION\n");
|
||||||
#endif
|
#endif
|
||||||
wiimote_data_report(wm,WM_RPT_BTN_EXP);
|
wiimote_data_report(wm,WM_RPT_BTN_EXP);
|
||||||
wm->handshake_state = 6;
|
wm->handshake_state = 6;
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
case 4:
|
case 4:
|
||||||
{
|
if(event != WM_RPT_READ)
|
||||||
if(event != WM_RPT_READ)
|
return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
int id = BIG_ENDIAN_LONG(*(int*)(data));
|
int id = BIG_ENDIAN_LONG(*(int*)(data));
|
||||||
|
|
||||||
#ifdef WIIMOTE_DBG
|
#ifdef WIIMOTE_DBG
|
||||||
printf("Expansion id=0x%04x\n",id);
|
printf("Expansion id=0x%04x\n",id);
|
||||||
#endif
|
#endif
|
||||||
/*EXP_ID_CODE_CLASSIC_CONTROLLER*/
|
/* EXP_ID_CODE_CLASSIC_CONTROLLER */
|
||||||
if(id != 0xa4200101)
|
|
||||||
{
|
if(id != 0xa4200101)
|
||||||
wm->handshake_state = 2;
|
{
|
||||||
|
wm->handshake_state = 2;
|
||||||
#if 0
|
#if 0
|
||||||
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP);
|
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP);
|
||||||
#endif
|
#endif
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
usleep(100000);
|
|
||||||
/* pedimos datos de calibracion del JOY! */
|
|
||||||
wiimote_read_data(wm, WM_EXP_MEM_CALIBR, 16);
|
|
||||||
wm->handshake_state = 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
case 5:
|
|
||||||
{
|
|
||||||
if(event != WM_RPT_READ)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
classic_ctrl_handshake(wm, &wm->exp.cc.classic, data,len);
|
|
||||||
wm->handshake_state = 3;
|
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
usleep(100000);
|
||||||
|
/* pedimos datos de calibracion del JOY! */
|
||||||
|
wiimote_read_data(wm, WM_EXP_MEM_CALIBR, 16);
|
||||||
|
wm->handshake_state = 5;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
return 0;
|
||||||
|
case 5:
|
||||||
|
if(event != WM_RPT_READ)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
classic_ctrl_handshake(wm, &wm->exp.cc.classic, data,len);
|
||||||
|
wm->handshake_state = 3;
|
||||||
|
continue;
|
||||||
case 6:
|
case 6:
|
||||||
{
|
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
|
||||||
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
|
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE);
|
||||||
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE);
|
wm->handshake_state = 1;
|
||||||
wm->handshake_state = 1;
|
if(wm->unid==0)
|
||||||
if(wm->unid==0)
|
wiimote_set_leds(wm, WIIMOTE_LED_1);
|
||||||
wiimote_set_leds(wm, WIIMOTE_LED_1);
|
else if(wm->unid==1)
|
||||||
else if(wm->unid==1)
|
wiimote_set_leds(wm, WIIMOTE_LED_2);
|
||||||
wiimote_set_leds(wm, WIIMOTE_LED_2);
|
else if(wm->unid==2)
|
||||||
else if(wm->unid==2)
|
wiimote_set_leds(wm, WIIMOTE_LED_3);
|
||||||
wiimote_set_leds(wm, WIIMOTE_LED_3);
|
else if(wm->unid==3)
|
||||||
else if(wm->unid==3)
|
wiimote_set_leds(wm, WIIMOTE_LED_4);
|
||||||
wiimote_set_leds(wm, WIIMOTE_LED_4);
|
return 1;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
{
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
} while(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @brief Send a packet to the wiimote.
|
* Send a packet to the wiimote.
|
||||||
*
|
|
||||||
* @param wm Pointer to a wiimote_t structure.
|
|
||||||
* @param report_type The report type to send (WIIMOTE_CMD_LED, WIIMOTE_CMD_RUMBLE, etc). Found in wiimote.h
|
|
||||||
* @param msg The payload.
|
|
||||||
* @param len Length of the payload in bytes.
|
|
||||||
*
|
*
|
||||||
* This function should replace any write()s directly to the wiimote device.
|
* This function should replace any write()s directly to the wiimote device.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int wiimote_send(struct wiimote_t* wm, byte report_type, byte* msg, int len)
|
int wiimote_send(struct wiimote_t* wm, byte report_type, byte* msg, int len)
|
||||||
{
|
{
|
||||||
byte buf[32];
|
byte buf[32];
|
||||||
@ -406,12 +377,8 @@ int wiimote_send(struct wiimote_t* wm, byte report_type, byte* msg, int len)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @brief Read data from the wiimote (event version).
|
* Read data from the wiimote (event version).
|
||||||
*
|
|
||||||
* @param wm Pointer to a wiimote_t structure.
|
|
||||||
* @param addr The address of wiimote memory to read from.
|
|
||||||
* @param len The length of the block to be read.
|
|
||||||
*
|
*
|
||||||
* The library can only handle one data read request at a time
|
* The library can only handle one data read request at a time
|
||||||
* because it must keep track of the buffer and other
|
* because it must keep track of the buffer and other
|
||||||
@ -420,17 +387,16 @@ int wiimote_send(struct wiimote_t* wm, byte report_type, byte* msg, int len)
|
|||||||
* to a pending list and be sent out when the previous
|
* to a pending list and be sent out when the previous
|
||||||
* finishes.
|
* finishes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int wiimote_read_data(struct wiimote_t* wm, unsigned int addr,
|
int wiimote_read_data(struct wiimote_t* wm, unsigned int addr,
|
||||||
unsigned short len)
|
unsigned short len)
|
||||||
{
|
{
|
||||||
|
byte buf[6];
|
||||||
|
|
||||||
/* No puden ser mas de 16 lo leido o vendra en trozos! */
|
/* No puden ser mas de 16 lo leido o vendra en trozos! */
|
||||||
|
|
||||||
if (!wm || !WIIMOTE_IS_CONNECTED(wm))
|
if (!wm || !WIIMOTE_IS_CONNECTED(wm) || !len)
|
||||||
return 0;
|
return 0;
|
||||||
if (!len)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
byte buf[6];
|
|
||||||
|
|
||||||
/* the offset is in big endian */
|
/* the offset is in big endian */
|
||||||
*(int*)(buf) = BIG_ENDIAN_LONG(addr);
|
*(int*)(buf) = BIG_ENDIAN_LONG(addr);
|
||||||
@ -446,13 +412,8 @@ int wiimote_read_data(struct wiimote_t* wm, unsigned int addr,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @brief Write data to the wiimote.
|
* Write data to the wiimote.
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*/
|
*/
|
||||||
int wiimote_write_data(struct wiimote_t* wm,
|
int wiimote_write_data(struct wiimote_t* wm,
|
||||||
unsigned int addr, byte* data, byte len)
|
unsigned int addr, byte* data, byte len)
|
||||||
@ -510,8 +471,10 @@ static void process_axis(struct axis_t* axis, byte raw)
|
|||||||
axis->max = raw + 2;
|
axis->max = raw + 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (raw < axis->min) axis->min = raw;
|
if (raw < axis->min)
|
||||||
if (raw > axis->max) axis->max = raw;
|
axis->min = raw;
|
||||||
|
if (raw > axis->max)
|
||||||
|
axis->max = raw;
|
||||||
axis->raw_value = raw;
|
axis->raw_value = raw;
|
||||||
|
|
||||||
if (raw < axis->center)
|
if (raw < axis->center)
|
||||||
@ -527,7 +490,7 @@ static void process_axis(struct axis_t* axis, byte raw)
|
|||||||
void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg)
|
void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg)
|
||||||
{
|
{
|
||||||
cc->btns = ~BIG_ENDIAN_SHORT(*(short*)(msg + 4)) & CLASSIC_CTRL_BUTTON_ALL;
|
cc->btns = ~BIG_ENDIAN_SHORT(*(short*)(msg + 4)) & CLASSIC_CTRL_BUTTON_ALL;
|
||||||
process_axis(&cc->ljs.x, (msg[0] & 0x3F));
|
process_axis(&cc->ljs.x, (msg[0] & 0x3F));
|
||||||
process_axis(&cc->ljs.y, (msg[1] & 0x3F));
|
process_axis(&cc->ljs.y, (msg[1] & 0x3F));
|
||||||
process_axis(&cc->rjs.x, ((msg[0] & 0xC0) >> 3) |
|
process_axis(&cc->rjs.x, ((msg[0] & 0xC0) >> 3) |
|
||||||
((msg[1] & 0xC0) >> 5) | ((msg[2] & 0x80) >> 7));
|
((msg[1] & 0xC0) >> 5) | ((msg[2] & 0x80) >> 7));
|
||||||
|
250
input/wiimote.h
250
input/wiimote.h
@ -45,157 +45,165 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef unsigned char byte;
|
typedef unsigned char byte;
|
||||||
typedef char sbyte;
|
typedef char sbyte;
|
||||||
|
|
||||||
/* Convert to big endian */
|
/* Convert to big endian */
|
||||||
#define BIG_ENDIAN_LONG(i) (htonl(i))
|
#define BIG_ENDIAN_LONG(i) (htonl(i))
|
||||||
#define BIG_ENDIAN_SHORT(i) (htons(i))
|
#define BIG_ENDIAN_SHORT(i) (htons(i))
|
||||||
|
|
||||||
#define absf(x) ((x >= 0) ? (x) : (x * -1.0f))
|
#define absf(x) ((x >= 0) ? (x) : (x * -1.0f))
|
||||||
#define diff_f(x, y) ((x >= y) ? (absf(x - y)) : (absf(y - x)))
|
#define diff_f(x, y) ((x >= y) ? (absf(x - y)) : (absf(y - x)))
|
||||||
|
|
||||||
/* wiimote state flags*/
|
/* wiimote state flags*/
|
||||||
#define WIIMOTE_STATE_DEV_FOUND 0x0001
|
#define WIIMOTE_STATE_DEV_FOUND 0x0001
|
||||||
#define WIIMOTE_STATE_HANDSHAKE 0x0002 /* actual connection exists but no handshake yet */
|
#define WIIMOTE_STATE_HANDSHAKE 0x0002 /* Actual connection exists but no handshake yet */
|
||||||
#define WIIMOTE_STATE_HANDSHAKE_COMPLETE 0x0004
|
#define WIIMOTE_STATE_HANDSHAKE_COMPLETE 0x0004
|
||||||
#define WIIMOTE_STATE_CONNECTED 0x0008
|
#define WIIMOTE_STATE_CONNECTED 0x0008
|
||||||
#define WIIMOTE_STATE_EXP 0x0040
|
#define WIIMOTE_STATE_EXP 0x0040
|
||||||
|
|
||||||
/* Communication channels */
|
/* Communication channels */
|
||||||
|
|
||||||
#define WM_SET_REPORT 0x50
|
#define WM_SET_REPORT 0x50
|
||||||
|
|
||||||
/* commands */
|
/* Commands */
|
||||||
#define WM_CMD_LED 0x11
|
#define WM_CMD_LED 0x11
|
||||||
#define WM_CMD_REPORT_TYPE 0x12
|
#define WM_CMD_REPORT_TYPE 0x12
|
||||||
#define WM_CMD_RUMBLE 0x13
|
#define WM_CMD_RUMBLE 0x13
|
||||||
#define WM_CMD_IR 0x13
|
#define WM_CMD_IR 0x13
|
||||||
#define WM_CMD_CTRL_STATUS 0x15
|
#define WM_CMD_CTRL_STATUS 0x15
|
||||||
#define WM_CMD_WRITE_DATA 0x16
|
#define WM_CMD_WRITE_DATA 0x16
|
||||||
#define WM_CMD_READ_DATA 0x17
|
#define WM_CMD_READ_DATA 0x17
|
||||||
#define WM_CMD_IR_2 0x1A
|
#define WM_CMD_IR_2 0x1A
|
||||||
|
|
||||||
/* input report ids */
|
/* Input report IDs */
|
||||||
#define WM_RPT_CTRL_STATUS 0x20
|
#define WM_RPT_CTRL_STATUS 0x20
|
||||||
#define WM_RPT_READ 0x21
|
#define WM_RPT_READ 0x21
|
||||||
#define WM_RPT_WRITE 0x22
|
#define WM_RPT_WRITE 0x22
|
||||||
#define WM_RPT_BTN 0x30
|
#define WM_RPT_BTN 0x30
|
||||||
#define WM_RPT_BTN_ACC 0x31
|
#define WM_RPT_BTN_ACC 0x31
|
||||||
#define WM_RPT_BTN_ACC_IR 0x33
|
#define WM_RPT_BTN_ACC_IR 0x33
|
||||||
#define WM_RPT_BTN_EXP 0x34
|
#define WM_RPT_BTN_EXP 0x34
|
||||||
#define WM_RPT_BTN_ACC_EXP 0x35
|
#define WM_RPT_BTN_ACC_EXP 0x35
|
||||||
#define WM_RPT_BTN_IR_EXP 0x36
|
#define WM_RPT_BTN_IR_EXP 0x36
|
||||||
#define WM_RPT_BTN_ACC_IR_EXP 0x37
|
#define WM_RPT_BTN_ACC_IR_EXP 0x37
|
||||||
|
|
||||||
#define WM_BT_INPUT 0x01
|
#define WM_BT_INPUT 0x01
|
||||||
#define WM_BT_OUTPUT 0x02
|
#define WM_BT_OUTPUT 0x02
|
||||||
|
|
||||||
/* controller status stuff */
|
/* controller status stuff */
|
||||||
#define WM_MAX_BATTERY_CODE 0xC8
|
#define WM_MAX_BATTERY_CODE 0xC8
|
||||||
|
|
||||||
#define EXP_ID_CODE_CLASSIC_CONTROLLER 0x9A1EFDFD
|
#define EXP_ID_CODE_CLASSIC_CONTROLLER 0x9A1EFDFD
|
||||||
|
|
||||||
/* offsets in wiimote memory */
|
/* offsets in wiimote memory */
|
||||||
#define WM_MEM_OFFSET_CALIBRATION 0x16
|
#define WM_MEM_OFFSET_CALIBRATION 0x16
|
||||||
#define WM_EXP_MEM_BASE 0x04A40000
|
#define WM_EXP_MEM_BASE 0x04A40000
|
||||||
#define WM_EXP_MEM_ENABLE 0x04A40040
|
#define WM_EXP_MEM_ENABLE 0x04A40040
|
||||||
#define WM_EXP_MEM_CALIBR 0x04A40020
|
#define WM_EXP_MEM_CALIBR 0x04A40020
|
||||||
|
|
||||||
#define EXP_HANDSHAKE_LEN 224
|
#define EXP_HANDSHAKE_LEN 224
|
||||||
|
|
||||||
/* controller status flags for the first message byte */
|
/* controller status flags for the first message byte */
|
||||||
/* bit 1 is unknown */
|
/* bit 1 is unknown */
|
||||||
#define WM_CTRL_STATUS_BYTE1_ATTACHMENT 0x02
|
#define WM_CTRL_STATUS_BYTE1_ATTACHMENT 0x02
|
||||||
#define WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED 0x04
|
#define WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED 0x04
|
||||||
#define WM_CTRL_STATUS_BYTE1_IR_ENABLED 0x08
|
#define WM_CTRL_STATUS_BYTE1_IR_ENABLED 0x08
|
||||||
#define WM_CTRL_STATUS_BYTE1_LED_1 0x10
|
#define WM_CTRL_STATUS_BYTE1_LED_1 0x10
|
||||||
#define WM_CTRL_STATUS_BYTE1_LED_2 0x20
|
#define WM_CTRL_STATUS_BYTE1_LED_2 0x20
|
||||||
#define WM_CTRL_STATUS_BYTE1_LED_3 0x40
|
#define WM_CTRL_STATUS_BYTE1_LED_3 0x40
|
||||||
#define WM_CTRL_STATUS_BYTE1_LED_4 0x80
|
#define WM_CTRL_STATUS_BYTE1_LED_4 0x80
|
||||||
|
|
||||||
/* led bit masks */
|
/* LED bit masks */
|
||||||
#define WIIMOTE_LED_NONE 0x00
|
#define WIIMOTE_LED_NONE 0x00
|
||||||
#define WIIMOTE_LED_1 0x10
|
#define WIIMOTE_LED_1 0x10
|
||||||
#define WIIMOTE_LED_2 0x20
|
#define WIIMOTE_LED_2 0x20
|
||||||
#define WIIMOTE_LED_3 0x40
|
#define WIIMOTE_LED_3 0x40
|
||||||
#define WIIMOTE_LED_4 0x80
|
#define WIIMOTE_LED_4 0x80
|
||||||
|
|
||||||
/* button masks */
|
/* button masks */
|
||||||
#define WIIMOTE_BUTTON_ALL 0x1F9F
|
#define WIIMOTE_BUTTON_ALL 0x1F9F
|
||||||
#define CLASSIC_CTRL_BUTTON_ALL 0xFEFF
|
#define CLASSIC_CTRL_BUTTON_ALL 0xFEFF
|
||||||
|
|
||||||
/* expansion codes */
|
/* expansion codes */
|
||||||
#define EXP_NONE 0
|
#define EXP_NONE 0
|
||||||
#define EXP_CLASSIC 2
|
#define EXP_CLASSIC 2
|
||||||
|
|
||||||
typedef struct axis_t {
|
typedef struct axis_t
|
||||||
bool has_center;
|
{
|
||||||
|
bool has_center;
|
||||||
|
|
||||||
byte min;
|
byte min;
|
||||||
byte center;
|
byte center;
|
||||||
byte max;
|
byte max;
|
||||||
byte raw_value;
|
byte raw_value;
|
||||||
float value;
|
float value;
|
||||||
} axis_t;
|
} axis_t;
|
||||||
|
|
||||||
typedef struct joystick_t {
|
typedef struct joystick_t
|
||||||
axis_t x;
|
{
|
||||||
axis_t y;
|
axis_t x;
|
||||||
} joystick_t;
|
axis_t y;
|
||||||
|
} joystick_t;
|
||||||
|
|
||||||
typedef struct classic_ctrl_t {
|
typedef struct classic_ctrl_t
|
||||||
short btns;
|
{
|
||||||
struct joystick_t ljs;
|
short btns;
|
||||||
struct joystick_t rjs;
|
struct joystick_t ljs;
|
||||||
} classic_ctrl_t;
|
struct joystick_t rjs;
|
||||||
|
} classic_ctrl_t;
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* @struct expansion_t
|
* Generic expansion device plugged into wiimote.
|
||||||
* @brief Generic expansion device plugged into wiimote.
|
*/
|
||||||
*/
|
typedef struct expansion_t
|
||||||
typedef struct expansion_t {
|
{
|
||||||
int type; /**< type of expansion attached */
|
/* Type of expansion attached. */
|
||||||
|
int type;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
struct classic_ctrl_t classic;
|
struct classic_ctrl_t classic;
|
||||||
} cc;
|
} cc;
|
||||||
} expansion_t;
|
} expansion_t;
|
||||||
|
|
||||||
/**
|
/* Wiimote structure. */
|
||||||
* @struct wiimote_t
|
typedef struct wiimote_t
|
||||||
* @brief Wiimote structure.
|
{
|
||||||
*/
|
/* User specified ID. */
|
||||||
typedef struct wiimote_t {
|
int unid;
|
||||||
int unid; /**< user specified id */
|
/* TODO/FIXME - refactor */
|
||||||
|
struct apple_pad_connection* connection;
|
||||||
|
/* Various state flags. */
|
||||||
|
int state;
|
||||||
|
/* Currently lit LEDs. */
|
||||||
|
byte leds;
|
||||||
|
/* Battery level. */
|
||||||
|
float battery_level;
|
||||||
|
/* The state of the connection handshake. */
|
||||||
|
byte handshake_state;
|
||||||
|
/* Wiimote expansion device. */
|
||||||
|
struct expansion_t exp;
|
||||||
|
/* What buttons have just been pressed. */
|
||||||
|
unsigned short btns;
|
||||||
|
} wiimote;
|
||||||
|
|
||||||
struct apple_pad_connection* connection;
|
/* Macro to manage states */
|
||||||
int state; /**< various state flags */
|
#define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s))
|
||||||
byte leds; /**< currently lit leds */
|
#define WIIMOTE_ENABLE_STATE(wm, s) (wm->state |= (s))
|
||||||
float battery_level; /**< battery level */
|
#define WIIMOTE_DISABLE_STATE(wm, s) (wm->state &= ~(s))
|
||||||
|
#define WIIMOTE_TOGGLE_STATE(wm, s) ((wm->state & (s)) ? WIIMOTE_DISABLE_STATE(wm, s) : WIIMOTE_ENABLE_STATE(wm, s))
|
||||||
byte handshake_state; /**< the state of the connection handshake */
|
#define WIIMOTE_IS_CONNECTED(wm) (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED))
|
||||||
|
|
||||||
struct expansion_t exp; /**< wiimote expansion device */
|
|
||||||
|
|
||||||
unsigned short btns; /**< what buttons have just been pressed */
|
|
||||||
} wiimote;
|
|
||||||
|
|
||||||
/* macro to manage states */
|
|
||||||
#define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s))
|
|
||||||
#define WIIMOTE_ENABLE_STATE(wm, s) (wm->state |= (s))
|
|
||||||
#define WIIMOTE_DISABLE_STATE(wm, s) (wm->state &= ~(s))
|
|
||||||
#define WIIMOTE_TOGGLE_STATE(wm, s) ((wm->state & (s)) ? WIIMOTE_DISABLE_STATE(wm, s) : WIIMOTE_ENABLE_STATE(wm, s))
|
|
||||||
#define WIIMOTE_IS_CONNECTED(wm) (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED))
|
|
||||||
|
|
||||||
int wiimote_handshake(struct wiimote_t* wm,
|
int wiimote_handshake(struct wiimote_t* wm,
|
||||||
byte event, byte* data, unsigned short len);
|
byte event, byte* data, unsigned short len);
|
||||||
|
|
||||||
void wiimote_status(struct wiimote_t* wm);
|
void wiimote_status(struct wiimote_t* wm);
|
||||||
void wiimote_data_report(struct wiimote_t* wm, byte type);
|
|
||||||
void wiimote_pressed_buttons(struct wiimote_t* wm, byte* msg);
|
|
||||||
void wiimote_handle_expansion(struct wiimote_t* wm, byte* msg);
|
|
||||||
|
|
||||||
|
void wiimote_data_report(struct wiimote_t* wm, byte type);
|
||||||
|
|
||||||
|
void wiimote_pressed_buttons(struct wiimote_t* wm, byte* msg);
|
||||||
|
|
||||||
|
void wiimote_handle_expansion(struct wiimote_t* wm, byte* msg);
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user