15static constexpr uint16_t REG_CLEAR_INTERRUPT = 0x0002;
16static constexpr uint16_t REG_CPU_START = 0x0004;
17static constexpr uint16_t REG_STATUS = 0x0020;
18static constexpr uint16_t REG_VERSION = 0x0026;
19static constexpr uint16_t REG_TOUCH_START = 0x0046;
20static constexpr uint16_t REG_POINT_MODE = 0x0050;
21static constexpr uint16_t REG_HDP_STATUS = 0x02FC;
22static constexpr uint16_t REG_HDP = 0x0300;
25static constexpr uint8_t STATUS_POINT = 0x01;
26static constexpr uint8_t STATUS_GESTURE = 0x02;
27static constexpr uint8_t STATUS_AUX = 0x08;
29static constexpr uint8_t STATUS_BIOS = 0x40;
30static constexpr uint8_t STATUS_CPU = 0x20;
31static constexpr uint8_t STATUS_RUNNING = 0x08;
33static constexpr uint8_t HDP_STATUS_DONE = 0x82;
34static constexpr uint8_t HDP_STATUS_MORE = 0x00;
36static constexpr uint8_t MAX_CONTACT_ID = 0x0A;
37static constexpr uint8_t GESTURE_REPORT_ID = 0xF6;
38static constexpr size_t REPORT_HEADER_LENGTH = 4;
39static constexpr size_t CONTACT_LENGTH = 6;
40static constexpr size_t MAX_REPORT_LENGTH = REPORT_HEADER_LENGTH + 10 * CONTACT_LENGTH;
41static constexpr uint8_t MAX_DRAIN_READS = 8;
43static constexpr uint32_t RESET_PULSE_WIDTH =
45static constexpr uint32_t RESET_DELAY = 20;
48 const uint8_t command[] = {
static_cast<uint8_t
>(
reg),
static_cast<uint8_t
>(
reg >> 8u)};
57 const uint8_t command[]{
58 static_cast<uint8_t
>(
reg),
59 static_cast<uint8_t
>(
reg >> 8u),
60 static_cast<uint8_t
>(
value),
61 static_cast<uint8_t
>(
value >> 8u),
87 LOG_STR(
"Failed to read firmware version")))
99 for (uint8_t attempt = 0; attempt != MAX_DRAIN_READS; attempt++) {
103 if (
status[5] == HDP_STATUS_DONE)
108 uint8_t remaining[MAX_REPORT_LENGTH];
112 ESP_LOGW(TAG,
"Invalid or unfinished report");
124 const uint8_t status_low =
status[0];
125 const uint8_t status_high =
status[1];
128 if (status_high & STATUS_BIOS) {
133 if (status_high & STATUS_CPU) {
139 if ((status_high & STATUS_RUNNING) &&
length == 0) {
142 if (!(status_low & (STATUS_POINT | STATUS_GESTURE))) {
143 const bool needs_clear = (status_high & STATUS_RUNNING) && (status_low & STATUS_AUX);
149 (
length - REPORT_HEADER_LENGTH) % CONTACT_LENGTH != 0) {
150 ESP_LOGW(TAG,
"Invalid report length: %u",
length);
155 uint8_t report[MAX_REPORT_LENGTH];
161 if (!(status_low & STATUS_POINT) ||
162 (
length > REPORT_HEADER_LENGTH && report[REPORT_HEADER_LENGTH] == GESTURE_REPORT_ID)) {
167 for (
size_t offset = REPORT_HEADER_LENGTH; offset <
length; offset += CONTACT_LENGTH) {
168 if (report[offset] > MAX_CONTACT_ID) {
173 for (
size_t offset = REPORT_HEADER_LENGTH; offset <
length; offset += CONTACT_LENGTH) {
174 const uint8_t *contact = report + offset;
193 "SPD2010 Touchscreen:\n"
194 " Raw X range: %d-%d\n"
195 " Raw Y range: %d-%d",
197 LOG_I2C_DEVICE(
this);
void status_set_warning()
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
void status_clear_warning()
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
virtual void digital_write(bool value)=0
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.
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
bool read_register_(uint16_t reg, uint8_t *data, size_t length) const
InternalGPIOPin * interrupt_pin_
void update_touches() override
void dump_config() override
bool clear_interrupt_() const
bool write_command_(uint16_t reg, uint16_t value) const
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)
@ ERROR_OK
No error found during execution of method.
ESPHOME_LOG_TAG(TAG, "spd2010.touchscreen")
constexpr uint8_t high_nibble(uint8_t byte)
The high 4 bits of a byte, e.g. high_nibble(0xA5) == 0xA.
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
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.
constexpr uint8_t low_nibble(uint8_t byte)
The low 4 bits of a byte, e.g. low_nibble(0xA5) == 0x5.