ESPHome 2025.5.0
Loading...
Searching...
No Matches
axs15231_touchscreen.cpp
Go to the documentation of this file.
2
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace axs15231 {
8
9static const char *const TAG = "ax15231.touchscreen";
10
11constexpr static const uint8_t AXS_READ_TOUCHPAD[11] = {0xb5, 0xab, 0xa5, 0x5a, 0x0, 0x0, 0x0, 0x8};
12
13#define ERROR_CHECK(err) \
14 if ((err) != i2c::ERROR_OK) { \
15 this->status_set_warning("Failed to communicate"); \
16 return; \
17 }
18
20 ESP_LOGCONFIG(TAG, "Setting up AXS15231 Touchscreen...");
21 if (this->reset_pin_ != nullptr) {
22 this->reset_pin_->setup();
23 this->reset_pin_->digital_write(false);
24 delay(5);
25 this->reset_pin_->digital_write(true);
26 delay(10);
27 }
28 if (this->interrupt_pin_ != nullptr) {
30 this->interrupt_pin_->setup();
32 }
33 if (this->x_raw_max_ == 0) {
34 this->x_raw_max_ = this->display_->get_native_width();
35 }
36 if (this->y_raw_max_ == 0) {
37 this->y_raw_max_ = this->display_->get_native_height();
38 }
39 ESP_LOGCONFIG(TAG, "AXS15231 Touchscreen setup complete");
40}
41
44 uint8_t data[8]{};
45
46 err = this->write(AXS_READ_TOUCHPAD, sizeof(AXS_READ_TOUCHPAD), false);
47 ERROR_CHECK(err);
48 err = this->read(data, sizeof(data));
49 ERROR_CHECK(err);
51 if (data[0] != 0 || data[1] == 0) // no touches
52 return;
53 uint16_t x = encode_uint16(data[2] & 0xF, data[3]);
54 uint16_t y = encode_uint16(data[4] & 0xF, data[5]);
55 this->add_raw_touch_position_(0, x, y);
56}
57
59 ESP_LOGCONFIG(TAG, "AXS15231 Touchscreen:");
60 LOG_I2C_DEVICE(this);
61 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
62 LOG_PIN(" Reset Pin: ", this->reset_pin_);
63 ESP_LOGCONFIG(TAG, " Width: %d", this->x_raw_max_);
64 ESP_LOGCONFIG(TAG, " Height: %d", this->y_raw_max_);
65}
66
67} // namespace axs15231
68} // namespace esphome
void status_clear_warning()
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
int get_native_width()
Get the native (original) width of the display in pixels.
Definition display.h:221
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:223
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
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.
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
@ FLAG_INPUT
Definition gpio.h:18
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:191
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:28
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6