Teensy 4.1 core updated for C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

usb_touch.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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 yield()
  33. #include "HardwareSerial.h"
  34. #include <string.h> // for memcpy()
  35. #ifdef MULTITOUCH_INTERFACE // defined by usb_dev.h -> usb_desc.h
  36. #if F_CPU >= 20000000
  37. static uint8_t prev_id=0;
  38. static uint8_t pressure[MULTITOUCH_FINGERS];
  39. static uint8_t contactid[MULTITOUCH_FINGERS];
  40. static uint16_t xpos[MULTITOUCH_FINGERS];
  41. static uint16_t ypos[MULTITOUCH_FINGERS];
  42. static uint8_t scan_state=0;
  43. static uint8_t scan_remain=0; // number of finger to left to transmit on this scan
  44. static uint8_t scan_index=0; // index of next (possible) finger to transmit
  45. static uint8_t scan_pressure[MULTITOUCH_FINGERS];
  46. static uint16_t scan_timestamp;
  47. void usb_touchscreen_press(uint8_t finger, uint32_t x, uint32_t y, uint32_t press)
  48. {
  49. uint8_t id;
  50. if (finger >= MULTITOUCH_FINGERS) return;
  51. press >>= 1;
  52. if (press > 127) press = 127;
  53. if (x > 32767) x = 32767;
  54. if (y > 32767) y = 32767;
  55. if (pressure[finger] == 0) {
  56. id = prev_id + 1;
  57. if (id == 0) id = 1;
  58. prev_id = id;
  59. contactid[finger] = id;
  60. }
  61. xpos[finger] = x;
  62. ypos[finger] = y;
  63. pressure[finger] = (press << 1) | 1;
  64. }
  65. void usb_touchscreen_release(uint8_t finger)
  66. {
  67. if (finger >= MULTITOUCH_FINGERS) return;
  68. pressure[finger] = 0;
  69. }
  70. // touch report
  71. // 0: on/off + pressure
  72. // 1: contact id
  73. // 2: X lsb
  74. // 3: X msb
  75. // 4: Y lsb
  76. // 5: Y msb
  77. // 6: scan time lsb
  78. // 7: scan time msb
  79. // 8: contact count
  80. static int usb_touchscreen_transmit(int index, int count)
  81. {
  82. usb_packet_t *tx_packet;
  83. tx_packet = usb_malloc();
  84. if (tx_packet == NULL) return 0;
  85. *(tx_packet->buf + 0) = pressure[index];
  86. *(tx_packet->buf + 1) = contactid[index];
  87. *(tx_packet->buf + 2) = xpos[index];
  88. *(tx_packet->buf + 3) = xpos[index] >> 8;
  89. *(tx_packet->buf + 4) = ypos[index];
  90. *(tx_packet->buf + 5) = ypos[index] >> 8;
  91. *(tx_packet->buf + 6) = scan_timestamp;
  92. *(tx_packet->buf + 7) = scan_timestamp >> 8;
  93. *(tx_packet->buf + 8) = count;
  94. tx_packet->len = 9;
  95. usb_tx(MULTITOUCH_ENDPOINT, tx_packet);
  96. return 1;
  97. }
  98. // Called by the start-of-frame interrupt.
  99. //
  100. void usb_touchscreen_update_callback(void)
  101. {
  102. int i, r, count=0;
  103. if (scan_state == 0) {
  104. if (usb_tx_packet_count(MULTITOUCH_ENDPOINT) > 1) {
  105. // wait to begin another scan if if more than
  106. // one prior packet remains to transmit
  107. return;
  108. }
  109. // copy the pressure data - whether each finger is
  110. // in contact at the moment of this "scan"
  111. memcpy(scan_pressure, pressure, MULTITOUCH_FINGERS);
  112. for (i=0; i < MULTITOUCH_FINGERS; i++) {
  113. if (scan_pressure[i] > 0) count++;
  114. }
  115. if (count > 0) {
  116. scan_remain = count;
  117. scan_index = 0;
  118. scan_timestamp = millis() * 10;
  119. } else {
  120. scan_remain = 0;
  121. }
  122. }
  123. if (scan_remain > 0) {
  124. for (i = scan_index; i < MULTITOUCH_FINGERS; i++) {
  125. if (scan_pressure[i] > 0) break;
  126. }
  127. r = usb_touchscreen_transmit(i, (scan_state == 0) ? scan_remain : 0);
  128. if (!r) return;
  129. scan_index = i + 1;
  130. scan_remain--;
  131. }
  132. if (++scan_state >= MULTITOUCH_ENDPOINT) {
  133. scan_state = 0;
  134. }
  135. }
  136. #endif // F_CPU
  137. #endif // MULTITOUCH_INTERFACE