1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-14 14:48:18 +00:00

Merge pull request #6 from mvturnho/mpr121_threshold

added option for debounce and thresholds.
This commit is contained in:
Michiel van Turnhout 2019-05-20 10:48:38 +02:00 committed by GitHub
commit 96b5c979ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
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']
AUTO_LOAD = ['binary_sensor']
@ -13,10 +13,16 @@ 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): 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))
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]))
yield cg.register_component(var, config)
yield i2c.register_i2c_device(var, config)

View File

@ -1,7 +1,7 @@
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 esphome.const import CONF_CHANNEL, CONF_ID, CONF_TOUCH_THRESHOLD, CONF_RELEASE_THRESHOLD
from . import mpr121_ns, MPR121Component, CONF_MPR121_ID
DEPENDENCIES = ['mpr121']
@ -11,6 +11,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)),
})
@ -19,6 +21,9 @@ def to_code(config):
yield binary_sensor.register_binary_sensor(var, config)
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])
cg.add(hub.register_channel(var))

View File

@ -17,10 +17,9 @@ void MPR121Component::setup() {
return;
}
// 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);
@ -36,7 +35,7 @@ void MPR121Component::setup() {
this->write_byte(MPR121_NCLT, 0x00);
this->write_byte(MPR121_FDLT, 0x00);
this->write_byte(MPR121_DEBOUNCE, 0);
this->write_byte(MPR121_DEBOUNCE, this->debounce_);
// default, 16uA charge current
this->write_byte(MPR121_CONFIG1, 0x10);
// 0.5uS encoding, 1ms period
@ -44,6 +43,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);
@ -59,6 +73,7 @@ void MPR121Component::dump_config() {
break;
}
}
void MPR121Component::loop() {
uint16_t val = 0;
this->read_byte_16(MPR121_TOUCHSTATUS_L, &val);

View File

@ -49,14 +49,23 @@ 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<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:
uint8_t channel_{0};
uint8_t touch_threshold_{12};
uint8_t release_threshold{6};
};
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 setup() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
@ -64,6 +73,8 @@ class MPR121Component : public Component, public i2c::I2CDevice {
protected:
std::vector<MPR121Channel *> channels_{};
uint16_t currtouched_{0};
uint8_t debounce_{0};
enum ErrorCode {
NONE = 0,
COMMUNICATION_FAILED,

View File

@ -448,6 +448,10 @@ CONF_WIFI = 'wifi'
CONF_WILL_MESSAGE = 'will_message'
CONF_WINDOW_SIZE = 'window_size'
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_BATTERY = 'mdi:battery'