(iOS) Avoid some conditionals - WIIMOTE_DBG is undefined for regular

builds anyway
This commit is contained in:
twinaphex 2013-03-18 18:52:37 +01:00
parent a8a277ec3b
commit 5790dbbc61
2 changed files with 203 additions and 176 deletions

View File

@ -72,7 +72,9 @@ int wiimote_remove(uint16_t source_cid, bd_addr_t *addr){
found=1; found=1;
struct wiimote_t *wm = NULL; struct wiimote_t *wm = NULL;
wm = &joys[i]; wm = &joys[i];
if(WIIMOTE_DBG)printf("%02x:%02x:%02x:%02x:%02x:%02x\n",wm->addr[0], wm->addr[1], wm->addr[2],wm->addr[3], wm->addr[4], wm->addr[5]); #ifdef WIIMOTE_DBG
printf("%02x:%02x:%02x:%02x:%02x:%02x\n",wm->addr[0], wm->addr[1], wm->addr[2],wm->addr[3], wm->addr[4], wm->addr[5]);
#endif
memcpy(addr,&(wm->addr),BD_ADDR_LEN); memcpy(addr,&(wm->addr),BD_ADDR_LEN);
unid = wm->unid; unid = wm->unid;
continue; continue;
@ -96,7 +98,9 @@ int wiimote_remove(uint16_t source_cid, bd_addr_t *addr){
if(found) if(found)
{ {
myosd_num_of_joys--; myosd_num_of_joys--;
if(WIIMOTE_DBG)printf("NUM JOYS %d\n",myosd_num_of_joys); #ifdef WIIMOTE_DBG
printf("NUM JOYS %d\n",myosd_num_of_joys);
#endif
return unid; return unid;
} }
return unid; return unid;
@ -117,7 +121,9 @@ struct wiimote_t* wiimote_get_by_source_cid(uint16_t source_cid){
int i = 0; int i = 0;
for (; i < myosd_num_of_joys; ++i) { for (; i < myosd_num_of_joys; ++i) {
if(WIIMOTE_DBG)printf("0x%02x 0x%02x\n",joys[i].i_source_cid,source_cid); #ifdef WIIMOTE_DBG
printf("0x%02x 0x%02x\n",joys[i].i_source_cid,source_cid);
#endif
if (joys[i].i_source_cid == source_cid) if (joys[i].i_source_cid == source_cid)
return &joys[i]; return &joys[i];
} }
@ -132,15 +138,18 @@ struct wiimote_t* wiimote_get_by_source_cid(uint16_t source_cid){
* *
* 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;
if (!wm || !WIIMOTE_IS_CONNECTED(wm)) if (!wm || !WIIMOTE_IS_CONNECTED(wm))
return; return;
if(WIIMOTE_DBG)printf("Requested wiimote status.\n"); #ifdef WIIMOTE_DBG
printf("Requested wiimote status.\n");
#endif
wiimote_send(wm, WM_CMD_CTRL_STATUS, &buf, 1); wiimote_send(wm, WM_CMD_CTRL_STATUS, &buf, 1);
} }
void wiimote_data_report(struct wiimote_t* wm, byte type) { void wiimote_data_report(struct wiimote_t* wm, byte type) {
@ -228,157 +237,173 @@ int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data, unsigned sh
while(1) while(1)
{ {
if(WIIMOTE_DBG)printf("Handshake %d\n",wm->handshake_state); #ifdef WIIMOTE_DBG
switch (wm->handshake_state) { printf("Handshake %d\n",wm->handshake_state);
case 0://no ha habido nunca handshake, debemos forzar un mensaje de staus para ver que pasa. #endif
{ switch (wm->handshake_state)
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); {
wiimote_set_leds(wm, WIIMOTE_LED_NONE); case 0://no ha habido nunca handshake, debemos forzar un mensaje de staus para ver que pasa.
{
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
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 see if there is an expansion */
wiimote_status(wm); wiimote_status(wm);
wm->handshake_state=1; wm->handshake_state=1;
return 0; return 0;
} }
case 1://estamos haciendo handshake o bien se necesita iniciar un nuevo handshake ya que se inserta(quita una expansion. case 1://estamos haciendo handshake o bien se necesita iniciar un nuevo handshake ya que se inserta(quita una expansion.
{ {
int attachment = 0; int attachment = 0;
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) == WM_CTRL_STATUS_BYTE1_ATTACHMENT) if ((data[2] & WM_CTRL_STATUS_BYTE1_ATTACHMENT) == WM_CTRL_STATUS_BYTE1_ATTACHMENT)
{ attachment = 1;
attachment = 1;
}
if(WIIMOTE_DBG)printf("attachment %d %d\n",attachment,WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)); #ifdef WIIMOTE_DBG
printf("attachment %d %d\n",attachment,WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP));
#endif
/* expansion port */ /* expansion port */
if (attachment && !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) { if (attachment && !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP))
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 */
if(WIIMOTE_DBG)printf("haciendo el handshake de la expansion\n"); #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))
{ {
if(WIIMOTE_DBG)printf("rehandshake\n"); #ifdef WIIMOTE_DBG
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE); printf("rehandshake\n");
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);//forzamos un handshake por si venimos de un hanshake completo #endif
} WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE);
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);//forzamos un handshake por si venimos de un hanshake completo
}
byte buf; byte buf;
//Old way. initialize the extension was by writing the single encryption byte 0x00 to 0x(4)A40040 //Old way. initialize the extension was by writing the single encryption byte 0x00 to 0x(4)A40040
//buf = 0x00; //buf = 0x00;
//wiimote_write_data(wm, WM_EXP_MEM_ENABLE, &buf, 1); //wiimote_write_data(wm, WM_EXP_MEM_ENABLE, &buf, 1);
//NEW WAY 0x55 to 0x(4)A400F0, then writing 0x00 to 0x(4)A400FB. (support clones) //NEW WAY 0x55 to 0x(4)A400F0, then writing 0x00 to 0x(4)A400FB. (support clones)
buf = 0x55; buf = 0x55;
wiimote_write_data(wm, 0x04A400F0, &buf, 1); wiimote_write_data(wm, 0x04A400F0, &buf, 1);
usleep(100000); usleep(100000);
buf = 0x00; buf = 0x00;
wiimote_write_data(wm, 0x04A400FB, &buf, 1); wiimote_write_data(wm, 0x04A400FB, &buf, 1);
//check extension type! //check extension type!
usleep(100000); usleep(100000);
wiimote_read_data(wm, WM_EXP_MEM_CALIBR+220, 4); wiimote_read_data(wm, WM_EXP_MEM_CALIBR+220, 4);
//wiimote_read_data(wm, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN); //wiimote_read_data(wm, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN);
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;
if(WIIMOTE_IS_SET(wm,WIIMOTE_STATE_HANDSHAKE_COMPLETE)) if(WIIMOTE_IS_SET(wm,WIIMOTE_STATE_HANDSHAKE_COMPLETE))
{ {
if(WIIMOTE_DBG)printf("rehandshake\n"); #ifdef WIIMOTE_DBG
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE); printf("rehandshake\n");
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);//forzamos un handshake por si venimos de un hanshake completo #endif
} WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE);
} WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);//forzamos un handshake por si venimos de un hanshake completo
}
}
if(!attachment && WIIMOTE_IS_SET(wm,WIIMOTE_STATE_HANDSHAKE)) if(!attachment && WIIMOTE_IS_SET(wm,WIIMOTE_STATE_HANDSHAKE))
{ {
wm->handshake_state = 2; wm->handshake_state = 2;
continue; continue;
} }
return 0; return 0;
} }
case 2://find handshake no expansion case 2://find handshake no expansion
{ {
if(WIIMOTE_DBG)printf("Finalizado HANDSHAKE SIN EXPANSION\n"); #ifdef WIIMOTE_DBG
wiimote_data_report(wm,WM_RPT_BTN); printf("Finalizado HANDSHAKE SIN EXPANSION\n");
wm->handshake_state = 6; #endif
continue; wiimote_data_report(wm,WM_RPT_BTN);
} wm->handshake_state = 6;
case 3://find handshake expansion continue;
{ }
if(WIIMOTE_DBG)printf("Finalizado HANDSHAKE CON EXPANSION\n"); case 3://find handshake expansion
wiimote_data_report(wm,WM_RPT_BTN_EXP); {
wm->handshake_state = 6; #ifdef WIIMOTE_DBG
continue; printf("Finalizado HANDSHAKE CON EXPANSION\n");
} #endif
case 4: wiimote_data_report(wm,WM_RPT_BTN_EXP);
{ wm->handshake_state = 6;
if(event != WM_RPT_READ) continue;
return 0; }
case 4:
{
if(event != WM_RPT_READ)
return 0;
int id = BIG_ENDIAN_LONG(*(int*)(data)); int id = BIG_ENDIAN_LONG(*(int*)(data));
if(WIIMOTE_DBG)printf("Expansion id=0x%04x\n",id); #ifdef WIIMOTE_DBG
printf("Expansion id=0x%04x\n",id);
#endif
if(id!=/*EXP_ID_CODE_CLASSIC_CONTROLLER*/0xa4200101) if(id!=/*EXP_ID_CODE_CLASSIC_CONTROLLER*/0xa4200101)
{ {
wm->handshake_state = 2; wm->handshake_state = 2;
//WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP); //WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP);
continue; continue;
} }
else else
{ {
usleep(100000); usleep(100000);
wiimote_read_data(wm, WM_EXP_MEM_CALIBR, 16);//pedimos datos de calibracion del JOY! wiimote_read_data(wm, WM_EXP_MEM_CALIBR, 16);//pedimos datos de calibracion del JOY!
wm->handshake_state = 5; wm->handshake_state = 5;
} }
return 0; return 0;
} }
case 5: case 5:
{ {
if(event != WM_RPT_READ) if(event != WM_RPT_READ)
return 0; return 0;
classic_ctrl_handshake(wm, &wm->exp.classic, data,len); classic_ctrl_handshake(wm, &wm->exp.classic, data,len);
wm->handshake_state = 3; wm->handshake_state = 3;
continue; 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;
} }
} }
} }
} }
@ -393,25 +418,25 @@ int wiimote_handshake(struct wiimote_t* wm, byte event, byte* data, unsigned sh
* *
* 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];
buf[0] = WM_SET_REPORT | WM_BT_OUTPUT; buf[0] = WM_SET_REPORT | WM_BT_OUTPUT;
buf[1] = report_type; buf[1] = report_type;
memcpy(buf+2, msg, len); memcpy(buf+2, msg, len);
if(WIIMOTE_DBG) #ifdef WIIMOTE_DBG
{ int x = 2;
int x = 2; printf("[DEBUG] (id %i) SEND: (%x) %.2x ", wm->unid, buf[0], buf[1]);
printf("[DEBUG] (id %i) SEND: (%x) %.2x ", wm->unid, buf[0], buf[1]); for (; x < len+2; ++x)
for (; x < len+2; ++x) printf("%.2x ", buf[x]);
printf("%.2x ", buf[x]); printf("\n");
printf("\n"); #endif
}
bt_send_l2cap( wm->c_source_cid, buf, len+2); bt_send_l2cap( wm->c_source_cid, buf, len+2);
return 1; return 1;
} }
/** /**
@ -444,7 +469,9 @@ int wiimote_read_data(struct wiimote_t* wm, unsigned int addr, unsigned short le
/* the length is in big endian */ /* the length is in big endian */
*(short*)(buf + 4) = BIG_ENDIAN_SHORT(len); *(short*)(buf + 4) = BIG_ENDIAN_SHORT(len);
if(WIIMOTE_DBG)printf("Request read at address: 0x%x length: %i", addr, len); #ifdef WIIMOTE_DBG
printf("Request read at address: 0x%x length: %i", addr, len);
#endif
wiimote_send(wm, WM_CMD_READ_DATA, buf, 6); wiimote_send(wm, WM_CMD_READ_DATA, buf, 6);
return 1; return 1;
@ -466,16 +493,15 @@ int wiimote_write_data(struct wiimote_t* wm, unsigned int addr, byte* data, byte
if (!data || !len) if (!data || !len)
return 0; return 0;
if(WIIMOTE_DBG)printf("Writing %i bytes to memory location 0x%x...\n", len, addr); #ifdef WIIMOTE_DBG
printf("Writing %i bytes to memory location 0x%x...\n", len, addr);
if(WIIMOTE_DBG) int i = 0;
{ printf("Write data is: ");
int i = 0; for (; i < len; ++i)
printf("Write data is: "); printf("%x ", data[i]);
for (; i < len; ++i) printf("\n");
printf("%x ", data[i]); #endif
printf("\n");
}
/* the offset is in big endian */ /* the offset is in big endian */
*(int*)(buf) = BIG_ENDIAN_LONG(addr); *(int*)(buf) = BIG_ENDIAN_LONG(addr);
@ -505,7 +531,8 @@ void calc_joystick_state(struct joystick_t* js, float x, float y);
* *
* @return Returns 1 if handshake was successful, 0 if not. * @return Returns 1 if handshake was successful, 0 if not.
*/ */
int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte* data, unsigned short len) { int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte* data, unsigned short len)
{
int offset = 0; int offset = 0;
cc->btns = 0; cc->btns = 0;
@ -517,15 +544,14 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte
for (i = 0; i < len; ++i) for (i = 0; i < len; ++i)
data[i] = (data[i] ^ 0x17) + 0x17; data[i] = (data[i] ^ 0x17) + 0x17;
*/ */
if(WIIMOTE_DBG) #ifdef WIIMOTE_DBG
{ int x = 0;
int x = 0; printf("[DECRIPTED]");
printf("[DECRIPTED]"); for (; x < len; x++)
for (; x < len; x++) printf("%.2x ", data[x]);
printf("%.2x ", data[x]); printf("\n");
printf("\n"); #endif
}
/* /*
if (data[offset] == 0xFF) if (data[offset] == 0xFF)
@ -608,8 +634,9 @@ void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg) {
rx = ((msg[0] & 0xC0) >> 3) | ((msg[1] & 0xC0) >> 5) | ((msg[2] & 0x80) >> 7); rx = ((msg[0] & 0xC0) >> 3) | ((msg[1] & 0xC0) >> 5) | ((msg[2] & 0x80) >> 7);
ry = (msg[2] & 0x1F); ry = (msg[2] & 0x1F);
if(WIIMOTE_DBG) #ifdef WIIMOTE_DBG
printf("lx ly rx ry %d %d %d %d\n",lx,ly,rx,ry); printf("lx ly rx ry %d %d %d %d\n",lx,ly,rx,ry);
#endif
calc_joystick_state(&cc->ljs, lx, ly); calc_joystick_state(&cc->ljs, lx, ly);
calc_joystick_state(&cc->rjs, rx, ry); calc_joystick_state(&cc->rjs, rx, ry);

View File

@ -52,7 +52,7 @@ extern "C" {
#define WIIMOTE_PI 3.14159265 #define WIIMOTE_PI 3.14159265
#define WIIMOTE_DBG 0 //#define WIIMOTE_DBG 0
/* Convert between radians and degrees */ /* Convert between radians and degrees */
#define RAD_TO_DEGREE(r) ((r * 180.0f) / WIIMOTE_PI) #define RAD_TO_DEGREE(r) ((r * 180.0f) / WIIMOTE_PI)