|
|
|
|
|
|
|
|
#ifdef MULTITOUCH_INTERFACE // defined by usb_dev.h -> usb_desc.h |
|
|
#ifdef MULTITOUCH_INTERFACE // defined by usb_dev.h -> usb_desc.h |
|
|
#if F_CPU >= 20000000 |
|
|
#if F_CPU >= 20000000 |
|
|
|
|
|
|
|
|
void usb_touchscreen_press(uint8_t finger, uint32_t x, uint32_t y, uint32_t pressure) |
|
|
|
|
|
|
|
|
static uint8_t prev_id=0; |
|
|
|
|
|
static uint8_t pressure[MULTITOUCH_FINGERS]; |
|
|
|
|
|
static uint8_t contactid[MULTITOUCH_FINGERS]; |
|
|
|
|
|
static uint16_t xpos[MULTITOUCH_FINGERS]; |
|
|
|
|
|
static uint16_t ypos[MULTITOUCH_FINGERS]; |
|
|
|
|
|
|
|
|
|
|
|
static uint8_t scan_state=0; |
|
|
|
|
|
static uint8_t scan_remain=0; // number of finger to left to transmit on this scan |
|
|
|
|
|
static uint8_t scan_index=0; // index of next (possible) finger to transmit |
|
|
|
|
|
static uint8_t scan_pressure[MULTITOUCH_FINGERS]; |
|
|
|
|
|
static uint16_t scan_timestamp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void usb_touchscreen_press(uint8_t finger, uint32_t x, uint32_t y, uint32_t press) |
|
|
{ |
|
|
{ |
|
|
// TODO... |
|
|
|
|
|
|
|
|
uint8_t id; |
|
|
|
|
|
|
|
|
|
|
|
if (finger >= MULTITOUCH_FINGERS) return; |
|
|
|
|
|
press >>= 1; |
|
|
|
|
|
if (press > 127) press = 127; |
|
|
|
|
|
if (x > 32767) x = 32767; |
|
|
|
|
|
if (y > 32767) y = 32767; |
|
|
|
|
|
if (pressure[finger] == 0) { |
|
|
|
|
|
id = prev_id + 1; |
|
|
|
|
|
if (id == 0) id = 1; |
|
|
|
|
|
prev_id = id; |
|
|
|
|
|
contactid[finger] = id; |
|
|
|
|
|
} |
|
|
|
|
|
xpos[finger] = x; |
|
|
|
|
|
ypos[finger] = y; |
|
|
|
|
|
pressure[finger] = (press << 1) | 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void usb_touchscreen_release(uint8_t finger) |
|
|
void usb_touchscreen_release(uint8_t finger) |
|
|
{ |
|
|
{ |
|
|
// TODO... |
|
|
|
|
|
|
|
|
if (finger >= MULTITOUCH_FINGERS) return; |
|
|
|
|
|
pressure[finger] = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// touch report |
|
|
|
|
|
// 0: on/off + pressure |
|
|
|
|
|
// 1: contact id |
|
|
|
|
|
// 2: X lsb |
|
|
|
|
|
// 3: X msb |
|
|
|
|
|
// 4: Y lsb |
|
|
|
|
|
// 5: Y msb |
|
|
|
|
|
// 6: scan time lsb |
|
|
|
|
|
// 7: scan time msb |
|
|
|
|
|
// 8: contact count |
|
|
|
|
|
|
|
|
|
|
|
static int usb_touchscreen_transmit(int index, int count) |
|
|
|
|
|
{ |
|
|
|
|
|
usb_packet_t *tx_packet; |
|
|
|
|
|
tx_packet = usb_malloc(); |
|
|
|
|
|
if (tx_packet == NULL) return 0; |
|
|
|
|
|
*(tx_packet->buf + 0) = pressure[index]; |
|
|
|
|
|
*(tx_packet->buf + 1) = contactid[index]; |
|
|
|
|
|
*(tx_packet->buf + 2) = xpos[index]; |
|
|
|
|
|
*(tx_packet->buf + 3) = xpos[index] >> 8; |
|
|
|
|
|
*(tx_packet->buf + 4) = ypos[index]; |
|
|
|
|
|
*(tx_packet->buf + 5) = ypos[index] >> 8; |
|
|
|
|
|
*(tx_packet->buf + 6) = scan_timestamp; |
|
|
|
|
|
*(tx_packet->buf + 7) = scan_timestamp >> 8; |
|
|
|
|
|
*(tx_packet->buf + 8) = count; |
|
|
|
|
|
tx_packet->len = 9; |
|
|
|
|
|
usb_tx(MULTITOUCH_ENDPOINT, tx_packet); |
|
|
|
|
|
delayMicroseconds(20); |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Called by the start-of-frame interrupt. |
|
|
// Called by the start-of-frame interrupt. |
|
|
// |
|
|
// |
|
|
void usb_touchscreen_update_callback(void) |
|
|
void usb_touchscreen_update_callback(void) |
|
|
{ |
|
|
{ |
|
|
// TODO... |
|
|
|
|
|
|
|
|
int i, r, count=0; |
|
|
|
|
|
|
|
|
|
|
|
digitalWriteFast(13, HIGH); |
|
|
|
|
|
if (scan_state == 0) { |
|
|
|
|
|
if (usb_tx_packet_count(MULTITOUCH_ENDPOINT) > 1) { |
|
|
|
|
|
// wait to begin another scan if if more than |
|
|
|
|
|
// one prior packet remains to transmit |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
// copy the pressure data - whether each finger is |
|
|
|
|
|
// in contact at the moment of this "scan" |
|
|
|
|
|
memcpy(scan_pressure, pressure, MULTITOUCH_FINGERS); |
|
|
|
|
|
for (i=0; i < MULTITOUCH_FINGERS; i++) { |
|
|
|
|
|
if (scan_pressure[i] > 0) count++; |
|
|
|
|
|
} |
|
|
|
|
|
if (count > 0) { |
|
|
|
|
|
scan_remain = count; |
|
|
|
|
|
scan_index = 0; |
|
|
|
|
|
scan_timestamp = millis() * 10; |
|
|
|
|
|
} else { |
|
|
|
|
|
scan_remain = 0; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (scan_remain > 0) { |
|
|
|
|
|
for (i = scan_index; i < MULTITOUCH_FINGERS; i++) { |
|
|
|
|
|
if (scan_pressure[i] > 0) break; |
|
|
|
|
|
} |
|
|
|
|
|
r = usb_touchscreen_transmit(i, (scan_state == 0) ? scan_remain : 0); |
|
|
|
|
|
if (!r) return; |
|
|
|
|
|
scan_index = i + 1; |
|
|
|
|
|
scan_remain--; |
|
|
|
|
|
} |
|
|
|
|
|
if (++scan_state >= MULTITOUCH_ENDPOINT) { |
|
|
|
|
|
scan_state = 0; |
|
|
|
|
|
} |
|
|
|
|
|
delayMicroseconds(10); |
|
|
|
|
|
digitalWriteFast(13, LOW); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // F_CPU |
|
|
#endif // F_CPU |
|
|
#endif // MULTITOUCH_INTERFACE |
|
|
#endif // MULTITOUCH_INTERFACE |