mpr121: Add GPIO support (#6776)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
polyfloyd
2024-05-23 23:08:28 +02:00
committed by GitHub
parent aed0593793
commit c2d67659f3
14 changed files with 332 additions and 206 deletions
@@ -0,0 +1,37 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import CONF_CHANNEL
from .. import (
mpr121_ns,
MPR121Component,
CONF_MPR121_ID,
CONF_TOUCH_THRESHOLD,
CONF_RELEASE_THRESHOLD,
)
DEPENDENCIES = ["mpr121"]
MPR121BinarySensor = mpr121_ns.class_("MPR121BinarySensor", binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(MPR121BinarySensor).extend(
{
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.int_range(min=0x05, max=0x30),
cv.Optional(CONF_RELEASE_THRESHOLD): cv.int_range(min=0x05, max=0x30),
}
)
async def to_code(config):
var = await binary_sensor.new_binary_sensor(config)
hub = await cg.get_variable(config[CONF_MPR121_ID])
cg.add(var.set_channel(config[CONF_CHANNEL]))
cg.register_parented(var, hub)
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(hub.register_channel(var))
@@ -0,0 +1,20 @@
#include "mpr121_binary_sensor.h"
namespace esphome {
namespace mpr121 {
void MPR121BinarySensor::setup() {
uint8_t touch_threshold = this->touch_threshold_.value_or(this->parent_->get_touch_threshold());
this->parent_->write_byte(MPR121_TOUCHTH_0 + 2 * this->channel_, touch_threshold);
uint8_t release_threshold = this->release_threshold_.value_or(this->parent_->get_release_threshold());
this->parent_->write_byte(MPR121_RELEASETH_0 + 2 * this->channel_, release_threshold);
}
void MPR121BinarySensor::process(uint16_t data) {
bool new_state = data & (1 << this->channel_);
this->publish_state(new_state);
}
} // namespace mpr121
} // namespace esphome
@@ -0,0 +1,26 @@
#pragma once
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "../mpr121.h"
namespace esphome {
namespace mpr121 {
class MPR121BinarySensor : public binary_sensor::BinarySensor, public MPR121Channel, public Parented<MPR121Component> {
public:
void set_channel(uint8_t channel) { this->channel_ = 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; };
void setup() override;
void process(uint16_t data) override;
protected:
uint8_t channel_{0};
optional<uint8_t> touch_threshold_{};
optional<uint8_t> release_threshold_{};
};
} // namespace mpr121
} // namespace esphome