Twitter client, with TextLCD, Ethernet Connection, and USB Host for keyboard.

Dependencies:   EthernetNetIf TextLCD mbed

Committer:
blmarket
Date:
Thu Apr 21 05:54:26 2011 +0000
Revision:
0:70571fd24107
Initial Release

Who changed what in which revision?

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