diff --git a/esphome/components/cst226/touchscreen/cst226_touchscreen.cpp b/esphome/components/cst226/touchscreen/cst226_touchscreen.cpp index d4e43d30f5..a25859fe17 100644 --- a/esphome/components/cst226/touchscreen/cst226_touchscreen.cpp +++ b/esphome/components/cst226/touchscreen/cst226_touchscreen.cpp @@ -72,6 +72,8 @@ void CST226Touchscreen::continue_setup_() { if (this->read16_(0xD1F8, buffer, 4)) { this->x_raw_max_ = buffer[0] + (buffer[1] << 8); this->y_raw_max_ = buffer[2] + (buffer[3] << 8); + if (this->swap_x_y_) + std::swap(this->x_raw_max_, this->y_raw_max_); } else { this->x_raw_max_ = this->display_->get_native_width(); this->y_raw_max_ = this->display_->get_native_height(); diff --git a/esphome/components/ektf2232/touchscreen/ektf2232.cpp b/esphome/components/ektf2232/touchscreen/ektf2232.cpp index ef8f1c6802..603b554273 100644 --- a/esphome/components/ektf2232/touchscreen/ektf2232.cpp +++ b/esphome/components/ektf2232/touchscreen/ektf2232.cpp @@ -34,26 +34,29 @@ void EKTF2232Touchscreen::setup() { // Get touch resolution uint8_t received[4]; - if (this->x_raw_max_ == this->x_raw_min_) { - this->write(GET_X_RES, 4); - if (this->read(received, 4)) { - ESP_LOGE(TAG, "Failed to read X resolution!"); + if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) { + auto err = this->write(GET_X_RES, 4); + if (err == i2c::ERROR_OK) { + err = this->read(received, 4); + if (err == i2c::ERROR_OK) { + this->x_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4); + err = this->write(GET_Y_RES, 4); + if (err == i2c::ERROR_OK) { + err = this->read(received, 4); + if (err == i2c::ERROR_OK) { + this->y_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4); + } + } + } + } + if (err != i2c::ERROR_OK) { + ESP_LOGE(TAG, "Failed to read calibration values!"); this->interrupt_pin_->detach_interrupt(); this->mark_failed(); return; } - this->x_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4); - } - - if (this->y_raw_max_ == this->y_raw_min_) { - this->write(GET_Y_RES, 4); - if (this->read(received, 4)) { - ESP_LOGE(TAG, "Failed to read Y resolution!"); - this->interrupt_pin_->detach_interrupt(); - this->mark_failed(); - return; - } - this->y_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4); + if (this->swap_x_y_) + std::swap(this->x_raw_max_, this->y_raw_max_); } this->set_power_state(true); } diff --git a/esphome/components/gt911/touchscreen/gt911_touchscreen.cpp b/esphome/components/gt911/touchscreen/gt911_touchscreen.cpp index 84811b818f..674ef51d64 100644 --- a/esphome/components/gt911/touchscreen/gt911_touchscreen.cpp +++ b/esphome/components/gt911/touchscreen/gt911_touchscreen.cpp @@ -60,20 +60,25 @@ void GT911Touchscreen::setup() { } } } - if (err == i2c::ERROR_OK) { - err = this->write(GET_MAX_VALUES, 2); + if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) { + // no calibration? Attempt to read the max values from the touchscreen. if (err == i2c::ERROR_OK) { - err = this->read(data, sizeof(data)); + err = this->write(GET_MAX_VALUES, 2); if (err == i2c::ERROR_OK) { - if (this->x_raw_max_ == this->x_raw_min_) { + err = this->read(data, sizeof(data)); + if (err == i2c::ERROR_OK) { this->x_raw_max_ = encode_uint16(data[1], data[0]); - } - if (this->y_raw_max_ == this->y_raw_min_) { this->y_raw_max_ = encode_uint16(data[3], data[2]); + if (this->swap_x_y_) + std::swap(this->x_raw_max_, this->y_raw_max_); } - esph_log_d(TAG, "calibration max_x/max_y %d/%d", this->x_raw_max_, this->y_raw_max_); } } + if (err != i2c::ERROR_OK) { + ESP_LOGE(TAG, "Failed to read calibration values from touchscreen!"); + this->mark_failed(); + return; + } } if (err != i2c::ERROR_OK) { ESP_LOGE(TAG, "Failed to communicate!");