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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // 8: contact count
  82. // Called by the start-of-frame interrupt.
  83. //
  84. void usb_touchscreen_update_callback(void)
  85. {
  86. uint8_t contact_count = 0;
  87. if (scan_index == 0) {
  88. if (usb_tx_packet_count(MULTITOUCH_ENDPOINT) > 1) {
  89. // wait to begin another scan if if more than
  90. // one prior packet remains to transmit
  91. return;
  92. }
  93. scan_timestamp = millis() * 10;
  94. scan_count = 0;
  95. // Get the contact count (usage 0x54)
  96. // Only set this value on first contact
  97. // Subsequent concurrent contacts should report 0
  98. for (uint8_t i = 0; i < MULTITOUCH_FINGERS; i++)
  99. {
  100. if (contactid[i]) contact_count++;
  101. }
  102. }
  103. while (scan_index < MULTITOUCH_FINGERS) {
  104. uint32_t press = pressure[scan_index];
  105. uint32_t id = contactid[scan_index];
  106. if (id) {
  107. usb_packet_t *tx_packet;
  108. tx_packet = usb_malloc();
  109. if (tx_packet == NULL) return;
  110. if (press == 0) {
  111. // End a finger touch. One final packet sent
  112. // with same Contact Identifier (usage 0x51)
  113. // and Tip Switch (usage 0x42) = zero
  114. id &= 0xFE;
  115. contactid[scan_index] = 0;
  116. }
  117. *(tx_packet->buf + 0) = id;
  118. *(tx_packet->buf + 1) = press;
  119. *(tx_packet->buf + 2) = xpos[scan_index];
  120. *(tx_packet->buf + 3) = xpos[scan_index] >> 8;
  121. *(tx_packet->buf + 4) = ypos[scan_index];
  122. *(tx_packet->buf + 5) = ypos[scan_index] >> 8;
  123. *(tx_packet->buf + 6) = scan_timestamp;
  124. *(tx_packet->buf + 7) = scan_timestamp >> 8;
  125. *(tx_packet->buf + 8) = contact_count;
  126. tx_packet->len = 9;
  127. usb_tx(MULTITOUCH_ENDPOINT, tx_packet);
  128. scan_index++;
  129. return;
  130. }
  131. scan_index++;
  132. }
  133. if (++scan_count >= MULTITOUCH_FINGERS) {
  134. // when done early, wait to restart another scan
  135. scan_index = 0;
  136. }
  137. }
  138. #endif // F_CPU
  139. #endif // MULTITOUCH_INTERFACE