USB Host Shield 2.0
hidboot.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16  */
17 #include "hidboot.h"
18 
19 void MouseReportParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
20  MOUSEINFO *pmi = (MOUSEINFO*)buf;
21  // Future:
22  // bool event;
23 
24 #if 0
25  if (prevState.mouseInfo.bmLeftButton == 0 && pmi->bmLeftButton == 1)
26  OnLeftButtonDown(pmi);
27 
28  if (prevState.mouseInfo.bmLeftButton == 1 && pmi->bmLeftButton == 0)
29  OnLeftButtonUp(pmi);
30 
31  if (prevState.mouseInfo.bmRightButton == 0 && pmi->bmRightButton == 1)
32  OnRightButtonDown(pmi);
33 
34  if (prevState.mouseInfo.bmRightButton == 1 && pmi->bmRightButton == 0)
35  OnRightButtonUp(pmi);
36 
37  if (prevState.mouseInfo.bmMiddleButton == 0 && pmi->bmMiddleButton == 1)
38  OnMiddleButtonDown(pmi);
39 
40  if (prevState.mouseInfo.bmMiddleButton == 1 && pmi->bmMiddleButton == 0)
41  OnMiddleButtonUp(pmi);
42 
43  if (prevState.mouseInfo.dX != pmi->dX || prevState.mouseInfo.dY != pmi->dY)
44  OnMouseMove(pmi);
45 
46  if (len > sizeof (MOUSEINFO))
47  for (uint8_t i = 0; i<sizeof (MOUSEINFO); i++)
48  prevState.bInfo[i] = buf[i];
49 #else
50  //
51  // Optimization idea:
52  //
53  // 1: Don't pass the structure on every event. Buttons would not need it.
54  // 2: Only pass x/y values in the movement routine.
55  //
56  // These two changes (with the ones I have made) will save extra flash.
57  // The only "bad" thing is that it could break old code.
58  //
59  // Future thoughts:
60  //
61  // The extra space gained can be used for a generic mouse event that can be called
62  // when there are _ANY_ changes. This one you _MAY_ want to pass everything, however the
63  // sketch could already have noted these facts to support drag/drop scroll wheel stuff, etc.
64  //
65 
66  // Why do we need to pass the structure for buttons?
67  // The function call not enough of a hint for what is happening?
68  if(prevState.mouseInfo.bmLeftButton != pmi->bmLeftButton ) {
69  if(pmi->bmLeftButton) {
70  OnLeftButtonDown(pmi);
71  } else {
72  OnLeftButtonUp(pmi);
73  }
74  // Future:
75  // event = true;
76  }
77 
78  if(prevState.mouseInfo.bmRightButton != pmi->bmRightButton) {
79  if(pmi->bmRightButton) {
80  OnRightButtonDown(pmi);
81  } else {
82  OnRightButtonUp(pmi);
83  }
84  // Future:
85  // event = true;
86  }
87 
88  if(prevState.mouseInfo.bmMiddleButton != pmi->bmMiddleButton) {
89  if(pmi->bmMiddleButton) {
90  OnMiddleButtonDown(pmi);
91  } else {
92  OnMiddleButtonUp(pmi);
93  }
94  // Future:
95  // event = true;
96  }
97 
98  //
99  // Scroll wheel(s), are not part of the spec, but we could support it.
100  // Logitech wireless keyboard and mouse combo reports scroll wheel in byte 4
101  // We wouldn't even need to save this information.
102  //if(len > 3) {
103  //}
104  //
105 
106  // Mice only report motion when they actually move!
107  // Why not just pass the x/y values to simplify things??
108  if(pmi->dX || pmi->dY) {
109  OnMouseMove(pmi);
110  // Future:
111  // event = true;
112  }
113 
114  //
115  // Future:
116  // Provide a callback that operates on the gathered events from above.
117  //
118  // if(event) OnMouse();
119  //
120 
121  // Only the first byte matters (buttons). We do NOT need to save position info.
122  prevState.bInfo[0] = buf[0];
123 #endif
124 
125 };
126 
127 void KeyboardReportParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
128  // On error - return
129  if (buf[2] == 1)
130  return;
131 
132  //KBDINFO *pki = (KBDINFO*)buf;
133 
134  // provide event for changed control key state
135  if (prevState.bInfo[0x00] != buf[0x00]) {
136  OnControlKeysChanged(prevState.bInfo[0x00], buf[0x00]);
137  }
138 
139  for (uint8_t i = 2; i < 8; i++) {
140  bool down = false;
141  bool up = false;
142 
143  for (uint8_t j = 2; j < 8; j++) {
144  if (buf[i] == prevState.bInfo[j] && buf[i] != 1)
145  down = true;
146  if (buf[j] == prevState.bInfo[i] && prevState.bInfo[i] != 1)
147  up = true;
148  }
149  if (!down) {
150  HandleLockingKeys(hid, buf[i]);
151  OnKeyDown(*buf, buf[i]);
152  }
153  if (!up)
154  OnKeyUp(prevState.bInfo[0], prevState.bInfo[i]);
155  }
156  for (uint8_t i = 0; i < 8; i++)
157  prevState.bInfo[i] = buf[i];
158 };
159 
160 const uint8_t KeyboardReportParser::numKeys[10] PROGMEM = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')'};
161 const uint8_t KeyboardReportParser::symKeysUp[12] PROGMEM = {'_', '+', '{', '}', '|', '~', ':', '"', '~', '<', '>', '?'};
162 const uint8_t KeyboardReportParser::symKeysLo[12] PROGMEM = {'-', '=', '[', ']', '\\', ' ', ';', '\'', '`', ',', '.', '/'};
163 const uint8_t KeyboardReportParser::padKeys[5] PROGMEM = {'/', '*', '-', '+', 0x13};
164 
165 uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) {
166  uint8_t shift = (mod & 0x22);
167 
168  // [a-z]
169  if (VALUE_WITHIN(key, 0x04, 0x1d)) {
170  // Upper case letters
171  if ((kbdLockingKeys.kbdLeds.bmCapsLock == 0 && shift) ||
172  (kbdLockingKeys.kbdLeds.bmCapsLock == 1 && shift == 0))
173  return (key - 4 + 'A');
174 
175  // Lower case letters
176  else
177  return (key - 4 + 'a');
178  }// Numbers
179  else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
180  if (shift)
181  return ((uint8_t)pgm_read_byte(&getNumKeys()[key - 0x1e]));
182  else
183  return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
184  }// Keypad Numbers
185  else if(VALUE_WITHIN(key, 0x59, 0x61)) {
186  if(kbdLockingKeys.kbdLeds.bmNumLock == 1)
187  return (key - 0x59 + '1');
188  } else if(VALUE_WITHIN(key, 0x2d, 0x38))
189  return ((shift) ? (uint8_t)pgm_read_byte(&getSymKeysUp()[key - 0x2d]) : (uint8_t)pgm_read_byte(&getSymKeysLo()[key - 0x2d]));
190  else if(VALUE_WITHIN(key, 0x54, 0x58))
191  return (uint8_t)pgm_read_byte(&getPadKeys()[key - 0x54]);
192  else {
193  switch(key) {
194  case UHS_HID_BOOT_KEY_SPACE: return (0x20);
195  case UHS_HID_BOOT_KEY_ENTER: return (0x13);
196  case UHS_HID_BOOT_KEY_ZERO2: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '0': 0);
197  case UHS_HID_BOOT_KEY_PERIOD: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '.': 0);
198  }
199  }
200  return ( 0);
201 }
Definition: usbhid.h:143
#define UHS_HID_BOOT_KEY_PERIOD
Definition: hidboot.h:29
uint8_t bmRightButton
Definition: hidboot.h:43
virtual void OnRightButtonDown(MOUSEINFO *mi)
Definition: hidboot.h:75
virtual void OnMiddleButtonUp(MOUSEINFO *mi)
Definition: hidboot.h:78
#define UHS_HID_BOOT_KEY_ZERO2
Definition: hidboot.h:28
#define pgm_read_byte(addr)
#define UHS_HID_BOOT_KEY_ZERO
Definition: hidboot.h:22
uint8_t bmMiddleButton
Definition: hidboot.h:44
void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: hidboot.cpp:127
virtual void OnLeftButtonUp(MOUSEINFO *mi)
Definition: hidboot.h:66
#define UHS_HID_BOOT_KEY_SPACE
Definition: hidboot.h:24
virtual void OnMouseMove(MOUSEINFO *mi)
Definition: hidboot.h:63
virtual void OnRightButtonUp(MOUSEINFO *mi)
Definition: hidboot.h:72
#define VALUE_WITHIN(v, l, h)
Definition: macros.h:28
virtual void OnMiddleButtonDown(MOUSEINFO *mi)
Definition: hidboot.h:81
#define UHS_HID_BOOT_KEY_ENTER
Definition: hidboot.h:23
int8_t dX
Definition: hidboot.h:47
virtual void OnLeftButtonDown(MOUSEINFO *mi)
Definition: hidboot.h:69
int8_t dY
Definition: hidboot.h:48
void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: hidboot.cpp:19
uint8_t bmLeftButton
Definition: hidboot.h:42
uint8_t OemToAscii(uint8_t mod, uint8_t key)
Definition: hidboot.cpp:165