![](/media/cache/profiles/ddcbe25988981920c872c1787382f04d.50x50_q85.jpg)
XOOMの動作状況を聞き処理を変えてみました。 USBケーブルを抜いた際に処理を終了するようにしました。
ADK.cpp@0:9fb6c423e32c, 2011-05-25 (annotated)
- Committer:
- abe00makoto
- Date:
- Wed May 25 09:34:38 2011 +0000
- Revision:
- 0:9fb6c423e32c
- Child:
- 1:371cbed7a29b
support ADK
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abe00makoto | 0:9fb6c423e32c | 1 | #include <stdio.h> |
abe00makoto | 0:9fb6c423e32c | 2 | #include <stdlib.h> |
abe00makoto | 0:9fb6c423e32c | 3 | #include <stdio.h> |
abe00makoto | 0:9fb6c423e32c | 4 | #include <string.h> |
abe00makoto | 0:9fb6c423e32c | 5 | |
abe00makoto | 0:9fb6c423e32c | 6 | #include "USBHost.h" |
abe00makoto | 0:9fb6c423e32c | 7 | #include "Utils.h" |
abe00makoto | 0:9fb6c423e32c | 8 | #include "ps3.h" |
abe00makoto | 0:9fb6c423e32c | 9 | #include "ADK.h" |
abe00makoto | 0:9fb6c423e32c | 10 | #include "mbed.h" |
abe00makoto | 0:9fb6c423e32c | 11 | |
abe00makoto | 0:9fb6c423e32c | 12 | #define ADKLOG 1 |
abe00makoto | 0:9fb6c423e32c | 13 | #if ADKLOG |
abe00makoto | 0:9fb6c423e32c | 14 | #define LOG(...) printf(__VA_ARGS__) |
abe00makoto | 0:9fb6c423e32c | 15 | #define Log(...) printf(__VA_ARGS__) |
abe00makoto | 0:9fb6c423e32c | 16 | #define log(...) printf(__VA_ARGS__) |
abe00makoto | 0:9fb6c423e32c | 17 | |
abe00makoto | 0:9fb6c423e32c | 18 | #else |
abe00makoto | 0:9fb6c423e32c | 19 | #define LOG(...) do {} while(0) |
abe00makoto | 0:9fb6c423e32c | 20 | #define Log(...) do {} while(0) |
abe00makoto | 0:9fb6c423e32c | 21 | #define log(...) do {} while(0) |
abe00makoto | 0:9fb6c423e32c | 22 | |
abe00makoto | 0:9fb6c423e32c | 23 | #endif |
abe00makoto | 0:9fb6c423e32c | 24 | |
abe00makoto | 0:9fb6c423e32c | 25 | //const manufacturer= |
abe00makoto | 0:9fb6c423e32c | 26 | |
abe00makoto | 0:9fb6c423e32c | 27 | |
abe00makoto | 0:9fb6c423e32c | 28 | PwmOut led1(LED1); |
abe00makoto | 0:9fb6c423e32c | 29 | PwmOut led2(LED2); |
abe00makoto | 0:9fb6c423e32c | 30 | PwmOut led3(LED3); |
abe00makoto | 0:9fb6c423e32c | 31 | PwmOut led4(LED4); |
abe00makoto | 0:9fb6c423e32c | 32 | |
abe00makoto | 0:9fb6c423e32c | 33 | void AdkUSB::loop() { |
abe00makoto | 0:9fb6c423e32c | 34 | log("enter loop\r\n"); |
abe00makoto | 0:9fb6c423e32c | 35 | u8 buf[3]; |
abe00makoto | 0:9fb6c423e32c | 36 | |
abe00makoto | 0:9fb6c423e32c | 37 | while (1) { |
abe00makoto | 0:9fb6c423e32c | 38 | //wait_ms(10); |
abe00makoto | 0:9fb6c423e32c | 39 | //printf("Adk.read run!-------------------------xxxxxxxxxxxxxxxxxxxxxxxx\r\n"); |
abe00makoto | 0:9fb6c423e32c | 40 | int len=this->read(buf,sizeof(buf),1); |
abe00makoto | 0:9fb6c423e32c | 41 | if (len >0) { |
abe00makoto | 0:9fb6c423e32c | 42 | log("buf[0]=%d,buf[1]=%d,buf[2]=%d\r\n",buf[0],buf[1],buf[2]); |
abe00makoto | 0:9fb6c423e32c | 43 | if (buf[0] == 0x2) { |
abe00makoto | 0:9fb6c423e32c | 44 | if (buf[1] == 0x0) { |
abe00makoto | 0:9fb6c423e32c | 45 | led1=((float)buf[2])/255.0; |
abe00makoto | 0:9fb6c423e32c | 46 | } else if (buf[1]==0x3) { |
abe00makoto | 0:9fb6c423e32c | 47 | led2=((float)buf[2])/255.0; |
abe00makoto | 0:9fb6c423e32c | 48 | } else if (buf[1]=0x6) { |
abe00makoto | 0:9fb6c423e32c | 49 | led3=((float)buf[2])/255.0; |
abe00makoto | 0:9fb6c423e32c | 50 | } |
abe00makoto | 0:9fb6c423e32c | 51 | } else if (buf[0] ==0x3) { |
abe00makoto | 0:9fb6c423e32c | 52 | if (buf[1] == 0x0) { |
abe00makoto | 0:9fb6c423e32c | 53 | led4=buf[2]? 1.0:0.0; |
abe00makoto | 0:9fb6c423e32c | 54 | } |
abe00makoto | 0:9fb6c423e32c | 55 | } |
abe00makoto | 0:9fb6c423e32c | 56 | |
abe00makoto | 0:9fb6c423e32c | 57 | //buf[0]=0x01; |
abe00makoto | 0:9fb6c423e32c | 58 | |
abe00makoto | 0:9fb6c423e32c | 59 | } else { |
abe00makoto | 0:9fb6c423e32c | 60 | led1=led2=led3=led4=0.0; |
abe00makoto | 0:9fb6c423e32c | 61 | log("---------------------------------------------------------------loop end\r\n"); |
abe00makoto | 0:9fb6c423e32c | 62 | return; |
abe00makoto | 0:9fb6c423e32c | 63 | } |
abe00makoto | 0:9fb6c423e32c | 64 | } |
abe00makoto | 0:9fb6c423e32c | 65 | } |
abe00makoto | 0:9fb6c423e32c | 66 | |
abe00makoto | 0:9fb6c423e32c | 67 | |
abe00makoto | 0:9fb6c423e32c | 68 | |
abe00makoto | 0:9fb6c423e32c | 69 | int getProtocol(int device); |
abe00makoto | 0:9fb6c423e32c | 70 | void sendString(int device, int index, const char *str); |
abe00makoto | 0:9fb6c423e32c | 71 | |
abe00makoto | 0:9fb6c423e32c | 72 | bool switchDevice(int device) { |
abe00makoto | 0:9fb6c423e32c | 73 | |
abe00makoto | 0:9fb6c423e32c | 74 | if (1==getProtocol(device)) { |
abe00makoto | 0:9fb6c423e32c | 75 | log("device supports protocol 1\r\n"); |
abe00makoto | 0:9fb6c423e32c | 76 | |
abe00makoto | 0:9fb6c423e32c | 77 | } else { |
abe00makoto | 0:9fb6c423e32c | 78 | log("could not read device protocol version\r\n"); |
abe00makoto | 0:9fb6c423e32c | 79 | return false; |
abe00makoto | 0:9fb6c423e32c | 80 | } |
abe00makoto | 0:9fb6c423e32c | 81 | |
abe00makoto | 0:9fb6c423e32c | 82 | |
abe00makoto | 0:9fb6c423e32c | 83 | sendString(device,ACCESSORY_STRING_MANUFACTURER,"Google, Inc."); |
abe00makoto | 0:9fb6c423e32c | 84 | sendString(device,ACCESSORY_STRING_MODEL,"DemoKit"); |
abe00makoto | 0:9fb6c423e32c | 85 | sendString(device,ACCESSORY_STRING_DESCRIPTION,"DemoKit Arduino Board"); |
abe00makoto | 0:9fb6c423e32c | 86 | sendString(device,ACCESSORY_STRING_VERSION,"1.0"); |
abe00makoto | 0:9fb6c423e32c | 87 | sendString(device,ACCESSORY_STRING_URI,"http://www.android.com"); |
abe00makoto | 0:9fb6c423e32c | 88 | sendString(device,ACCESSORY_STRING_SERIAL,"0000000012345678"); |
abe00makoto | 0:9fb6c423e32c | 89 | |
abe00makoto | 0:9fb6c423e32c | 90 | |
abe00makoto | 0:9fb6c423e32c | 91 | USBControlTransfer(device, |
abe00makoto | 0:9fb6c423e32c | 92 | HOST_TO_DEVICE|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, |
abe00makoto | 0:9fb6c423e32c | 93 | ACCESSORY_START, |
abe00makoto | 0:9fb6c423e32c | 94 | 0,//value |
abe00makoto | 0:9fb6c423e32c | 95 | 0, //index |
abe00makoto | 0:9fb6c423e32c | 96 | 0, |
abe00makoto | 0:9fb6c423e32c | 97 | 0, |
abe00makoto | 0:9fb6c423e32c | 98 | 0, |
abe00makoto | 0:9fb6c423e32c | 99 | 0 ); |
abe00makoto | 0:9fb6c423e32c | 100 | |
abe00makoto | 0:9fb6c423e32c | 101 | return true; |
abe00makoto | 0:9fb6c423e32c | 102 | |
abe00makoto | 0:9fb6c423e32c | 103 | } |
abe00makoto | 0:9fb6c423e32c | 104 | |
abe00makoto | 0:9fb6c423e32c | 105 | |
abe00makoto | 0:9fb6c423e32c | 106 | int getProtocol(int device) { |
abe00makoto | 0:9fb6c423e32c | 107 | u16 data=-1; |
abe00makoto | 0:9fb6c423e32c | 108 | USBControlTransfer(device, |
abe00makoto | 0:9fb6c423e32c | 109 | DEVICE_TO_HOST|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, |
abe00makoto | 0:9fb6c423e32c | 110 | ACCESSORY_GET_PROTOCOL, |
abe00makoto | 0:9fb6c423e32c | 111 | 0,//value |
abe00makoto | 0:9fb6c423e32c | 112 | 0, //index |
abe00makoto | 0:9fb6c423e32c | 113 | (u8*)&data, |
abe00makoto | 0:9fb6c423e32c | 114 | 2, |
abe00makoto | 0:9fb6c423e32c | 115 | 0, |
abe00makoto | 0:9fb6c423e32c | 116 | 0 ); |
abe00makoto | 0:9fb6c423e32c | 117 | //printf("return %d\r\n",data); |
abe00makoto | 0:9fb6c423e32c | 118 | return data; |
abe00makoto | 0:9fb6c423e32c | 119 | |
abe00makoto | 0:9fb6c423e32c | 120 | } |
abe00makoto | 0:9fb6c423e32c | 121 | |
abe00makoto | 0:9fb6c423e32c | 122 | void sendString(int device, int index, const char *str) { |
abe00makoto | 0:9fb6c423e32c | 123 | |
abe00makoto | 0:9fb6c423e32c | 124 | LOG("send_string start(%d,%d,%s) %d \r\n",device,index,str,strlen(str)+1); |
abe00makoto | 0:9fb6c423e32c | 125 | |
abe00makoto | 0:9fb6c423e32c | 126 | //this is diffrent google's sample but error ocard same sample |
abe00makoto | 0:9fb6c423e32c | 127 | USBControlTransfer(device, |
abe00makoto | 0:9fb6c423e32c | 128 | DEVICE_TO_HOST /*HOST_TO_DEVIC use error why? please teach me*/|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, |
abe00makoto | 0:9fb6c423e32c | 129 | ACCESSORY_SEND_STRING, |
abe00makoto | 0:9fb6c423e32c | 130 | 0,//value |
abe00makoto | 0:9fb6c423e32c | 131 | index, |
abe00makoto | 0:9fb6c423e32c | 132 | (u8*)str, |
abe00makoto | 0:9fb6c423e32c | 133 | strlen(str)+1 |
abe00makoto | 0:9fb6c423e32c | 134 | ); |
abe00makoto | 0:9fb6c423e32c | 135 | |
abe00makoto | 0:9fb6c423e32c | 136 | LOG("send_string end(%d,%d,%s)\r\n",device,index,str); |
abe00makoto | 0:9fb6c423e32c | 137 | |
abe00makoto | 0:9fb6c423e32c | 138 | } |
abe00makoto | 0:9fb6c423e32c | 139 | |
abe00makoto | 0:9fb6c423e32c | 140 | //int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData) |
abe00makoto | 0:9fb6c423e32c | 141 | int AdkUSB::read(u8 *buff, int len, int nakLimit) { |
abe00makoto | 0:9fb6c423e32c | 142 | int ret=USBBulkTransfer(_device,input_ep|0x80,buff,len,0,0); |
abe00makoto | 0:9fb6c423e32c | 143 | log("adkUSB read ------- xx %d \r\n",ret); |
abe00makoto | 0:9fb6c423e32c | 144 | return ret; |
abe00makoto | 0:9fb6c423e32c | 145 | } |
abe00makoto | 0:9fb6c423e32c | 146 | int AdkUSB::write(u8 *buff, int len) { |
abe00makoto | 0:9fb6c423e32c | 147 | log("adkUSB write ------- xxx \r\n"); |
abe00makoto | 0:9fb6c423e32c | 148 | return USBBulkTransfer(_device,output_ep,buff,len,0,0); |
abe00makoto | 0:9fb6c423e32c | 149 | } |
abe00makoto | 0:9fb6c423e32c | 150 | |
abe00makoto | 0:9fb6c423e32c | 151 | |
abe00makoto | 0:9fb6c423e32c | 152 | AdkUSB::AdkUSB(int device, int configuration, int interfaceNumber) { |
abe00makoto | 0:9fb6c423e32c | 153 | log("connecting Android \r\n"); |
abe00makoto | 0:9fb6c423e32c | 154 | _device = device; |
abe00makoto | 0:9fb6c423e32c | 155 | _configuration = configuration; |
abe00makoto | 0:9fb6c423e32c | 156 | _interfaceNumber = interfaceNumber; |
abe00makoto | 0:9fb6c423e32c | 157 | printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber); |
abe00makoto | 0:9fb6c423e32c | 158 | int err; |
abe00makoto | 0:9fb6c423e32c | 159 | |
abe00makoto | 0:9fb6c423e32c | 160 | u8 buffer[255]; |
abe00makoto | 0:9fb6c423e32c | 161 | err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,4); |
abe00makoto | 0:9fb6c423e32c | 162 | |
abe00makoto | 0:9fb6c423e32c | 163 | if (err < 0) { |
abe00makoto | 0:9fb6c423e32c | 164 | log("Failed to get descriptor\r\n"); |
abe00makoto | 0:9fb6c423e32c | 165 | return; |
abe00makoto | 0:9fb6c423e32c | 166 | } |
abe00makoto | 0:9fb6c423e32c | 167 | |
abe00makoto | 0:9fb6c423e32c | 168 | |
abe00makoto | 0:9fb6c423e32c | 169 | int len = buffer[2] | (buffer[3] << 8); |
abe00makoto | 0:9fb6c423e32c | 170 | if (len > sizeof(buffer)) { |
abe00makoto | 0:9fb6c423e32c | 171 | log("config descriptor too large\n"); |
abe00makoto | 0:9fb6c423e32c | 172 | /* might want to truncate here */ |
abe00makoto | 0:9fb6c423e32c | 173 | return; |
abe00makoto | 0:9fb6c423e32c | 174 | } |
abe00makoto | 0:9fb6c423e32c | 175 | err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,len); |
abe00makoto | 0:9fb6c423e32c | 176 | u8* p = buffer; |
abe00makoto | 0:9fb6c423e32c | 177 | input_ep=0; |
abe00makoto | 0:9fb6c423e32c | 178 | output_ep=0; |
abe00makoto | 0:9fb6c423e32c | 179 | EndpointDescriptor *epDesc; |
abe00makoto | 0:9fb6c423e32c | 180 | while (p<(buffer+len)) { |
abe00makoto | 0:9fb6c423e32c | 181 | u8 descLen = p[0]; |
abe00makoto | 0:9fb6c423e32c | 182 | u8 descType = p[1]; |
abe00makoto | 0:9fb6c423e32c | 183 | log("descLen=%d,descType=%d\r\n",descLen,descType); |
abe00makoto | 0:9fb6c423e32c | 184 | switch (descType) { |
abe00makoto | 0:9fb6c423e32c | 185 | case DESCRIPTOR_TYPE_CONFIGURATION: |
abe00makoto | 0:9fb6c423e32c | 186 | log("config desc\r\n"); |
abe00makoto | 0:9fb6c423e32c | 187 | break; |
abe00makoto | 0:9fb6c423e32c | 188 | case DESCRIPTOR_TYPE_INTERFACE: |
abe00makoto | 0:9fb6c423e32c | 189 | log("interface desc\r\n"); |
abe00makoto | 0:9fb6c423e32c | 190 | break; |
abe00makoto | 0:9fb6c423e32c | 191 | case DESCRIPTOR_TYPE_ENDPOINT: |
abe00makoto | 0:9fb6c423e32c | 192 | epDesc=(EndpointDescriptor*)p; |
abe00makoto | 0:9fb6c423e32c | 193 | if (!input_ep && (epDesc->bEndpointAddress& 0x80)) { |
abe00makoto | 0:9fb6c423e32c | 194 | input_ep=epDesc->bEndpointAddress& 0x7f; |
abe00makoto | 0:9fb6c423e32c | 195 | //PacketSize drop |
abe00makoto | 0:9fb6c423e32c | 196 | log("input Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); |
abe00makoto | 0:9fb6c423e32c | 197 | |
abe00makoto | 0:9fb6c423e32c | 198 | } else if (!output_ep) { |
abe00makoto | 0:9fb6c423e32c | 199 | output_ep=epDesc->bEndpointAddress& 0x7f; |
abe00makoto | 0:9fb6c423e32c | 200 | //PacketSize drop |
abe00makoto | 0:9fb6c423e32c | 201 | log("output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); |
abe00makoto | 0:9fb6c423e32c | 202 | } else { |
abe00makoto | 0:9fb6c423e32c | 203 | //other |
abe00makoto | 0:9fb6c423e32c | 204 | log("non input,output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); |
abe00makoto | 0:9fb6c423e32c | 205 | } |
abe00makoto | 0:9fb6c423e32c | 206 | break; |
abe00makoto | 0:9fb6c423e32c | 207 | default: |
abe00makoto | 0:9fb6c423e32c | 208 | log("unkown desc type(%d) \r\n",descType); |
abe00makoto | 0:9fb6c423e32c | 209 | } |
abe00makoto | 0:9fb6c423e32c | 210 | p+=descLen; |
abe00makoto | 0:9fb6c423e32c | 211 | } |
abe00makoto | 0:9fb6c423e32c | 212 | |
abe00makoto | 0:9fb6c423e32c | 213 | if (!(input_ep && output_ep)) { |
abe00makoto | 0:9fb6c423e32c | 214 | log("can't find accessory endpoints\r\n"); |
abe00makoto | 0:9fb6c423e32c | 215 | return; |
abe00makoto | 0:9fb6c423e32c | 216 | } |
abe00makoto | 0:9fb6c423e32c | 217 | |
abe00makoto | 0:9fb6c423e32c | 218 | log("SetConfiguration\r\n"); |
abe00makoto | 0:9fb6c423e32c | 219 | err = SetConfiguration(device,configuration); |
abe00makoto | 0:9fb6c423e32c | 220 | if (err < 0) { |
abe00makoto | 0:9fb6c423e32c | 221 | log("SetConfiguration error\r\n"); |
abe00makoto | 0:9fb6c423e32c | 222 | wait(10); |
abe00makoto | 0:9fb6c423e32c | 223 | } |
abe00makoto | 0:9fb6c423e32c | 224 | |
abe00makoto | 0:9fb6c423e32c | 225 | log("ADK Stand by\r\n"); |
abe00makoto | 0:9fb6c423e32c | 226 | } |