ESPHome  2024.10.2
ttp229_bsf.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 
7 #include <vector>
8 
9 namespace esphome {
10 namespace ttp229_bsf {
11 
13  public:
14  void set_channel(uint8_t channel) { channel_ = channel; }
15  void process(uint16_t data) { this->publish_state(data & (1 << this->channel_)); }
16 
17  protected:
18  uint8_t channel_;
19 };
20 
21 class TTP229BSFComponent : public Component {
22  public:
23  void set_sdo_pin(GPIOPin *sdo_pin) { sdo_pin_ = sdo_pin; }
24  void set_scl_pin(GPIOPin *scl_pin) { scl_pin_ = scl_pin; }
25  void register_channel(TTP229BSFChannel *channel) { this->channels_.push_back(channel); }
26  void setup() override;
27  void dump_config() override;
28  float get_setup_priority() const override { return setup_priority::DATA; }
29  void loop() override {
30  // check datavalid if sdo is high
31  if (!this->sdo_pin_->digital_read()) {
32  return;
33  }
34  uint16_t touched = 0;
35  for (uint8_t i = 0; i < 16; i++) {
36  this->scl_pin_->digital_write(false);
37  delayMicroseconds(2); // 500KHz
38  bool bitval = !this->sdo_pin_->digital_read();
39  this->scl_pin_->digital_write(true);
40  delayMicroseconds(2); // 500KHz
41 
42  touched |= uint16_t(bitval) << i;
43  }
44  for (auto *channel : this->channels_) {
45  channel->process(touched);
46  }
47  }
48 
49  protected:
52  std::vector<TTP229BSFChannel *> channels_{};
53 };
54 
55 } // namespace ttp229_bsf
56 } // namespace esphome
void setup()
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void set_scl_pin(GPIOPin *scl_pin)
Definition: ttp229_bsf.h:24
float get_setup_priority() const override
Definition: ttp229_bsf.h:28
void set_channel(uint8_t channel)
Definition: ttp229_bsf.h:14
void publish_state(bool state)
Publish a new state to the front-end.
void set_sdo_pin(GPIOPin *sdo_pin)
Definition: ttp229_bsf.h:23
void register_channel(TTP229BSFChannel *channel)
Definition: ttp229_bsf.h:25
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28