mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-30 09:48:27 +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,7 +12,6 @@ DEPENDENCIES = ["i2c"]
|
||||
TT21100Touchscreen = tt21100_ns.class_(
|
||||
"TT21100Touchscreen",
|
||||
touchscreen.Touchscreen,
|
||||
cg.Component,
|
||||
i2c.I2CDevice,
|
||||
)
|
||||
TT21100ButtonListener = tt21100_ns.class_("TT21100ButtonListener")
|
||||
@@ -24,17 +23,14 @@ CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend(
|
||||
cv.Required(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
|
||||
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
|
||||
}
|
||||
)
|
||||
.extend(i2c.i2c_device_schema(0x24))
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
).extend(i2c.i2c_device_schema(0x24))
|
||||
)
|
||||
|
||||
|
||||
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))
|
||||
|
||||
@@ -44,8 +44,6 @@ struct TT21100TouchReport {
|
||||
TT21100TouchRecord touch_record[MAX_TOUCH_POINTS];
|
||||
} __attribute__((packed));
|
||||
|
||||
void TT21100TouchscreenStore::gpio_intr(TT21100TouchscreenStore *store) { store->touch = true; }
|
||||
|
||||
float TT21100Touchscreen::get_setup_priority() const { return setup_priority::HARDWARE - 1.0f; }
|
||||
|
||||
void TT21100Touchscreen::setup() {
|
||||
@@ -54,9 +52,8 @@ void TT21100Touchscreen::setup() {
|
||||
// Register interrupt pin
|
||||
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
|
||||
this->interrupt_pin_->setup();
|
||||
this->store_.pin = this->interrupt_pin_->to_isr();
|
||||
this->interrupt_pin_->attach_interrupt(TT21100TouchscreenStore::gpio_intr, &this->store_,
|
||||
gpio::INTERRUPT_FALLING_EDGE);
|
||||
|
||||
this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
|
||||
|
||||
// Perform reset if necessary
|
||||
if (this->reset_pin_ != nullptr) {
|
||||
@@ -65,19 +62,11 @@ void TT21100Touchscreen::setup() {
|
||||
}
|
||||
|
||||
// Update display dimensions if they were updated during display setup
|
||||
this->display_width_ = this->display_->get_width();
|
||||
this->display_height_ = this->display_->get_height();
|
||||
this->rotation_ = static_cast<TouchRotation>(this->display_->get_rotation());
|
||||
|
||||
// Trigger initial read to activate the interrupt
|
||||
this->store_.touch = true;
|
||||
this->x_raw_max_ = this->get_width_();
|
||||
this->y_raw_max_ = this->get_height_();
|
||||
}
|
||||
|
||||
void TT21100Touchscreen::loop() {
|
||||
if (!this->store_.touch)
|
||||
return;
|
||||
this->store_.touch = false;
|
||||
|
||||
void TT21100Touchscreen::update_touches() {
|
||||
// Read report length
|
||||
uint16_t data_len;
|
||||
this->read((uint8_t *) &data_len, sizeof(data_len));
|
||||
@@ -111,12 +100,6 @@ void TT21100Touchscreen::loop() {
|
||||
|
||||
uint8_t touch_count = (data_len - (sizeof(*report) - sizeof(report->touch_record))) / sizeof(TT21100TouchRecord);
|
||||
|
||||
if (touch_count == 0) {
|
||||
for (auto *listener : this->touch_listeners_)
|
||||
listener->release();
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < touch_count; i++) {
|
||||
auto *touch = &report->touch_record[i];
|
||||
|
||||
@@ -126,30 +109,7 @@ void TT21100Touchscreen::loop() {
|
||||
i, touch->touch_type, touch->tip, touch->event_id, touch->touch_id, touch->x, touch->y,
|
||||
touch->pressure, touch->major_axis_length, touch->orientation);
|
||||
|
||||
TouchPoint tp;
|
||||
switch (this->rotation_) {
|
||||
case ROTATE_0_DEGREES:
|
||||
// Origin is top right, so mirror X by default
|
||||
tp.x = this->display_width_ - touch->x;
|
||||
tp.y = touch->y;
|
||||
break;
|
||||
case ROTATE_90_DEGREES:
|
||||
tp.x = touch->y;
|
||||
tp.y = touch->x;
|
||||
break;
|
||||
case ROTATE_180_DEGREES:
|
||||
tp.x = touch->x;
|
||||
tp.y = this->display_height_ - touch->y;
|
||||
break;
|
||||
case ROTATE_270_DEGREES:
|
||||
tp.x = this->display_height_ - touch->y;
|
||||
tp.y = this->display_width_ - touch->x;
|
||||
break;
|
||||
}
|
||||
tp.id = touch->tip;
|
||||
tp.state = touch->pressure;
|
||||
|
||||
this->defer([this, tp]() { this->send_touch_(tp); });
|
||||
this->set_raw_touch_position_(touch->tip, touch->x, touch->y, touch->pressure);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,27 +5,21 @@
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace tt21100 {
|
||||
|
||||
using namespace touchscreen;
|
||||
|
||||
struct TT21100TouchscreenStore {
|
||||
volatile bool touch;
|
||||
ISRInternalGPIOPin pin;
|
||||
|
||||
static void gpio_intr(TT21100TouchscreenStore *store);
|
||||
};
|
||||
|
||||
class TT21100ButtonListener {
|
||||
public:
|
||||
virtual void update_button(uint8_t index, uint16_t state) = 0;
|
||||
};
|
||||
|
||||
class TT21100Touchscreen : public Touchscreen, public Component, public i2c::I2CDevice {
|
||||
class TT21100Touchscreen : public Touchscreen, public i2c::I2CDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
|
||||
@@ -37,7 +31,7 @@ class TT21100Touchscreen : public Touchscreen, public Component, public i2c::I2C
|
||||
protected:
|
||||
void reset_();
|
||||
|
||||
TT21100TouchscreenStore store_;
|
||||
void update_touches() override;
|
||||
|
||||
InternalGPIOPin *interrupt_pin_;
|
||||
GPIOPin *reset_pin_{nullptr};
|
||||
|
||||
Reference in New Issue
Block a user