Skip to content

Commit

Permalink
TTP229-LSF i2c device support (esphome#560)
Browse files Browse the repository at this point in the history
## 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
mvturnho authored and OttoWinter committed Mar 31, 2019
1 parent 8fa12de commit b3c7e31
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/esphome/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,12 @@ binary_sensor::MPR121Component *Application::make_mpr121(uint8_t address) {
}
#endif

#ifdef USE_TTP229_LSF
binary_sensor::TTP229LSFComponent *Application::make_ttp229_lsf(uint8_t address) {
return this->register_component(new TTP229LSFComponent(this->i2c_, address));
}
#endif

#ifdef USE_ULN2003
stepper::ULN2003 *Application::make_uln2003(const GPIOOutputPin &pin_a, const GPIOOutputPin &pin_b,
const GPIOOutputPin &pin_c, const GPIOOutputPin &pin_d) {
Expand Down
5 changes: 5 additions & 0 deletions src/esphome/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "esphome/binary_sensor/status_binary_sensor.h"
#include "esphome/binary_sensor/template_binary_sensor.h"
#include "esphome/binary_sensor/mpr121_sensor.h"
#include "esphome/binary_sensor/ttp229_lsf_sensor.h"
#include "esphome/cover/cover.h"
#include "esphome/cover/mqtt_cover_component.h"
#include "esphome/cover/template_cover.h"
Expand Down Expand Up @@ -426,6 +427,10 @@ class Application {
binary_sensor::MPR121Component *make_mpr121(uint8_t address = 0x5A);
#endif

#ifdef USE_TTP229_LSF
binary_sensor::TTP229LSFComponent *make_ttp229_lsf(uint8_t address = 0x57);
#endif

/* ____ _____ _ _ ____ ___ ____
* / ___|| ____| \ | / ___| / _ \| _ \
* \___ \| _| | \| \___ \| | | | |_) |
Expand Down
66 changes: 66 additions & 0 deletions src/esphome/binary_sensor/ttp229_lsf_sensor.cpp
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
48 changes: 48 additions & 0 deletions src/esphome/binary_sensor/ttp229_lsf_sensor.h
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
1 change: 1 addition & 0 deletions src/esphome/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
#define USE_COPY_OUTPUT
#define USE_WIFI_INFO_TEXT_SENSOR
#define USE_SERVO
#define USE_TTP229_LSF
#endif

#ifdef USE_REMOTE_RECEIVER
Expand Down

0 comments on commit b3c7e31

Please sign in to comment.