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

Committer:
techand
Date:
Fri Dec 23 04:33:33 2011 +0000
Revision:
0:7b556109fd46

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
techand 0:7b556109fd46 1 #include <stdio.h>
techand 0:7b556109fd46 2 #include <stdlib.h>
techand 0:7b556109fd46 3 #include <stdio.h>
techand 0:7b556109fd46 4 #include <string.h>
techand 0:7b556109fd46 5
techand 0:7b556109fd46 6 #include "USBHost.h"
techand 0:7b556109fd46 7 #include "Utils.h"
techand 0:7b556109fd46 8 #include "ps3.h"
techand 0:7b556109fd46 9 #include "ADK.h"
techand 0:7b556109fd46 10 #include "mbed.h"
techand 0:7b556109fd46 11
techand 0:7b556109fd46 12 //#define ADKLOG 1
techand 0:7b556109fd46 13 #define ADKLOG 0
techand 0:7b556109fd46 14 #if ADKLOG
techand 0:7b556109fd46 15 #define LOG(...) printf(__VA_ARGS__)
techand 0:7b556109fd46 16 #define Log(...) printf(__VA_ARGS__)
techand 0:7b556109fd46 17 #define log(...) printf(__VA_ARGS__)
techand 0:7b556109fd46 18
techand 0:7b556109fd46 19 #else
techand 0:7b556109fd46 20 #define LOG(...) do {} while(0)
techand 0:7b556109fd46 21 #define Log(...) do {} while(0)
techand 0:7b556109fd46 22 #define log(...) do {} while(0)
techand 0:7b556109fd46 23
techand 0:7b556109fd46 24 #endif
techand 0:7b556109fd46 25
techand 0:7b556109fd46 26
techand 0:7b556109fd46 27 PwmOut red1(p26);
techand 0:7b556109fd46 28 PwmOut gren1(p24);
techand 0:7b556109fd46 29 PwmOut blue1(p25);
techand 0:7b556109fd46 30
techand 0:7b556109fd46 31 PwmOut red2(p22);
techand 0:7b556109fd46 32 PwmOut gren2(p23);
techand 0:7b556109fd46 33 PwmOut blue2(p21);
techand 0:7b556109fd46 34
techand 0:7b556109fd46 35 PwmOut led1(LED1);
techand 0:7b556109fd46 36 PwmOut led2(LED2);
techand 0:7b556109fd46 37 PwmOut led3(LED3);
techand 0:7b556109fd46 38 PwmOut led4(LED4);
techand 0:7b556109fd46 39
techand 0:7b556109fd46 40 /*
techand 0:7b556109fd46 41 InterruptIn sw1(p21);
techand 0:7b556109fd46 42 InterruptIn sw2(p22);
techand 0:7b556109fd46 43 InterruptIn sw3(p23);
techand 0:7b556109fd46 44
techand 0:7b556109fd46 45 DigitalIn sw1(p21);
techand 0:7b556109fd46 46 DigitalIn sw2(p22);
techand 0:7b556109fd46 47 DigitalIn sw3(p23);
techand 0:7b556109fd46 48 */
techand 0:7b556109fd46 49 DigitalIn sw1(p17);
techand 0:7b556109fd46 50 DigitalIn sw2(p19);
techand 0:7b556109fd46 51 DigitalIn sw3(p20);
techand 0:7b556109fd46 52
techand 0:7b556109fd46 53 u8 readbuff[3];
techand 0:7b556109fd46 54 u8 writebuf[3];
techand 0:7b556109fd46 55 //switch data backup
techand 0:7b556109fd46 56 u8 sw1b,sw2b,sw3b;
techand 0:7b556109fd46 57
techand 0:7b556109fd46 58
techand 0:7b556109fd46 59 void AdkUSB::setup() {
techand 0:7b556109fd46 60 sw1.mode(PullUp);
techand 0:7b556109fd46 61 sw2.mode(PullUp);
techand 0:7b556109fd46 62 sw3.mode(PullUp);
techand 0:7b556109fd46 63 sw1b=sw2b=sw3b=sw1;
techand 0:7b556109fd46 64 }
techand 0:7b556109fd46 65
techand 0:7b556109fd46 66 void AdkUSB::loop() {
techand 0:7b556109fd46 67 log("enter loop\r\n");
techand 0:7b556109fd46 68 u8 buf[3];
techand 0:7b556109fd46 69 int ret=-1;
techand 0:7b556109fd46 70 //booting wait
techand 0:7b556109fd46 71 //wait(10);
techand 0:7b556109fd46 72 while (!this->_loopend) {
techand 0:7b556109fd46 73 bool w_flag=false;
techand 0:7b556109fd46 74 //wait_ms(4);
techand 0:7b556109fd46 75
techand 0:7b556109fd46 76 buf[0]=0x01;
techand 0:7b556109fd46 77 //switch1
techand 0:7b556109fd46 78 if (sw1!=sw1b) {
techand 0:7b556109fd46 79 log("sw1=%d\r\n",sw1.read());
techand 0:7b556109fd46 80 buf[1]=0;
techand 0:7b556109fd46 81 buf[2]=!sw1;
techand 0:7b556109fd46 82 ret=this->write(buf,sizeof(buf));
techand 0:7b556109fd46 83 //wait_ms(4);
techand 0:7b556109fd46 84 sw1b=sw1;
techand 0:7b556109fd46 85 w_flag=true;
techand 0:7b556109fd46 86 }
techand 0:7b556109fd46 87 //switch2
techand 0:7b556109fd46 88 if (sw2!=sw2b) {
techand 0:7b556109fd46 89 log("sw2=%d\r\n",sw2.read());
techand 0:7b556109fd46 90 buf[1]=1;
techand 0:7b556109fd46 91 buf[2]=!sw2;
techand 0:7b556109fd46 92 ret=this->write(buf,sizeof(buf));
techand 0:7b556109fd46 93 //wait_ms(4);
techand 0:7b556109fd46 94 sw2b=sw2;
techand 0:7b556109fd46 95 w_flag=true;
techand 0:7b556109fd46 96 }
techand 0:7b556109fd46 97 //switch3
techand 0:7b556109fd46 98 if (sw3!=sw3b) {
techand 0:7b556109fd46 99 log("sw3=%d\r\n",sw3.read());
techand 0:7b556109fd46 100 buf[1]=2;
techand 0:7b556109fd46 101 buf[2]=!sw3;
techand 0:7b556109fd46 102 ret=this->write(buf,sizeof(buf));
techand 0:7b556109fd46 103 //wait_ms(4);
techand 0:7b556109fd46 104 sw3b=sw3;
techand 0:7b556109fd46 105 w_flag=true;
techand 0:7b556109fd46 106 }
techand 0:7b556109fd46 107
techand 0:7b556109fd46 108 if (!w_flag) {
techand 0:7b556109fd46 109 buf[0]=buf[1]=buf[2]=0;
techand 0:7b556109fd46 110 ret=this->write(buf,sizeof(buf));
techand 0:7b556109fd46 111 }
techand 0:7b556109fd46 112 }
techand 0:7b556109fd46 113
techand 0:7b556109fd46 114 //reset
techand 0:7b556109fd46 115 // led1=led2=led3=led4=0.0;
techand 0:7b556109fd46 116 red1=gren1=blue1=led4=0.0;
techand 0:7b556109fd46 117 red2=gren2=blue2=0.0;
techand 0:7b556109fd46 118 log("---------------------------------------------------------------loop end\r\n");
techand 0:7b556109fd46 119 }
techand 0:7b556109fd46 120 void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) {
techand 0:7b556109fd46 121
techand 0:7b556109fd46 122 log("AdkreadCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n",
techand 0:7b556109fd46 123 device,endpoint,status,buf,len,userData);
techand 0:7b556109fd46 124
techand 0:7b556109fd46 125 AdkUSB* t = (AdkUSB*)userData;
techand 0:7b556109fd46 126 if (status!=0) {
techand 0:7b556109fd46 127 log("loop end.\r\n");
techand 0:7b556109fd46 128 t->loopend();
techand 0:7b556109fd46 129 return;
techand 0:7b556109fd46 130 }
techand 0:7b556109fd46 131
techand 0:7b556109fd46 132
techand 0:7b556109fd46 133 // gren=((float)buf[2])/255.0;
techand 0:7b556109fd46 134 if (buf[0] == 0x2) {
techand 0:7b556109fd46 135 if (buf[1] == 0x0) {
techand 0:7b556109fd46 136 log("led1=%d\r\n",buf[2]);
techand 0:7b556109fd46 137 // led1=((float)buf[2])/255.0;
techand 0:7b556109fd46 138 gren2=((float)buf[2])/255.0;
techand 0:7b556109fd46 139 // } else if (buf[1]==0x3) {
techand 0:7b556109fd46 140 } else if (buf[1]==0x1) {
techand 0:7b556109fd46 141 // led2=((float)buf[2])/255.0;
techand 0:7b556109fd46 142 red2=((float)buf[2])/255.0;
techand 0:7b556109fd46 143 // } else if (buf[1]==0x6) {
techand 0:7b556109fd46 144 } else if (buf[1]==0x2) {
techand 0:7b556109fd46 145 // led3=((float)buf[2])/255.0;
techand 0:7b556109fd46 146 blue2=((float)buf[2])/255.0;
techand 0:7b556109fd46 147 } else if (buf[1]==0x3) {
techand 0:7b556109fd46 148 red1=((float)buf[2])/255.0;
techand 0:7b556109fd46 149 } else if (buf[1]==0x4) {
techand 0:7b556109fd46 150 blue1=((float)buf[2])/255.0;
techand 0:7b556109fd46 151 } else if (buf[1]==0x5) {
techand 0:7b556109fd46 152 gren1=((float)buf[2])/255.0;
techand 0:7b556109fd46 153 }
techand 0:7b556109fd46 154 } else if (buf[0] ==0x3) {
techand 0:7b556109fd46 155 if (buf[1] == 0x0) {
techand 0:7b556109fd46 156 led4=buf[2]? 1.0:0.0;
techand 0:7b556109fd46 157 }
techand 0:7b556109fd46 158 }
techand 0:7b556109fd46 159
techand 0:7b556109fd46 160 USBBulkTransfer(device, endpoint , buf, len, AdkreadCallback, userData);
techand 0:7b556109fd46 161 wait_ms(4);
techand 0:7b556109fd46 162
techand 0:7b556109fd46 163 }
techand 0:7b556109fd46 164
techand 0:7b556109fd46 165 /*
techand 0:7b556109fd46 166 void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) {
techand 0:7b556109fd46 167
techand 0:7b556109fd46 168 log("AdkwriteCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n",
techand 0:7b556109fd46 169 device,endpoint,status,buf,len,userData);
techand 0:7b556109fd46 170
techand 0:7b556109fd46 171 AdkUSB* t = (AdkUSB*)userData;
techand 0:7b556109fd46 172
techand 0:7b556109fd46 173 }
techand 0:7b556109fd46 174 */
techand 0:7b556109fd46 175
techand 0:7b556109fd46 176
techand 0:7b556109fd46 177 int getProtocol(int device);
techand 0:7b556109fd46 178 void sendString(int device, int index, const char *str);
techand 0:7b556109fd46 179
techand 0:7b556109fd46 180 bool switchDevice(int device) {
techand 0:7b556109fd46 181
techand 0:7b556109fd46 182 if (1==getProtocol(device)) {
techand 0:7b556109fd46 183 log("device supports protocol 1\r\n");
techand 0:7b556109fd46 184
techand 0:7b556109fd46 185 } else {
techand 0:7b556109fd46 186 log("could not read device protocol version\r\n");
techand 0:7b556109fd46 187 return false;
techand 0:7b556109fd46 188 }
techand 0:7b556109fd46 189
techand 0:7b556109fd46 190
techand 0:7b556109fd46 191 sendString(device,ACCESSORY_STRING_MANUFACTURER,"Google, Inc.");
techand 0:7b556109fd46 192 sendString(device,ACCESSORY_STRING_MODEL,"DemoKit");
techand 0:7b556109fd46 193 sendString(device,ACCESSORY_STRING_DESCRIPTION,"DemoKit Arduino Board");
techand 0:7b556109fd46 194 sendString(device,ACCESSORY_STRING_VERSION,"1.0");
techand 0:7b556109fd46 195 sendString(device,ACCESSORY_STRING_URI,"http://www.android.com");
techand 0:7b556109fd46 196 sendString(device,ACCESSORY_STRING_SERIAL,"0000000012345678");
techand 0:7b556109fd46 197 USBControlTransfer(device,
techand 0:7b556109fd46 198 HOST_TO_DEVICE |REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
techand 0:7b556109fd46 199 ACCESSORY_START,
techand 0:7b556109fd46 200 0,//value
techand 0:7b556109fd46 201 0, //index
techand 0:7b556109fd46 202 0,
techand 0:7b556109fd46 203 0,
techand 0:7b556109fd46 204 0,
techand 0:7b556109fd46 205 0 );
techand 0:7b556109fd46 206
techand 0:7b556109fd46 207 wait_ms(400);
techand 0:7b556109fd46 208
techand 0:7b556109fd46 209 return true;
techand 0:7b556109fd46 210
techand 0:7b556109fd46 211 }
techand 0:7b556109fd46 212
techand 0:7b556109fd46 213
techand 0:7b556109fd46 214 int getProtocol(int device) {
techand 0:7b556109fd46 215 s16 data=-1;
techand 0:7b556109fd46 216 USBControlTransfer(device,
techand 0:7b556109fd46 217 DEVICE_TO_HOST|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
techand 0:7b556109fd46 218 ACCESSORY_GET_PROTOCOL,
techand 0:7b556109fd46 219 0,//value
techand 0:7b556109fd46 220 0, //index
techand 0:7b556109fd46 221 (u8*)&data,
techand 0:7b556109fd46 222 2,
techand 0:7b556109fd46 223 0,
techand 0:7b556109fd46 224 0 );
techand 0:7b556109fd46 225 //printf("return %d\r\n",data);
techand 0:7b556109fd46 226 return data;
techand 0:7b556109fd46 227
techand 0:7b556109fd46 228 }
techand 0:7b556109fd46 229
techand 0:7b556109fd46 230 void sendString(int device, int index, const char *str) {
techand 0:7b556109fd46 231
techand 0:7b556109fd46 232 LOG("send_string start(%d,%d,%s) %d \r\n",device,index,str,strlen(str)+1);
techand 0:7b556109fd46 233 u8 buffer[255];
techand 0:7b556109fd46 234 strcpy((char*)buffer,str);
techand 0:7b556109fd46 235 //thankyou curryman san
techand 0:7b556109fd46 236 USBControlTransfer(device,
techand 0:7b556109fd46 237 HOST_TO_DEVICE|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
techand 0:7b556109fd46 238 ACCESSORY_SEND_STRING,
techand 0:7b556109fd46 239 0,//value
techand 0:7b556109fd46 240 index,
techand 0:7b556109fd46 241 buffer,
techand 0:7b556109fd46 242 strlen(str)+1
techand 0:7b556109fd46 243 );
techand 0:7b556109fd46 244
techand 0:7b556109fd46 245 LOG("send_string end(%d,%d,%s)\r\n",device,index,str);
techand 0:7b556109fd46 246
techand 0:7b556109fd46 247 }
techand 0:7b556109fd46 248
techand 0:7b556109fd46 249 //int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData)
techand 0:7b556109fd46 250 int AdkUSB::read(u8 *buff, int len) {
techand 0:7b556109fd46 251 int ret=USBBulkTransfer(_device,input_ep|0x80,buff,len,0,0);
techand 0:7b556109fd46 252 log("adkUSB read ret=%d \r\n",ret);
techand 0:7b556109fd46 253 return ret;
techand 0:7b556109fd46 254 }
techand 0:7b556109fd46 255
techand 0:7b556109fd46 256 void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData);
techand 0:7b556109fd46 257
techand 0:7b556109fd46 258 int AdkUSB::write(u8 *buff, int len) {
techand 0:7b556109fd46 259 log("adkUSB write ------- xxx \r\n");
techand 0:7b556109fd46 260 int ret=USBBulkTransfer(_device,output_ep,buff,len/*,AdkwriteCallback,this*/);
techand 0:7b556109fd46 261 log("ret=%d \r\n",ret);
techand 0:7b556109fd46 262 return ret;
techand 0:7b556109fd46 263 }
techand 0:7b556109fd46 264
techand 0:7b556109fd46 265 void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData);
techand 0:7b556109fd46 266
techand 0:7b556109fd46 267 AdkUSB::AdkUSB(int device, int configuration, int interfaceNumber) {
techand 0:7b556109fd46 268
techand 0:7b556109fd46 269 log("connecting Android \r\n");
techand 0:7b556109fd46 270 _device = device;
techand 0:7b556109fd46 271 _configuration = configuration;
techand 0:7b556109fd46 272 _interfaceNumber = interfaceNumber;
techand 0:7b556109fd46 273 //for loop()
techand 0:7b556109fd46 274 _loopend=false;
techand 0:7b556109fd46 275 printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber);
techand 0:7b556109fd46 276 int err;
techand 0:7b556109fd46 277
techand 0:7b556109fd46 278 u8 buffer[255];
techand 0:7b556109fd46 279 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,4);
techand 0:7b556109fd46 280
techand 0:7b556109fd46 281 if (err < 0) {
techand 0:7b556109fd46 282 log("Failed to get descriptor\r\n");
techand 0:7b556109fd46 283 return;
techand 0:7b556109fd46 284 }
techand 0:7b556109fd46 285
techand 0:7b556109fd46 286
techand 0:7b556109fd46 287 int len = buffer[2] | (buffer[3] << 8);
techand 0:7b556109fd46 288 if (len > sizeof(buffer)) {
techand 0:7b556109fd46 289 log("config descriptor too large\n");
techand 0:7b556109fd46 290 /* might want to truncate here */
techand 0:7b556109fd46 291 return;
techand 0:7b556109fd46 292 }
techand 0:7b556109fd46 293 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,len);
techand 0:7b556109fd46 294 u8* p = buffer;
techand 0:7b556109fd46 295 input_ep=0;
techand 0:7b556109fd46 296 output_ep=0;
techand 0:7b556109fd46 297 EndpointDescriptor *epDesc;
techand 0:7b556109fd46 298 while (p<(buffer+len)) {
techand 0:7b556109fd46 299 u8 descLen = p[0];
techand 0:7b556109fd46 300 u8 descType = p[1];
techand 0:7b556109fd46 301 log("descLen=%d,descType=%d\r\n",descLen,descType);
techand 0:7b556109fd46 302 switch (descType) {
techand 0:7b556109fd46 303 case DESCRIPTOR_TYPE_CONFIGURATION:
techand 0:7b556109fd46 304 log("config desc\r\n");
techand 0:7b556109fd46 305 break;
techand 0:7b556109fd46 306 case DESCRIPTOR_TYPE_INTERFACE:
techand 0:7b556109fd46 307 log("interface desc\r\n");
techand 0:7b556109fd46 308 break;
techand 0:7b556109fd46 309 case DESCRIPTOR_TYPE_ENDPOINT:
techand 0:7b556109fd46 310 epDesc=(EndpointDescriptor*)p;
techand 0:7b556109fd46 311 if (!input_ep && (epDesc->bEndpointAddress& 0x80)) {
techand 0:7b556109fd46 312 input_ep=epDesc->bEndpointAddress& 0x7f;
techand 0:7b556109fd46 313 //PacketSize drop
techand 0:7b556109fd46 314 log("input Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
techand 0:7b556109fd46 315
techand 0:7b556109fd46 316 } else if (!output_ep) {
techand 0:7b556109fd46 317 output_ep=epDesc->bEndpointAddress& 0x7f;
techand 0:7b556109fd46 318 //PacketSize drop
techand 0:7b556109fd46 319 log("output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
techand 0:7b556109fd46 320 } else {
techand 0:7b556109fd46 321 //other
techand 0:7b556109fd46 322 log("non input,output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
techand 0:7b556109fd46 323 }
techand 0:7b556109fd46 324 break;
techand 0:7b556109fd46 325 default:
techand 0:7b556109fd46 326 log("unkown desc type(%d) \r\n",descType);
techand 0:7b556109fd46 327 }
techand 0:7b556109fd46 328 p+=descLen;
techand 0:7b556109fd46 329 }
techand 0:7b556109fd46 330
techand 0:7b556109fd46 331 if (!(input_ep && output_ep)) {
techand 0:7b556109fd46 332 log("can't find accessory endpoints\r\n");
techand 0:7b556109fd46 333 return;
techand 0:7b556109fd46 334 }
techand 0:7b556109fd46 335
techand 0:7b556109fd46 336 log("SetConfiguration\r\n");
techand 0:7b556109fd46 337 err = SetConfiguration(device,configuration);
techand 0:7b556109fd46 338 if (err < 0) {
techand 0:7b556109fd46 339 log("SetConfiguration error\r\n");
techand 0:7b556109fd46 340 return;
techand 0:7b556109fd46 341 }
techand 0:7b556109fd46 342
techand 0:7b556109fd46 343 log("interrupt setup\r\n");
techand 0:7b556109fd46 344 //interrupt setup
techand 0:7b556109fd46 345 int ret=USBBulkTransfer(_device,input_ep|0x80,readbuff,sizeof(readbuff),AdkreadCallback,this);
techand 0:7b556109fd46 346 log("ret=%d \r\n",ret);
techand 0:7b556109fd46 347 log("ADK Standby\r\n");
techand 0:7b556109fd46 348
techand 0:7b556109fd46 349 this->setup();
techand 0:7b556109fd46 350 }
techand 0:7b556109fd46 351