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.

180 lines
5.4KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2019 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 <string.h> // for memcpy()
  34. #include "avr/pgmspace.h" // for PROGMEM, DMAMEM, FASTRUN
  35. #include "debug/printf.h"
  36. #include "core_pins.h"
  37. #ifdef MULTITOUCH_INTERFACE // defined by usb_dev.h -> usb_desc.h
  38. static uint8_t prev_id=0;
  39. static uint8_t scan_index=0;
  40. static uint8_t scan_count=0;
  41. static uint8_t contactid[MULTITOUCH_FINGERS];
  42. static uint8_t pressure[MULTITOUCH_FINGERS];
  43. static uint16_t xpos[MULTITOUCH_FINGERS];
  44. static uint16_t ypos[MULTITOUCH_FINGERS];
  45. static uint16_t scan_timestamp;
  46. #define TX_BUFSIZE 32
  47. static transfer_t tx_transfer __attribute__ ((used, aligned(32)));
  48. DMAMEM static uint8_t txbuffer[TX_BUFSIZE] __attribute__ ((aligned(32)));
  49. extern volatile uint8_t usb_high_speed;
  50. #if MULTITOUCH_SIZE > TX_BUFSIZE
  51. #error "Internal error, transmit buffer size is too small for touchscreen endpoint"
  52. #endif
  53. void usb_touchscreen_configure(void)
  54. {
  55. memset(&tx_transfer, 0, sizeof(tx_transfer));
  56. usb_config_tx(MULTITOUCH_ENDPOINT, MULTITOUCH_SIZE, 0, NULL);
  57. usb_start_sof_interrupts(MULTITOUCH_INTERFACE);
  58. }
  59. void usb_touchscreen_press(uint8_t finger, uint32_t x, uint32_t y, uint32_t press)
  60. {
  61. if (finger >= MULTITOUCH_FINGERS) return;
  62. if (press > 255) press = 255;
  63. if (x > 32767) x = 32767;
  64. if (y > 32767) y = 32767;
  65. __disable_irq();
  66. if (pressure[finger] == 0 && press > 0) {
  67. // Begin a new finger touch, assign a
  68. // new Contact Identifier (usage 0x51)
  69. uint8_t id = prev_id + 1;
  70. if (id == 0 || id > 127) id = 1;
  71. prev_id = id;
  72. contactid[finger] = (id << 1) | 1;
  73. }
  74. xpos[finger] = x;
  75. ypos[finger] = y;
  76. pressure[finger] = press;
  77. __enable_irq();
  78. }
  79. // User should allow at least 12 ms after release before starting a
  80. // new touch with press, in case the host's driver does not properly
  81. // parse a change in contact identifier to mean a new touch.
  82. void usb_touchscreen_release(uint8_t finger)
  83. {
  84. if (finger >= MULTITOUCH_FINGERS) return;
  85. pressure[finger] = 0;
  86. }
  87. // touch report
  88. // 0: contact id + on/off
  89. // 1: pressure
  90. // 2: X lsb
  91. // 3: X msb
  92. // 4: Y lsb
  93. // 5: Y msb
  94. // 6: scan time lsb
  95. // 7: scan time msb
  96. // 8: contact count
  97. // Called by the start-of-frame interrupt.
  98. //
  99. void usb_touchscreen_update_callback(void)
  100. {
  101. static uint8_t microframe_count=0;
  102. uint8_t contact_count = 0;
  103. if (usb_high_speed) {
  104. // if 480 speed, run only every 8th micro-frame
  105. if (++microframe_count < 8) return;
  106. microframe_count = 0;
  107. }
  108. // do nothing if previous packet not yet sent
  109. uint32_t status = usb_transfer_status(&tx_transfer);
  110. if ((status & 0x80)) {
  111. return;
  112. }
  113. if (status & 0x68) {
  114. // TODO: what if status has errors???
  115. }
  116. if (scan_index == 0) {
  117. scan_timestamp = millis() * 10;
  118. scan_count = 0;
  119. // Get the contact count (usage 0x54)
  120. // Only set this value on first contact
  121. // Subsequent concurrent contacts should report 0
  122. for (uint8_t i = 0; i < MULTITOUCH_FINGERS; i++) {
  123. if (contactid[i]) contact_count++;
  124. }
  125. }
  126. while (scan_index < MULTITOUCH_FINGERS) {
  127. uint32_t press = pressure[scan_index];
  128. uint32_t id = contactid[scan_index];
  129. if (id) {
  130. if (press == 0) {
  131. // End a finger touch. One final packet sent
  132. // with same Contact Identifier (usage 0x51)
  133. // and Tip Switch (usage 0x42) = zero
  134. id &= 0xFE;
  135. contactid[scan_index] = 0;
  136. }
  137. //uint8_t buffer[MULTITOUCH_SIZE]; // MULTITOUCH_SIZE = 8
  138. txbuffer[0] = id;
  139. txbuffer[1] = press;
  140. txbuffer[2] = xpos[scan_index];
  141. txbuffer[3] = xpos[scan_index] >> 8;
  142. txbuffer[4] = ypos[scan_index];
  143. txbuffer[5] = ypos[scan_index] >> 8;
  144. txbuffer[6] = scan_timestamp;
  145. txbuffer[7] = scan_timestamp >> 8;
  146. txbuffer[8] = contact_count;
  147. //delayNanoseconds(30);
  148. usb_prepare_transfer(&tx_transfer, txbuffer, MULTITOUCH_SIZE, 0);
  149. arm_dcache_flush_delete(txbuffer, TX_BUFSIZE);
  150. usb_transmit(MULTITOUCH_ENDPOINT, &tx_transfer);
  151. scan_index++;
  152. return;
  153. }
  154. scan_index++;
  155. }
  156. if (++scan_count >= MULTITOUCH_FINGERS) {
  157. // when done early, wait to restart another scan
  158. scan_index = 0;
  159. }
  160. }
  161. #endif // MULTITOUCH_INTERFACE