From 8ef2098763bb8b2580633e3488e9d0f311c8aec0 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 24 Jan 2018 13:41:02 +0100 Subject: [PATCH] trezorhal: unix: refactor touch get/pack functionality --- embed/prodtest/main.c | 4 ++-- embed/trezorhal/touch.c | 20 ++++++++++++++++---- embed/trezorhal/touch.h | 16 +++------------- embed/unix/touch.c | 6 +++--- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/embed/prodtest/main.c b/embed/prodtest/main.c index c4841fc6aa..dd614cd2a9 100644 --- a/embed/prodtest/main.c +++ b/embed/prodtest/main.c @@ -194,8 +194,8 @@ static void test_touch(const char *args) uint32_t evt = 0; if (touch_click_timeout(&evt, timeout * 1000)) { - uint32_t x = (evt >> 12) & 0xFFF; - uint32_t y = (evt >> 0) & 0xFFF; + uint16_t x = touch_get_x(evt); + uint16_t y = touch_get_y(evt); vcp_printf("OK %d %d", x, y); } else { vcp_printf("ERROR TIMEOUT"); diff --git a/embed/trezorhal/touch.c b/embed/trezorhal/touch.c index 33c71dcb95..4775d0e50a 100644 --- a/embed/trezorhal/touch.c +++ b/embed/trezorhal/touch.c @@ -13,6 +13,18 @@ #include "secbool.h" #include "touch.h" +#define TOUCH_ADDRESS (0x38U << 1) // the HAL requires the 7-bit address to be shifted by one bit +#define TOUCH_PACKET_SIZE 7U +#define EVENT_PRESS_DOWN 0x00U +#define EVENT_CONTACT 0x80U +#define EVENT_LIFT_UP 0x40U +#define EVENT_NO_EVENT 0xC0U +#define GESTURE_NO_GESTURE 0x00U +#define X_POS_MSB (touch_data[3] & 0x0FU) +#define X_POS_LSB (touch_data[4]) +#define Y_POS_MSB (touch_data[5] & 0x0FU) +#define Y_POS_LSB (touch_data[6]) + static I2C_HandleTypeDef i2c_handle; void touch_init(void) @@ -91,14 +103,14 @@ uint32_t touch_read(void) const uint32_t number_of_touch_points = touch_data[2] & 0x0F; // valid values are 0, 1, 2 (invalid 0xF before first touch) (tested with FT6206) const uint32_t event_flag = touch_data[3] & 0xC0; - if (touch_data[1] == GESTURE_NO_GESTURE) { + uint32_t xy = touch_pack_xy((X_POS_MSB << 8) | X_POS_LSB, (Y_POS_MSB << 8) | Y_POS_LSB); if ((number_of_touch_points == 1) && (event_flag == EVENT_PRESS_DOWN)) { - return TOUCH_START | X_Y_POS; + return TOUCH_START | xy; } else if ((number_of_touch_points == 1) && (event_flag == EVENT_CONTACT)) { - return TOUCH_MOVE | X_Y_POS; + return TOUCH_MOVE | xy; } else if ((number_of_touch_points == 0) && (event_flag == EVENT_LIFT_UP)) { - return TOUCH_END | X_Y_POS; + return TOUCH_END | xy; } } diff --git a/embed/trezorhal/touch.h b/embed/trezorhal/touch.h index 0a778b2cdb..05ef7c0070 100644 --- a/embed/trezorhal/touch.h +++ b/embed/trezorhal/touch.h @@ -14,21 +14,11 @@ #define TOUCH_MOVE (2U << 24) #define TOUCH_END (4U << 24) -#define TOUCH_ADDRESS (0x38U << 1) // the HAL requires the 7-bit address to be shifted by one bit -#define TOUCH_PACKET_SIZE 7U -#define EVENT_PRESS_DOWN 0x00U -#define EVENT_CONTACT 0x80U -#define EVENT_LIFT_UP 0x40U -#define EVENT_NO_EVENT 0xC0U -#define GESTURE_NO_GESTURE 0x00U -#define X_POS_MSB (touch_data[3] & 0xFU) -#define X_POS_LSB (touch_data[4]) -#define Y_POS_MSB (touch_data[5] & 0xFU) -#define Y_POS_LSB (touch_data[6]) -#define X_Y_POS ((X_POS_MSB << 20) | (X_POS_LSB << 12) | (Y_POS_MSB << 8) | (Y_POS_LSB)) - void touch_init(void); uint32_t touch_read(void); uint32_t touch_click(void); +inline uint16_t touch_get_x(uint32_t evt) { return (evt >> 12) & 0xFFF; } +inline uint16_t touch_get_y(uint32_t evt) { return (evt >> 0) & 0xFFF; } +inline uint32_t touch_pack_xy(uint16_t x, uint16_t y) { return ((x & 0xFFF) << 12) | (y & 0xFFF); } #endif diff --git a/embed/unix/touch.c b/embed/unix/touch.c index 4bcdac513b..2f337b4a52 100644 --- a/embed/unix/touch.c +++ b/embed/unix/touch.c @@ -31,16 +31,16 @@ uint32_t touch_read(void) if (x < 0 || y < 0 || x >= DISPLAY_RESX || y >= DISPLAY_RESY) break; switch (event.type) { case SDL_MOUSEBUTTONDOWN: - return TOUCH_START | (x << 12) | y; + return TOUCH_START | touch_pack_xy(x, y); case SDL_MOUSEMOTION: // remove other SDL_MOUSEMOTION events from queue SDL_FlushEvent(SDL_MOUSEMOTION); if (event.motion.state) { - return TOUCH_MOVE | (x << 12) | y; + return TOUCH_MOVE | touch_pack_xy(x, y); } break; case SDL_MOUSEBUTTONUP: - return TOUCH_END | (x << 12) | y; + return TOUCH_END | touch_pack_xy(x, y); } break; case SDL_KEYUP: