9static const size_t MAX_TOUCHES = 3;
15 ESP_LOGCONFIG(TAG,
"Setting up GSL3670 touchscreen...");
19 if (this->interrupt_pin_ !=
nullptr) {
20 this->interrupt_pin_->
setup();
33 this->load_firmware_();
34 this->startup_chip_();
36 this->startup_chip_();
38 ESP_LOGCONFIG(TAG,
"GSL3670 initialised OK");
43 "GSL3670 Touchscreen:\n"
48 LOG_PIN(
" Reset Pin: ", this->reset_pin_);
49 LOG_PIN(
" Interrupt Pin: ", this->interrupt_pin_);
50 ESP_LOGCONFIG(TAG,
" Firmware records: %zu", this->firmware_len_);
60 ESP_LOGW(TAG,
"I2C read failed (%d)", err);
66 for (uint8_t j = 0; j != finger_num; j++) {
68 const uint8_t *record = &buf[(j + 1) * 4];
72 ESP_LOGV(TAG,
"Touch id=%u, x=%u y=%u",
id,
x,
y);
73 if (
x <= 8192 &&
y <= 8192)
82void GSL3670Touchscreen::clear_reg_() {
83 ESP_LOGD(TAG,
"clear_reg");
85 pulse_reset(this->reset_pin_, {.low_ms = 1, .high_after_ms = 5});
87 this->write_reg8_(0x88, 0x01);
89 this->write_reg8_(0xe4, 0x04);
91 this->write_reg8_(0xe0, 0x00);
99void GSL3670Touchscreen::reset_() {
100 ESP_LOGD(TAG,
"reset");
102 pulse_reset(this->reset_pin_, {.low_ms = 1, .high_after_ms = 5});
104 this->write_reg8_(0xe4, 0x04);
106 uint8_t zeros[4] = {0, 0, 0, 0};
107 this->write_reg_(0xbc, zeros, 4);
110void GSL3670Touchscreen::load_firmware_() {
111 if (firmware_ ==
nullptr || firmware_len_ == 0) {
112 ESP_LOGW(TAG,
"No firmware supplied – skipping");
116 ESP_LOGD(TAG,
"Loading firmware (%zu blocks)...", firmware_len_);
118 static constexpr size_t FW_BLK_SIZE = 128 + 4;
120 for (
size_t i = 0; i != this->firmware_len_; i++) {
121 auto offset = i * FW_BLK_SIZE;
122 uint8_t
val = this->firmware_[offset + 0];
123 ESP_LOGV(TAG,
"Firmware address 0x%02X",
val);
124 this->write_reg_(0xf0, &
val, 1);
125 this->write_reg_(0, this->firmware_ + offset + 4, 128);
127 ESP_LOGD(TAG,
"Firmware load complete");
134void GSL3670Touchscreen::startup_chip_() {
135 ESP_LOGD(TAG,
"startup_chip");
136 this->write_reg8_(0xe0, 0x00);
144bool GSL3670Touchscreen::write_reg_(uint8_t reg,
const uint8_t *data,
size_t len) {
147 ESP_LOGW(TAG,
"I2C write reg 0x%02X len %zu failed (%d)",
reg,
len, err);
153bool GSL3670Touchscreen::write_reg8_(uint8_t reg, uint8_t
val) {
return write_reg_(
reg, &
val, 1); }
int get_native_width()
Get the native (original) width of the display in pixels.
int get_native_height()
Get the native (original) height of the display in pixels.
void dump_config() override
void update_touches() override
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
void attach_interrupt_(InternalGPIOPin *irq_pin, esphome::gpio::InterruptType type)
Call this function to send touch points to the on_touch listener and the binary_sensors.
display::Display * display_
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
ESPHOME_LOG_TAG(TAG, "gsl3670.touchscreen")
@ ERROR_OK
No error found during execution of method.
T clamp_at_most(T value, U max)
constexpr uint8_t high_nibble(uint8_t byte)
The high 4 bits of a byte, e.g. high_nibble(0xA5) == 0xA.
void setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)
constexpr uint8_t low_nibble(uint8_t byte)
The low 4 bits of a byte, e.g. low_nibble(0xA5) == 0x5.
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.