From cb48212defc5e8c5287c063492b2b141986d13f2 Mon Sep 17 00:00:00 2001 From: mvturnho Date: Tue, 21 May 2019 08:42:26 +0200 Subject: [PATCH] add threasholds to mpr121 component to change the default for all channels --- esphome/components/mpr121/__init__.py | 10 ++++++++-- esphome/components/mpr121/binary_sensor.py | 6 +++++- esphome/components/mpr121/mpr121.h | 13 +++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/esphome/components/mpr121/__init__.py b/esphome/components/mpr121/__init__.py index 41846d621c..1bd8a468a1 100644 --- a/esphome/components/mpr121/__init__.py +++ b/esphome/components/mpr121/__init__.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import i2c -from esphome.const import CONF_ID, CONF_RELEASE_DEBOUNCE, CONF_TOUCH_DEBOUNCE +from esphome.const import CONF_ID, CONF_RELEASE_DEBOUNCE, CONF_TOUCH_DEBOUNCE, CONF_TOUCH_THRESHOLD, CONF_RELEASE_THRESHOLD DEPENDENCIES = ['i2c'] AUTO_LOAD = ['binary_sensor'] @@ -14,7 +14,9 @@ MULTI_CONF = True CONFIG_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(MPR121Component), cv.Optional(CONF_RELEASE_DEBOUNCE): cv.All(cv.Coerce(int), cv.Range(min=0, max=7)), - cv.Optional(CONF_TOUCH_DEBOUNCE): cv.All(cv.Coerce(int), cv.Range(min=0, max=7)) + cv.Optional(CONF_TOUCH_DEBOUNCE): cv.All(cv.Coerce(int), cv.Range(min=0, max=7)), + cv.Optional(CONF_TOUCH_THRESHOLD): cv.All(cv.Coerce(int), cv.Range(min=0x05, max=0x30)), + cv.Optional(CONF_RELEASE_THRESHOLD): cv.All(cv.Coerce(int), cv.Range(min=0x05, max=0x30)), }).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x5A)) @@ -24,5 +26,9 @@ def to_code(config): cg.add(var.set_touch_debounce(config[CONF_TOUCH_DEBOUNCE])) if CONF_RELEASE_DEBOUNCE in config: cg.add(var.set_release_debounce(config[CONF_RELEASE_DEBOUNCE])) + if CONF_TOUCH_THRESHOLD in config: + cg.add(var.set_touch_threshold(config[CONF_TOUCH_THRESHOLD])) + if CONF_RELEASE_THRESHOLD in config: + cg.add(var.set_release_threshold(config[CONF_RELEASE_THRESHOLD])) yield cg.register_component(var, config) yield i2c.register_i2c_device(var, config) diff --git a/esphome/components/mpr121/binary_sensor.py b/esphome/components/mpr121/binary_sensor.py index a3a733cdf9..90e2630ba2 100644 --- a/esphome/components/mpr121/binary_sensor.py +++ b/esphome/components/mpr121/binary_sensor.py @@ -20,10 +20,14 @@ def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield binary_sensor.register_binary_sensor(var, config) + hub = yield cg.get_variable(config[CONF_MPR121_ID]) cg.add(var.set_channel(config[CONF_CHANNEL])) if CONF_TOUCH_THRESHOLD in config: cg.add(var.set_touch_threshold(config[CONF_TOUCH_THRESHOLD])) + else: + cg.add(var.set_touch_threshold(hub.get_touch_threshold())) if CONF_RELEASE_THRESHOLD in config: cg.add(var.set_release_threshold(config[CONF_RELEASE_THRESHOLD])) - hub = yield cg.get_variable(config[CONF_MPR121_ID]) + else: + cg.add(var.set_release_threshold(hub.get_release_threshold())) cg.add(hub.register_channel(var)) diff --git a/esphome/components/mpr121/mpr121.h b/esphome/components/mpr121/mpr121.h index 9948f4701c..2053333fee 100644 --- a/esphome/components/mpr121/mpr121.h +++ b/esphome/components/mpr121/mpr121.h @@ -7,6 +7,9 @@ namespace esphome { namespace mpr121 { +#define DEFAULT_TOUCH_THRESHOLD 12 +#define DEFAULT_RELEASE_THRESHOLD 6 + enum { MPR121_TOUCHSTATUS_L = 0x00, MPR121_TOUCHSTATUS_H = 0x01, @@ -57,8 +60,8 @@ class MPR121Channel : public binary_sensor::BinarySensor { protected: uint8_t channel_{0}; - uint8_t touch_threshold_{12}; - uint8_t release_threshold{6}; + uint8_t touch_threshold_{DEFAULT_TOUCH_THRESHOLD}; + uint8_t release_threshold{DEFAULT_RELEASE_THRESHOLD}; }; class MPR121Component : public Component, public i2c::I2CDevice { @@ -66,6 +69,10 @@ class MPR121Component : public Component, public i2c::I2CDevice { void register_channel(MPR121Channel *channel) { this->channels_.push_back(channel); } void set_touch_debounce(uint8_t debounce); void set_release_debounce(uint8_t debounce); + void set_touch_threshold(uint8_t touch_threshold) { this->touch_threshold_ = touch_threshold; }; + void set_release_threshold(uint8_t release_threshold) { this->release_threshold = release_threshold; }; + uint8_t get_touch_threshold() { return this->touch_threshold_; }; + uint8_t get_release_threshold() { return this->release_threshold; }; void setup() override; void dump_config() override; float get_setup_priority() const override { return setup_priority::DATA; } @@ -75,6 +82,8 @@ class MPR121Component : public Component, public i2c::I2CDevice { std::vector channels_{}; uint16_t currtouched_{0}; uint8_t debounce_{0}; + uint8_t touch_threshold_{DEFAULT_TOUCH_THRESHOLD}; + uint8_t release_threshold{DEFAULT_RELEASE_THRESHOLD}; enum ErrorCode { NONE = 0, COMMUNICATION_FAILED,