mirror of
https://github.com/esphome/esphome.git
synced 2025-10-28 13:43:54 +00: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:
@@ -3,7 +3,7 @@ import esphome.config_validation as cv
|
||||
|
||||
from esphome import pins
|
||||
from esphome.components import i2c, touchscreen
|
||||
from esphome.const import CONF_INTERRUPT_PIN, CONF_ID, CONF_ROTATION
|
||||
from esphome.const import CONF_INTERRUPT_PIN, CONF_ID
|
||||
from .. import gt911_ns
|
||||
|
||||
|
||||
@@ -11,36 +11,21 @@ GT911ButtonListener = gt911_ns.class_("GT911ButtonListener")
|
||||
GT911Touchscreen = gt911_ns.class_(
|
||||
"GT911Touchscreen",
|
||||
touchscreen.Touchscreen,
|
||||
cg.Component,
|
||||
i2c.I2CDevice,
|
||||
)
|
||||
|
||||
ROTATIONS = {
|
||||
0: touchscreen.TouchRotation.ROTATE_0_DEGREES,
|
||||
90: touchscreen.TouchRotation.ROTATE_90_DEGREES,
|
||||
180: touchscreen.TouchRotation.ROTATE_180_DEGREES,
|
||||
270: touchscreen.TouchRotation.ROTATE_270_DEGREES,
|
||||
}
|
||||
CONFIG_SCHEMA = (
|
||||
touchscreen.TOUCHSCREEN_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(GT911Touchscreen),
|
||||
cv.Optional(CONF_ROTATION): cv.enum(ROTATIONS),
|
||||
cv.Required(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
|
||||
}
|
||||
)
|
||||
.extend(i2c.i2c_device_schema(0x5D))
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
)
|
||||
CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(GT911Touchscreen),
|
||||
cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
|
||||
}
|
||||
).extend(i2c.i2c_device_schema(0x5D))
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
await touchscreen.register_touchscreen(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
|
||||
interrupt_pin = await cg.gpio_pin_expression(config[CONF_INTERRUPT_PIN])
|
||||
cg.add(var.set_interrupt_pin(interrupt_pin))
|
||||
if CONF_ROTATION in config:
|
||||
cg.add(var.set_rotation(ROTATIONS[config[CONF_ROTATION]]))
|
||||
if interrupt_pin := config.get(CONF_INTERRUPT_PIN):
|
||||
cg.add(var.set_interrupt_pin(await cg.gpio_pin_expression(interrupt_pin)))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -8,30 +8,23 @@
|
||||
namespace esphome {
|
||||
namespace gt911 {
|
||||
|
||||
struct Store {
|
||||
volatile bool available;
|
||||
|
||||
static void gpio_intr(Store *store);
|
||||
};
|
||||
|
||||
class GT911ButtonListener {
|
||||
public:
|
||||
virtual void update_button(uint8_t index, bool state) = 0;
|
||||
};
|
||||
|
||||
class GT911Touchscreen : public touchscreen::Touchscreen, public Component, public i2c::I2CDevice {
|
||||
class GT911Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
void set_rotation(touchscreen::TouchRotation rotation) { this->rotation_ = rotation; }
|
||||
|
||||
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
|
||||
void register_button_listener(GT911ButtonListener *listener) { this->button_listeners_.push_back(listener); }
|
||||
|
||||
protected:
|
||||
InternalGPIOPin *interrupt_pin_;
|
||||
Store store_;
|
||||
void update_touches() override;
|
||||
|
||||
InternalGPIOPin *interrupt_pin_{};
|
||||
std::vector<GT911ButtonListener *> button_listeners_;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user