From b15fbae7f296d7fc9bc1f01d57079b21a44a4e71 Mon Sep 17 00:00:00 2001 From: Michiel van Turnhout Date: Mon, 27 May 2019 12:43:15 +0200 Subject: [PATCH] fixed issues --- esphome/components/mpr121/__init__.py | 12 ++++-------- esphome/components/mpr121/binary_sensor.py | 4 ++-- esphome/components/mpr121/mpr121.cpp | 6 ++++-- esphome/components/mpr121/mpr121.h | 11 +++++------ 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/esphome/components/mpr121/__init__.py b/esphome/components/mpr121/__init__.py index 86d92bb28e..e02756b0dd 100644 --- a/esphome/components/mpr121/__init__.py +++ b/esphome/components/mpr121/__init__.py @@ -27,13 +27,9 @@ CONFIG_SCHEMA = cv.Schema({ 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])) + 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 00ea19b6c8..a1c0d97c6e 100644 --- a/esphome/components/mpr121/binary_sensor.py +++ b/esphome/components/mpr121/binary_sensor.py @@ -12,8 +12,8 @@ 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)), + cv.Optional(CONF_TOUCH_THRESHOLD): cv.int_range(min=0x05, max=0x30), + cv.Optional(CONF_RELEASE_THRESHOLD): cv.int_range(min=0x05, max=0x30), }) diff --git a/esphome/components/mpr121/mpr121.cpp b/esphome/components/mpr121/mpr121.cpp index 2ba6161cd5..a24a703306 100644 --- a/esphome/components/mpr121/mpr121.cpp +++ b/esphome/components/mpr121/mpr121.cpp @@ -19,8 +19,10 @@ void MPR121Component::setup() { // set touch sensitivity for all 12 channels 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_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); diff --git a/esphome/components/mpr121/mpr121.h b/esphome/components/mpr121/mpr121.h index 6ced102ada..3388f04926 100644 --- a/esphome/components/mpr121/mpr121.h +++ b/esphome/components/mpr121/mpr121.h @@ -46,19 +46,18 @@ 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_))); } - 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}; optional touch_threshold_{}; - uint8_t release_threshold_{DEFAULT_RELEASE_THRESHOLD}; + optional release_threshold_{}; }; class MPR121Component : public Component, public i2c::I2CDevice { @@ -78,8 +77,8 @@ class MPR121Component : public Component, public i2c::I2CDevice { protected: std::vector channels_{}; uint8_t debounce_{0}; - uint8_t touch_threshold_{12}; - uint8_t release_threshold_{6}; + uint8_t touch_threshold_{}; + uint8_t release_threshold_{}; enum ErrorCode { NONE = 0, COMMUNICATION_FAILED,