Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

keyboardHIDExtras.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* USB keyboard power Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. #define TOPUSAGE_SYS_CONTROL 0x10080
  26. #define TOPUSAGE_CONSUMER_CONTROL 0x0c0001
  27. bool KeyboardHIDExtrasController::claim_collection(Device_t *dev, uint32_t topusage)
  28. {
  29. // Lets try to claim a few specific Keyboard related collection/reports
  30. //Serial.printf("KBH Claim %x\n", topusage);
  31. if ((topusage != TOPUSAGE_SYS_CONTROL)
  32. && (topusage != TOPUSAGE_CONSUMER_CONTROL)
  33. ) return false;
  34. // only claim from one physical device
  35. //Serial.println("KeyboardHIDExtrasController claim collection");
  36. if (mydevice != NULL && dev != mydevice) return false;
  37. mydevice = dev;
  38. collections_claimed_++;
  39. return true;
  40. }
  41. void KeyboardHIDExtrasController::disconnect_collection(Device_t *dev)
  42. {
  43. if (--collections_claimed_ == 0) {
  44. mydevice = NULL;
  45. }
  46. }
  47. void KeyboardHIDExtrasController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  48. {
  49. //Serial.printf("KPC:hid_input_begin TUSE: %x TYPE: %x Range:%x %x\n", topusage, type, lgmin, lgmax);
  50. topusage_ = topusage; // remember which report we are processing.
  51. hid_input_begin_ = true;
  52. hid_input_data_ = false;
  53. }
  54. void KeyboardHIDExtrasController::hid_input_data(uint32_t usage, int32_t value)
  55. {
  56. // Hack ignore 0xff00 high words as these are user values...
  57. if ((usage & 0xffff0000) == 0xff000000) return;
  58. //Serial.printf("KeyboardHIDExtrasController: topusage= %x usage=%X, value=%d\n", topusage_, usage, value);
  59. // See if the value is in our keys_down list
  60. usage &= 0xffff; // only keep the actual key
  61. if (usage == 0) return; // lets not process 0, if only 0 happens, we will handle it on the end to remove existing pressed items.
  62. // Remember if we have received any logical key up events. Some keyboard appear to send them
  63. // others do no...
  64. hid_input_data_ = true;
  65. uint8_t key_index;
  66. for (key_index = 0; key_index < count_keys_down_; key_index++) {
  67. if (keys_down[key_index] == usage) {
  68. if (value) return; // still down
  69. if (keyReleasedFunction) {
  70. keyReleasedFunction(topusage_, usage);
  71. }
  72. // Remove from list
  73. count_keys_down_--;
  74. for (;key_index < count_keys_down_; key_index++) {
  75. keys_down[key_index] = keys_down[key_index+1];
  76. }
  77. return;
  78. }
  79. }
  80. // Was not in list
  81. if (!value) return; // still 0
  82. if (keyPressedFunction) {
  83. keyPressedFunction(topusage_, usage);
  84. }
  85. if (count_keys_down_ < MAX_KEYS_DOWN) {
  86. keys_down[count_keys_down_++] = usage;
  87. }
  88. }
  89. void KeyboardHIDExtrasController::hid_input_end()
  90. {
  91. //Serial.println("KPC:hid_input_end");
  92. if (hid_input_begin_) {
  93. // See if we received any data from parser if not, assume all keys released...
  94. if (!hid_input_data_ ) {
  95. if (keyPressedFunction) {
  96. while (count_keys_down_) {
  97. count_keys_down_--;
  98. keyReleasedFunction(topusage_, keys_down[count_keys_down_]);
  99. }
  100. }
  101. count_keys_down_ = 0;
  102. }
  103. event_ = true;
  104. hid_input_begin_ = false;
  105. }
  106. }