USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
20  MOUSEINFO *pmi = (MOUSEINFO*)buf;
21 
22  if (prevState.mouseInfo.bmLeftButton == 0 && pmi->bmLeftButton == 1)
23  OnLeftButtonDown(pmi);
24 
25  if (prevState.mouseInfo.bmLeftButton == 1 && pmi->bmLeftButton == 0)
26  OnLeftButtonUp(pmi);
27 
28  if (prevState.mouseInfo.bmRightButton == 0 && pmi->bmRightButton == 1)
29  OnRightButtonDown(pmi);
30 
31  if (prevState.mouseInfo.bmRightButton == 1 && pmi->bmRightButton == 0)
32  OnRightButtonUp(pmi);
33 
34  if (prevState.mouseInfo.bmMiddleButton == 0 && pmi->bmMiddleButton == 1)
35  OnMiddleButtonDown(pmi);
36 
37  if (prevState.mouseInfo.bmMiddleButton == 1 && pmi->bmMiddleButton == 0)
38  OnMiddleButtonUp(pmi);
39 
40  if (prevState.mouseInfo.dX != pmi->dX || prevState.mouseInfo.dY != pmi->dY)
41  OnMouseMove(pmi);
42 
43  if (len > sizeof (MOUSEINFO))
44  for (uint8_t i = 0; i<sizeof (MOUSEINFO); i++)
45  prevState.bInfo[i] = buf[i];
46 };
47 
48 void KeyboardReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
49  // On error - return
50  if (buf[2] == 1)
51  return;
52 
53  //KBDINFO *pki = (KBDINFO*)buf;
54 
55  for (uint8_t i = 2; i < 8; i++) {
56  bool down = false;
57  bool up = false;
58 
59  for (uint8_t j = 2; j < 8; j++) {
60  if (buf[i] == prevState.bInfo[j] && buf[i] != 1)
61  down = true;
62  if (buf[j] == prevState.bInfo[i] && prevState.bInfo[i] != 1)
63  up = true;
64  }
65  if (!down) {
66  HandleLockingKeys(hid, buf[i]);
67  OnKeyDown(*buf, buf[i]);
68  }
69  if (!up)
70  OnKeyUp(prevState.bInfo[0], prevState.bInfo[i]);
71  }
72  for (uint8_t i = 0; i < 8; i++)
73  prevState.bInfo[i] = buf[i];
74 };
75 
76 uint8_t KeyboardReportParser::HandleLockingKeys(HID *hid, uint8_t key) {
77  uint8_t old_keys = kbdLockingKeys.bLeds;
78 
79  switch (key) {
80  case KEY_NUM_LOCK:
81  kbdLockingKeys.kbdLeds.bmNumLock = ~kbdLockingKeys.kbdLeds.bmNumLock;
82  break;
83  case KEY_CAPS_LOCK:
84  kbdLockingKeys.kbdLeds.bmCapsLock = ~kbdLockingKeys.kbdLeds.bmCapsLock;
85  break;
86  case KEY_SCROLL_LOCK:
87  kbdLockingKeys.kbdLeds.bmScrollLock = ~kbdLockingKeys.kbdLeds.bmScrollLock;
88  break;
89  }
90 
91  if (old_keys != kbdLockingKeys.bLeds && hid)
92  return (hid->SetReport(0, 0/*hid->GetIface()*/, 2, 0, 1, &kbdLockingKeys.bLeds));
93 
94  return 0;
95 }
96 
97 const uint8_t KeyboardReportParser::numKeys[] PROGMEM = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')'};
98 const uint8_t KeyboardReportParser::symKeysUp[] PROGMEM = {'_', '+', '{', '}', '|', '~', ':', '"', '~', '<', '>', '?'};
99 const uint8_t KeyboardReportParser::symKeysLo[] PROGMEM = {'-', '=', '[', ']', '\\', ' ', ';', '\'', '`', ',', '.', '/'};
100 const uint8_t KeyboardReportParser::padKeys[] PROGMEM = {'/', '*', '-', '+', 0x13};
101 
102 uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) {
103  uint8_t shift = (mod & 0x22);
104 
105  // [a-z]
106  if (key > 0x03 && key < 0x1e) {
107  // Upper case letters
108  if ((kbdLockingKeys.kbdLeds.bmCapsLock == 0 && (mod & 2)) ||
109  (kbdLockingKeys.kbdLeds.bmCapsLock == 1 && (mod & 2) == 0))
110  return (key - 4 + 'A');
111 
112  // Lower case letters
113  else
114  return (key - 4 + 'a');
115  }// Numbers
116  else if (key > 0x1d && key < 0x27) {
117  if (shift)
118  return ((uint8_t)pgm_read_byte(&numKeys[key - 0x1e]));
119  else
120  return (key - 0x1e + '1');
121  }// Keypad Numbers
122  else if (key > 0x58 && key < 0x62) {
123  if (kbdLockingKeys.kbdLeds.bmNumLock == 1)
124  return (key - 0x59 + '1');
125  } else if (key > 0x2c && key < 0x39)
126  return ((shift) ? (uint8_t)pgm_read_byte(&symKeysUp[key - 0x2d]) : (uint8_t)pgm_read_byte(&symKeysLo[key - 0x2d]));
127  else if (key > 0x53 && key < 0x59)
128  return (uint8_t)pgm_read_byte(&padKeys[key - 0x54]);
129  else {
130  switch (key) {
131  case KEY_SPACE: return (0x20);
132  case KEY_ENTER: return (0x13);
133  case KEY_ZERO: return ((shift) ? ')': '0');
134  case KEY_ZERO2: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '0': 0);
135  case KEY_PERIOD: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '.': 0);
136  }
137  }
138  return ( 0);
139 }