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  // provide event for changed control key state
56  if (prevState.bInfo[0x00] != buf[0x00]) {
57  OnControlKeysChanged(prevState.bInfo[0x00], buf[0x00]);
58  }
59 
60  for (uint8_t i = 2; i < 8; i++) {
61  bool down = false;
62  bool up = false;
63 
64  for (uint8_t j = 2; j < 8; j++) {
65  if (buf[i] == prevState.bInfo[j] && buf[i] != 1)
66  down = true;
67  if (buf[j] == prevState.bInfo[i] && prevState.bInfo[i] != 1)
68  up = true;
69  }
70  if (!down) {
71  HandleLockingKeys(hid, buf[i]);
72  OnKeyDown(*buf, buf[i]);
73  }
74  if (!up)
75  OnKeyUp(prevState.bInfo[0], prevState.bInfo[i]);
76  }
77  for (uint8_t i = 0; i < 8; i++)
78  prevState.bInfo[i] = buf[i];
79 };
80 
81 uint8_t KeyboardReportParser::HandleLockingKeys(HID *hid, uint8_t key) {
82  uint8_t old_keys = kbdLockingKeys.bLeds;
83 
84  switch (key) {
85  case KEY_NUM_LOCK:
86  kbdLockingKeys.kbdLeds.bmNumLock = ~kbdLockingKeys.kbdLeds.bmNumLock;
87  break;
88  case KEY_CAPS_LOCK:
89  kbdLockingKeys.kbdLeds.bmCapsLock = ~kbdLockingKeys.kbdLeds.bmCapsLock;
90  break;
91  case KEY_SCROLL_LOCK:
92  kbdLockingKeys.kbdLeds.bmScrollLock = ~kbdLockingKeys.kbdLeds.bmScrollLock;
93  break;
94  }
95 
96  if (old_keys != kbdLockingKeys.bLeds && hid)
97  return (hid->SetReport(0, 0/*hid->GetIface()*/, 2, 0, 1, &kbdLockingKeys.bLeds));
98 
99  return 0;
100 }
101 
102 const uint8_t KeyboardReportParser::numKeys[] PROGMEM = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')'};
103 const uint8_t KeyboardReportParser::symKeysUp[] PROGMEM = {'_', '+', '{', '}', '|', '~', ':', '"', '~', '<', '>', '?'};
104 const uint8_t KeyboardReportParser::symKeysLo[] PROGMEM = {'-', '=', '[', ']', '\\', ' ', ';', '\'', '`', ',', '.', '/'};
105 const uint8_t KeyboardReportParser::padKeys[] PROGMEM = {'/', '*', '-', '+', 0x13};
106 
107 uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) {
108  uint8_t shift = (mod & 0x22);
109 
110  // [a-z]
111  if (key > 0x03 && key < 0x1e) {
112  // Upper case letters
113  if ((kbdLockingKeys.kbdLeds.bmCapsLock == 0 && (mod & 2)) ||
114  (kbdLockingKeys.kbdLeds.bmCapsLock == 1 && (mod & 2) == 0))
115  return (key - 4 + 'A');
116 
117  // Lower case letters
118  else
119  return (key - 4 + 'a');
120  }// Numbers
121  else if (key > 0x1d && key < 0x27) {
122  if (shift)
123  return ((uint8_t)pgm_read_byte(&numKeys[key - 0x1e]));
124  else
125  return (key - 0x1e + '1');
126  }// Keypad Numbers
127  else if (key > 0x58 && key < 0x62) {
128  if (kbdLockingKeys.kbdLeds.bmNumLock == 1)
129  return (key - 0x59 + '1');
130  } else if (key > 0x2c && key < 0x39)
131  return ((shift) ? (uint8_t)pgm_read_byte(&symKeysUp[key - 0x2d]) : (uint8_t)pgm_read_byte(&symKeysLo[key - 0x2d]));
132  else if (key > 0x53 && key < 0x59)
133  return (uint8_t)pgm_read_byte(&padKeys[key - 0x54]);
134  else {
135  switch (key) {
136  case KEY_SPACE: return (0x20);
137  case KEY_ENTER: return (0x13);
138  case KEY_ZERO: return ((shift) ? ')': '0');
139  case KEY_ZERO2: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '0': 0);
140  case KEY_PERIOD: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '.': 0);
141  }
142  }
143  return ( 0);
144 }