ESPHome  2024.11.0
axs15231_touchscreen.cpp
Go to the documentation of this file.
1 #include "axs15231_touchscreen.h"
2 
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/log.h"
5 
6 namespace esphome {
7 namespace axs15231 {
8 
9 static const char *const TAG = "ax15231.touchscreen";
10 
11 constexpr 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  this->x_raw_max_ = this->display_->get_native_width();
34  this->y_raw_max_ = this->display_->get_native_height();
35  ESP_LOGCONFIG(TAG, "AXS15231 Touchscreen setup complete");
36 }
37 
39  i2c::ErrorCode err;
40  uint8_t data[8]{};
41 
42  err = this->write(AXS_READ_TOUCHPAD, sizeof(AXS_READ_TOUCHPAD), false);
43  ERROR_CHECK(err);
44  err = this->read(data, sizeof(data));
45  ERROR_CHECK(err);
46  this->status_clear_warning();
47  if (data[0] != 0) // no touches
48  return;
49  uint16_t x = encode_uint16(data[2] & 0xF, data[3]);
50  uint16_t y = encode_uint16(data[4] & 0xF, data[5]);
51  this->add_raw_touch_position_(0, x, y);
52 }
53 
55  ESP_LOGCONFIG(TAG, "AXS15231 Touchscreen:");
56  LOG_I2C_DEVICE(this);
57  LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
58  LOG_PIN(" Reset Pin: ", this->reset_pin_);
59  ESP_LOGCONFIG(TAG, " Width: %d", this->x_raw_max_);
60  ESP_LOGCONFIG(TAG, " Height: %d", this->y_raw_max_);
61 }
62 
63 } // namespace axs15231
64 } // namespace esphome
virtual void digital_write(bool value)=0
uint16_t x
Definition: tt21100.cpp:17
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
int get_native_height()
Get the native (original) height of the display in pixels.
Definition: display.h:223
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
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:186
uint16_t y
Definition: tt21100.cpp:18
int get_native_width()
Get the native (original) width of the display in pixels.
Definition: display.h:221
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.
Definition: touchscreen.cpp:12
void status_clear_warning()
Definition: component.cpp:166
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:183
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
Definition: touchscreen.cpp:74
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26