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