![](/media/cache/profiles/ddcbe25988981920c872c1787382f04d.50x50_q85.jpg)
XOOMの動作状況を聞き処理を変えてみました。 USBケーブルを抜いた際に処理を終了するようにしました。
Diff: ADK.cpp
- Revision:
- 3:432e5675d240
- Parent:
- 2:a05c7cbe396f
--- a/ADK.cpp Thu May 26 19:08:55 2011 +0000 +++ b/ADK.cpp Fri May 27 18:51:15 2011 +0000 @@ -23,6 +23,7 @@ #endif + PwmOut led1(LED1); PwmOut led2(LED2); PwmOut led3(LED3); @@ -48,23 +49,19 @@ sw1.mode(PullUp); sw2.mode(PullUp); sw3.mode(PullUp); - - //sw1.fall(sw1_write); - // sw1.rise(sw1_write); - - sw1b=sw2b=sw3b=0; - + sw1b=sw2b=sw3b=sw1; } void AdkUSB::loop() { log("enter loop\r\n"); u8 buf[3]; int ret=-1; - - while (1) { + //booting wait + //wait(10); + while (!this->_loopend) { bool w_flag=false; //wait_ms(4); - + buf[0]=0x01; //switch1 if (sw1!=sw1b) { @@ -72,7 +69,7 @@ buf[1]=0; buf[2]=!sw1; ret=this->write(buf,sizeof(buf)); - wait_ms(4); + //wait_ms(4); sw1b=sw1; w_flag=true; } @@ -82,7 +79,7 @@ buf[1]=1; buf[2]=!sw2; ret=this->write(buf,sizeof(buf)); - wait_ms(4); + //wait_ms(4); sw2b=sw2; w_flag=true; } @@ -92,31 +89,33 @@ buf[1]=2; buf[2]=!sw3; ret=this->write(buf,sizeof(buf)); - wait_ms(4); + //wait_ms(4); sw3b=sw3; w_flag=true; } - if(!w_flag) - { - buf[0]=buf[1]=buf[2]=0; - ret=this->write(buf,sizeof(buf)); - } - - if (ret<0) { - led1=led2=led3=led4=0.0; - log("---------------------------------------------------------------loop end\r\n"); - return; - + if (!w_flag) { + buf[0]=buf[1]=buf[2]=0; + ret=this->write(buf,sizeof(buf)); } } + + //reset + led1=led2=led3=led4=0.0; + log("---------------------------------------------------------------loop end\r\n"); } void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) { log("AdkreadCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n", device,endpoint,status,buf,len,userData); - //AdkUSB* t = (AdkUSB*)userData; + AdkUSB* t = (AdkUSB*)userData; + if (status!=0) { + log("loop end.\r\n"); + t->loopend(); + return; + } + if (buf[0] == 0x2) { if (buf[1] == 0x0) { @@ -170,7 +169,6 @@ sendString(device,ACCESSORY_STRING_VERSION,"1.0"); sendString(device,ACCESSORY_STRING_URI,"http://www.android.com"); sendString(device,ACCESSORY_STRING_SERIAL,"0000000012345678"); - USBControlTransfer(device, HOST_TO_DEVICE |REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, ACCESSORY_START, @@ -181,7 +179,7 @@ 0, 0 ); - wait(1); + wait_ms(400); return true; @@ -207,14 +205,15 @@ void sendString(int device, int index, const char *str) { LOG("send_string start(%d,%d,%s) %d \r\n",device,index,str,strlen(str)+1); - - //this is diffrent google's sample but error ocard same sample + u8 buffer[255]; + strcpy((char*)buffer,str); + //thankyou curryman san USBControlTransfer(device, - DEVICE_TO_HOST /*HOST_TO_DEVIC use error why? please teach me*/|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, + HOST_TO_DEVICE|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, ACCESSORY_SEND_STRING, 0,//value index, - (u8*)str, + buffer, strlen(str)+1 ); @@ -241,10 +240,13 @@ void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData); AdkUSB::AdkUSB(int device, int configuration, int interfaceNumber) { + log("connecting Android \r\n"); _device = device; _configuration = configuration; _interfaceNumber = interfaceNumber; + //for loop() + _loopend=false; printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber); int err; @@ -310,7 +312,7 @@ err = SetConfiguration(device,configuration); if (err < 0) { log("SetConfiguration error\r\n"); - wait(10); + return; } log("interrupt setup\r\n");