new touchscreen interface structure

This commit is contained in:
SGE 2023-12-18 00:16:20 +01:00
parent f3684362ad
commit 01cd131fd6
3 changed files with 11 additions and 78 deletions

View file

@ -10,45 +10,24 @@ static const char *const TAG = "icnt86";
#define UWORD uint16_t #define UWORD uint16_t
#define UDOUBLE uint32_t #define UDOUBLE uint32_t
void ICNT86TouchscreenStore::gpio_intr(ICNT86TouchscreenStore *store) { store->touch = true; }
float ICNT86Touchscreen::get_setup_priority() const { return setup_priority::HARDWARE - 1.0f; }
void ICNT86Touchscreen::setup() { void ICNT86Touchscreen::setup() {
ESP_LOGCONFIG(TAG, "Setting up icnt86 Touchscreen..."); ESP_LOGCONFIG(TAG, "Setting up icnt86 Touchscreen...");
// Register interrupt pin // Register interrupt pin
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP); this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->interrupt_pin_->setup(); this->interrupt_pin_->setup();
this->store_.pin = this->interrupt_pin_->to_isr(); this->attach_interrupt_(interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
this->interrupt_pin_->attach_interrupt(ICNT86TouchscreenStore::gpio_intr, &this->store_,
gpio::INTERRUPT_FALLING_EDGE);
// Perform reset if necessary // Perform reset if necessary
if (this->reset_pin_ != nullptr) { if (this->reset_pin_ != nullptr) {
this->reset_pin_->setup(); this->reset_pin_->setup();
this->reset_(); this->reset_();
} }
// 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 // Trigger initial read to activate the interrupt
this->store_.touch = true; this->store_.touched = true;
} }
void ICNT86Touchscreen::loop() { void ICNT86Touchscreen::update_touches() {
if (!this->store_.touch) {
for (auto *listener : this->touch_listeners_)
listener->release();
return;
}
ESP_LOGD(TAG, "touch");
this->store_.touch = false;
char buf[100]; char buf[100];
char mask[1] = {0x00}; char mask[1] = {0x00};
// Read report length // Read report length
@ -56,47 +35,22 @@ void ICNT86Touchscreen::loop() {
this->ICNT_Read_(0x1001, buf, 1); this->ICNT_Read_(0x1001, buf, 1);
uint8_t touch_count = buf[0]; uint8_t touch_count = buf[0];
ESP_LOGD(TAG, "Touch count: %d", touch_count);
if (buf[0] == 0x00 || (touch_count > 5 || touch_count < 1)) { // No new touch if (touch_count == 0x00 || (touch_count > 5 || touch_count < 1)) { // No new touch
this->reset_touch_sensor_();
return; return;
} else { } else {
this->ICNT_Read_(0x1002, buf, touch_count * 7); this->ICNT_Read_(0x1002, buf, touch_count * 7);
this->ICNT_Write_(0x1001, mask, 1); this->ICNT_Write_(0x1001, mask, 1);
ESP_LOGD(TAG, "Touch count: %d", touch_count);
for (UBYTE i = 0; i < touch_count; i++) { for (UBYTE i = 0; i < touch_count; i++) {
UWORD X = ((UWORD) buf[2 + 7 * i] << 8) + buf[1 + 7 * i]; UWORD X = ((UWORD) buf[2 + 7 * i] << 8) + buf[1 + 7 * i];
UWORD Y = ((UWORD) buf[4 + 7 * i] << 8) + buf[3 + 7 * i]; UWORD Y = ((UWORD) buf[4 + 7 * i] << 8) + buf[3 + 7 * i];
UWORD P = buf[5 + 7 * i]; UWORD P = buf[5 + 7 * i];
UWORD TouchEvenid = buf[6 + 7 * i]; UWORD TouchEvenid = buf[6 + 7 * i];
ESP_LOGD(TAG, "Touch x: %d, y: %d, p: %d", X, Y, P);
TouchPoint tp; this->set_raw_touch_position_(TouchEvenid, X, Y, P);
switch (this->rotation_) {
case ROTATE_0_DEGREES:
// Origin is top right, so mirror X by default
tp.x = Y;
tp.y = X;
break;
case ROTATE_90_DEGREES:
tp.x = X;
tp.y = Y;
break;
case ROTATE_180_DEGREES:
tp.x = this->display_width_ - Y;
tp.y = this->display_height_ - X;
break;
case ROTATE_270_DEGREES:
tp.x = this->display_height_ - X;
tp.y = this->display_width_ - Y;
break;
}
tp.id = TouchEvenid;
tp.state = P;
ESP_LOGD(TAG, "Touch x: %d, y: %d, p: %d", tp.x, tp.y, P);
this->defer([this, tp]() { this->send_touch_(tp); });
} }
} }
} }
@ -125,14 +79,6 @@ void ICNT86Touchscreen::dump_config() {
LOG_PIN(" Reset Pin: ", this->reset_pin_); LOG_PIN(" Reset Pin: ", this->reset_pin_);
} }
void ICNT86Touchscreen::reset_touch_sensor_() {
this->ICNT_Write_(0x1001, mask, 1);
touch_count = 0;
for (auto *listener : this->touch_listeners_)
listener->release();
return;
}
void ICNT86Touchscreen::ICNT_Read_(UWORD Reg, char *Data, UBYTE len) { this->I2C_Read_Byte_(Reg, Data, len); } void ICNT86Touchscreen::ICNT_Read_(UWORD Reg, char *Data, UBYTE len) { this->I2C_Read_Byte_(Reg, Data, len); }
void ICNT86Touchscreen::ICNT_Write_(UWORD Reg, char *Data, UBYTE len) { this->I2C_Write_Byte_(Reg, Data, len); } void ICNT86Touchscreen::ICNT_Write_(UWORD Reg, char *Data, UBYTE len) { this->I2C_Write_Byte_(Reg, Data, len); }

View file

@ -8,29 +8,20 @@
namespace esphome { namespace esphome {
namespace icnt86 { namespace icnt86 {
using namespace touchscreen;
#define UBYTE uint8_t #define UBYTE uint8_t
#define UWORD uint16_t #define UWORD uint16_t
#define UDOUBLE uint32_t #define UDOUBLE uint32_t
struct ICNT86TouchscreenStore { class ICNT86Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
volatile bool touch;
ISRInternalGPIOPin pin;
static void gpio_intr(ICNT86TouchscreenStore *store);
};
class ICNT86Touchscreen : public Touchscreen, public Component, public i2c::I2CDevice {
public: public:
void setup() override; void setup() override;
void loop() override;
void dump_config() override; void dump_config() override;
float get_setup_priority() const override;
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; } void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; } void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; }
protected: protected:
void update_touches() override;
void reset_(); void reset_();
void I2C_Write_Byte_(UWORD Reg, char *Data, UBYTE len); void I2C_Write_Byte_(UWORD Reg, char *Data, UBYTE len);
void ICNT_Read_(UWORD Reg, char *Data, UBYTE len); void ICNT_Read_(UWORD Reg, char *Data, UBYTE len);
@ -38,10 +29,7 @@ class ICNT86Touchscreen : public Touchscreen, public Component, public i2c::I2CD
void I2C_Read_Byte_(UWORD Reg, char *Data, UBYTE len); void I2C_Read_Byte_(UWORD Reg, char *Data, UBYTE len);
void ICNT_ReadVersion_(); void ICNT_ReadVersion_();
void reset_touch_sensor_(); void reset_touch_sensor_();
InternalGPIOPin *interrupt_pin_{};
ICNT86TouchscreenStore store_;
InternalGPIOPin *interrupt_pin_;
GPIOPin *reset_pin_{nullptr}; GPIOPin *reset_pin_{nullptr};

View file

@ -33,7 +33,7 @@ CONF_TRANSFORM = "transform"
TOUCHSCREEN_SCHEMA = cv.Schema( TOUCHSCREEN_SCHEMA = cv.Schema(
{ {
cv.GenerateID(CONF_DISPLAY): cv.use_id(display.Display), cv.GenerateID(CONF_DISPLAY): cv.use_id(display.DisplayBuffer),
cv.Optional(CONF_TRANSFORM): cv.Schema( cv.Optional(CONF_TRANSFORM): cv.Schema(
{ {
cv.Optional(CONF_SWAP_XY, default=False): cv.boolean, cv.Optional(CONF_SWAP_XY, default=False): cv.boolean,
@ -49,7 +49,6 @@ TOUCHSCREEN_SCHEMA = cv.Schema(
async def register_touchscreen(var, config): async def register_touchscreen(var, config):
await cg.register_component(var, config)
disp = await cg.get_variable(config[CONF_DISPLAY]) disp = await cg.get_variable(config[CONF_DISPLAY])
cg.add(var.set_display(disp)) cg.add(var.set_display(disp))