You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

hidboot.cpp 7.6KB

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