[136] | 1 | /*
|
---|
[224] | 2 | Copyright (c) 2015 Arduino LLC. All right reserved.
|
---|
[136] | 3 |
|
---|
| 4 | This library is free software; you can redistribute it and/or
|
---|
| 5 | modify it under the terms of the GNU Lesser General Public
|
---|
| 6 | License as published by the Free Software Foundation; either
|
---|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 8 |
|
---|
| 9 | This library is distributed in the hope that it will be useful,
|
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 12 | See the GNU Lesser General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU Lesser General Public
|
---|
| 15 | License along with this library; if not, write to the Free Software
|
---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 17 | */
|
---|
[224] | 18 | /*
|
---|
| 19 | **Modified 04/04/2016 by Arduino.org development team
|
---|
| 20 | */
|
---|
[136] | 21 |
|
---|
[224] | 22 | #pragma once
|
---|
[136] | 23 |
|
---|
[224] | 24 | #define WEAK __attribute__ ((weak))
|
---|
[136] | 25 |
|
---|
[224] | 26 | #define HSTPIPCFG_PTYPE_BLK 1
|
---|
| 27 | #define HSTPIPCFG_PTOKEN_IN 2
|
---|
| 28 | #define HSTPIPCFG_PTOKEN_OUT 3
|
---|
| 29 | #define HSTPIPCFG_PBK_1_BANK 4
|
---|
| 30 | #define HSTPIPCFG_PTYPE_INTRPT 5
|
---|
[136] | 31 |
|
---|
[224] | 32 | #define EP0 0
|
---|
| 33 | #define EPX_SIZE 64 // 64 for Full Speed, EPT size max is 1024
|
---|
[136] | 34 |
|
---|
| 35 | #if defined __cplusplus
|
---|
| 36 |
|
---|
| 37 | #include "Stream.h"
|
---|
| 38 | #include "RingBuffer.h"
|
---|
| 39 |
|
---|
| 40 | //================================================================================
|
---|
[224] | 41 | // USB
|
---|
[136] | 42 | //================================================================================
|
---|
[224] | 43 | //================================================================================
|
---|
| 44 | // Low level API
|
---|
| 45 | typedef struct {
|
---|
| 46 | union {
|
---|
| 47 | uint8_t bmRequestType;
|
---|
| 48 | struct {
|
---|
| 49 | uint8_t direction : 5;
|
---|
| 50 | uint8_t type : 2;
|
---|
| 51 | uint8_t transferDirection : 1;
|
---|
| 52 | };
|
---|
| 53 | };
|
---|
| 54 | uint8_t bRequest;
|
---|
| 55 | uint8_t wValueL;
|
---|
| 56 | uint8_t wValueH;
|
---|
| 57 | uint16_t wIndex;
|
---|
| 58 | uint16_t wLength;
|
---|
| 59 | } Setup;
|
---|
[136] | 60 |
|
---|
[224] | 61 |
|
---|
| 62 | class USBDevice_ {
|
---|
[136] | 63 | public:
|
---|
[224] | 64 | USBDevice_() {};
|
---|
| 65 |
|
---|
| 66 | // USB Device API
|
---|
| 67 | void init();
|
---|
| 68 | bool attach(); // Serial port goes down too...
|
---|
| 69 | bool detach();
|
---|
| 70 |
|
---|
[136] | 71 | bool configured();
|
---|
| 72 |
|
---|
[224] | 73 | private:
|
---|
| 74 | bool initialized;
|
---|
[136] | 75 | };
|
---|
[224] | 76 |
|
---|
[136] | 77 | extern USBDevice_ USBDevice;
|
---|
| 78 |
|
---|
| 79 | //================================================================================
|
---|
| 80 | // Serial over CDC (Serial1 is the physical port)
|
---|
| 81 |
|
---|
| 82 | class Serial_ : public Stream
|
---|
| 83 | {
|
---|
| 84 | public:
|
---|
[224] | 85 | Serial_(USBDevice_ &_usb) : usb(_usb) { }
|
---|
[136] | 86 | void begin(uint32_t baud_count);
|
---|
| 87 | void begin(unsigned long, uint8_t);
|
---|
| 88 | void end(void);
|
---|
| 89 |
|
---|
| 90 | virtual int available(void);
|
---|
| 91 | virtual void accept(void);
|
---|
| 92 | virtual int peek(void);
|
---|
| 93 | virtual int read(void);
|
---|
| 94 | virtual void flush(void);
|
---|
| 95 | virtual size_t write(uint8_t);
|
---|
| 96 | virtual size_t write(const uint8_t *buffer, size_t size);
|
---|
| 97 | using Print::write; // pull in write(str) from Print
|
---|
| 98 | operator bool();
|
---|
[224] | 99 |
|
---|
| 100 | // This method allows processing "SEND_BREAK" requests sent by
|
---|
| 101 | // the USB host. Those requests indicate that the host wants to
|
---|
| 102 | // send a BREAK signal and are accompanied by a single uint16_t
|
---|
| 103 | // value, specifying the duration of the break. The value 0
|
---|
| 104 | // means to end any current break, while the value 0xffff means
|
---|
| 105 | // to start an indefinite break.
|
---|
| 106 | // readBreak() will return the value of the most recent break
|
---|
| 107 | // request, but will return it at most once, returning -1 when
|
---|
| 108 | // readBreak() is called again (until another break request is
|
---|
| 109 | // received, which is again returned once).
|
---|
| 110 | // This also mean that if two break requests are received
|
---|
| 111 | // without readBreak() being called in between, the value of the
|
---|
| 112 | // first request is lost.
|
---|
| 113 | // Note that the value returned is a long, so it can return
|
---|
| 114 | // 0-0xffff as well as -1.
|
---|
| 115 | int32_t readBreak();
|
---|
| 116 |
|
---|
| 117 | // These return the settings specified by the USB host for the
|
---|
| 118 | // serial port. These aren't really used, but are offered here
|
---|
| 119 | // in case a sketch wants to act on these settings.
|
---|
| 120 | uint32_t baud();
|
---|
| 121 | uint8_t stopbits();
|
---|
| 122 | uint8_t paritytype();
|
---|
| 123 | uint8_t numbits();
|
---|
| 124 | bool dtr();
|
---|
| 125 | bool rts();
|
---|
| 126 | enum {
|
---|
| 127 | ONE_STOP_BIT = 0,
|
---|
| 128 | ONE_AND_HALF_STOP_BIT = 1,
|
---|
| 129 | TWO_STOP_BITS = 2,
|
---|
| 130 | };
|
---|
| 131 | enum {
|
---|
| 132 | NO_PARITY = 0,
|
---|
| 133 | ODD_PARITY = 1,
|
---|
| 134 | EVEN_PARITY = 2,
|
---|
| 135 | MARK_PARITY = 3,
|
---|
| 136 | SPACE_PARITY = 4,
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | private:
|
---|
| 140 | USBDevice_ &usb;
|
---|
| 141 | RingBuffer *_cdc_rx_buffer;
|
---|
[136] | 142 | };
|
---|
| 143 | extern Serial_ SerialUSB;
|
---|
| 144 |
|
---|
| 145 | //================================================================================
|
---|
| 146 | //================================================================================
|
---|
| 147 | // Mouse
|
---|
| 148 |
|
---|
| 149 | #define MOUSE_LEFT 1
|
---|
| 150 | #define MOUSE_RIGHT 2
|
---|
| 151 | #define MOUSE_MIDDLE 4
|
---|
| 152 | #define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
|
---|
| 153 |
|
---|
| 154 | class Mouse_
|
---|
| 155 | {
|
---|
| 156 | private:
|
---|
| 157 | uint8_t _buttons;
|
---|
| 158 | void buttons(uint8_t b);
|
---|
| 159 | public:
|
---|
| 160 | Mouse_(void);
|
---|
| 161 | void begin(void);
|
---|
| 162 | void end(void);
|
---|
| 163 | void click(uint8_t b = MOUSE_LEFT);
|
---|
| 164 | void move(signed char x, signed char y, signed char wheel = 0);
|
---|
| 165 | void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
|
---|
| 166 | void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
|
---|
| 167 | bool isPressed(uint8_t b = MOUSE_ALL); // check all buttons by default
|
---|
| 168 | };
|
---|
| 169 | extern Mouse_ Mouse;
|
---|
| 170 |
|
---|
| 171 | //================================================================================
|
---|
| 172 | //================================================================================
|
---|
| 173 | // Keyboard
|
---|
| 174 |
|
---|
| 175 | #define KEY_LEFT_CTRL 0x80
|
---|
| 176 | #define KEY_LEFT_SHIFT 0x81
|
---|
| 177 | #define KEY_LEFT_ALT 0x82
|
---|
| 178 | #define KEY_LEFT_GUI 0x83
|
---|
| 179 | #define KEY_RIGHT_CTRL 0x84
|
---|
| 180 | #define KEY_RIGHT_SHIFT 0x85
|
---|
| 181 | #define KEY_RIGHT_ALT 0x86
|
---|
| 182 | #define KEY_RIGHT_GUI 0x87
|
---|
| 183 |
|
---|
| 184 | #define KEY_UP_ARROW 0xDA
|
---|
| 185 | #define KEY_DOWN_ARROW 0xD9
|
---|
| 186 | #define KEY_LEFT_ARROW 0xD8
|
---|
| 187 | #define KEY_RIGHT_ARROW 0xD7
|
---|
| 188 | #define KEY_BACKSPACE 0xB2
|
---|
| 189 | #define KEY_TAB 0xB3
|
---|
| 190 | #define KEY_RETURN 0xB0
|
---|
| 191 | #define KEY_ESC 0xB1
|
---|
| 192 | #define KEY_INSERT 0xD1
|
---|
| 193 | #define KEY_DELETE 0xD4
|
---|
| 194 | #define KEY_PAGE_UP 0xD3
|
---|
| 195 | #define KEY_PAGE_DOWN 0xD6
|
---|
| 196 | #define KEY_HOME 0xD2
|
---|
| 197 | #define KEY_END 0xD5
|
---|
| 198 | #define KEY_CAPS_LOCK 0xC1
|
---|
| 199 | #define KEY_F1 0xC2
|
---|
| 200 | #define KEY_F2 0xC3
|
---|
| 201 | #define KEY_F3 0xC4
|
---|
| 202 | #define KEY_F4 0xC5
|
---|
| 203 | #define KEY_F5 0xC6
|
---|
| 204 | #define KEY_F6 0xC7
|
---|
| 205 | #define KEY_F7 0xC8
|
---|
| 206 | #define KEY_F8 0xC9
|
---|
| 207 | #define KEY_F9 0xCA
|
---|
| 208 | #define KEY_F10 0xCB
|
---|
| 209 | #define KEY_F11 0xCC
|
---|
| 210 | #define KEY_F12 0xCD
|
---|
| 211 |
|
---|
| 212 | // Low level key report: up to 6 keys and shift, ctrl etc at once
|
---|
| 213 | typedef struct
|
---|
| 214 | {
|
---|
| 215 | uint8_t modifiers;
|
---|
| 216 | uint8_t reserved;
|
---|
| 217 | uint8_t keys[6];
|
---|
| 218 | } KeyReport;
|
---|
| 219 |
|
---|
| 220 | class Keyboard_ : public Print
|
---|
| 221 | {
|
---|
| 222 | private:
|
---|
| 223 | KeyReport _keyReport;
|
---|
| 224 | void sendReport(KeyReport* keys);
|
---|
| 225 | public:
|
---|
| 226 | Keyboard_(void);
|
---|
| 227 | void begin(void);
|
---|
| 228 | void end(void);
|
---|
| 229 | virtual size_t write(uint8_t k);
|
---|
| 230 | virtual size_t press(uint8_t k);
|
---|
| 231 | virtual size_t release(uint8_t k);
|
---|
| 232 | virtual void releaseAll(void);
|
---|
| 233 | };
|
---|
| 234 | extern Keyboard_ Keyboard;
|
---|
| 235 |
|
---|
| 236 | //================================================================================
|
---|
| 237 | //================================================================================
|
---|
| 238 | // HID 'Driver'
|
---|
| 239 |
|
---|
| 240 | const void* WEAK HID_GetInterface(void);
|
---|
| 241 | uint32_t WEAK HID_GetInterfaceLength(void);
|
---|
| 242 | uint32_t HID_SizeReportDescriptor(void);
|
---|
| 243 |
|
---|
| 244 | uint32_t HID_GetDescriptor(void);
|
---|
| 245 | bool HID_Setup(Setup& setup);
|
---|
| 246 | void HID_SendReport(uint8_t id, const void* data, uint32_t len);
|
---|
| 247 |
|
---|
| 248 | //================================================================================
|
---|
| 249 | //================================================================================
|
---|
| 250 | // MSC 'Driver'
|
---|
| 251 |
|
---|
| 252 | uint32_t MSC_GetInterface(uint8_t* interfaceNum);
|
---|
| 253 | uint32_t MSC_GetDescriptor(uint32_t i);
|
---|
[224] | 254 | bool MSC_Setup(Setup& pSetup);
|
---|
[136] | 255 | bool MSC_Data(uint8_t rx,uint8_t tx);
|
---|
| 256 |
|
---|
| 257 | //================================================================================
|
---|
| 258 | //================================================================================
|
---|
| 259 | // CDC 'Driver'
|
---|
| 260 |
|
---|
[224] | 261 | int CDC_GetInterface(uint8_t* interfaceNum);
|
---|
| 262 | const void* _CDC_GetInterface(void);
|
---|
| 263 | uint32_t _CDC_GetInterfaceLength(void);
|
---|
[136] | 264 | uint32_t CDC_GetOtherInterface(uint8_t* interfaceNum);
|
---|
| 265 | uint32_t CDC_GetDescriptor(uint32_t i);
|
---|
[224] | 266 | bool CDC_Setup(Setup& pSetup);
|
---|
[136] | 267 |
|
---|
[224] | 268 |
|
---|
[136] | 269 | //================================================================================
|
---|
| 270 | //================================================================================
|
---|
| 271 |
|
---|
[224] | 272 | uint32_t USBD_SendControl(const void* _data, uint32_t len);
|
---|
[136] | 273 | uint32_t USBD_RecvControl(void* d, uint32_t len);
|
---|
[224] | 274 | void USBD_Calibrate();
|
---|
[136] | 275 | uint32_t USBD_SendInterfaces(void);
|
---|
| 276 | bool USBD_ClassInterfaceRequest(Setup& setup);
|
---|
| 277 |
|
---|
| 278 | uint32_t USBD_Available(uint32_t ep);
|
---|
| 279 | uint32_t USBD_SendSpace(uint32_t ep);
|
---|
| 280 | uint32_t USBD_Send(uint32_t ep, const void* d, uint32_t len);
|
---|
| 281 | uint32_t USBD_Recv(uint32_t ep, void* data, uint32_t len); // non-blocking
|
---|
| 282 | uint32_t USBD_Recv(uint32_t ep); // non-blocking
|
---|
[224] | 283 | uint8_t USBD_armRecv(uint32_t ep);
|
---|
[136] | 284 | void USBD_Flush(uint32_t ep);
|
---|
| 285 | uint32_t USBD_Connected(void);
|
---|
| 286 |
|
---|
| 287 | #endif // __cplusplus
|
---|