mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-28 00:38:28 +02:00
Updating the touchscreen interface structure (#4596)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: NP v/d Spek <github_mail@lumensoft.nl> Co-authored-by: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> Co-authored-by: Gustavo Ambrozio <gustavo@gustavo.eng.br>
This commit is contained in:
@@ -12,6 +12,7 @@ static const uint8_t GET_TOUCH_STATE[2] = {0x81, 0x4E};
|
||||
static const uint8_t CLEAR_TOUCH_STATE[3] = {0x81, 0x4E, 0x00};
|
||||
static const uint8_t GET_TOUCHES[2] = {0x81, 0x4F};
|
||||
static const uint8_t GET_SWITCHES[2] = {0x80, 0x4D};
|
||||
static const uint8_t GET_MAX_VALUES[2] = {0x80, 0x48};
|
||||
static const size_t MAX_TOUCHES = 5; // max number of possible touches reported
|
||||
|
||||
#define ERROR_CHECK(err) \
|
||||
@@ -21,24 +22,35 @@ static const size_t MAX_TOUCHES = 5; // max number of possible touches reported
|
||||
return; \
|
||||
}
|
||||
|
||||
void IRAM_ATTR HOT Store::gpio_intr(Store *store) { store->available = true; }
|
||||
|
||||
void GT911Touchscreen::setup() {
|
||||
i2c::ErrorCode err;
|
||||
ESP_LOGCONFIG(TAG, "Setting up GT911 Touchscreen...");
|
||||
// datasheet says NOT to use pullup/down on the int line.
|
||||
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT);
|
||||
this->interrupt_pin_->setup();
|
||||
|
||||
// check the configuration of the int line.
|
||||
uint8_t data;
|
||||
uint8_t data[4];
|
||||
err = this->write(GET_SWITCHES, 2);
|
||||
if (err == i2c::ERROR_OK) {
|
||||
err = this->read(&data, 1);
|
||||
err = this->read(data, 1);
|
||||
if (err == i2c::ERROR_OK) {
|
||||
ESP_LOGD(TAG, "Read from switches: 0x%02X", data);
|
||||
this->interrupt_pin_->attach_interrupt(Store::gpio_intr, &this->store_,
|
||||
(data & 1) ? gpio::INTERRUPT_FALLING_EDGE : gpio::INTERRUPT_RISING_EDGE);
|
||||
ESP_LOGD(TAG, "Read from switches: 0x%02X", data[0]);
|
||||
if (this->interrupt_pin_ != nullptr) {
|
||||
// datasheet says NOT to use pullup/down on the int line.
|
||||
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT);
|
||||
this->interrupt_pin_->setup();
|
||||
this->attach_interrupt_(this->interrupt_pin_,
|
||||
(data[0] & 1) ? gpio::INTERRUPT_FALLING_EDGE : gpio::INTERRUPT_RISING_EDGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (err == i2c::ERROR_OK) {
|
||||
err = this->write(GET_MAX_VALUES, 2);
|
||||
if (err == i2c::ERROR_OK) {
|
||||
err = this->read(data, sizeof(data));
|
||||
if (err == i2c::ERROR_OK) {
|
||||
this->x_raw_max_ = encode_uint16(data[1], data[0]);
|
||||
this->y_raw_max_ = encode_uint16(data[3], data[2]);
|
||||
esph_log_d(TAG, "Read max_x/max_y %d/%d", this->x_raw_max_, this->y_raw_max_);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (err != i2c::ERROR_OK) {
|
||||
@@ -46,31 +58,28 @@ void GT911Touchscreen::setup() {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGCONFIG(TAG, "GT911 Touchscreen setup complete");
|
||||
}
|
||||
|
||||
void GT911Touchscreen::loop() {
|
||||
void GT911Touchscreen::update_touches() {
|
||||
i2c::ErrorCode err;
|
||||
touchscreen::TouchPoint tp;
|
||||
uint8_t touch_state = 0;
|
||||
uint8_t data[MAX_TOUCHES + 1][8]; // 8 bytes each for each point, plus extra space for the key byte
|
||||
|
||||
if (!this->store_.available)
|
||||
return;
|
||||
this->store_.available = false;
|
||||
|
||||
err = this->write(GET_TOUCH_STATE, sizeof(GET_TOUCH_STATE), false);
|
||||
ERROR_CHECK(err);
|
||||
err = this->read(&touch_state, 1);
|
||||
ERROR_CHECK(err);
|
||||
this->write(CLEAR_TOUCH_STATE, sizeof(CLEAR_TOUCH_STATE));
|
||||
|
||||
if ((touch_state & 0x80) == 0)
|
||||
return;
|
||||
uint8_t num_of_touches = touch_state & 0x07;
|
||||
|
||||
if ((touch_state & 0x80) == 0 || num_of_touches > MAX_TOUCHES) {
|
||||
this->skip_update_ = true; // skip send touch events, touchscreen is not ready yet.
|
||||
return;
|
||||
}
|
||||
|
||||
if (num_of_touches == 0)
|
||||
this->send_release_();
|
||||
if (num_of_touches > MAX_TOUCHES) // should never happen
|
||||
return;
|
||||
|
||||
err = this->write(GET_TOUCHES, sizeof(GET_TOUCHES), false);
|
||||
@@ -80,29 +89,10 @@ void GT911Touchscreen::loop() {
|
||||
ERROR_CHECK(err);
|
||||
|
||||
for (uint8_t i = 0; i != num_of_touches; i++) {
|
||||
tp.id = data[i][0];
|
||||
uint16_t id = data[i][0];
|
||||
uint16_t x = encode_uint16(data[i][2], data[i][1]);
|
||||
uint16_t y = encode_uint16(data[i][4], data[i][3]);
|
||||
|
||||
switch (this->rotation_) {
|
||||
case touchscreen::ROTATE_0_DEGREES:
|
||||
tp.x = x;
|
||||
tp.y = y;
|
||||
break;
|
||||
case touchscreen::ROTATE_90_DEGREES:
|
||||
tp.x = y;
|
||||
tp.y = this->display_width_ - x;
|
||||
break;
|
||||
case touchscreen::ROTATE_180_DEGREES:
|
||||
tp.x = this->display_width_ - x;
|
||||
tp.y = this->display_height_ - y;
|
||||
break;
|
||||
case touchscreen::ROTATE_270_DEGREES:
|
||||
tp.x = this->display_height_ - y;
|
||||
tp.y = x;
|
||||
break;
|
||||
}
|
||||
this->defer([this, tp]() { this->send_touch_(tp); });
|
||||
this->set_raw_touch_position_(id, x, y);
|
||||
}
|
||||
auto keys = data[num_of_touches][0];
|
||||
for (size_t i = 0; i != 4; i++) {
|
||||
@@ -115,7 +105,6 @@ void GT911Touchscreen::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "GT911 Touchscreen:");
|
||||
LOG_I2C_DEVICE(this);
|
||||
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
|
||||
ESP_LOGCONFIG(TAG, " Rotation: %d", (int) this->rotation_);
|
||||
}
|
||||
|
||||
} // namespace gt911
|
||||
|
||||
Reference in New Issue
Block a user