The modified AndroidAccessory Library
Dependents: ADKTerm droidcycle uva_nc androidservo ... more
AndroidAccessory.h@2:98fbe1660f0a, 2011-08-02 (annotated)
- Committer:
- p07gbar
- Date:
- Tue Aug 02 08:36:49 2011 +0000
- Revision:
- 2:98fbe1660f0a
- Parent:
- 1:0d5fc52b4dcd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
p07gbar | 1:0d5fc52b4dcd | 1 | /* mbed AndroidAccessory Library |
p07gbar | 1:0d5fc52b4dcd | 2 | * Created by p07gbar from work by Makoto Abe |
p07gbar | 1:0d5fc52b4dcd | 3 | * |
p07gbar | 1:0d5fc52b4dcd | 4 | */ |
p07gbar | 0:1151e4446dbc | 5 | #ifndef ADK_H_INCLUDED |
p07gbar | 0:1151e4446dbc | 6 | #define ADK_H_INCLUDED |
p07gbar | 0:1151e4446dbc | 7 | |
p07gbar | 0:1151e4446dbc | 8 | #include "mbed.h" |
p07gbar | 0:1151e4446dbc | 9 | #include "USBHost.h" |
p07gbar | 0:1151e4446dbc | 10 | |
p07gbar | 0:1151e4446dbc | 11 | |
p07gbar | 0:1151e4446dbc | 12 | //#define ADKLOG 1 |
p07gbar | 0:1151e4446dbc | 13 | #if ADKLOG |
p07gbar | 0:1151e4446dbc | 14 | #define LOG(...) printf(__VA_ARGS__) |
p07gbar | 0:1151e4446dbc | 15 | #define Log(...) printf(__VA_ARGS__) |
p07gbar | 0:1151e4446dbc | 16 | #define log(...) printf(__VA_ARGS__) |
p07gbar | 0:1151e4446dbc | 17 | |
p07gbar | 0:1151e4446dbc | 18 | #else |
p07gbar | 0:1151e4446dbc | 19 | #define LOG(...) do {} while(0) |
p07gbar | 0:1151e4446dbc | 20 | #define Log(...) do {} while(0) |
p07gbar | 0:1151e4446dbc | 21 | #define log(...) do {} while(0) |
p07gbar | 0:1151e4446dbc | 22 | |
p07gbar | 0:1151e4446dbc | 23 | #endif |
p07gbar | 0:1151e4446dbc | 24 | |
p07gbar | 0:1151e4446dbc | 25 | #define ACCESSORY_STRING_MANUFACTURER 0 |
p07gbar | 0:1151e4446dbc | 26 | #define ACCESSORY_STRING_MODEL 1 |
p07gbar | 0:1151e4446dbc | 27 | #define ACCESSORY_STRING_DESCRIPTION 2 |
p07gbar | 0:1151e4446dbc | 28 | #define ACCESSORY_STRING_VERSION 3 |
p07gbar | 0:1151e4446dbc | 29 | #define ACCESSORY_STRING_URI 4 |
p07gbar | 0:1151e4446dbc | 30 | #define ACCESSORY_STRING_SERIAL 5 |
p07gbar | 0:1151e4446dbc | 31 | |
p07gbar | 0:1151e4446dbc | 32 | #define ACCESSORY_GET_PROTOCOL 51 |
p07gbar | 0:1151e4446dbc | 33 | #define ACCESSORY_SEND_STRING 52 |
p07gbar | 0:1151e4446dbc | 34 | #define ACCESSORY_START 53 |
p07gbar | 0:1151e4446dbc | 35 | |
p07gbar | 0:1151e4446dbc | 36 | |
p07gbar | 1:0d5fc52b4dcd | 37 | |
p07gbar | 1:0d5fc52b4dcd | 38 | /** An AndroidAccessory control class |
p07gbar | 1:0d5fc52b4dcd | 39 | * |
p07gbar | 1:0d5fc52b4dcd | 40 | * It allows easy creation of a mbed android ADK accessory, with minimal low level fussing. |
p07gbar | 1:0d5fc52b4dcd | 41 | * Base code should have methods resetDevice(), setupDevice(), callbackRead(u8 *buff, int len) and callBackWrite() functions |
p07gbar | 1:0d5fc52b4dcd | 42 | * |
p07gbar | 1:0d5fc52b4dcd | 43 | */ |
p07gbar | 0:1151e4446dbc | 44 | |
p07gbar | 0:1151e4446dbc | 45 | class AndroidAccessory { |
p07gbar | 0:1151e4446dbc | 46 | public: |
p07gbar | 0:1151e4446dbc | 47 | |
p07gbar | 1:0d5fc52b4dcd | 48 | |
p07gbar | 1:0d5fc52b4dcd | 49 | |
p07gbar | 1:0d5fc52b4dcd | 50 | /** Create a AndroidAccessory object |
p07gbar | 1:0d5fc52b4dcd | 51 | * |
p07gbar | 1:0d5fc52b4dcd | 52 | * Create a AndroidAccessoryobject with specified buffer sizes and infomation |
p07gbar | 1:0d5fc52b4dcd | 53 | * |
p07gbar | 1:0d5fc52b4dcd | 54 | * @param rbuffsize The size of the read buffer |
p07gbar | 1:0d5fc52b4dcd | 55 | * @param wbuffsize The size of the write buffer |
p07gbar | 1:0d5fc52b4dcd | 56 | * @param manufacturer The manufacturer of the accessory |
p07gbar | 1:0d5fc52b4dcd | 57 | * @param model The model of the accessory |
p07gbar | 1:0d5fc52b4dcd | 58 | * @param description A short description of the accessory |
p07gbar | 1:0d5fc52b4dcd | 59 | * @param version The current version of the accessory |
p07gbar | 1:0d5fc52b4dcd | 60 | * @param uri Some data to go with the accessory (URL or more description) |
p07gbar | 1:0d5fc52b4dcd | 61 | * @param serial The serial number of the accessory |
p07gbar | 1:0d5fc52b4dcd | 62 | */ |
p07gbar | 0:1151e4446dbc | 63 | AndroidAccessory(int rbuffsize,int wbuffsize, |
p07gbar | 0:1151e4446dbc | 64 | const char* manufacturer, |
p07gbar | 0:1151e4446dbc | 65 | const char *model, |
p07gbar | 0:1151e4446dbc | 66 | const char *description, |
p07gbar | 0:1151e4446dbc | 67 | const char *version, |
p07gbar | 0:1151e4446dbc | 68 | const char *uri, |
p07gbar | 0:1151e4446dbc | 69 | const char *serial |
p07gbar | 0:1151e4446dbc | 70 | ); |
p07gbar | 1:0d5fc52b4dcd | 71 | |
p07gbar | 1:0d5fc52b4dcd | 72 | /** Init the device |
p07gbar | 2:98fbe1660f0a | 73 | * This is meant to be implimented by the user of the class |
p07gbar | 1:0d5fc52b4dcd | 74 | * |
p07gbar | 1:0d5fc52b4dcd | 75 | * @param device Device number |
p07gbar | 1:0d5fc52b4dcd | 76 | * @param configuration Configuration |
p07gbar | 1:0d5fc52b4dcd | 77 | * @param interfaceNumber Inteface number |
p07gbar | 1:0d5fc52b4dcd | 78 | */ |
p07gbar | 0:1151e4446dbc | 79 | virtual void init(int device, int configuration, int interfaceNumber); |
p07gbar | 1:0d5fc52b4dcd | 80 | |
p07gbar | 1:0d5fc52b4dcd | 81 | /** Reset the device |
p07gbar | 1:0d5fc52b4dcd | 82 | * This is meant to be implimented by the user of the class |
p07gbar | 1:0d5fc52b4dcd | 83 | * |
p07gbar | 1:0d5fc52b4dcd | 84 | */ |
p07gbar | 0:1151e4446dbc | 85 | virtual void resetDevice()=0; |
p07gbar | 1:0d5fc52b4dcd | 86 | |
p07gbar | 1:0d5fc52b4dcd | 87 | /** Setup the device |
p07gbar | 1:0d5fc52b4dcd | 88 | * This is meant to be implimented by the user of the class. Called when the device is first intialised |
p07gbar | 1:0d5fc52b4dcd | 89 | * |
p07gbar | 1:0d5fc52b4dcd | 90 | */ |
p07gbar | 0:1151e4446dbc | 91 | virtual void setupDevice()=0; |
p07gbar | 1:0d5fc52b4dcd | 92 | |
p07gbar | 1:0d5fc52b4dcd | 93 | /** Callback on Read |
p07gbar | 1:0d5fc52b4dcd | 94 | * This is meant to be implimented by the user of the class. Called when some data has been read in. |
p07gbar | 1:0d5fc52b4dcd | 95 | * |
p07gbar | 1:0d5fc52b4dcd | 96 | * @param buff The buffered read in data |
p07gbar | 1:0d5fc52b4dcd | 97 | * @param len The length of the packet recived |
p07gbar | 1:0d5fc52b4dcd | 98 | * |
p07gbar | 1:0d5fc52b4dcd | 99 | */ |
p07gbar | 0:1151e4446dbc | 100 | virtual int callbackRead(u8 *buff, int len)=0; |
p07gbar | 1:0d5fc52b4dcd | 101 | |
p07gbar | 1:0d5fc52b4dcd | 102 | /** Callback after Write |
p07gbar | 1:0d5fc52b4dcd | 103 | * This is meant to be implimented by the user of the class. Called when the write has been finished. |
p07gbar | 1:0d5fc52b4dcd | 104 | * |
p07gbar | 1:0d5fc52b4dcd | 105 | */ |
p07gbar | 0:1151e4446dbc | 106 | virtual int callbackWrite()=0; |
p07gbar | 1:0d5fc52b4dcd | 107 | |
p07gbar | 1:0d5fc52b4dcd | 108 | /** Write over USB |
p07gbar | 1:0d5fc52b4dcd | 109 | * This sends the data in the buffer over USB in a packet |
p07gbar | 1:0d5fc52b4dcd | 110 | * |
p07gbar | 1:0d5fc52b4dcd | 111 | * @param buff The buffer to write out |
p07gbar | 1:0d5fc52b4dcd | 112 | * @param len The length of the packet to send |
p07gbar | 1:0d5fc52b4dcd | 113 | * |
p07gbar | 1:0d5fc52b4dcd | 114 | */ |
p07gbar | 0:1151e4446dbc | 115 | int write(u8 *buff, int len); |
p07gbar | 1:0d5fc52b4dcd | 116 | |
p07gbar | 1:0d5fc52b4dcd | 117 | /** Write over USB |
p07gbar | 1:0d5fc52b4dcd | 118 | * This sends the data in the buffer over USB in a packet, sends _writebuff and _writebuffsize |
p07gbar | 1:0d5fc52b4dcd | 119 | * |
p07gbar | 1:0d5fc52b4dcd | 120 | */ |
p07gbar | 0:1151e4446dbc | 121 | int write() { |
p07gbar | 0:1151e4446dbc | 122 | return write(_writebuff,_writebuffsize); |
p07gbar | 0:1151e4446dbc | 123 | } |
p07gbar | 1:0d5fc52b4dcd | 124 | |
p07gbar | 1:0d5fc52b4dcd | 125 | /** Write over USB with no callback |
p07gbar | 1:0d5fc52b4dcd | 126 | * This sends the data in the buffer over USB in a packet, waits until the packet is sent, rather than doing a callback |
p07gbar | 1:0d5fc52b4dcd | 127 | * |
p07gbar | 1:0d5fc52b4dcd | 128 | * @param buff The buffer to write out |
p07gbar | 1:0d5fc52b4dcd | 129 | * @param len The length of the packet to send |
p07gbar | 1:0d5fc52b4dcd | 130 | * |
p07gbar | 1:0d5fc52b4dcd | 131 | */ |
p07gbar | 0:1151e4446dbc | 132 | int writeNC(u8 *buff, int len); |
p07gbar | 1:0d5fc52b4dcd | 133 | |
p07gbar | 1:0d5fc52b4dcd | 134 | /** Write over USB |
p07gbar | 1:0d5fc52b4dcd | 135 | * This sends the data in the buffer over USB in a packet, waits until the packet is sent, rather than doing a callback, sends _writebuff and _writebuffsize |
p07gbar | 1:0d5fc52b4dcd | 136 | * |
p07gbar | 1:0d5fc52b4dcd | 137 | */ |
p07gbar | 0:1151e4446dbc | 138 | int writeNC() { |
p07gbar | 0:1151e4446dbc | 139 | return writeNC(_writebuff,_writebuffsize); |
p07gbar | 0:1151e4446dbc | 140 | } |
p07gbar | 0:1151e4446dbc | 141 | |
p07gbar | 1:0d5fc52b4dcd | 142 | /** Read the buffer USB |
p07gbar | 1:0d5fc52b4dcd | 143 | * This sends the data in the buffer over USB in a packet, waits until the packet is sent, rather than doing a callback |
p07gbar | 1:0d5fc52b4dcd | 144 | * |
p07gbar | 1:0d5fc52b4dcd | 145 | * @param buff The buffer to read into |
p07gbar | 1:0d5fc52b4dcd | 146 | * @param len The length of the packet to read in |
p07gbar | 1:0d5fc52b4dcd | 147 | * |
p07gbar | 1:0d5fc52b4dcd | 148 | * @param returns The number of bytes read |
p07gbar | 1:0d5fc52b4dcd | 149 | * |
p07gbar | 1:0d5fc52b4dcd | 150 | */ |
p07gbar | 0:1151e4446dbc | 151 | int read(u8 *buff, int len); |
p07gbar | 0:1151e4446dbc | 152 | |
p07gbar | 0:1151e4446dbc | 153 | |
p07gbar | 0:1151e4446dbc | 154 | void adkEnd() { |
p07gbar | 0:1151e4446dbc | 155 | // _initok=false; |
p07gbar | 0:1151e4446dbc | 156 | resetDevice(); |
p07gbar | 0:1151e4446dbc | 157 | }; //if connection close |
p07gbar | 0:1151e4446dbc | 158 | bool switchDevice(int device); |
p07gbar | 0:1151e4446dbc | 159 | |
p07gbar | 0:1151e4446dbc | 160 | //buffer |
p07gbar | 0:1151e4446dbc | 161 | u8* _readbuff; |
p07gbar | 0:1151e4446dbc | 162 | int _readbuffsize; |
p07gbar | 0:1151e4446dbc | 163 | u8* _writebuff; |
p07gbar | 0:1151e4446dbc | 164 | int _writebuffsize; |
p07gbar | 0:1151e4446dbc | 165 | u8* _strbuff;//255bytes; |
p07gbar | 0:1151e4446dbc | 166 | void sendString(const char *str); |
p07gbar | 0:1151e4446dbc | 167 | |
p07gbar | 0:1151e4446dbc | 168 | private: |
p07gbar | 0:1151e4446dbc | 169 | |
p07gbar | 0:1151e4446dbc | 170 | void sendString(int device, int index, const char *str); |
p07gbar | 0:1151e4446dbc | 171 | int getProtocol(int device); |
p07gbar | 0:1151e4446dbc | 172 | |
p07gbar | 0:1151e4446dbc | 173 | const char *manufacturer; |
p07gbar | 0:1151e4446dbc | 174 | const char *model; |
p07gbar | 0:1151e4446dbc | 175 | const char *description; |
p07gbar | 0:1151e4446dbc | 176 | const char *version; |
p07gbar | 0:1151e4446dbc | 177 | const char *uri; |
p07gbar | 0:1151e4446dbc | 178 | const char *serial; |
p07gbar | 0:1151e4446dbc | 179 | |
p07gbar | 0:1151e4446dbc | 180 | //endpoints |
p07gbar | 0:1151e4446dbc | 181 | int input_ep; |
p07gbar | 0:1151e4446dbc | 182 | int output_ep; |
p07gbar | 0:1151e4446dbc | 183 | |
p07gbar | 0:1151e4446dbc | 184 | int _device; |
p07gbar | 0:1151e4446dbc | 185 | int _configuration; |
p07gbar | 0:1151e4446dbc | 186 | int _interfaceNumber; |
p07gbar | 0:1151e4446dbc | 187 | |
p07gbar | 0:1151e4446dbc | 188 | //bool _initok; |
p07gbar | 0:1151e4446dbc | 189 | |
p07gbar | 0:1151e4446dbc | 190 | }; |
p07gbar | 0:1151e4446dbc | 191 | |
p07gbar | 0:1151e4446dbc | 192 | extern AndroidAccessory* _adk; |
p07gbar | 0:1151e4446dbc | 193 | |
p07gbar | 0:1151e4446dbc | 194 | |
p07gbar | 0:1151e4446dbc | 195 | #endif |