1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 07:08:20 +00:00

add threasholds to mpr121 component

to change the default for all channels
This commit is contained in:
mvturnho 2019-05-21 08:42:26 +02:00
parent fc7fb0d0de
commit cb48212def
3 changed files with 24 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import i2c 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'] DEPENDENCIES = ['i2c']
AUTO_LOAD = ['binary_sensor'] AUTO_LOAD = ['binary_sensor']
@ -14,7 +14,9 @@ MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema({ CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(MPR121Component), 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_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)) }).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])) cg.add(var.set_touch_debounce(config[CONF_TOUCH_DEBOUNCE]))
if CONF_RELEASE_DEBOUNCE in config: if CONF_RELEASE_DEBOUNCE in config:
cg.add(var.set_release_debounce(config[CONF_RELEASE_DEBOUNCE])) 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 cg.register_component(var, config)
yield i2c.register_i2c_device(var, config) yield i2c.register_i2c_device(var, config)

View File

@ -20,10 +20,14 @@ def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
yield binary_sensor.register_binary_sensor(var, config) 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])) cg.add(var.set_channel(config[CONF_CHANNEL]))
if CONF_TOUCH_THRESHOLD in config: if CONF_TOUCH_THRESHOLD in config:
cg.add(var.set_touch_threshold(config[CONF_TOUCH_THRESHOLD])) 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: if CONF_RELEASE_THRESHOLD in config:
cg.add(var.set_release_threshold(config[CONF_RELEASE_THRESHOLD])) 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)) cg.add(hub.register_channel(var))

View File

@ -7,6 +7,9 @@
namespace esphome { namespace esphome {
namespace mpr121 { namespace mpr121 {
#define DEFAULT_TOUCH_THRESHOLD 12
#define DEFAULT_RELEASE_THRESHOLD 6
enum { enum {
MPR121_TOUCHSTATUS_L = 0x00, MPR121_TOUCHSTATUS_L = 0x00,
MPR121_TOUCHSTATUS_H = 0x01, MPR121_TOUCHSTATUS_H = 0x01,
@ -57,8 +60,8 @@ class MPR121Channel : public binary_sensor::BinarySensor {
protected: protected:
uint8_t channel_{0}; uint8_t channel_{0};
uint8_t touch_threshold_{12}; uint8_t touch_threshold_{DEFAULT_TOUCH_THRESHOLD};
uint8_t release_threshold{6}; uint8_t release_threshold{DEFAULT_RELEASE_THRESHOLD};
}; };
class MPR121Component : public Component, public i2c::I2CDevice { 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 register_channel(MPR121Channel *channel) { this->channels_.push_back(channel); }
void set_touch_debounce(uint8_t debounce); void set_touch_debounce(uint8_t debounce);
void set_release_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 setup() override;
void dump_config() override; void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; } float get_setup_priority() const override { return setup_priority::DATA; }
@ -75,6 +82,8 @@ class MPR121Component : public Component, public i2c::I2CDevice {
std::vector<MPR121Channel *> channels_{}; std::vector<MPR121Channel *> channels_{};
uint16_t currtouched_{0}; uint16_t currtouched_{0};
uint8_t debounce_{0}; uint8_t debounce_{0};
uint8_t touch_threshold_{DEFAULT_TOUCH_THRESHOLD};
uint8_t release_threshold{DEFAULT_RELEASE_THRESHOLD};
enum ErrorCode { enum ErrorCode {
NONE = 0, NONE = 0,
COMMUNICATION_FAILED, COMMUNICATION_FAILED,