TTP229 BSF support (#547)

* TTP229 BSF support

* MULTI_CONF
This commit is contained in:
Otto Winter 2019-05-13 13:11:10 +02:00 committed by GitHub
commit 5bb963fa82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 138 additions and 0 deletions

View file

@ -0,0 +1,29 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.const import CONF_ID, CONF_SDO_PIN, CONF_SCL_PIN
DEPENDENCIES = ['i2c']
AUTO_LOAD = ['binary_sensor']
CONF_TTP229_ID = 'ttp229_id'
ttp229_bsf_ns = cg.esphome_ns.namespace('ttp229_bsf')
TTP229BSFComponent = ttp229_bsf_ns.class_('TTP229BSFComponent', cg.Component)
MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(TTP229BSFComponent),
cv.Required(CONF_SDO_PIN): pins.gpio_input_pullup_pin_schema,
cv.Required(CONF_SCL_PIN): pins.gpio_output_pin_schema,
}).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
sdo = yield cg.gpio_pin_expression(config[CONF_SDO_PIN])
cg.add(var.set_sdo_pin(sdo))
scl = yield cg.gpio_pin_expression(config[CONF_SCL_PIN])
cg.add(var.set_scl_pin(scl))

View file

@ -0,0 +1,23 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import CONF_CHANNEL, CONF_ID
from . import ttp229_bsf_ns, TTP229BSFComponent, CONF_TTP229_ID
DEPENDENCIES = ['ttp229_bsf']
TTP229Channel = ttp229_bsf_ns.class_('TTP229Channel', binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(TTP229Channel),
cv.GenerateID(CONF_TTP229_ID): cv.use_id(TTP229BSFComponent),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=15),
})
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield binary_sensor.register_binary_sensor(var, config)
cg.add(var.set_channel(config[CONF_CHANNEL]))
hub = yield cg.get_variable(config[CONF_TTP229_ID])
cg.add(hub.register_channel(var))

View file

@ -0,0 +1,23 @@
#include "ttp229_bsf.h"
#include "esphome/core/log.h"
namespace esphome {
namespace ttp229_bsf {
static const char *TAG = "ttp229_bsf";
void TTP229BSFComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up ttp229_bsf... ");
this->sdo_pin_->setup();
this->scl_pin_->setup();
this->scl_pin_->digital_write(true);
delay(2);
}
void TTP229BSFComponent::dump_config() {
ESP_LOGCONFIG(TAG, "ttp229:");
LOG_PIN(" SCL pin: ", this->scl_pin_);
LOG_PIN(" SDO pin: ", this->sdo_pin_);
}
} // namespace ttp229_bsf
} // namespace esphome

View file

@ -0,0 +1,53 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
namespace esphome {
namespace ttp229_bsf {
class TTP229BSFChannel : public binary_sensor::BinarySensor {
public:
void set_channel(uint8_t channel) { channel_ = channel; }
void process(uint16_t data) { this->publish_state(data & (1 << this->channel_)); }
protected:
uint8_t channel_;
};
class TTP229BSFComponent : public Component {
public:
void set_sdo_pin(GPIOPin *sdo_pin) { sdo_pin_ = sdo_pin; }
void set_scl_pin(GPIOPin *scl_pin) { scl_pin_ = scl_pin; }
void register_channel(TTP229BSFChannel *channel) { this->channels_.push_back(channel); }
void setup() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void loop() override {
// check datavalid if sdo is high
if (!this->sdo_pin_->digital_read()) {
return;
}
uint16_t touched = 0;
for (uint8_t i = 0; i < 16; i++) {
this->scl_pin_->digital_write(false);
delayMicroseconds(2); // 500KHz
bool bitval = !this->sdo_pin_->digital_read();
this->scl_pin_->digital_write(true);
delayMicroseconds(2); // 500KHz
touched |= uint16_t(bitval) << i;
}
for (auto *channel : this->channels_) {
channel->process(touched);
}
}
protected:
GPIOPin *sdo_pin_;
GPIOPin *scl_pin_;
std::vector<TTP229BSFChannel *> channels_{};
};
} // namespace ttp229_bsf
} // namespace esphome

View file

@ -11,6 +11,7 @@ ttp229_lsf_ns = cg.esphome_ns.namespace('ttp229_lsf')
TTP229LSFComponent = ttp229_lsf_ns.class_('TTP229LSFComponent', cg.Component, i2c.I2CDevice)
MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(TTP229LSFComponent),
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x57))

View file

@ -347,7 +347,9 @@ CONF_SAMSUNG = 'samsung'
CONF_SCAN = 'scan'
CONF_SCAN_INTERVAL = 'scan_interval'
CONF_SCL = 'scl'
CONF_SCL_PIN = 'scl_pin'
CONF_SDA = 'sda'
CONF_SDO_PIN = 'sdo_pin'
CONF_SECOND = 'second'
CONF_SECONDS = 'seconds'
CONF_SEGMENTS = 'segments'

View file

@ -189,6 +189,9 @@ binary_sensor:
- platform: ttp229_lsf
channel: 1
name: TTP229 LSF Test
- platform: ttp229_bsf
channel: 1
name: TTP229 BSF Test
- platform: custom
lambda: |-
auto s = new CustomBinarySensor();
@ -392,3 +395,7 @@ servo:
output: out
ttp229_lsf:
ttp229_bsf:
sdo_pin: D0
scl_pin: D1