-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
|
||
from esphome import pins | ||
from esphome.components import i2c, touchscreen | ||
from esphome.const import CONF_ID, CONF_INTERRUPT_PIN, CONF_RESET_PIN | ||
|
||
CODEOWNERS = ["@kroimon"] | ||
DEPENDENCIES = ["i2c"] | ||
|
||
tt21100_ns = cg.esphome_ns.namespace("tt21100") | ||
TT21100Touchscreen = tt21100_ns.class_( | ||
"TT21100Touchscreen", | ||
touchscreen.Touchscreen, | ||
cg.Component, | ||
i2c.I2CDevice, | ||
) | ||
|
||
CONF_TT21100_ID = "tt21100_id" | ||
|
||
CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend( | ||
cv.Schema( | ||
{ | ||
cv.GenerateID(): cv.declare_id(TT21100Touchscreen), | ||
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) | ||
) | ||
|
||
|
||
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) | ||
|
||
interrupt_pin = await cg.gpio_pin_expression(config[CONF_INTERRUPT_PIN]) | ||
cg.add(var.set_interrupt_pin(interrupt_pin)) | ||
if CONF_RESET_PIN in config: | ||
rts_pin = await cg.gpio_pin_expression(config[CONF_RESET_PIN]) | ||
cg.add(var.set_reset_pin(rts_pin)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include "tt21100.h" | ||
#include "esphome/core/helpers.h" | ||
#include "esphome/core/log.h" | ||
|
||
namespace esphome { | ||
namespace tt21100 { | ||
|
||
static const char *const TAG = "tt21100"; | ||
|
||
#define TOUCH_MAX_POINTS 5 | ||
Check failure on line 10 in esphome/components/tt21100/tt21100.cpp GitHub Actions / Run script/ci-custom
|
||
#define TOUCH_TT21100_MAX_DATA_LEN (7+TOUCH_MAX_POINTS*10) /* 7 Header + (Points * 10 data bytes) */ | ||
|
||
void TT21100TouchscreenStore::gpio_intr(TT21100TouchscreenStore *store) { store->touch = true; } | ||
|
||
void TT21100Touchscreen::setup() { | ||
ESP_LOGCONFIG(TAG, "Setting up TT21100 Touchscreen..."); | ||
|
||
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); | ||
|
||
if (this->reset_pin_ != nullptr) { | ||
this->reset_pin_->setup(); | ||
this->reset_(); | ||
} | ||
|
||
this->store_.touch = false; | ||
} | ||
|
||
void TT21100Touchscreen::loop() { | ||
if (!this->store_.touch) | ||
return; | ||
this->store_.touch = false; | ||
|
||
// Read report length | ||
uint16_t data_len; | ||
this->read((uint8_t *)&data_len, sizeof(data_len)); | ||
|
||
// Read report data | ||
uint8_t data[TOUCH_TT21100_MAX_DATA_LEN]; | ||
if (data_len > 0 && data_len < sizeof(data)) { | ||
this->read(data, data_len); | ||
|
||
if (data_len == 14) { | ||
// Button event | ||
auto *report = (TT21100ButtonReport *) data; | ||
|
||
ESP_LOGD(TAG, "Button report: Len=%d, ID=%d, Time=%5u, Val=[%u] - [%04X][%04X][%04X][%04X]", | ||
report->length, report->report_id, report->timestamp, report->btn_val, | ||
report->btn_signal[0], report->btn_signal[1], report->btn_signal[2], report->btn_signal[3]); | ||
|
||
// TODO: handle report as binary sensor | ||
|
||
} else if (data_len >= 7) { | ||
// Touch point event | ||
auto *report = (TT21100TouchReport *) data; | ||
|
||
ESP_LOGD(TAG, "Touch report: Len=%d, ID=%d, Time=%5u, LargeObject=%u, RecordNum=%u, RecordCounter=%u, NoiseEfect=%u", | ||
report->length, report->report_id, report->timestamp, report->large_object, report->record_num, | ||
report->report_counter, report->noise_efect); | ||
|
||
uint8_t touch_count = (data_len - sizeof(TT21100TouchReport)) / 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]; | ||
|
||
ESP_LOGD(TAG, "Touch %d: Type=%u, Tip=%u, EventId=%u, TouchId=%u, X=%d, Y=%d, Pressure=%u, MajorAxisLen=%d, Orientation=%u", | ||
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; | ||
tp.x = touch->x; | ||
tp.y = touch->y; | ||
tp.id = touch->tip; | ||
tp.state = touch->pressure; | ||
|
||
this->defer([this, tp]() { this->send_touch_(tp); }); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void TT21100Touchscreen::reset_() { | ||
if (this->reset_pin_ != nullptr) { | ||
this->reset_pin_->digital_write(false); | ||
delay(10); | ||
this->reset_pin_->digital_write(true); | ||
delay(10); | ||
} | ||
} | ||
|
||
void TT21100Touchscreen::dump_config() { | ||
ESP_LOGCONFIG(TAG, "TT21100 Touchscreen:"); | ||
LOG_I2C_DEVICE(this); | ||
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_); | ||
LOG_PIN(" Reset Pin: ", this->reset_pin_); | ||
} | ||
|
||
} // namespace tt21100 | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#pragma once | ||
|
||
#include "esphome/components/i2c/i2c.h" | ||
#include "esphome/components/touchscreen/touchscreen.h" | ||
#include "esphome/core/automation.h" | ||
#include "esphome/core/component.h" | ||
#include "esphome/core/hal.h" | ||
|
||
namespace esphome { | ||
namespace tt21100 { | ||
|
||
struct TT21100TouchscreenStore { | ||
volatile bool touch; | ||
ISRInternalGPIOPin pin; | ||
|
||
static void gpio_intr(TT21100TouchscreenStore *store); | ||
}; | ||
|
||
using namespace touchscreen; | ||
|
||
class TT21100Touchscreen : public Touchscreen, public Component, public i2c::I2CDevice { | ||
public: | ||
void setup() override; | ||
void loop() override; | ||
void dump_config() override; | ||
|
||
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; } | ||
void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; } | ||
|
||
protected: | ||
void reset_(); | ||
|
||
InternalGPIOPin *interrupt_pin_; | ||
GPIOPin *reset_pin_{nullptr}; | ||
TT21100TouchscreenStore store_; | ||
}; | ||
|
||
struct TT21100TouchRecord { | ||
uint8_t : 5; | ||
uint8_t touch_type: 3; | ||
uint8_t tip: 1; | ||
uint8_t event_id: 2; | ||
uint8_t touch_id: 5; | ||
uint16_t x; | ||
uint16_t y; | ||
uint8_t pressure; | ||
uint16_t major_axis_length; | ||
uint8_t orientation; | ||
} __attribute__((packed)); | ||
|
||
struct TT21100TouchReport { | ||
uint16_t length; | ||
uint8_t report_id; | ||
uint16_t timestamp; | ||
uint8_t : 2; | ||
uint8_t large_object : 1; | ||
uint8_t record_num : 5; | ||
uint8_t report_counter: 2; | ||
uint8_t : 3; | ||
uint8_t noise_efect: 3; | ||
TT21100TouchRecord touch_record[0]; | ||
} __attribute__((packed)); | ||
|
||
struct TT21100ButtonReport { | ||
uint16_t length; /*!< Always 14(0x000E) */ | ||
uint8_t report_id; /*!< Always 0x03 */ | ||
uint16_t timestamp; /*!< Number in units of 100 us */ | ||
uint8_t btn_val; /*!< Only use bit[0..3] */ | ||
uint16_t btn_signal[4]; | ||
} __attribute__((packed)); | ||
|
||
} // namespace tt21100 | ||
} // namespace esphome |