diff --git a/esphome/components/mpr121/__init__.py b/esphome/components/mpr121/__init__.py index 9e1bd3726d..b1ef9eaef5 100644 --- a/esphome/components/mpr121/__init__.py +++ b/esphome/components/mpr121/__init__.py @@ -3,6 +3,11 @@ import esphome.config_validation as cv from esphome.components import i2c from esphome.const import CONF_ID +CONF_TOUCH_THRESHOLD = "touch_threshold" +CONF_RELEASE_THRESHOLD = "release_threshold" +CONF_TOUCH_DEBOUNCE = "touch_debounce" +CONF_RELEASE_DEBOUNCE = "release_debounce" + DEPENDENCIES = ['i2c'] AUTO_LOAD = ['binary_sensor'] @@ -13,10 +18,18 @@ MPR121Component = mpr121_ns.class_('MPR121Component', cg.Component, i2c.I2CDevic MULTI_CONF = True CONFIG_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(MPR121Component), + cv.Optional(CONF_RELEASE_DEBOUNCE, default=0): cv.int_range(min=0, max=7), + cv.Optional(CONF_TOUCH_DEBOUNCE, default=0): cv.int_range(min=0, max=7), + cv.Optional(CONF_TOUCH_THRESHOLD, default=0x0b): cv.int_range(min=0x05, max=0x30), + cv.Optional(CONF_RELEASE_THRESHOLD, default=0x06): cv.int_range(min=0x05, max=0x30), }).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x5A)) def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) + cg.add(var.set_touch_debounce(config[CONF_TOUCH_DEBOUNCE])) + cg.add(var.set_release_debounce(config[CONF_RELEASE_DEBOUNCE])) + cg.add(var.set_touch_threshold(config[CONF_TOUCH_THRESHOLD])) + 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 100dacd6dd..dddfeb40e1 100644 --- a/esphome/components/mpr121/binary_sensor.py +++ b/esphome/components/mpr121/binary_sensor.py @@ -2,7 +2,8 @@ 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 mpr121_ns, MPR121Component, CONF_MPR121_ID +from . import mpr121_ns, MPR121Component, CONF_MPR121_ID, CONF_TOUCH_THRESHOLD, \ + CONF_RELEASE_THRESHOLD DEPENDENCIES = ['mpr121'] MPR121Channel = mpr121_ns.class_('MPR121Channel', binary_sensor.BinarySensor) @@ -11,14 +12,20 @@ CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(MPR121Channel), cv.GenerateID(CONF_MPR121_ID): cv.use_id(MPR121Component), cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=11), + cv.Optional(CONF_TOUCH_THRESHOLD): cv.int_range(min=0x05, max=0x30), + cv.Optional(CONF_RELEASE_THRESHOLD): cv.int_range(min=0x05, max=0x30), }) 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])) - hub = yield cg.get_variable(config[CONF_MPR121_ID]) + 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])) + cg.add(hub.register_channel(var)) diff --git a/esphome/components/mpr121/mpr121.cpp b/esphome/components/mpr121/mpr121.cpp index c43a6319f4..a24a703306 100644 --- a/esphome/components/mpr121/mpr121.cpp +++ b/esphome/components/mpr121/mpr121.cpp @@ -18,9 +18,11 @@ void MPR121Component::setup() { } // set touch sensitivity for all 12 channels - for (uint8_t i = 0; i < 12; i++) { - this->write_byte(MPR121_TOUCHTH_0 + 2 * i, 12); - this->write_byte(MPR121_RELEASETH_0 + 2 * i, 6); + for (auto *channel : this->channels_) { + this->write_byte(MPR121_TOUCHTH_0 + 2 * channel->channel_, + channel->touch_threshold_.value_or(this->touch_threshold_)); + this->write_byte(MPR121_RELEASETH_0 + 2 * channel->channel_, + channel->release_threshold_.value_or(this->release_threshold_)); } this->write_byte(MPR121_MHDR, 0x01); this->write_byte(MPR121_NHDR, 0x01); @@ -44,6 +46,19 @@ void MPR121Component::setup() { // start with first 5 bits of baseline tracking this->write_byte(MPR121_ECR, 0x8F); } + +void MPR121Component::set_touch_debounce(uint8_t debounce) { + uint8_t mask = debounce << 4; + this->debounce_ &= 0x0f; + this->debounce_ |= mask; +} + +void MPR121Component::set_release_debounce(uint8_t debounce) { + uint8_t mask = debounce & 0x0f; + this->debounce_ &= 0xf0; + this->debounce_ |= mask; +}; + void MPR121Component::dump_config() { ESP_LOGCONFIG(TAG, "MPR121:"); LOG_I2C_DEVICE(this); diff --git a/esphome/components/mpr121/mpr121.h b/esphome/components/mpr121/mpr121.h index d5a2ec3243..3388f04926 100644 --- a/esphome/components/mpr121/mpr121.h +++ b/esphome/components/mpr121/mpr121.h @@ -46,17 +46,29 @@ enum { }; class MPR121Channel : public binary_sensor::BinarySensor { + friend class MPR121Component; + public: void set_channel(uint8_t channel) { channel_ = channel; } void process(uint16_t data) { this->publish_state(static_cast(data & (1 << this->channel_))); } + 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; }; protected: uint8_t channel_{0}; + optional touch_threshold_{}; + optional release_threshold_{}; }; class MPR121Component : public Component, public i2c::I2CDevice { public: 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; } @@ -64,6 +76,9 @@ class MPR121Component : public Component, public i2c::I2CDevice { protected: std::vector channels_{}; + uint8_t debounce_{0}; + uint8_t touch_threshold_{}; + uint8_t release_threshold_{}; enum ErrorCode { NONE = 0, COMMUNICATION_FAILED,