forked from esphome/esphome-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TTP229-LSF i2c device support (esphome#560)
## Description: **Related issue (if applicable):** fixes <link to issue> **Pull request in [esphome](https://github.com/esphome/esphome) with python changes (if applicable):** esphome/esphome#<esphome PR number goes here> **Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here> ## Checklist: - [ ] The code change is tested and works locally. - [ ] The code change follows the [standards](https://esphome.io/guides/contributing.html#contributing-to-esphome-core) If user exposed functionality or configuration variables are added/changed: - [ ] Documentation added/updated in [esphome-docs](https://github.com/esphome/esphome-docs).
- Loading branch information
1 parent
8fa12de
commit b3c7e31
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "esphome/defines.h" | ||
#ifdef USE_TTP229_LSF | ||
|
||
#include "esphome/binary_sensor/ttp229_lsf_sensor.h" | ||
#include "esphome/log.h" | ||
#include "esphome/helpers.h" | ||
|
||
ESPHOME_NAMESPACE_BEGIN | ||
|
||
namespace binary_sensor { | ||
|
||
static const char *TAG = "binary_sensor.ttp229"; | ||
|
||
TTP229Channel::TTP229Channel(const std::string &name, int channel_num) : BinarySensor(name), channel_(channel_num) {} | ||
|
||
void TTP229Channel::process(uint16_t data) { this->publish_state(data & (1 << this->channel_)); } | ||
|
||
TTP229LSFComponent::TTP229LSFComponent(I2CComponent *parent, uint8_t address) : I2CDevice(parent, address) {} | ||
|
||
void TTP229LSFComponent::setup() { | ||
ESP_LOGCONFIG(TAG, "Setting up ttp229..."); | ||
if (!this->parent_->raw_request_from(this->address_, 2)) { | ||
this->error_code_ = COMMUNICATION_FAILED; | ||
this->mark_failed(); | ||
return; | ||
} | ||
} | ||
|
||
void TTP229LSFComponent::dump_config() { | ||
ESP_LOGCONFIG(TAG, "ttp229:"); | ||
LOG_I2C_DEVICE(this); | ||
switch (this->error_code_) { | ||
case COMMUNICATION_FAILED: | ||
ESP_LOGE(TAG, "Communication with TTP229 failed!"); | ||
break; | ||
case NONE: | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
float TTP229LSFComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } | ||
|
||
TTP229Channel *TTP229LSFComponent::add_channel(binary_sensor::TTP229Channel *channel) { | ||
this->channels_.push_back(channel); | ||
return channel; | ||
} | ||
|
||
void TTP229LSFComponent::loop() { | ||
uint16_t touched = 0; | ||
if (!this->parent_->raw_receive_16(this->address_, &touched, 1)) { | ||
this->status_set_warning(); | ||
return; | ||
} | ||
this->status_clear_warning(); | ||
touched = reverse_bits_16(touched); | ||
for (auto *channel : this->channels_) { | ||
channel->process(touched); | ||
} | ||
} | ||
|
||
} // namespace binary_sensor | ||
|
||
ESPHOME_NAMESPACE_END | ||
|
||
#endif // USE_TTP229_LSF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#ifndef ESPHOME_BINARY_SENSOR_TTP229_LSF_H | ||
#define ESPHOME_BINARY_SENSOR_TTP229_LSF_H | ||
|
||
#include "esphome/defines.h" | ||
|
||
#ifdef USE_TTP229_LSF | ||
|
||
#include "esphome/binary_sensor/binary_sensor.h" | ||
#include "esphome/i2c_component.h" | ||
#include "esphome/component.h" | ||
|
||
ESPHOME_NAMESPACE_BEGIN | ||
|
||
namespace binary_sensor { | ||
|
||
class TTP229Channel : public binary_sensor::BinarySensor { | ||
public: | ||
TTP229Channel(const std::string &name, int channel_num); | ||
void process(uint16_t data); | ||
|
||
protected: | ||
int channel_; | ||
}; | ||
|
||
class TTP229LSFComponent : public Component, public I2CDevice { | ||
public: | ||
TTP229LSFComponent(I2CComponent *parent, uint8_t address); | ||
binary_sensor::TTP229Channel *add_channel(binary_sensor::TTP229Channel *channel); | ||
void setup() override; | ||
void dump_config() override; | ||
float get_setup_priority() const override; | ||
void loop() override; | ||
|
||
protected: | ||
std::vector<TTP229Channel *> channels_{}; | ||
enum ErrorCode { | ||
NONE = 0, | ||
COMMUNICATION_FAILED, | ||
} error_code_{NONE}; | ||
}; | ||
|
||
} // namespace binary_sensor | ||
|
||
ESPHOME_NAMESPACE_END | ||
|
||
#endif // USE_TTP229_LSF | ||
|
||
#endif // ESPHOME_BINARY_SENSOR_TTP229_LSF_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters