HID.cpp
Go to the documentation of this file.
1 
2 
3 /* Copyright (c) 2011, Peter Barrett
4 **
5 ** Permission to use, copy, modify, and/or distribute this software for
6 ** any purpose with or without fee is hereby granted, provided that the
7 ** above copyright notice and this permission notice appear in all copies.
8 **
9 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12 ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13 ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14 ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15 ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 ** SOFTWARE.
17 */
18 
19 #include "USBAPI.h"
20 
21 #if defined(USBCON)
22 #ifdef HID_ENABLED
23 
24 //#define RAWHID_ENABLED
25 
26 // Singletons for mouse and keyboard
27 
28 Mouse_ Mouse;
29 Keyboard_ Keyboard;
30 
31 //================================================================================
32 //================================================================================
33 
34 // HID report descriptor
35 
36 #define LSB(_x) ((_x) & 0xFF)
37 #define MSB(_x) ((_x) >> 8)
38 
39 #define RAWHID_USAGE_PAGE 0xFFC0
40 #define RAWHID_USAGE 0x0C00
41 #define RAWHID_TX_SIZE 64
42 #define RAWHID_RX_SIZE 64
43 
44 extern const u8 _hidReportDescriptor[] PROGMEM;
45 const u8 _hidReportDescriptor[] = {
46 
47  // Mouse
48  0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54
49  0x09, 0x02, // USAGE (Mouse)
50  0xa1, 0x01, // COLLECTION (Application)
51  0x09, 0x01, // USAGE (Pointer)
52  0xa1, 0x00, // COLLECTION (Physical)
53  0x85, 0x01, // REPORT_ID (1)
54  0x05, 0x09, // USAGE_PAGE (Button)
55  0x19, 0x01, // USAGE_MINIMUM (Button 1)
56  0x29, 0x03, // USAGE_MAXIMUM (Button 3)
57  0x15, 0x00, // LOGICAL_MINIMUM (0)
58  0x25, 0x01, // LOGICAL_MAXIMUM (1)
59  0x95, 0x03, // REPORT_COUNT (3)
60  0x75, 0x01, // REPORT_SIZE (1)
61  0x81, 0x02, // INPUT (Data,Var,Abs)
62  0x95, 0x01, // REPORT_COUNT (1)
63  0x75, 0x05, // REPORT_SIZE (5)
64  0x81, 0x03, // INPUT (Cnst,Var,Abs)
65  0x05, 0x01, // USAGE_PAGE (Generic Desktop)
66  0x09, 0x30, // USAGE (X)
67  0x09, 0x31, // USAGE (Y)
68  0x09, 0x38, // USAGE (Wheel)
69  0x15, 0x81, // LOGICAL_MINIMUM (-127)
70  0x25, 0x7f, // LOGICAL_MAXIMUM (127)
71  0x75, 0x08, // REPORT_SIZE (8)
72  0x95, 0x03, // REPORT_COUNT (3)
73  0x81, 0x06, // INPUT (Data,Var,Rel)
74  0xc0, // END_COLLECTION
75  0xc0, // END_COLLECTION
76 
77  // Keyboard
78  0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
79  0x09, 0x06, // USAGE (Keyboard)
80  0xa1, 0x01, // COLLECTION (Application)
81  0x85, 0x02, // REPORT_ID (2)
82  0x05, 0x07, // USAGE_PAGE (Keyboard)
83 
84  0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
85  0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
86  0x15, 0x00, // LOGICAL_MINIMUM (0)
87  0x25, 0x01, // LOGICAL_MAXIMUM (1)
88  0x75, 0x01, // REPORT_SIZE (1)
89 
90  0x95, 0x08, // REPORT_COUNT (8)
91  0x81, 0x02, // INPUT (Data,Var,Abs)
92  0x95, 0x01, // REPORT_COUNT (1)
93  0x75, 0x08, // REPORT_SIZE (8)
94  0x81, 0x03, // INPUT (Cnst,Var,Abs)
95 
96  0x95, 0x06, // REPORT_COUNT (6)
97  0x75, 0x08, // REPORT_SIZE (8)
98  0x15, 0x00, // LOGICAL_MINIMUM (0)
99  0x25, 0x65, // LOGICAL_MAXIMUM (101)
100  0x05, 0x07, // USAGE_PAGE (Keyboard)
101 
102  0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
103  0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
104  0x81, 0x00, // INPUT (Data,Ary,Abs)
105  0xc0, // END_COLLECTION
106 
107 #ifdef RAWHID_ENABLED
108  // RAW HID
109  0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30
110  0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
111 
112  0xA1, 0x01, // Collection 0x01
113  0x85, 0x03, // REPORT_ID (3)
114  0x75, 0x08, // report size = 8 bits
115  0x15, 0x00, // logical minimum = 0
116  0x26, 0xFF, 0x00, // logical maximum = 255
117 
118  0x95, 64, // report count TX
119  0x09, 0x01, // usage
120  0x81, 0x02, // Input (array)
121 
122  0x95, 64, // report count RX
123  0x09, 0x02, // usage
124  0x91, 0x02, // Output (array)
125  0xC0 // end collection
126 #endif
127 };
128 
129 extern const HIDDescriptor _hidInterface PROGMEM;
130 const HIDDescriptor _hidInterface =
131 {
132  D_INTERFACE(HID_INTERFACE,1,3,0,0),
133  D_HIDREPORT(sizeof(_hidReportDescriptor)),
134  D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
135 };
136 
137 //================================================================================
138 //================================================================================
139 // Driver
140 
141 u8 _hid_protocol = 1;
142 u8 _hid_idle = 1;
143 
144 #define WEAK __attribute__ ((weak))
145 
146 int WEAK HID_GetInterface(u8* interfaceNum)
147 {
148  interfaceNum[0] += 1; // uses 1
149  return USB_SendControl(TRANSFER_PGM,&_hidInterface,sizeof(_hidInterface));
150 }
151 
152 int WEAK HID_GetDescriptor(int /* i */)
153 {
154  return USB_SendControl(TRANSFER_PGM,_hidReportDescriptor,sizeof(_hidReportDescriptor));
155 }
156 
157 void WEAK HID_SendReport(u8 id, const void* data, int len)
158 {
159  USB_Send(HID_TX, &id, 1);
160  USB_Send(HID_TX | TRANSFER_RELEASE,data,len);
161 }
162 
163 bool WEAK HID_Setup(Setup& setup)
164 {
165  u8 r = setup.bRequest;
166  u8 requestType = setup.bmRequestType;
167  if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
168  {
169  if (HID_GET_REPORT == r)
170  {
171  //HID_GetReport();
172  return true;
173  }
174  if (HID_GET_PROTOCOL == r)
175  {
176  //Send8(_hid_protocol); // TODO
177  return true;
178  }
179  }
180 
181  if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
182  {
183  if (HID_SET_PROTOCOL == r)
184  {
185  _hid_protocol = setup.wValueL;
186  return true;
187  }
188 
189  if (HID_SET_IDLE == r)
190  {
191  _hid_idle = setup.wValueL;
192  return true;
193  }
194  }
195  return false;
196 }
197 
198 //================================================================================
199 //================================================================================
200 // Mouse
201 
202 Mouse_::Mouse_(void) : _buttons(0)
203 {
204 }
205 
206 void Mouse_::begin(void)
207 {
208 }
209 
210 void Mouse_::end(void)
211 {
212 }
213 
214 void Mouse_::click(uint8_t b)
215 {
216  _buttons = b;
217  move(0,0,0);
218  _buttons = 0;
219  move(0,0,0);
220 }
221 
222 void Mouse_::move(signed char x, signed char y, signed char wheel)
223 {
224  u8 m[4];
225  m[0] = _buttons;
226  m[1] = x;
227  m[2] = y;
228  m[3] = wheel;
229  HID_SendReport(1,m,4);
230 }
231 
232 void Mouse_::buttons(uint8_t b)
233 {
234  if (b != _buttons)
235  {
236  _buttons = b;
237  move(0,0,0);
238  }
239 }
240 
241 void Mouse_::press(uint8_t b)
242 {
243  buttons(_buttons | b);
244 }
245 
246 void Mouse_::release(uint8_t b)
247 {
248  buttons(_buttons & ~b);
249 }
250 
251 bool Mouse_::isPressed(uint8_t b)
252 {
253  if ((b & _buttons) > 0)
254  return true;
255  return false;
256 }
257 
258 //================================================================================
259 //================================================================================
260 // Keyboard
261 
262 Keyboard_::Keyboard_(void)
263 {
264 }
265 
266 void Keyboard_::begin(void)
267 {
268 }
269 
270 void Keyboard_::end(void)
271 {
272 }
273 
274 void Keyboard_::sendReport(KeyReport* keys)
275 {
276  HID_SendReport(2,keys,sizeof(KeyReport));
277 }
278 
279 extern
280 const uint8_t _asciimap[128] PROGMEM;
281 
282 #define SHIFT 0x80
283 const uint8_t _asciimap[128] =
284 {
285  0x00, // NUL
286  0x00, // SOH
287  0x00, // STX
288  0x00, // ETX
289  0x00, // EOT
290  0x00, // ENQ
291  0x00, // ACK
292  0x00, // BEL
293  0x2a, // BS Backspace
294  0x2b, // TAB Tab
295  0x28, // LF Enter
296  0x00, // VT
297  0x00, // FF
298  0x00, // CR
299  0x00, // SO
300  0x00, // SI
301  0x00, // DEL
302  0x00, // DC1
303  0x00, // DC2
304  0x00, // DC3
305  0x00, // DC4
306  0x00, // NAK
307  0x00, // SYN
308  0x00, // ETB
309  0x00, // CAN
310  0x00, // EM
311  0x00, // SUB
312  0x00, // ESC
313  0x00, // FS
314  0x00, // GS
315  0x00, // RS
316  0x00, // US
317 
318  0x2c, // ' '
319  0x1e|SHIFT, // !
320  0x34|SHIFT, // "
321  0x20|SHIFT, // #
322  0x21|SHIFT, // $
323  0x22|SHIFT, // %
324  0x24|SHIFT, // &
325  0x34, // '
326  0x26|SHIFT, // (
327  0x27|SHIFT, // )
328  0x25|SHIFT, // *
329  0x2e|SHIFT, // +
330  0x36, // ,
331  0x2d, // -
332  0x37, // .
333  0x38, // /
334  0x27, // 0
335  0x1e, // 1
336  0x1f, // 2
337  0x20, // 3
338  0x21, // 4
339  0x22, // 5
340  0x23, // 6
341  0x24, // 7
342  0x25, // 8
343  0x26, // 9
344  0x33|SHIFT, // :
345  0x33, // ;
346  0x36|SHIFT, // <
347  0x2e, // =
348  0x37|SHIFT, // >
349  0x38|SHIFT, // ?
350  0x1f|SHIFT, // @
351  0x04|SHIFT, // A
352  0x05|SHIFT, // B
353  0x06|SHIFT, // C
354  0x07|SHIFT, // D
355  0x08|SHIFT, // E
356  0x09|SHIFT, // F
357  0x0a|SHIFT, // G
358  0x0b|SHIFT, // H
359  0x0c|SHIFT, // I
360  0x0d|SHIFT, // J
361  0x0e|SHIFT, // K
362  0x0f|SHIFT, // L
363  0x10|SHIFT, // M
364  0x11|SHIFT, // N
365  0x12|SHIFT, // O
366  0x13|SHIFT, // P
367  0x14|SHIFT, // Q
368  0x15|SHIFT, // R
369  0x16|SHIFT, // S
370  0x17|SHIFT, // T
371  0x18|SHIFT, // U
372  0x19|SHIFT, // V
373  0x1a|SHIFT, // W
374  0x1b|SHIFT, // X
375  0x1c|SHIFT, // Y
376  0x1d|SHIFT, // Z
377  0x2f, // [
378  0x31, // bslash
379  0x30, // ]
380  0x23|SHIFT, // ^
381  0x2d|SHIFT, // _
382  0x35, // `
383  0x04, // a
384  0x05, // b
385  0x06, // c
386  0x07, // d
387  0x08, // e
388  0x09, // f
389  0x0a, // g
390  0x0b, // h
391  0x0c, // i
392  0x0d, // j
393  0x0e, // k
394  0x0f, // l
395  0x10, // m
396  0x11, // n
397  0x12, // o
398  0x13, // p
399  0x14, // q
400  0x15, // r
401  0x16, // s
402  0x17, // t
403  0x18, // u
404  0x19, // v
405  0x1a, // w
406  0x1b, // x
407  0x1c, // y
408  0x1d, // z
409  0x2f|SHIFT, //
410  0x31|SHIFT, // |
411  0x30|SHIFT, // }
412  0x35|SHIFT, // ~
413  0 // DEL
414 };
415 
416 uint8_t USBPutChar(uint8_t c);
417 
418 // press() adds the specified key (printing, non-printing, or modifier)
419 // to the persistent key report and sends the report. Because of the way
420 // USB HID works, the host acts like the key remains pressed until we
421 // call release(), releaseAll(), or otherwise clear the report and resend.
422 size_t Keyboard_::press(uint8_t k)
423 {
424  uint8_t i;
425  if (k >= 136) { // it's a non-printing key (not a modifier)
426  k = k - 136;
427  } else if (k >= 128) { // it's a modifier key
428  _keyReport.modifiers |= (1<<(k-128));
429  k = 0;
430  } else { // it's a printing key
431  k = pgm_read_byte(_asciimap + k);
432  if (!k) {
433  setWriteError();
434  return 0;
435  }
436  if (k & 0x80) { // it's a capital letter or other character reached with shift
437  _keyReport.modifiers |= 0x02; // the left shift modifier
438  k &= 0x7F;
439  }
440  }
441 
442  // Add k to the key report only if it's not already present
443  // and if there is an empty slot.
444  if (_keyReport.keys[0] != k && _keyReport.keys[1] != k &&
445  _keyReport.keys[2] != k && _keyReport.keys[3] != k &&
446  _keyReport.keys[4] != k && _keyReport.keys[5] != k) {
447 
448  for (i=0; i<6; i++) {
449  if (_keyReport.keys[i] == 0x00) {
450  _keyReport.keys[i] = k;
451  break;
452  }
453  }
454  if (i == 6) {
455  setWriteError();
456  return 0;
457  }
458  }
459  sendReport(&_keyReport);
460  return 1;
461 }
462 
463 // release() takes the specified key out of the persistent key report and
464 // sends the report. This tells the OS the key is no longer pressed and that
465 // it shouldn't be repeated any more.
466 size_t Keyboard_::release(uint8_t k)
467 {
468  uint8_t i;
469  if (k >= 136) { // it's a non-printing key (not a modifier)
470  k = k - 136;
471  } else if (k >= 128) { // it's a modifier key
472  _keyReport.modifiers &= ~(1<<(k-128));
473  k = 0;
474  } else { // it's a printing key
475  k = pgm_read_byte(_asciimap + k);
476  if (!k) {
477  return 0;
478  }
479  if (k & 0x80) { // it's a capital letter or other character reached with shift
480  _keyReport.modifiers &= ~(0x02); // the left shift modifier
481  k &= 0x7F;
482  }
483  }
484 
485  // Test the key report to see if k is present. Clear it if it exists.
486  // Check all positions in case the key is present more than once (which it shouldn't be)
487  for (i=0; i<6; i++) {
488  if (0 != k && _keyReport.keys[i] == k) {
489  _keyReport.keys[i] = 0x00;
490  }
491  }
492 
493  sendReport(&_keyReport);
494  return 1;
495 }
496 
497 void Keyboard_::releaseAll(void)
498 {
499  _keyReport.keys[0] = 0;
500  _keyReport.keys[1] = 0;
501  _keyReport.keys[2] = 0;
502  _keyReport.keys[3] = 0;
503  _keyReport.keys[4] = 0;
504  _keyReport.keys[5] = 0;
505  _keyReport.modifiers = 0;
506  sendReport(&_keyReport);
507 }
508 
509 size_t Keyboard_::write(uint8_t c)
510 {
511  uint8_t p = press(c); // Keydown
512  release(c); // Keyup
513  return p; // just return the result of press() since release() almost always returns 1
514 }
515 
516 #endif
517 
518 #endif /* if defined(USBCON) */
GLint GLint GLint GLint GLint GLint y
void setWriteError(int err=1)
Definition: Print.h:44
const GLfloat * c
#define D_ENDPOINT(_addr, _attr, _packetSize, _interval)
Definition: USBCore.h:275
GLenum GLsizei len
GLint GLint GLint GLint GLint x
unsigned char u8
Definition: USBAPI.h:29
#define REQUEST_HOSTTODEVICE_CLASS_INTERFACE
Definition: USBCore.h:53
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
GLfloat GLfloat p
#define REQUEST_DEVICETOHOST_CLASS_INTERFACE
Definition: USBCore.h:52
GLdouble GLdouble GLdouble r
#define USB_ENDPOINT_TYPE_INTERRUPT
Definition: USBCore.h:110
const GLdouble * m
GLdouble GLdouble GLdouble b
const T & move(const T &t)
#define D_INTERFACE(_n, _numEndpoints, _class, _subClass, _protocol)
Definition: USBCore.h:272
#define USB_ENDPOINT_IN(addr)
Definition: USBCore.h:104


arduino_daq
Author(s):
autogenerated on Mon Jun 10 2019 12:46:03