BlackOneとAndroidの連携デモプログラム AndroidAccessoryを改造してBlackOneとAndroidが連携できるようにしました。 サポートしているのは、デモアプリの ”Buttons” B1-SW1, B2-SW2, B3-SW3 ”LED2” RGB-LED のみです。 LCDに表示するイメージをマイクロSDカードに入れてLCDのソケットに挿入しておく必要があります。 イメージは、320X240ドットで”\Image”という名前のフォルダの直下に”10.jpg”という名前で入れてください。

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADK.cpp Source File

ADK.cpp

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 
00006 #include "USBHost.h"
00007 #include "Utils.h"
00008 #include "ps3.h"
00009 #include "ADK.h"
00010 #include "mbed.h"
00011 
00012 //#define  ADKLOG 1
00013 #define  ADKLOG 0
00014 #if ADKLOG
00015 #define  LOG(...)       printf(__VA_ARGS__)
00016 #define  Log(...)       printf(__VA_ARGS__)
00017 #define  log(...)       printf(__VA_ARGS__)
00018 
00019 #else
00020 #define  LOG(...)       do {} while(0)
00021 #define  Log(...)       do {} while(0)
00022 #define  log(...)       do {} while(0)
00023 
00024 #endif
00025 
00026 
00027 PwmOut red1(p26);
00028 PwmOut gren1(p24);
00029 PwmOut blue1(p25);
00030 
00031 PwmOut red2(p22);
00032 PwmOut gren2(p23);
00033 PwmOut blue2(p21);
00034 
00035 PwmOut led1(LED1);
00036 PwmOut led2(LED2);
00037 PwmOut led3(LED3);
00038 PwmOut led4(LED4);
00039 
00040 /*
00041 InterruptIn  sw1(p21);
00042 InterruptIn  sw2(p22);
00043 InterruptIn  sw3(p23);
00044 
00045 DigitalIn sw1(p21);
00046 DigitalIn sw2(p22);
00047 DigitalIn sw3(p23);
00048 */
00049 DigitalIn sw1(p17);
00050 DigitalIn sw2(p19);
00051 DigitalIn sw3(p20);
00052 
00053 u8 readbuff[3];
00054 u8 writebuf[3];
00055 //switch data backup
00056 u8 sw1b,sw2b,sw3b;
00057 
00058 
00059 void AdkUSB::setup() {
00060     sw1.mode(PullUp);
00061     sw2.mode(PullUp);
00062     sw3.mode(PullUp);
00063     sw1b=sw2b=sw3b=sw1;
00064 }
00065 
00066 void AdkUSB::loop() {
00067     log("enter loop\r\n");
00068     u8 buf[3];
00069     int ret=-1;
00070     //booting wait
00071     //wait(10);
00072     while (!this->_loopend) {
00073         bool w_flag=false;
00074         //wait_ms(4);
00075 
00076         buf[0]=0x01;
00077         //switch1
00078         if (sw1!=sw1b) {
00079             log("sw1=%d\r\n",sw1.read());
00080             buf[1]=0;
00081             buf[2]=!sw1;
00082             ret=this->write(buf,sizeof(buf));
00083             //wait_ms(4);
00084             sw1b=sw1;
00085             w_flag=true;
00086         }
00087         //switch2
00088         if (sw2!=sw2b) {
00089             log("sw2=%d\r\n",sw2.read());
00090             buf[1]=1;
00091             buf[2]=!sw2;
00092             ret=this->write(buf,sizeof(buf));
00093             //wait_ms(4);
00094             sw2b=sw2;
00095             w_flag=true;
00096         }
00097         //switch3
00098         if (sw3!=sw3b) {
00099             log("sw3=%d\r\n",sw3.read());
00100             buf[1]=2;
00101             buf[2]=!sw3;
00102             ret=this->write(buf,sizeof(buf));
00103             //wait_ms(4);
00104             sw3b=sw3;
00105             w_flag=true;
00106         }
00107 
00108         if (!w_flag) {
00109             buf[0]=buf[1]=buf[2]=0;
00110             ret=this->write(buf,sizeof(buf));
00111         }
00112     }
00113 
00114     //reset
00115 //    led1=led2=led3=led4=0.0;
00116       red1=gren1=blue1=led4=0.0;
00117       red2=gren2=blue2=0.0;
00118     log("---------------------------------------------------------------loop end\r\n");
00119 }
00120 void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) {
00121 
00122     log("AdkreadCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n",
00123         device,endpoint,status,buf,len,userData);
00124 
00125     AdkUSB* t = (AdkUSB*)userData;
00126     if (status!=0) {
00127         log("loop end.\r\n");
00128         t->loopend();
00129         return;
00130     }
00131 
00132 
00133 //    gren=((float)buf[2])/255.0;
00134     if (buf[0] == 0x2) {
00135         if (buf[1] == 0x0) {
00136             log("led1=%d\r\n",buf[2]);
00137 //            led1=((float)buf[2])/255.0;
00138             gren2=((float)buf[2])/255.0;
00139 //        } else if (buf[1]==0x3) {
00140           } else if (buf[1]==0x1) {
00141 //            led2=((float)buf[2])/255.0;
00142             red2=((float)buf[2])/255.0;
00143 //        } else if (buf[1]==0x6) {
00144           } else if (buf[1]==0x2) {
00145 //            led3=((float)buf[2])/255.0;
00146             blue2=((float)buf[2])/255.0;
00147           } else if (buf[1]==0x3) {
00148             red1=((float)buf[2])/255.0;
00149           } else if (buf[1]==0x4) {
00150             blue1=((float)buf[2])/255.0;
00151           } else if (buf[1]==0x5) {
00152             gren1=((float)buf[2])/255.0;
00153         }
00154     } else if (buf[0] ==0x3) {
00155         if (buf[1] == 0x0) {
00156             led4=buf[2]? 1.0:0.0;
00157         }
00158     }
00159 
00160     USBBulkTransfer(device, endpoint , buf, len, AdkreadCallback, userData);
00161     wait_ms(4);
00162 
00163 }
00164 
00165 /*
00166 void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) {
00167 
00168     log("AdkwriteCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n",
00169         device,endpoint,status,buf,len,userData);
00170 
00171     AdkUSB* t = (AdkUSB*)userData;
00172 
00173 }
00174 */
00175 
00176 
00177 int getProtocol(int device);
00178 void sendString(int device, int index, const char *str);
00179 
00180 bool switchDevice(int device) {
00181 
00182     if (1==getProtocol(device)) {
00183         log("device supports protocol 1\r\n");
00184 
00185     } else {
00186         log("could not read device protocol version\r\n");
00187         return false;
00188     }
00189 
00190 
00191     sendString(device,ACCESSORY_STRING_MANUFACTURER,"Google, Inc.");
00192     sendString(device,ACCESSORY_STRING_MODEL,"DemoKit");
00193     sendString(device,ACCESSORY_STRING_DESCRIPTION,"DemoKit Arduino Board");
00194     sendString(device,ACCESSORY_STRING_VERSION,"1.0");
00195     sendString(device,ACCESSORY_STRING_URI,"http://www.android.com");
00196     sendString(device,ACCESSORY_STRING_SERIAL,"0000000012345678");
00197     USBControlTransfer(device,
00198                        HOST_TO_DEVICE |REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
00199                        ACCESSORY_START,
00200                        0,//value
00201                        0, //index
00202                        0,
00203                        0,
00204                        0,
00205                        0 );
00206 
00207     wait_ms(400);
00208 
00209     return true;
00210 
00211 }
00212 
00213 
00214 int getProtocol(int device) {
00215     s16 data=-1;
00216     USBControlTransfer(device,
00217                        DEVICE_TO_HOST|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
00218                        ACCESSORY_GET_PROTOCOL,
00219                        0,//value
00220                        0, //index
00221                        (u8*)&data,
00222                        2,
00223                        0,
00224                        0 );
00225     //printf("return %d\r\n",data);
00226     return data;
00227 
00228 }
00229 
00230 void sendString(int device, int index, const char *str) {
00231 
00232     LOG("send_string start(%d,%d,%s)  %d \r\n",device,index,str,strlen(str)+1);
00233     u8 buffer[255];
00234     strcpy((char*)buffer,str);
00235     //thankyou curryman san
00236     USBControlTransfer(device,
00237                        HOST_TO_DEVICE|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
00238                        ACCESSORY_SEND_STRING,
00239                        0,//value
00240                        index,
00241                        buffer,
00242                        strlen(str)+1
00243                       );
00244 
00245     LOG("send_string end(%d,%d,%s)\r\n",device,index,str);
00246 
00247 }
00248 
00249 //int  USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData)
00250 int AdkUSB::read(u8 *buff, int len) {
00251     int ret=USBBulkTransfer(_device,input_ep|0x80,buff,len,0,0);
00252     log("adkUSB read  ret=%d \r\n",ret);
00253     return ret;
00254 }
00255 
00256 void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData);
00257 
00258 int AdkUSB::write(u8 *buff, int len) {
00259     log("adkUSB write -------                                                     xxx \r\n");
00260     int ret=USBBulkTransfer(_device,output_ep,buff,len/*,AdkwriteCallback,this*/);
00261     log("ret=%d \r\n",ret);
00262     return ret;
00263 }
00264 
00265 void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData);
00266 
00267 AdkUSB::AdkUSB(int device, int configuration, int interfaceNumber) {
00268 
00269     log("connecting Android \r\n");
00270     _device = device;
00271     _configuration = configuration;
00272     _interfaceNumber = interfaceNumber;
00273     //for loop()
00274     _loopend=false;
00275     printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber);
00276     int err;
00277 
00278     u8 buffer[255];
00279     err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,4);
00280 
00281     if (err < 0) {
00282         log("Failed to get descriptor\r\n");
00283         return;
00284     }
00285 
00286 
00287     int len = buffer[2] | (buffer[3] << 8);
00288     if (len > sizeof(buffer)) {
00289         log("config descriptor too large\n");
00290         /* might want to truncate here */
00291         return;
00292     }
00293     err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,len);
00294     u8* p = buffer;
00295     input_ep=0;
00296     output_ep=0;
00297     EndpointDescriptor *epDesc;
00298     while (p<(buffer+len)) {
00299         u8 descLen  = p[0];
00300         u8 descType = p[1];
00301         log("descLen=%d,descType=%d\r\n",descLen,descType);
00302         switch (descType) {
00303             case DESCRIPTOR_TYPE_CONFIGURATION:
00304                 log("config desc\r\n");
00305                 break;
00306             case DESCRIPTOR_TYPE_INTERFACE:
00307                 log("interface desc\r\n");
00308                 break;
00309             case DESCRIPTOR_TYPE_ENDPOINT:
00310                 epDesc=(EndpointDescriptor*)p;
00311                 if (!input_ep && (epDesc->bEndpointAddress& 0x80)) {
00312                     input_ep=epDesc->bEndpointAddress& 0x7f;
00313                     //PacketSize drop
00314                     log("input Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
00315 
00316                 } else if (!output_ep) {
00317                     output_ep=epDesc->bEndpointAddress& 0x7f;
00318                     //PacketSize drop
00319                     log("output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
00320                 } else {
00321                     //other
00322                     log("non input,output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
00323                 }
00324                 break;
00325             default:
00326                 log("unkown desc type(%d) \r\n",descType);
00327         }
00328         p+=descLen;
00329     }
00330 
00331     if (!(input_ep && output_ep)) {
00332         log("can't find accessory endpoints\r\n");
00333         return;
00334     }
00335 
00336     log("SetConfiguration\r\n");
00337     err = SetConfiguration(device,configuration);
00338     if (err < 0) {
00339         log("SetConfiguration error\r\n");
00340         return;
00341     }
00342 
00343     log("interrupt setup\r\n");
00344     //interrupt setup
00345     int ret=USBBulkTransfer(_device,input_ep|0x80,readbuff,sizeof(readbuff),AdkreadCallback,this);
00346     log("ret=%d \r\n",ret);
00347     log("ADK Standby\r\n");
00348 
00349     this->setup();
00350 }
00351