From 78951c197a10072c8a1846d9adc76612ea495db0 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 21 Feb 2022 09:58:53 +1300 Subject: [PATCH] Fix lilygo touchscreen rotation (#3221) --- .../touchscreen/lilygo_t5_47_touchscreen.cpp | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/esphome/components/lilygo_t5_47/touchscreen/lilygo_t5_47_touchscreen.cpp b/esphome/components/lilygo_t5_47/touchscreen/lilygo_t5_47_touchscreen.cpp index b5cf63980b..b92d7d6f10 100644 --- a/esphome/components/lilygo_t5_47/touchscreen/lilygo_t5_47_touchscreen.cpp +++ b/esphome/components/lilygo_t5_47/touchscreen/lilygo_t5_47_touchscreen.cpp @@ -113,8 +113,27 @@ void LilygoT547Touchscreen::loop() { if (tp.state == 0x06) tp.state = 0x07; - tp.y = (uint16_t)((buffer[i * 5 + 1 + offset] << 4) | ((buffer[i * 5 + 3 + offset] >> 4) & 0x0F)); - tp.x = (uint16_t)((buffer[i * 5 + 2 + offset] << 4) | (buffer[i * 5 + 3 + offset] & 0x0F)); + uint16_t y = (uint16_t)((buffer[i * 5 + 1 + offset] << 4) | ((buffer[i * 5 + 3 + offset] >> 4) & 0x0F)); + uint16_t x = (uint16_t)((buffer[i * 5 + 2 + offset] << 4) | (buffer[i * 5 + 3 + offset] & 0x0F)); + + switch (this->rotation_) { + case ROTATE_0_DEGREES: + tp.y = this->display_height_ - y; + tp.x = x; + break; + case ROTATE_90_DEGREES: + tp.x = this->display_height_ - y; + tp.y = this->display_width_ - x; + break; + case ROTATE_180_DEGREES: + tp.y = y; + tp.x = this->display_width_ - x; + break; + case ROTATE_270_DEGREES: + tp.x = y; + tp.y = x; + break; + } this->defer([this, tp]() { this->send_touch_(tp); }); } @@ -122,8 +141,28 @@ void LilygoT547Touchscreen::loop() { TouchPoint tp; tp.id = (buffer[0] >> 4) & 0x0F; tp.state = 0x06; - tp.y = (uint16_t)((buffer[0 * 5 + 1] << 4) | ((buffer[0 * 5 + 3] >> 4) & 0x0F)); - tp.x = (uint16_t)((buffer[0 * 5 + 2] << 4) | (buffer[0 * 5 + 3] & 0x0F)); + + uint16_t y = (uint16_t)((buffer[0 * 5 + 1] << 4) | ((buffer[0 * 5 + 3] >> 4) & 0x0F)); + uint16_t x = (uint16_t)((buffer[0 * 5 + 2] << 4) | (buffer[0 * 5 + 3] & 0x0F)); + + switch (this->rotation_) { + case ROTATE_0_DEGREES: + tp.y = this->display_height_ - y; + tp.x = x; + break; + case ROTATE_90_DEGREES: + tp.x = this->display_height_ - y; + tp.y = this->display_width_ - x; + break; + case ROTATE_180_DEGREES: + tp.y = y; + tp.x = this->display_width_ - x; + break; + case ROTATE_270_DEGREES: + tp.x = y; + tp.y = x; + break; + } this->defer([this, tp]() { this->send_touch_(tp); }); }