Code to control a Traxster robot using a Wimote and Android app
Fork of BlueUSB by
BlueUSB/AutoEvents.cpp@1:accdaa84fe8d, 2014-04-28 (annotated)
- Committer:
- aswild
- Date:
- Mon Apr 28 16:06:32 2014 +0000
- Revision:
- 1:accdaa84fe8d
Wiimote controlled Traxster robot using BlueUSB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 1:accdaa84fe8d | 1 | |
aswild | 1:accdaa84fe8d | 2 | /* |
aswild | 1:accdaa84fe8d | 3 | Copyright (c) 2010 Peter Barrett |
aswild | 1:accdaa84fe8d | 4 | |
aswild | 1:accdaa84fe8d | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
aswild | 1:accdaa84fe8d | 6 | of this software and associated documentation files (the "Software"), to deal |
aswild | 1:accdaa84fe8d | 7 | in the Software without restriction, including without limitation the rights |
aswild | 1:accdaa84fe8d | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
aswild | 1:accdaa84fe8d | 9 | copies of the Software, and to permit persons to whom the Software is |
aswild | 1:accdaa84fe8d | 10 | furnished to do so, subject to the following conditions: |
aswild | 1:accdaa84fe8d | 11 | |
aswild | 1:accdaa84fe8d | 12 | The above copyright notice and this permission notice shall be included in |
aswild | 1:accdaa84fe8d | 13 | all copies or substantial portions of the Software. |
aswild | 1:accdaa84fe8d | 14 | |
aswild | 1:accdaa84fe8d | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
aswild | 1:accdaa84fe8d | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
aswild | 1:accdaa84fe8d | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
aswild | 1:accdaa84fe8d | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
aswild | 1:accdaa84fe8d | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
aswild | 1:accdaa84fe8d | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
aswild | 1:accdaa84fe8d | 21 | THE SOFTWARE. |
aswild | 1:accdaa84fe8d | 22 | */ |
aswild | 1:accdaa84fe8d | 23 | |
aswild | 1:accdaa84fe8d | 24 | #include "mbed.h" |
aswild | 1:accdaa84fe8d | 25 | #include "USBHost.h" |
aswild | 1:accdaa84fe8d | 26 | #include "Utils.h" |
aswild | 1:accdaa84fe8d | 27 | |
aswild | 1:accdaa84fe8d | 28 | #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol) |
aswild | 1:accdaa84fe8d | 29 | #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1) |
aswild | 1:accdaa84fe8d | 30 | #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2) |
aswild | 1:accdaa84fe8d | 31 | |
aswild | 1:accdaa84fe8d | 32 | u8 auto_mouse[4]; // buttons,dx,dy,scroll |
aswild | 1:accdaa84fe8d | 33 | u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6 |
aswild | 1:accdaa84fe8d | 34 | u8 auto_joystick[4]; // x,y,buttons,throttle |
aswild | 1:accdaa84fe8d | 35 | |
aswild | 1:accdaa84fe8d | 36 | void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData) |
aswild | 1:accdaa84fe8d | 37 | { |
aswild | 1:accdaa84fe8d | 38 | int evt = (int)userData; |
aswild | 1:accdaa84fe8d | 39 | switch (evt) |
aswild | 1:accdaa84fe8d | 40 | { |
aswild | 1:accdaa84fe8d | 41 | case AUTO_KEYBOARD: |
aswild | 1:accdaa84fe8d | 42 | printf("AUTO_KEYBOARD "); |
aswild | 1:accdaa84fe8d | 43 | break; |
aswild | 1:accdaa84fe8d | 44 | case AUTO_MOUSE: |
aswild | 1:accdaa84fe8d | 45 | printf("AUTO_MOUSE "); |
aswild | 1:accdaa84fe8d | 46 | break; |
aswild | 1:accdaa84fe8d | 47 | default: |
aswild | 1:accdaa84fe8d | 48 | printf("HUH "); |
aswild | 1:accdaa84fe8d | 49 | } |
aswild | 1:accdaa84fe8d | 50 | printfBytes("data",data,len); |
aswild | 1:accdaa84fe8d | 51 | USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData); |
aswild | 1:accdaa84fe8d | 52 | } |
aswild | 1:accdaa84fe8d | 53 | |
aswild | 1:accdaa84fe8d | 54 | // Establish transfers for interrupt events |
aswild | 1:accdaa84fe8d | 55 | void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed) |
aswild | 1:accdaa84fe8d | 56 | { |
aswild | 1:accdaa84fe8d | 57 | if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80)) |
aswild | 1:accdaa84fe8d | 58 | return; |
aswild | 1:accdaa84fe8d | 59 | |
aswild | 1:accdaa84fe8d | 60 | // Make automatic interrupt enpoints for known devices |
aswild | 1:accdaa84fe8d | 61 | u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol); |
aswild | 1:accdaa84fe8d | 62 | u8* dst = 0; |
aswild | 1:accdaa84fe8d | 63 | int len; |
aswild | 1:accdaa84fe8d | 64 | switch (evt) |
aswild | 1:accdaa84fe8d | 65 | { |
aswild | 1:accdaa84fe8d | 66 | case AUTO_MOUSE: |
aswild | 1:accdaa84fe8d | 67 | dst = auto_mouse; |
aswild | 1:accdaa84fe8d | 68 | len = sizeof(auto_mouse); |
aswild | 1:accdaa84fe8d | 69 | break; |
aswild | 1:accdaa84fe8d | 70 | case AUTO_KEYBOARD: |
aswild | 1:accdaa84fe8d | 71 | dst = auto_keyboard; |
aswild | 1:accdaa84fe8d | 72 | len = sizeof(auto_keyboard); |
aswild | 1:accdaa84fe8d | 73 | break; |
aswild | 1:accdaa84fe8d | 74 | default: |
aswild | 1:accdaa84fe8d | 75 | printf("Interrupt endpoint %02X %08X\n",ed->bEndpointAddress,evt); |
aswild | 1:accdaa84fe8d | 76 | break; |
aswild | 1:accdaa84fe8d | 77 | } |
aswild | 1:accdaa84fe8d | 78 | if (dst) |
aswild | 1:accdaa84fe8d | 79 | { |
aswild | 1:accdaa84fe8d | 80 | printf("Auto Event for %02X %08X\n",ed->bEndpointAddress,evt); |
aswild | 1:accdaa84fe8d | 81 | USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt); |
aswild | 1:accdaa84fe8d | 82 | } |
aswild | 1:accdaa84fe8d | 83 | } |
aswild | 1:accdaa84fe8d | 84 | |
aswild | 1:accdaa84fe8d | 85 | void PrintString(int device, int i) |
aswild | 1:accdaa84fe8d | 86 | { |
aswild | 1:accdaa84fe8d | 87 | u8 buffer[256]; |
aswild | 1:accdaa84fe8d | 88 | int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255); |
aswild | 1:accdaa84fe8d | 89 | if (le < 0) |
aswild | 1:accdaa84fe8d | 90 | return; |
aswild | 1:accdaa84fe8d | 91 | char* dst = (char*)buffer; |
aswild | 1:accdaa84fe8d | 92 | for (int j = 2; j < le; j += 2) |
aswild | 1:accdaa84fe8d | 93 | *dst++ = buffer[j]; |
aswild | 1:accdaa84fe8d | 94 | *dst = 0; |
aswild | 1:accdaa84fe8d | 95 | printf("%d:%s\n",i,(const char*)buffer); |
aswild | 1:accdaa84fe8d | 96 | } |
aswild | 1:accdaa84fe8d | 97 | |
aswild | 1:accdaa84fe8d | 98 | // Walk descriptors and create endpoints for a given device |
aswild | 1:accdaa84fe8d | 99 | int StartAutoEvent(int device, int configuration, int interfaceNumber) |
aswild | 1:accdaa84fe8d | 100 | { |
aswild | 1:accdaa84fe8d | 101 | u8 buffer[255]; |
aswild | 1:accdaa84fe8d | 102 | int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); |
aswild | 1:accdaa84fe8d | 103 | if (err < 0) |
aswild | 1:accdaa84fe8d | 104 | return err; |
aswild | 1:accdaa84fe8d | 105 | |
aswild | 1:accdaa84fe8d | 106 | int len = buffer[2] | (buffer[3] << 8); |
aswild | 1:accdaa84fe8d | 107 | u8* d = buffer; |
aswild | 1:accdaa84fe8d | 108 | u8* end = d + len; |
aswild | 1:accdaa84fe8d | 109 | while (d < end) |
aswild | 1:accdaa84fe8d | 110 | { |
aswild | 1:accdaa84fe8d | 111 | if (d[1] == DESCRIPTOR_TYPE_INTERFACE) |
aswild | 1:accdaa84fe8d | 112 | { |
aswild | 1:accdaa84fe8d | 113 | InterfaceDescriptor* id = (InterfaceDescriptor*)d; |
aswild | 1:accdaa84fe8d | 114 | if (id->bInterfaceNumber == interfaceNumber) |
aswild | 1:accdaa84fe8d | 115 | { |
aswild | 1:accdaa84fe8d | 116 | d += d[0]; |
aswild | 1:accdaa84fe8d | 117 | while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) |
aswild | 1:accdaa84fe8d | 118 | { |
aswild | 1:accdaa84fe8d | 119 | if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) |
aswild | 1:accdaa84fe8d | 120 | AddAutoEvent(device,id,(EndpointDescriptor*)d); |
aswild | 1:accdaa84fe8d | 121 | d += d[0]; |
aswild | 1:accdaa84fe8d | 122 | } |
aswild | 1:accdaa84fe8d | 123 | } |
aswild | 1:accdaa84fe8d | 124 | } |
aswild | 1:accdaa84fe8d | 125 | d += d[0]; |
aswild | 1:accdaa84fe8d | 126 | } |
aswild | 1:accdaa84fe8d | 127 | return 0; |
aswild | 1:accdaa84fe8d | 128 | } |
aswild | 1:accdaa84fe8d | 129 | |
aswild | 1:accdaa84fe8d | 130 | // Implemented in main.cpp |
aswild | 1:accdaa84fe8d | 131 | int OnDiskInsert(int device) {return -1;} |
aswild | 1:accdaa84fe8d | 132 | |
aswild | 1:accdaa84fe8d | 133 | // Implemented in TestShell.cpp |
aswild | 1:accdaa84fe8d | 134 | int OnBluetoothInsert(int device); |
aswild | 1:accdaa84fe8d | 135 | |
aswild | 1:accdaa84fe8d | 136 | void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) |
aswild | 1:accdaa84fe8d | 137 | { |
aswild | 1:accdaa84fe8d | 138 | printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); |
aswild | 1:accdaa84fe8d | 139 | char s[128]; |
aswild | 1:accdaa84fe8d | 140 | for (int i = 1; i < 3; i++) |
aswild | 1:accdaa84fe8d | 141 | { |
aswild | 1:accdaa84fe8d | 142 | if (GetString(device,i,s,sizeof(s)) < 0) |
aswild | 1:accdaa84fe8d | 143 | break; |
aswild | 1:accdaa84fe8d | 144 | printf("%d: %s\n",i,s); |
aswild | 1:accdaa84fe8d | 145 | } |
aswild | 1:accdaa84fe8d | 146 | |
aswild | 1:accdaa84fe8d | 147 | switch (interfaceDesc->bInterfaceClass) |
aswild | 1:accdaa84fe8d | 148 | { |
aswild | 1:accdaa84fe8d | 149 | case CLASS_MASS_STORAGE: |
aswild | 1:accdaa84fe8d | 150 | if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50) |
aswild | 1:accdaa84fe8d | 151 | OnDiskInsert(device); // it's SCSI! |
aswild | 1:accdaa84fe8d | 152 | break; |
aswild | 1:accdaa84fe8d | 153 | case CLASS_WIRELESS_CONTROLLER: |
aswild | 1:accdaa84fe8d | 154 | if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01) |
aswild | 1:accdaa84fe8d | 155 | OnBluetoothInsert(device); // it's bluetooth! |
aswild | 1:accdaa84fe8d | 156 | break; |
aswild | 1:accdaa84fe8d | 157 | default: |
aswild | 1:accdaa84fe8d | 158 | StartAutoEvent(device,1,0); |
aswild | 1:accdaa84fe8d | 159 | break; |
aswild | 1:accdaa84fe8d | 160 | } |
aswild | 1:accdaa84fe8d | 161 | } |