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