1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
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
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
19 void MouseReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
20 MOUSEINFO *pmi = (MOUSEINFO*)buf;
25 if (prevState.mouseInfo.bmLeftButton == 0 && pmi->bmLeftButton == 1)
26 OnLeftButtonDown(pmi);
28 if (prevState.mouseInfo.bmLeftButton == 1 && pmi->bmLeftButton == 0)
31 if (prevState.mouseInfo.bmRightButton == 0 && pmi->bmRightButton == 1)
32 OnRightButtonDown(pmi);
34 if (prevState.mouseInfo.bmRightButton == 1 && pmi->bmRightButton == 0)
37 if (prevState.mouseInfo.bmMiddleButton == 0 && pmi->bmMiddleButton == 1)
38 OnMiddleButtonDown(pmi);
40 if (prevState.mouseInfo.bmMiddleButton == 1 && pmi->bmMiddleButton == 0)
41 OnMiddleButtonUp(pmi);
43 if (prevState.mouseInfo.dX != pmi->dX || prevState.mouseInfo.dY != pmi->dY)
46 if (len > sizeof (MOUSEINFO))
47 for (uint8_t i = 0; i<sizeof (MOUSEINFO); i++)
48 prevState.bInfo[i] = buf[i];
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.
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.
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.
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);
78 if(prevState.mouseInfo.bmRightButton != pmi->bmRightButton) {
79 if(pmi->bmRightButton) {
80 OnRightButtonDown(pmi);
88 if(prevState.mouseInfo.bmMiddleButton != pmi->bmMiddleButton) {
89 if(pmi->bmMiddleButton) {
90 OnMiddleButtonDown(pmi);
92 OnMiddleButtonUp(pmi);
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.
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) {
116 // Provide a callback that operates on the gathered events from above.
118 // if(event) OnMouse();
121 // Only the first byte matters (buttons). We do NOT need to save position info.
122 prevState.bInfo[0] = buf[0];
127 void KeyboardReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
132 //KBDINFO *pki = (KBDINFO*)buf;
134 // provide event for changed control key state
135 if (prevState.bInfo[0x00] != buf[0x00]) {
136 OnControlKeysChanged(prevState.bInfo[0x00], buf[0x00]);
139 for (uint8_t i = 2; i < 8; i++) {
143 for (uint8_t j = 2; j < 8; j++) {
144 if (buf[i] == prevState.bInfo[j] && buf[i] != 1)
146 if (buf[j] == prevState.bInfo[i] && prevState.bInfo[i] != 1)
150 HandleLockingKeys(hid, buf[i]);
151 OnKeyDown(*buf, buf[i]);
154 OnKeyUp(prevState.bInfo[0], prevState.bInfo[i]);
156 for (uint8_t i = 0; i < 8; i++)
157 prevState.bInfo[i] = buf[i];
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};
165 uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) {
166 uint8_t shift = (mod & 0x22);
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');
175 // Lower case letters
177 return (key - 4 + 'a');
179 else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
181 return ((uint8_t)pgm_read_byte(&getNumKeys()[key - 0x1e]));
183 return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
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]);
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);