ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
cstxxx.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
8#include "esphome/core/gpio.h"
9#include "esphome/core/hal.h"
11#include "esphome/core/log.h"
12
13namespace esphome::cstxxx {
14
15static constexpr uint16_t REG_TOUCH_DATA = 0xD000;
16static constexpr uint16_t REG_FINGER_COUNT = 0xD005;
17static constexpr uint16_t REG_COMMAND_MODE = 0xD101;
18static constexpr uint16_t REG_NORMAL_MODE = 0xD109;
19static constexpr uint16_t REG_RESOLUTION = 0xD1F8;
20static constexpr uint16_t REG_FW_CHECK_CODE = 0xD1FC;
21static constexpr uint16_t REG_CHIP_INFO = 0xD204;
22static constexpr uint16_t REG_FW_VERSION = 0xD208;
23
24static constexpr uint8_t FINGER_COUNT_IDX = REG_FINGER_COUNT - REG_TOUCH_DATA;
25static constexpr uint8_t SYNC_IDX = FINGER_COUNT_IDX + 1;
26static constexpr uint8_t SYNC_BYTE = 0xAB;
27static constexpr uint16_t FW_CHECK_CODE = 0xCACA;
28
30constexpr size_t report_size(uint8_t points) { return points * 5 + 2; }
31
33 uint8_t id;
34 uint8_t event;
35 uint16_t x;
36 uint16_t y;
37 uint8_t z;
38};
39
43inline TouchRecord decode_touch(const uint8_t *report, uint8_t index) {
44 const uint8_t *r = report + index * 5 + (index == 0 ? 0 : 2);
45 return {
46 .id = high_nibble(r[0]),
47 .event = low_nibble(r[0]),
48 .x = encode_uint12(r[1], high_nibble(r[3])),
49 .y = encode_uint12(r[2], low_nibble(r[3])),
50 .z = r[4],
51 };
52}
53
55 public:
57 void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; }
58
59 protected:
62 if (this->interrupt_pin_ == nullptr)
63 return;
64 this->interrupt_pin_->setup();
65 this->attach_interrupt_(this->interrupt_pin_, type);
66 }
68 bool ESPHOME_ALWAYS_INLINE read_words_(uint16_t reg, uint16_t &low, uint16_t &high) {
69 uint16_t words[2];
70 if (this->read_register16(reg, reinterpret_cast<uint8_t *>(words), sizeof(words)) != i2c::ERROR_OK)
71 return false;
72 low = convert_little_endian(words[0]);
73 high = convert_little_endian(words[1]);
74 return true;
75 }
78 return this->setup_check(this->write_register16(REG_COMMAND_MODE, nullptr, 0) == i2c::ERROR_OK,
79 LOG_STR("Failed to enter command mode"));
80 }
83 uint16_t boot_time, check_code;
84 if (!this->setup_check(this->read_words_(REG_FW_CHECK_CODE, boot_time, check_code)))
85 return false;
86 if (check_code == FW_CHECK_CODE)
87 return true;
88 this->fail_check_code_(check_code);
89 return false;
90 }
92 void fail_check_code_(uint16_t check_code);
93
97};
98
99} // namespace esphome::cstxxx
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void fail_check_code_(uint16_t check_code)
Logs a wrong firmware check code and marks the component failed.
Definition cstxxx.cpp:8
bool ESPHOME_ALWAYS_INLINE read_words_(uint16_t reg, uint16_t &low, uint16_t &high)
Reads two little-endian words starting at reg.
Definition cstxxx.h:68
bool check_firmware_()
Checks the firmware check code, marking the component failed if it is unreadable or wrong.
Definition cstxxx.h:82
void set_interrupt_pin(InternalGPIOPin *pin)
Definition cstxxx.h:56
bool enter_command_mode_()
Enters command mode, marking the component failed if the write fails.
Definition cstxxx.h:77
void setup_interrupt_pin_(gpio::InterruptType type)
Sets up the interrupt pin, if configured, to fire on type.
Definition cstxxx.h:61
void set_reset_pin(GPIOPin *pin)
Definition cstxxx.h:57
InternalGPIOPin * interrupt_pin_
Definition cstxxx.h:94
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const
write an array of bytes to a specific register in the I²C device
Definition i2c.cpp:83
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:62
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.
uint8_t type
constexpr size_t report_size(uint8_t points)
Bytes of a touch report holding points records; two status bytes follow the first record.
Definition cstxxx.h:30
TouchRecord decode_touch(const uint8_t *report, uint8_t index)
Decodes record index of a touch report.
Definition cstxxx.h:43
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
constexpr T convert_little_endian(T val)
Convert a value between host byte order and little endian (least significant byte first) order.
Definition helpers.h:1369
constexpr uint8_t high_nibble(uint8_t byte)
The high 4 bits of a byte, e.g. high_nibble(0xA5) == 0xA.
Definition helpers.h:1217
constexpr uint16_t encode_uint12(uint8_t high_bits, uint8_t low_bits)
Encode a 12-bit value from its high 8 and low 4 bits, e.g.
Definition helpers.h:1223
constexpr uint8_t low_nibble(uint8_t byte)
The low 4 bits of a byte, e.g. low_nibble(0xA5) == 0x5.
Definition helpers.h:1219