From 59421d4b3a49453b0c3d80dccdbc3bbe1f2a8284 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 7 Jun 2017 17:12:07 +0200 Subject: [PATCH] trezorhal: fix bug in touch ignoring the first TOUCH_START event --- micropython/trezorhal/touch.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/micropython/trezorhal/touch.c b/micropython/trezorhal/touch.c index 8081ca458..e40a2ef5c 100644 --- a/micropython/trezorhal/touch.c +++ b/micropython/trezorhal/touch.c @@ -44,26 +44,32 @@ int touch_init(void) { return 0; } +#define TOUCH_ADDRESS 56 +#define TOUCH_PACKET_SIZE 16 + uint32_t touch_read(void) { - static uint8_t data[16], old_data[16]; - if (HAL_OK != HAL_I2C_Master_Receive(&i2c_handle, 56 << 1, data, 16, 1)) { + static uint8_t data[TOUCH_PACKET_SIZE], old_data[TOUCH_PACKET_SIZE]; + if (HAL_OK != HAL_I2C_Master_Receive(&i2c_handle, TOUCH_ADDRESS << 1, data, TOUCH_PACKET_SIZE, 1)) { return 0; // read failure } - if (0 == memcmp(data, old_data, 16)) { + if (0 == memcmp(data, old_data, TOUCH_PACKET_SIZE)) { return 0; // no new event } - uint32_t r = 0; - if (old_data[2] == 0 && data[2] == 1) { - r = TOUCH_START | (data[4] << 8) | data[6]; // touch start - } else - if (old_data[2] == 1 && data[2] == 1) { - r = TOUCH_MOVE | (data[4] << 8) | data[6]; // touch move + memcpy(old_data, data, TOUCH_PACKET_SIZE); + + if (data[0] == 0xff && data[1] == 0x00) { + if (data[2] == 0x01 && data[3] == 0x00) { + return TOUCH_START | (data[4] << 8) | data[6]; // touch start + } else + if (data[2] == 0x01 && data[3] == 0x80) { + return TOUCH_MOVE | (data[4] << 8) | data[6]; // touch move + } else + if (data[2] == 0x00 && data[3] == 0x40) { + return TOUCH_END | (data[4] << 8) | data[6]; // touch end + } } - if (old_data[2] == 1 && data[2] == 0) { - r = TOUCH_END | (data[4] << 8) | data[6]; // touch end - } - memcpy(old_data, data, 16); - return r; + + return 0; } void I2C1_EV_IRQHandler(void) {