|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
-
-
- #include "usb_dev.h"
- #include "usb_touch.h"
- #include "core_pins.h"
- #include <string.h> // for memcpy()
- #include "avr/pgmspace.h"
- #include "debug/printf.h"
- #include "core_pins.h"
-
-
- #ifdef MULTITOUCH_INTERFACE
-
- static uint8_t prev_id=0;
- static uint8_t scan_index=0;
- static uint8_t scan_count=0;
- static uint8_t contactid[MULTITOUCH_FINGERS];
- static uint8_t pressure[MULTITOUCH_FINGERS];
- static uint16_t xpos[MULTITOUCH_FINGERS];
- static uint16_t ypos[MULTITOUCH_FINGERS];
- static uint16_t scan_timestamp;
-
- #define TX_NUM 20
- #define TX_BUFSIZE 32
- static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
- DMAMEM static uint8_t txbuffer[TX_NUM * TX_BUFSIZE] __attribute__ ((aligned(32)));
- static uint8_t tx_head=0;
- #if MULTITOUCH_SIZE > TX_BUFSIZE
- #error "Internal error, transmit buffer size is too small for touchscreen endpoint"
- #endif
-
-
- void usb_touchscreen_configure(void)
- {
- memset(tx_transfer, 0, sizeof(tx_transfer));
- tx_head = 0;
- usb_config_tx(MULTITOUCH_ENDPOINT, MULTITOUCH_SIZE, 0, NULL);
-
- }
-
-
- void usb_touchscreen_press(uint8_t finger, uint32_t x, uint32_t y, uint32_t press)
- {
- if (finger >= MULTITOUCH_FINGERS) return;
- if (press > 255) press = 255;
- if (x > 32767) x = 32767;
- if (y > 32767) y = 32767;
- __disable_irq();
- if (pressure[finger] == 0 && press > 0) {
-
-
- uint8_t id = prev_id + 1;
- if (id == 0 || id > 127) id = 1;
- prev_id = id;
- contactid[finger] = (id << 1) | 1;
- }
- xpos[finger] = x;
- ypos[finger] = y;
- pressure[finger] = press;
- __enable_irq();
- }
-
-
-
-
-
- void usb_touchscreen_release(uint8_t finger)
- {
- if (finger >= MULTITOUCH_FINGERS) return;
- pressure[finger] = 0;
- }
-
-
- static int transmit(const uint8_t *data)
- {
- if (!usb_configuration) return 0;
- uint32_t head = tx_head;
- transfer_t *xfer = tx_transfer + head;
- uint32_t status = usb_transfer_status(xfer);
- if ((status & 0x80)) return 0;
- if (status & 0x68) {
-
- }
- uint8_t *buffer = txbuffer + head * TX_BUFSIZE;
- memcpy(buffer, data, MULTITOUCH_SIZE);
- usb_prepare_transfer(xfer, buffer, MULTITOUCH_SIZE, 0);
- arm_dcache_flush_delete(buffer, TX_BUFSIZE);
- usb_transmit(MULTITOUCH_ENDPOINT, xfer);
- if (++head >= TX_NUM) head = 0;
- tx_head = head;
- return 1;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void usb_touchscreen_update_callback(void)
- {
-
-
- if (scan_index == 0) {
- if (usb_tx_packet_count(MULTITOUCH_ENDPOINT) > 1) {
-
-
- return;
- }
- scan_timestamp = millis() * 10;
- scan_count = 0;
- }
- while (scan_index < MULTITOUCH_FINGERS) {
- uint32_t press = pressure[scan_index];
- uint32_t id = contactid[scan_index];
- if (id) {
- if (press == 0) {
-
-
-
- id &= 0xFE;
- contactid[scan_index] = 0;
- }
- uint8_t buffer[MULTITOUCH_SIZE];
- buffer[0] = id;
- buffer[1] = press;
- buffer[2] = xpos[scan_index];
- buffer[3] = xpos[scan_index] >> 8;
- buffer[4] = ypos[scan_index];
- buffer[5] = ypos[scan_index] >> 8;
- buffer[6] = scan_timestamp;
- buffer[7] = scan_timestamp >> 8;
- if (transmit(buffer)) scan_index++;
- return;
- }
- scan_index++;
- }
- if (++scan_count >= MULTITOUCH_FINGERS) {
-
- scan_index = 0;
- }
- }
-
-
- #endif
|