mirror of
https://github.com/esphome/esphome.git
synced 2025-03-15 15:18:16 +00:00
Merge pull request #6 from mvturnho/mpr121_threshold
added option for debounce and thresholds.
This commit is contained in:
commit
96b5c979ca
@ -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
|
from esphome.const import CONF_ID, CONF_RELEASE_DEBOUNCE, CONF_TOUCH_DEBOUNCE
|
||||||
|
|
||||||
DEPENDENCIES = ['i2c']
|
DEPENDENCIES = ['i2c']
|
||||||
AUTO_LOAD = ['binary_sensor']
|
AUTO_LOAD = ['binary_sensor']
|
||||||
@ -13,10 +13,16 @@ MPR121Component = mpr121_ns.class_('MPR121Component', cg.Component, i2c.I2CDevic
|
|||||||
MULTI_CONF = True
|
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_TOUCH_DEBOUNCE): cv.All(cv.Coerce(int), cv.Range(min=0, max=7))
|
||||||
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x5A))
|
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x5A))
|
||||||
|
|
||||||
|
|
||||||
def to_code(config):
|
def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
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]))
|
||||||
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)
|
||||||
|
@ -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 binary_sensor
|
from esphome.components import binary_sensor
|
||||||
from esphome.const import CONF_CHANNEL, CONF_ID
|
from esphome.const import CONF_CHANNEL, CONF_ID, CONF_TOUCH_THRESHOLD, CONF_RELEASE_THRESHOLD
|
||||||
from . import mpr121_ns, MPR121Component, CONF_MPR121_ID
|
from . import mpr121_ns, MPR121Component, CONF_MPR121_ID
|
||||||
|
|
||||||
DEPENDENCIES = ['mpr121']
|
DEPENDENCIES = ['mpr121']
|
||||||
@ -11,6 +11,8 @@ CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
|
|||||||
cv.GenerateID(): cv.declare_id(MPR121Channel),
|
cv.GenerateID(): cv.declare_id(MPR121Channel),
|
||||||
cv.GenerateID(CONF_MPR121_ID): cv.use_id(MPR121Component),
|
cv.GenerateID(CONF_MPR121_ID): cv.use_id(MPR121Component),
|
||||||
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=11),
|
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)),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -19,6 +21,9 @@ def to_code(config):
|
|||||||
yield binary_sensor.register_binary_sensor(var, config)
|
yield binary_sensor.register_binary_sensor(var, config)
|
||||||
|
|
||||||
cg.add(var.set_channel(config[CONF_CHANNEL]))
|
cg.add(var.set_channel(config[CONF_CHANNEL]))
|
||||||
|
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]))
|
||||||
hub = yield cg.get_variable(config[CONF_MPR121_ID])
|
hub = yield cg.get_variable(config[CONF_MPR121_ID])
|
||||||
cg.add(hub.register_channel(var))
|
cg.add(hub.register_channel(var))
|
||||||
|
@ -17,10 +17,9 @@ void MPR121Component::setup() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set touch sensitivity for all 12 channels
|
for (auto *channel : this->channels_) {
|
||||||
for (uint8_t i = 0; i < 12; i++) {
|
this->write_byte(MPR121_TOUCHTH_0 + 2 * channel->get_channel(), channel->get_touch_threshold());
|
||||||
this->write_byte(MPR121_TOUCHTH_0 + 2 * i, 12);
|
this->write_byte(MPR121_RELEASETH_0 + 2 * channel->get_channel(), channel->get_release_threshold());
|
||||||
this->write_byte(MPR121_RELEASETH_0 + 2 * i, 6);
|
|
||||||
}
|
}
|
||||||
this->write_byte(MPR121_MHDR, 0x01);
|
this->write_byte(MPR121_MHDR, 0x01);
|
||||||
this->write_byte(MPR121_NHDR, 0x01);
|
this->write_byte(MPR121_NHDR, 0x01);
|
||||||
@ -36,7 +35,7 @@ void MPR121Component::setup() {
|
|||||||
this->write_byte(MPR121_NCLT, 0x00);
|
this->write_byte(MPR121_NCLT, 0x00);
|
||||||
this->write_byte(MPR121_FDLT, 0x00);
|
this->write_byte(MPR121_FDLT, 0x00);
|
||||||
|
|
||||||
this->write_byte(MPR121_DEBOUNCE, 0);
|
this->write_byte(MPR121_DEBOUNCE, this->debounce_);
|
||||||
// default, 16uA charge current
|
// default, 16uA charge current
|
||||||
this->write_byte(MPR121_CONFIG1, 0x10);
|
this->write_byte(MPR121_CONFIG1, 0x10);
|
||||||
// 0.5uS encoding, 1ms period
|
// 0.5uS encoding, 1ms period
|
||||||
@ -44,6 +43,21 @@ void MPR121Component::setup() {
|
|||||||
// start with first 5 bits of baseline tracking
|
// start with first 5 bits of baseline tracking
|
||||||
this->write_byte(MPR121_ECR, 0x8F);
|
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() {
|
void MPR121Component::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "MPR121:");
|
ESP_LOGCONFIG(TAG, "MPR121:");
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
@ -59,6 +73,7 @@ void MPR121Component::dump_config() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MPR121Component::loop() {
|
void MPR121Component::loop() {
|
||||||
uint16_t val = 0;
|
uint16_t val = 0;
|
||||||
this->read_byte_16(MPR121_TOUCHSTATUS_L, &val);
|
this->read_byte_16(MPR121_TOUCHSTATUS_L, &val);
|
||||||
|
@ -49,14 +49,23 @@ class MPR121Channel : public binary_sensor::BinarySensor {
|
|||||||
public:
|
public:
|
||||||
void set_channel(uint8_t channel) { channel_ = channel; }
|
void set_channel(uint8_t channel) { channel_ = channel; }
|
||||||
void process(uint16_t data) { this->publish_state(static_cast<bool>(data & (1 << this->channel_))); }
|
void process(uint16_t data) { this->publish_state(static_cast<bool>(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:
|
protected:
|
||||||
uint8_t channel_{0};
|
uint8_t channel_{0};
|
||||||
|
uint8_t touch_threshold_{12};
|
||||||
|
uint8_t release_threshold{6};
|
||||||
};
|
};
|
||||||
|
|
||||||
class MPR121Component : public Component, public i2c::I2CDevice {
|
class MPR121Component : public Component, public i2c::I2CDevice {
|
||||||
public:
|
public:
|
||||||
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_release_debounce(uint8_t debounce);
|
||||||
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; }
|
||||||
@ -64,6 +73,8 @@ class MPR121Component : public Component, public i2c::I2CDevice {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector<MPR121Channel *> channels_{};
|
std::vector<MPR121Channel *> channels_{};
|
||||||
|
uint16_t currtouched_{0};
|
||||||
|
uint8_t debounce_{0};
|
||||||
enum ErrorCode {
|
enum ErrorCode {
|
||||||
NONE = 0,
|
NONE = 0,
|
||||||
COMMUNICATION_FAILED,
|
COMMUNICATION_FAILED,
|
||||||
|
@ -448,6 +448,10 @@ CONF_WIFI = 'wifi'
|
|||||||
CONF_WILL_MESSAGE = 'will_message'
|
CONF_WILL_MESSAGE = 'will_message'
|
||||||
CONF_WINDOW_SIZE = 'window_size'
|
CONF_WINDOW_SIZE = 'window_size'
|
||||||
CONF_ZERO = 'zero'
|
CONF_ZERO = 'zero'
|
||||||
|
CONF_TOUCH_THRESHOLD = "touch_threshold"
|
||||||
|
CONF_RELEASE_THRESHOLD = "release_threshold"
|
||||||
|
CONF_TOUCH_DEBOUNCE = "touch_debounce"
|
||||||
|
CONF_RELEASE_DEBOUNCE = "release_debounce"
|
||||||
|
|
||||||
ICON_ARROW_EXPAND_VERTICAL = 'mdi:arrow-expand-vertical'
|
ICON_ARROW_EXPAND_VERTICAL = 'mdi:arrow-expand-vertical'
|
||||||
ICON_BATTERY = 'mdi:battery'
|
ICON_BATTERY = 'mdi:battery'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user