Add CST816 touchscreen driver (#5941)

* Add CST816 touchscreen
This commit is contained in:
Clyde Stubbs 2024-03-11 16:34:46 +11:00 committed by GitHub
parent 9c95e570c7
commit d6bcc465a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 305 additions and 0 deletions

View file

@ -80,6 +80,7 @@ esphome/components/copy/* @OttoWinter
esphome/components/cover/* @esphome/core
esphome/components/cs5460a/* @balrog-kun
esphome/components/cse7761/* @berfenger
esphome/components/cst816/* @clydebarrow
esphome/components/ct_clamp/* @jesserockz
esphome/components/current_based/* @djwmarcx
esphome/components/dac7678/* @NickB1

View file

@ -0,0 +1,6 @@
import esphome.codegen as cg
CODEOWNERS = ["@clydebarrow"]
DEPENDENCIES = ["i2c"]
cst816_ns = cg.esphome_ns.namespace("cst816")

View file

@ -0,0 +1,28 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from .. import cst816_ns
from ..touchscreen import CST816Touchscreen, CST816ButtonListener
CONF_CST816_ID = "cst816_id"
CST816Button = cst816_ns.class_(
"CST816Button",
binary_sensor.BinarySensor,
cg.Component,
CST816ButtonListener,
cg.Parented.template(CST816Touchscreen),
)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(CST816Button).extend(
{
cv.GenerateID(CONF_CST816_ID): cv.use_id(CST816Touchscreen),
}
)
async def to_code(config):
var = await binary_sensor.new_binary_sensor(config)
await cg.register_component(var, config)
await cg.register_parented(var, config[CONF_CST816_ID])

View file

@ -0,0 +1,27 @@
#pragma once
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/cst816/touchscreen/cst816_touchscreen.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace cst816 {
class CST816Button : public binary_sensor::BinarySensor,
public Component,
public CST816ButtonListener,
public Parented<CST816Touchscreen> {
public:
void setup() override {
this->parent_->register_button_listener(this);
this->publish_initial_state(false);
}
void dump_config() override { LOG_BINARY_SENSOR("", "CST816 Button", this); }
void update_button(bool state) override { this->publish_state(state); }
};
} // namespace cst816
} // namespace esphome

View file

@ -0,0 +1,34 @@
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_INTERRUPT_PIN, CONF_ID, CONF_RESET_PIN
from .. import cst816_ns
CST816Touchscreen = cst816_ns.class_(
"CST816Touchscreen",
touchscreen.Touchscreen,
i2c.I2CDevice,
)
CST816ButtonListener = cst816_ns.class_("CST816ButtonListener")
CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(CST816Touchscreen),
cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
}
).extend(i2c.i2c_device_schema(0x15))
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await touchscreen.register_touchscreen(var, config)
await i2c.register_i2c_device(var, config)
if interrupt_pin := config.get(CONF_INTERRUPT_PIN):
cg.add(var.set_interrupt_pin(await cg.gpio_pin_expression(interrupt_pin)))
if reset_pin := config.get(CONF_RESET_PIN):
cg.add(var.set_reset_pin(await cg.gpio_pin_expression(reset_pin)))

View file

@ -0,0 +1,113 @@
#include "cst816_touchscreen.h"
namespace esphome {
namespace cst816 {
void CST816Touchscreen::continue_setup_() {
if (this->interrupt_pin_ != nullptr) {
this->interrupt_pin_->setup();
this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
}
if (!this->read_byte(REG_CHIP_ID, &this->chip_id_)) {
this->mark_failed();
esph_log_e(TAG, "Failed to read chip id");
return;
}
switch (this->chip_id_) {
case CST820_CHIP_ID:
case CST716_CHIP_ID:
case CST816S_CHIP_ID:
case CST816D_CHIP_ID:
case CST816T_CHIP_ID:
break;
default:
this->mark_failed();
esph_log_e(TAG, "Unknown chip ID 0x%02X", this->chip_id_);
return;
}
this->write_byte(REG_IRQ_CTL, IRQ_EN_MOTION);
if (this->x_raw_max_ == this->x_raw_min_) {
this->x_raw_max_ = this->display_->get_native_width();
}
if (this->y_raw_max_ == this->y_raw_min_) {
this->y_raw_max_ = this->display_->get_native_height();
}
esph_log_config(TAG, "CST816 Touchscreen setup complete");
}
void CST816Touchscreen::update_button_state_(bool state) {
if (this->button_touched_ == state)
return;
this->button_touched_ = state;
for (auto *listener : this->button_listeners_)
listener->update_button(state);
}
void CST816Touchscreen::setup() {
esph_log_config(TAG, "Setting up CST816 Touchscreen...");
if (this->reset_pin_ != nullptr) {
this->reset_pin_->setup();
this->reset_pin_->digital_write(true);
delay(5);
this->reset_pin_->digital_write(false);
delay(5);
this->reset_pin_->digital_write(true);
this->set_timeout(30, [this] { this->continue_setup_(); });
} else {
this->continue_setup_();
}
}
void CST816Touchscreen::update_touches() {
uint8_t data[13];
if (!this->read_bytes(REG_STATUS, data, sizeof data)) {
this->status_set_warning();
return;
}
uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
if (num_of_touches == 0) {
this->update_button_state_(false);
return;
}
uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]);
uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]);
esph_log_v(TAG, "Read touch %d/%d", x, y);
if (x >= this->x_raw_max_) {
this->update_button_state_(true);
} else {
this->add_raw_touch_position_(0, x, y);
}
}
void CST816Touchscreen::dump_config() {
ESP_LOGCONFIG(TAG, "CST816 Touchscreen:");
LOG_I2C_DEVICE(this);
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
const char *name;
switch (this->chip_id_) {
case CST820_CHIP_ID:
name = "CST820";
break;
case CST816S_CHIP_ID:
name = "CST816S";
break;
case CST816D_CHIP_ID:
name = "CST816D";
break;
case CST716_CHIP_ID:
name = "CST716";
break;
case CST816T_CHIP_ID:
name = "CST816T";
break;
default:
name = "Unknown";
break;
}
ESP_LOGCONFIG(TAG, " Chip type: %s", name);
}
} // namespace cst816
} // namespace esphome

View file

@ -0,0 +1,60 @@
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/touchscreen/touchscreen.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome {
namespace cst816 {
static const char *const TAG = "cst816.touchscreen";
static const uint8_t REG_STATUS = 0x00;
static const uint8_t REG_TOUCH_NUM = 0x02;
static const uint8_t REG_XPOS_HIGH = 0x03;
static const uint8_t REG_XPOS_LOW = 0x04;
static const uint8_t REG_YPOS_HIGH = 0x05;
static const uint8_t REG_YPOS_LOW = 0x06;
static const uint8_t REG_DIS_AUTOSLEEP = 0xFE;
static const uint8_t REG_CHIP_ID = 0xA7;
static const uint8_t REG_FW_VERSION = 0xA9;
static const uint8_t REG_SLEEP = 0xE5;
static const uint8_t REG_IRQ_CTL = 0xFA;
static const uint8_t IRQ_EN_MOTION = 0x70;
static const uint8_t CST820_CHIP_ID = 0xB7;
static const uint8_t CST816S_CHIP_ID = 0xB4;
static const uint8_t CST816D_CHIP_ID = 0xB6;
static const uint8_t CST816T_CHIP_ID = 0xB5;
static const uint8_t CST716_CHIP_ID = 0x20;
class CST816ButtonListener {
public:
virtual void update_button(bool state) = 0;
};
class CST816Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
public:
void setup() override;
void update_touches() override;
void register_button_listener(CST816ButtonListener *listener) { this->button_listeners_.push_back(listener); }
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 continue_setup_();
void update_button_state_(bool state);
InternalGPIOPin *interrupt_pin_{};
GPIOPin *reset_pin_{};
uint8_t chip_id_{};
std::vector<CST816ButtonListener *> button_listeners_;
bool button_touched_{};
};
} // namespace cst816
} // namespace esphome

View file

@ -0,0 +1,36 @@
touchscreen:
- platform: cst816
id: my_touchscreen
interrupt_pin:
number: 21
reset_pin: GPIO16
transform:
mirror_x: false
mirror_y: false
swap_xy: false
i2c:
sda: 3
scl: 2
display:
- id: my_display
platform: ili9xxx
dimensions: 480x320
model: ST7796
cs_pin: 15
dc_pin: 20
reset_pin: 22
transform:
swap_xy: true
mirror_x: true
mirror_y: true
auto_clear_enabled: false
spi:
clk_pin: 14
mosi_pin: 13
binary_sensor:
- platform: cst816
name: Home Button