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.

119 lines
3.8KB

  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. if ((topusage != TOPUSAGE_SYS_CONTROL) && (topusage != TOPUSAGE_CONSUMER_CONTROL)) return false;
  31. // only claim from one physical device
  32. //Serial.println("KeyboardHIDExtrasController claim collection");
  33. if (mydevice != NULL && dev != mydevice) return false;
  34. mydevice = dev;
  35. collections_claimed_++;
  36. return true;
  37. }
  38. void KeyboardHIDExtrasController::disconnect_collection(Device_t *dev)
  39. {
  40. if (--collections_claimed_ == 0) {
  41. mydevice = NULL;
  42. }
  43. }
  44. void KeyboardHIDExtrasController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  45. {
  46. //Serial.printf("KPC:hid_input_begin TUSE: %x TYPE: %x Range:%x %x\n", topusage, type, lgmin, lgmax);
  47. topusage_ = topusage; // remember which report we are processing.
  48. hid_input_begin_ = true;
  49. hid_input_data_ = false;
  50. }
  51. void KeyboardHIDExtrasController::hid_input_data(uint32_t usage, int32_t value)
  52. {
  53. //Serial.printf("KeyboardHIDExtrasController: usage=%X, value=%d\n", usage, value);
  54. // See if the value is in our keys_down list
  55. usage &= 0xffff; // only keep the actual key
  56. if (usage == 0) return; // lets not process 0, if only 0 happens, we will handle it on the end to remove existing pressed items.
  57. // Remember if we have received any logical key up events. Some keyboard appear to send them
  58. // others do no...
  59. hid_input_data_ = true;
  60. uint8_t key_index;
  61. for (key_index = 0; key_index < count_keys_down_; key_index++) {
  62. if (keys_down[key_index] == usage) {
  63. if (value) return; // still down
  64. if (keyReleasedFunction) {
  65. keyReleasedFunction(topusage_, usage);
  66. }
  67. // Remove from list
  68. count_keys_down_--;
  69. for (;key_index < count_keys_down_; key_index++) {
  70. keys_down[key_index] = keys_down[key_index+1];
  71. }
  72. return;
  73. }
  74. }
  75. // Was not in list
  76. if (!value) return; // still 0
  77. if (keyPressedFunction) {
  78. keyPressedFunction(topusage_, usage);
  79. }
  80. if (count_keys_down_ < MAX_KEYS_DOWN) {
  81. keys_down[count_keys_down_++] = usage;
  82. }
  83. }
  84. void KeyboardHIDExtrasController::hid_input_end()
  85. {
  86. //Serial.println("KPC:hid_input_end");
  87. if (hid_input_begin_) {
  88. // See if we received any data from parser if not, assume all keys released...
  89. if (!hid_input_data_ ) {
  90. if (keyPressedFunction) {
  91. while (count_keys_down_) {
  92. count_keys_down_--;
  93. keyReleasedFunction(topusage_, keys_down[count_keys_down_]);
  94. }
  95. }
  96. count_keys_down_ = 0;
  97. }
  98. event_ = true;
  99. hid_input_begin_ = false;
  100. }
  101. }