ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ns2009.cpp
Go to the documentation of this file.
1#include "ns2009.h"
2
4#include "esphome/core/log.h"
5
6namespace esphome::ns2009 {
7
8ESPHOME_LOG_TAG(TAG, "ns2009");
9
10static constexpr uint8_t GET_X = 0xC0;
11static constexpr uint8_t GET_Y = 0xD0;
12static constexpr uint8_t GET_Z = 0xE2;
13
15 if (!this->read_byte(GET_Z).has_value()) {
16 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
17 }
18}
19
21 auto data_z = this->read_byte(GET_Z);
22 if (!data_z.has_value()) {
23 ESP_LOGW(TAG, "failed to read %s, skipping update", LOG_STR_LITERAL("pressure"));
24 this->skip_update_ = true;
25 return;
26 }
27 uint8_t z = *data_z;
29 return;
30 }
31
32 auto x = this->read_axis_(GET_X);
33 if (!x.has_value()) {
34 ESP_LOGW(TAG, "failed to read %s, skipping update", LOG_STR_LITERAL("X position"));
35 this->skip_update_ = true;
36 return;
37 }
38 auto y = this->read_axis_(GET_Y);
39 if (!y.has_value()) {
40 ESP_LOGW(TAG, "failed to read %s, skipping update", LOG_STR_LITERAL("Y position"));
41 this->skip_update_ = true;
42 return;
43 }
44
45 ESP_LOGV(TAG, "X %4d Y %4d Z %3d", *x, *y, z);
46 this->add_raw_touch_position_(0, *x, *y, z);
47}
48
49optional<uint16_t> NS2009Component::read_axis_(uint8_t cmd) {
50 auto data = this->read_bytes<2>(cmd);
51 if (!data.has_value()) {
52 return {};
53 }
54 return encode_uint16((*data)[0], (*data)[1]) >> 4; // 12 bit followed by 4 0's
55}
56
58 ESP_LOGCONFIG(TAG,
59 "NS2009 Touchscreen:\n"
60 " Threshold: %u",
61 this->threshold_);
62 LOG_I2C_DEVICE(this);
63}
64
65} // namespace esphome::ns2009
void mark_failed()
Mark this component as failed.
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:217
optional< uint16_t > read_axis_(uint8_t cmd)
Read a 12 bit axis value, or nothing when the read fails.
Definition ns2009.cpp:49
uint8_t threshold_
The threshold at which a touch is registered.
Definition ns2009.h:22
void update_touches() override
Definition ns2009.cpp:20
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
bool z
Definition msa3xx.h:1
ESPHOME_LOG_TAG(TAG, "ns2009")
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:980
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6