Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

usb_touch.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // Called by the start-of-frame interrupt.
  97. //
  98. void usb_touchscreen_update_callback(void)
  99. {
  100. static uint8_t microframe_count=0;
  101. if (usb_high_speed) {
  102. // if 480 speed, run only every 8th micro-frame
  103. if (++microframe_count < 8) return;
  104. microframe_count = 0;
  105. }
  106. // do nothing if previous packet not yet sent
  107. uint32_t status = usb_transfer_status(&tx_transfer);
  108. if ((status & 0x80)) {
  109. return;
  110. }
  111. if (status & 0x68) {
  112. // TODO: what if status has errors???
  113. }
  114. if (scan_index == 0) {
  115. scan_timestamp = millis() * 10;
  116. scan_count = 0;
  117. }
  118. while (scan_index < MULTITOUCH_FINGERS) {
  119. uint32_t press = pressure[scan_index];
  120. uint32_t id = contactid[scan_index];
  121. if (id) {
  122. if (press == 0) {
  123. // End a finger touch. One final packet sent
  124. // with same Contact Identifier (usage 0x51)
  125. // and Tip Switch (usage 0x42) = zero
  126. id &= 0xFE;
  127. contactid[scan_index] = 0;
  128. }
  129. uint8_t buffer[MULTITOUCH_SIZE]; // MULTITOUCH_SIZE = 8
  130. txbuffer[0] = id;
  131. txbuffer[1] = press;
  132. txbuffer[2] = xpos[scan_index];
  133. txbuffer[3] = xpos[scan_index] >> 8;
  134. txbuffer[4] = ypos[scan_index];
  135. txbuffer[5] = ypos[scan_index] >> 8;
  136. txbuffer[6] = scan_timestamp;
  137. txbuffer[7] = scan_timestamp >> 8;
  138. //delayNanoseconds(30);
  139. usb_prepare_transfer(&tx_transfer, txbuffer, MULTITOUCH_SIZE, 0);
  140. arm_dcache_flush_delete(txbuffer, TX_BUFSIZE);
  141. usb_transmit(MULTITOUCH_ENDPOINT, &tx_transfer);
  142. scan_index++;
  143. return;
  144. }
  145. scan_index++;
  146. }
  147. if (++scan_count >= MULTITOUCH_FINGERS) {
  148. // when done early, wait to restart another scan
  149. scan_index = 0;
  150. }
  151. }
  152. #endif // MULTITOUCH_INTERFACE