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