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.

142 line
4.3KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2018 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "usb_dev.h"
  31. #include "usb_touch.h"
  32. #include "core_pins.h" // for millis()
  33. #include "HardwareSerial.h"
  34. #ifdef MULTITOUCH_INTERFACE // defined by usb_dev.h -> usb_desc.h
  35. #if F_CPU >= 20000000
  36. static uint8_t prev_id=0;
  37. static uint8_t scan_index=0;
  38. static uint8_t scan_count=0;
  39. static uint8_t contactid[MULTITOUCH_FINGERS];
  40. static uint8_t pressure[MULTITOUCH_FINGERS];
  41. static uint16_t xpos[MULTITOUCH_FINGERS];
  42. static uint16_t ypos[MULTITOUCH_FINGERS];
  43. static uint16_t scan_timestamp;
  44. void usb_touchscreen_press(uint8_t finger, uint32_t x, uint32_t y, uint32_t press)
  45. {
  46. if (finger >= MULTITOUCH_FINGERS) return;
  47. if (press > 255) press = 255;
  48. if (x > 32767) x = 32767;
  49. if (y > 32767) y = 32767;
  50. __disable_irq();
  51. if (pressure[finger] == 0 && press > 0) {
  52. // Begin a new finger touch, assign a
  53. // new Contact Identifier (usage 0x51)
  54. uint8_t id = prev_id + 1;
  55. if (id == 0 || id > 127) id = 1;
  56. prev_id = id;
  57. contactid[finger] = (id << 1) | 1;
  58. }
  59. xpos[finger] = x;
  60. ypos[finger] = y;
  61. pressure[finger] = press;
  62. __enable_irq();
  63. }
  64. // User should allow at least 12 ms after release before starting a
  65. // new touch with press, in case the host's driver does not properly
  66. // parse a change in contact identifier to mean a new touch.
  67. void usb_touchscreen_release(uint8_t finger)
  68. {
  69. if (finger >= MULTITOUCH_FINGERS) return;
  70. pressure[finger] = 0;
  71. }
  72. // touch report
  73. // 0: contact id + on/off
  74. // 1: pressure
  75. // 2: X lsb
  76. // 3: X msb
  77. // 4: Y lsb
  78. // 5: Y msb
  79. // 6: scan time lsb
  80. // 7: scan time msb
  81. // Called by the start-of-frame interrupt.
  82. //
  83. void usb_touchscreen_update_callback(void)
  84. {
  85. if (scan_index == 0) {
  86. if (usb_tx_packet_count(MULTITOUCH_ENDPOINT) > 1) {
  87. // wait to begin another scan if if more than
  88. // one prior packet remains to transmit
  89. return;
  90. }
  91. scan_timestamp = millis() * 10;
  92. scan_count = 0;
  93. }
  94. while (scan_index < MULTITOUCH_FINGERS) {
  95. uint32_t press = pressure[scan_index];
  96. uint32_t id = contactid[scan_index];
  97. if (id) {
  98. usb_packet_t *tx_packet;
  99. tx_packet = usb_malloc();
  100. if (tx_packet == NULL) return;
  101. if (press == 0) {
  102. // End a finger touch. One final packet sent
  103. // with same Contact Identifier (usage 0x51)
  104. // and Tip Switch (usage 0x42) = zero
  105. id &= 0xFE;
  106. contactid[scan_index] = 0;
  107. }
  108. *(tx_packet->buf + 0) = id;
  109. *(tx_packet->buf + 1) = press;
  110. *(tx_packet->buf + 2) = xpos[scan_index];
  111. *(tx_packet->buf + 3) = xpos[scan_index] >> 8;
  112. *(tx_packet->buf + 4) = ypos[scan_index];
  113. *(tx_packet->buf + 5) = ypos[scan_index] >> 8;
  114. *(tx_packet->buf + 6) = scan_timestamp;
  115. *(tx_packet->buf + 7) = scan_timestamp >> 8;
  116. tx_packet->len = 8;
  117. usb_tx(MULTITOUCH_ENDPOINT, tx_packet);
  118. scan_index++;
  119. return;
  120. }
  121. scan_index++;
  122. }
  123. if (++scan_count >= MULTITOUCH_FINGERS) {
  124. // when done early, wait to restart another scan
  125. scan_index = 0;
  126. }
  127. }
  128. #endif // F_CPU
  129. #endif // MULTITOUCH_INTERFACE