Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_NUM 20
  47. #define TX_BUFSIZE 32
  48. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  49. DMAMEM static uint8_t txbuffer[TX_NUM * TX_BUFSIZE] __attribute__ ((aligned(32)));
  50. static uint8_t tx_head=0;
  51. #if MULTITOUCH_SIZE > TX_BUFSIZE
  52. #error "Internal error, transmit buffer size is too small for touchscreen endpoint"
  53. #endif
  54. void usb_touchscreen_configure(void)
  55. {
  56. memset(tx_transfer, 0, sizeof(tx_transfer));
  57. tx_head = 0;
  58. usb_config_tx(MULTITOUCH_ENDPOINT, MULTITOUCH_SIZE, 0, NULL);
  59. // TODO: need to cause usb_touchscreen_update_callback to be run on SOF
  60. }
  61. void usb_touchscreen_press(uint8_t finger, uint32_t x, uint32_t y, uint32_t press)
  62. {
  63. if (finger >= MULTITOUCH_FINGERS) return;
  64. if (press > 255) press = 255;
  65. if (x > 32767) x = 32767;
  66. if (y > 32767) y = 32767;
  67. __disable_irq();
  68. if (pressure[finger] == 0 && press > 0) {
  69. // Begin a new finger touch, assign a
  70. // new Contact Identifier (usage 0x51)
  71. uint8_t id = prev_id + 1;
  72. if (id == 0 || id > 127) id = 1;
  73. prev_id = id;
  74. contactid[finger] = (id << 1) | 1;
  75. }
  76. xpos[finger] = x;
  77. ypos[finger] = y;
  78. pressure[finger] = press;
  79. __enable_irq();
  80. }
  81. // User should allow at least 12 ms after release before starting a
  82. // new touch with press, in case the host's driver does not properly
  83. // parse a change in contact identifier to mean a new touch.
  84. void usb_touchscreen_release(uint8_t finger)
  85. {
  86. if (finger >= MULTITOUCH_FINGERS) return;
  87. pressure[finger] = 0;
  88. }
  89. static int transmit(const uint8_t *data)
  90. {
  91. if (!usb_configuration) return 0;
  92. uint32_t head = tx_head;
  93. transfer_t *xfer = tx_transfer + head;
  94. uint32_t status = usb_transfer_status(xfer);
  95. if ((status & 0x80)) return 0;
  96. if (status & 0x68) {
  97. // TODO: what if status has errors???
  98. }
  99. delayNanoseconds(30); // TODO: is this needed? (probably not)
  100. uint8_t *buffer = txbuffer + head * TX_BUFSIZE;
  101. memcpy(buffer, data, MULTITOUCH_SIZE);
  102. usb_prepare_transfer(xfer, buffer, MULTITOUCH_SIZE, 0);
  103. arm_dcache_flush_delete(buffer, TX_BUFSIZE);
  104. usb_transmit(MULTITOUCH_ENDPOINT, xfer);
  105. if (++head >= TX_NUM) head = 0;
  106. tx_head = head;
  107. return 1;
  108. }
  109. // touch report
  110. // 0: contact id + on/off
  111. // 1: pressure
  112. // 2: X lsb
  113. // 3: X msb
  114. // 4: Y lsb
  115. // 5: Y msb
  116. // 6: scan time lsb
  117. // 7: scan time msb
  118. // Called by the start-of-frame interrupt.
  119. //
  120. void usb_touchscreen_update_callback(void)
  121. {
  122. // TODO: if 480 speed, run only every 8th time
  123. if (scan_index == 0) {
  124. if (usb_tx_packet_count(MULTITOUCH_ENDPOINT) > 1) {
  125. // wait to begin another scan if if more than
  126. // one prior packet remains to transmit
  127. return;
  128. }
  129. scan_timestamp = millis() * 10;
  130. scan_count = 0;
  131. }
  132. while (scan_index < MULTITOUCH_FINGERS) {
  133. uint32_t press = pressure[scan_index];
  134. uint32_t id = contactid[scan_index];
  135. if (id) {
  136. if (press == 0) {
  137. // End a finger touch. One final packet sent
  138. // with same Contact Identifier (usage 0x51)
  139. // and Tip Switch (usage 0x42) = zero
  140. id &= 0xFE;
  141. contactid[scan_index] = 0;
  142. }
  143. uint8_t buffer[MULTITOUCH_SIZE]; // MULTITOUCH_SIZE = 8
  144. buffer[0] = id;
  145. buffer[1] = press;
  146. buffer[2] = xpos[scan_index];
  147. buffer[3] = xpos[scan_index] >> 8;
  148. buffer[4] = ypos[scan_index];
  149. buffer[5] = ypos[scan_index] >> 8;
  150. buffer[6] = scan_timestamp;
  151. buffer[7] = scan_timestamp >> 8;
  152. if (transmit(buffer)) scan_index++;
  153. return;
  154. }
  155. scan_index++;
  156. }
  157. if (++scan_count >= MULTITOUCH_FINGERS) {
  158. // when done early, wait to restart another scan
  159. scan_index = 0;
  160. }
  161. }
  162. #endif // MULTITOUCH_INTERFACE