diff --git a/esphome/components/mpr121/__init__.py b/esphome/components/mpr121/__init__.py index 9e1bd3726d..04d35bee87 100644 --- a/esphome/components/mpr121/__init__.py +++ b/esphome/components/mpr121/__init__.py @@ -2,6 +2,8 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import i2c from esphome.const import CONF_ID +from mpr121_const import CONF_RELEASE_DEBOUNCE, CONF_TOUCH_DEBOUNCE, \ + CONF_TOUCH_THRESHOLD, CONF_RELEASE_THRESHOLD DEPENDENCIES = ['i2c'] AUTO_LOAD = ['binary_sensor'] @@ -13,10 +15,22 @@ 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.All(cv.Coerce(int), cv.Range(min=0, max=7)), + cv.Optional(CONF_TOUCH_THRESHOLD, default=0x06): cv.All(cv.Coerce(int), cv.Range(min=0x05, max=0x30)), + cv.Optional(CONF_RELEASE_THRESHOLD, default=0x0b): cv.All(cv.Coerce(int), cv.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]) + if CONF_TOUCH_DEBOUNCE in 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 100dacd6dd..ad544d1f64 100644 --- a/esphome/components/mpr121/binary_sensor.py +++ b/esphome/components/mpr121/binary_sensor.py @@ -3,6 +3,7 @@ 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 mpr121_const import CONF_TOUCH_THRESHOLD, CONF_RELEASE_THRESHOLD DEPENDENCIES = ['mpr121'] MPR121Channel = mpr121_ns.class_('MPR121Channel', binary_sensor.BinarySensor) @@ -11,14 +12,24 @@ 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.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)), }) 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_MPR121_ID]) 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])) + else: + cg.add(var.set_release_threshold(hub.get_release_threshold())) cg.add(hub.register_channel(var)) diff --git a/esphome/components/mpr121/mpr121.cpp b/esphome/components/mpr121/mpr121.cpp index c43a6319f4..7e47c51915 100644 --- a/esphome/components/mpr121/mpr121.cpp +++ b/esphome/components/mpr121/mpr121.cpp @@ -18,9 +18,9 @@ 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->get_channel(), channel->get_touch_threshold()); + this->write_byte(MPR121_RELEASETH_0 + 2 * channel->get_channel(), channel->get_release_threshold()); } this->write_byte(MPR121_MHDR, 0x01); this->write_byte(MPR121_NHDR, 0x01); @@ -44,6 +44,21 @@ 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; + ESP_LOGD(TAG, "debounce:%02x", this->debounce_); +} + +void MPR121Component::set_release_debounce(uint8_t debounce) { + uint8_t mask = debounce & 0x0f; + this->debounce_ &= 0xf0; + this->debounce_ |= mask; + ESP_LOGD(TAG, "debounce:%02x", this->debounce_); +}; + 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..5ddcc6e4bc 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, @@ -49,14 +52,27 @@ class MPR121Channel : public binary_sensor::BinarySensor { public: void set_channel(uint8_t channel) { channel_ = channel; } void process(uint16_t data) { this->publish_state(static_cast(data & (1 << this->channel_))); } + int get_channel() { return 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; }; + uint8_t get_touch_threshold() { return this->touch_threshold_; }; + uint8_t get_release_threshold() { return this->release_threshold_; }; protected: uint8_t channel_{0}; + uint8_t touch_threshold_{DEFAULT_TOUCH_THRESHOLD}; + uint8_t release_threshold_{DEFAULT_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 +80,9 @@ class MPR121Component : public Component, public i2c::I2CDevice { protected: std::vector channels_{}; + uint8_t debounce_{0}; + uint8_t touch_threshold_{DEFAULT_TOUCH_THRESHOLD}; + uint8_t release_threshold_{DEFAULT_RELEASE_THRESHOLD}; enum ErrorCode { NONE = 0, COMMUNICATION_FAILED, diff --git a/esphome/components/mpr121/mpr121_const.py b/esphome/components/mpr121/mpr121_const.py new file mode 100644 index 0000000000..0118950e5a --- /dev/null +++ b/esphome/components/mpr121/mpr121_const.py @@ -0,0 +1,4 @@ +CONF_TOUCH_THRESHOLD = "touch_threshold" +CONF_RELEASE_THRESHOLD = "release_threshold" +CONF_TOUCH_DEBOUNCE = "touch_debounce" +CONF_RELEASE_DEBOUNCE = "release_debounce"