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.

joystick.cpp 2.9KB

7 vuotta sitten
7 vuotta sitten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* USB EHCI 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. bool JoystickController::claim_collection(Device_t *dev, uint32_t topusage)
  26. {
  27. // only claim Desktop/Joystick and Desktop/Gamepad
  28. if (topusage != 0x10004 && topusage != 0x10005) return false;
  29. // only claim from one physical device
  30. if (mydevice != NULL && dev != mydevice) return false;
  31. mydevice = dev;
  32. collections_claimed++;
  33. anychange = true; // always report values on first read
  34. return true;
  35. }
  36. void JoystickController::disconnect_collection(Device_t *dev)
  37. {
  38. if (--collections_claimed == 0) {
  39. mydevice = NULL;
  40. }
  41. }
  42. void JoystickController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  43. {
  44. // TODO: set up translation from logical min/max to consistent 16 bit scale
  45. }
  46. void JoystickController::hid_input_data(uint32_t usage, int32_t value)
  47. {
  48. //Serial.printf("Joystick: usage=%X, value=%d\n", usage, value);
  49. uint32_t usage_page = usage >> 16;
  50. usage &= 0xFFFF;
  51. if (usage_page == 9 && usage >= 1 && usage <= 32) {
  52. uint32_t bit = 1 << (usage -1);
  53. if (value == 0) {
  54. if (buttons & bit) {
  55. buttons &= ~bit;
  56. anychange = true;
  57. }
  58. } else {
  59. if (!(buttons & bit)) {
  60. buttons |= bit;
  61. anychange = true;
  62. }
  63. }
  64. } else if (usage_page == 1 && usage >= 0x30 && usage <= 0x39) {
  65. // TODO: need scaling of value to consistent API, 16 bit signed?
  66. // TODO: many joysticks repeat slider usage. Detect & map to axes?
  67. uint32_t i = usage - 0x30;
  68. if (axis[i] != value) {
  69. axis[i] = value;
  70. anychange = true;
  71. }
  72. }
  73. // TODO: hat switch?
  74. }
  75. void JoystickController::hid_input_end()
  76. {
  77. if (anychange) {
  78. joystickEvent = true;
  79. }
  80. }
  81. void JoystickController::joystickDataClear() {
  82. joystickEvent = false;
  83. anychange = false;
  84. }