Compare commits

..
73 changed files with 2165 additions and 557 deletions

No files matched your search

+2
View File
@@ -146,6 +146,7 @@ esphome/components/cover/* @esphome/core
esphome/components/cs5460a/* @balrog-kun
esphome/components/cse7761/* @berfenger
esphome/components/cst226/* @clydebarrow
esphome/components/cst3240/* @tpruvot
esphome/components/cst328/* @latonita
esphome/components/cst816/* @clydebarrow
esphome/components/cst9220/* @clydebarrow
@@ -467,6 +468,7 @@ esphome/components/nfc/* @jesserockz @kbx81
esphome/components/noblex/* @AGalfra
esphome/components/noise/* @esphome/core
esphome/components/npi19/* @bakerkj
esphome/components/npm1300/* @tomaszduda23
esphome/components/nrf52/* @tomaszduda23
esphome/components/ns2009/* @tchilov
esphome/components/ns2009/touchscreen/* @tchilov
-9
View File
@@ -17,15 +17,6 @@ cap1188_ns = cg.esphome_ns.namespace("cap1188")
CONF_CAP1188_ID = "cap1188_id"
CAP1188Component = cap1188_ns.class_("CAP1188Component", cg.Component, i2c.I2CDevice)
# Publishes the channel binary sensors from the touch status read on each loop
CHANNELS_PUBLISHER = cg.HubFunction(
"channels",
cg.void,
[(cg.uint8, "touched")],
define="USE_CAP1188_CHANNELS_PUBLISHER",
)
TOUCHED = CHANNELS_PUBLISHER.param("touched")
MULTI_CONF = True
CONFIG_SCHEMA = (
cv.Schema(
+6 -6
View File
@@ -1,15 +1,15 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_CHANNEL
from esphome.types import ConfigType
from . import CHANNELS_PUBLISHER, CONF_CAP1188_ID, TOUCHED, CAP1188Component, cap1188_ns
from . import CONF_CAP1188_ID, CAP1188Component, cap1188_ns
DEPENDENCIES = ["cap1188"]
CAP1188Channel = cap1188_ns.class_("CAP1188Channel", binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(CAP1188Channel).extend(
{
cv.GenerateID(CONF_CAP1188_ID): cv.use_id(CAP1188Component),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=7),
@@ -20,6 +20,6 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
async def to_code(config: ConfigType) -> None:
var = await binary_sensor.new_binary_sensor(config)
hub = await cg.get_variable(config[CONF_CAP1188_ID])
CHANNELS_PUBLISHER.add(
hub, cap1188_ns.publish_channel(parent_ref(var), TOUCHED, config[CONF_CHANNEL])
)
cg.add(var.set_channel(config[CONF_CHANNEL]))
cg.add(hub.register_channel(var))
+3 -4
View File
@@ -79,10 +79,9 @@ void CAP1188Component::loop() {
if (touched)
this->update_register(CAP1188_MAIN, CAP1188_MAIN_INT, 0);
#ifdef USE_CAP1188_CHANNELS_PUBLISHER
if (this->channels_publisher_ != nullptr)
this->channels_publisher_(touched);
#endif
for (auto *channel : this->channels_) {
channel->process(touched);
}
}
} // namespace esphome::cap1188
+13 -8
View File
@@ -4,7 +4,9 @@
#include "esphome/core/hal.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/output/binary_output.h"
#include "esphome/core/helpers.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include <vector>
namespace esphome::cap1188 {
@@ -24,16 +26,18 @@ enum {
CAP1188_SENSITIVITY = 0x1f,
};
// A template on the sensor type keeps binary_sensor.h out of this hub header
template<typename T> inline void publish_channel(T *sensor, uint8_t touched, uint8_t channel) {
sensor->publish_state((touched >> channel) & 1);
}
class CAP1188Channel final : public binary_sensor::BinarySensor {
public:
void set_channel(uint8_t channel) { channel_ = channel; }
void process(uint8_t data) { this->publish_state(static_cast<bool>(data & (1 << this->channel_))); }
protected:
uint8_t channel_{0};
};
class CAP1188Component final : public Component, public i2c::I2CDevice {
public:
#ifdef USE_CAP1188_CHANNELS_PUBLISHER
HUB_PUBLISHER(channels, uint8_t)
#endif
void register_channel(CAP1188Channel *channel) { this->channels_.push_back(channel); }
void set_touch_threshold(uint8_t touch_threshold) { this->touch_threshold_ = touch_threshold; };
void set_allow_multiple_touches(bool allow_multiple_touches) {
this->allow_multiple_touches_ = allow_multiple_touches ? 0x41 : 0x80;
@@ -46,6 +50,7 @@ class CAP1188Component final : public Component, public i2c::I2CDevice {
protected:
void finish_setup_();
std::vector<CAP1188Channel *> channels_{};
uint8_t touch_threshold_{0x20};
uint8_t allow_multiple_touches_{0x80};
+7
View File
@@ -0,0 +1,7 @@
import esphome.codegen as cg
CODEOWNERS = ["@tpruvot"]
DEPENDENCIES = ["i2c"]
DOMAIN = "cst3240"
cst3240_ns = cg.esphome_ns.namespace("cst3240")
@@ -0,0 +1,14 @@
from esphome.components import cstxxx
from esphome.types import ConfigType
from .. import cst3240_ns
AUTO_LOAD = ["cstxxx"]
CST3240Touchscreen = cst3240_ns.class_("CST3240Touchscreen", cstxxx.CSTTouchscreen)
CONFIG_SCHEMA = cstxxx.cst_touchscreen_schema(CST3240Touchscreen, 0x5A)
async def to_code(config: ConfigType) -> None:
await cstxxx.new_cst_touchscreen(config)
@@ -0,0 +1,112 @@
#include "cst3240_touchscreen.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <utility>
namespace esphome::cst3240 {
ESPHOME_LOG_TAG(TAG, "cst3240.touchscreen");
void CST3240Touchscreen::setup() {
if (this->reset_pin_ == nullptr) {
this->continue_setup_();
return;
}
pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 5});
this->set_timeout(30, [this] { this->continue_setup_(); });
}
void CST3240Touchscreen::continue_setup_() {
if (!this->enter_command_mode_())
return;
if (!this->setup_check(this->read_words_(cstxxx::REG_CHIP_INFO, this->project_id_, this->chip_id_),
LOG_STR("Failed to read chip ID")))
return;
// Only warn: the chip ID has not been confirmed on enough boards to fail setup on it
if (this->chip_id_ != CST3240_CHIP_ID) {
ESP_LOGW(TAG, "Unexpected chip ID 0x%04X", this->chip_id_);
}
this->read_words_(cstxxx::REG_FW_VERSION, this->fw_build_, this->fw_version_);
if (this->x_raw_max_ == this->x_raw_min_ || this->y_raw_max_ == this->y_raw_min_) {
uint16_t x_max, y_max;
if (this->read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
this->x_raw_max_ = x_max;
this->y_raw_max_ = y_max;
if (this->swap_x_y_)
std::swap(this->x_raw_max_, this->y_raw_max_);
} else {
this->x_raw_max_ = this->display_->get_native_width();
this->y_raw_max_ = this->display_->get_native_height();
}
}
this->status_comm_check(this->write_register16(cstxxx::REG_NORMAL_MODE, nullptr, 0) == i2c::ERROR_OK);
this->setup_interrupt_pin_(gpio::INTERRUPT_RISING_EDGE);
this->setup_complete_ = true;
}
void CST3240Touchscreen::update_touches() {
if (!this->setup_complete_) {
this->skip_update_ = true;
return;
}
uint8_t data[CST3240_REPORT_SIZE];
if (!this->status_comm_result(this->read_register16(cstxxx::REG_TOUCH_DATA, data, sizeof(data)) == i2c::ERROR_OK)) {
this->skip_update_ = true;
return;
}
const uint8_t sync = data[cstxxx::SYNC_IDX];
if (sync != cstxxx::SYNC_BYTE) {
this->skip_update_ = true;
if (sync != 0) {
ESP_LOGV(TAG, "Skipping report, sync byte 0x%02X", sync);
this->clear_report_();
}
return;
}
const uint8_t count = data[cstxxx::FINGER_COUNT_IDX] & 0x0F;
if (count <= CST3240_MAX_TOUCHES) {
for (uint8_t i = 0; i < count; i++) {
auto touch = cstxxx::decode_touch(data, i);
// A released finger is still listed once, with both press bits clear
if ((touch.event & TOUCH_EVENT_PRESSED) == 0)
continue;
this->add_raw_touch_position_(touch.id, touch.x, touch.y, touch.z);
}
}
if (this->interrupt_pin_ == nullptr) {
this->clear_report_();
} else if (!this->write_byte_reg16(cstxxx::REG_FINGER_COUNT, 0) ||
!this->write_byte_reg16(cstxxx::REG_TOUCH_DATA, cstxxx::SYNC_BYTE)) {
ESP_LOGW(TAG, "Failed to acknowledge the report");
}
}
void CST3240Touchscreen::clear_report_() {
// Built on the stack: write_register16() heap allocates a frame this long
uint8_t frame[2 + CST3240_REPORT_SIZE]{};
put_be16(frame, cstxxx::REG_TOUCH_DATA);
if (this->write(frame, sizeof(frame)) != i2c::ERROR_OK) {
ESP_LOGW(TAG, "Failed to clear the report");
}
}
void CST3240Touchscreen::dump_config() {
ESP_LOGCONFIG(TAG,
"CST3240 Touchscreen:\n"
" Chip ID: 0x%04X, Project ID: 0x%04X\n"
" FW version: %d.%d.%d\n"
" X/Y resolution: %d/%d",
this->chip_id_, this->project_id_, this->fw_version_ >> 8, this->fw_version_ & 0xFF, this->fw_build_,
this->x_raw_max_, this->y_raw_max_);
LOG_I2C_DEVICE(this);
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
}
} // namespace esphome::cst3240
@@ -0,0 +1,31 @@
#pragma once
#include "esphome/components/cstxxx/cstxxx.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
namespace esphome::cst3240 {
static constexpr uint16_t CST3240_CHIP_ID = 0x3240;
static constexpr uint8_t CST3240_MAX_TOUCHES = 5;
static constexpr uint8_t CST3240_REPORT_SIZE = cstxxx::report_size(CST3240_MAX_TOUCHES);
static constexpr uint8_t TOUCH_EVENT_PRESSED = 0x06;
class CST3240Touchscreen : public cstxxx::CSTTouchscreen {
public:
void setup() override;
void dump_config() override;
protected:
void update_touches() override;
void continue_setup_();
/// Zeroes the touch report block, which polling relies on to tell new reports from old.
void clear_report_();
uint16_t chip_id_{};
uint16_t project_id_{};
uint16_t fw_version_{};
uint16_t fw_build_{};
};
} // namespace esphome::cst3240
+3 -1
View File
@@ -1,6 +1,5 @@
import esphome.codegen as cg
from esphome.components import logger
from esphome.components.nrf52.framework import include_west_project
from esphome.components.zephyr import zephyr_add_prj_conf
from esphome.config_helpers import filter_source_files_from_platform
import esphome.config_validation as cv
@@ -53,6 +52,9 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config: ConfigType) -> None:
if CORE.using_zephyr:
# Imported here so configs for other platforms do not load the nrf52 package
from esphome.components.nrf52.framework import include_west_project
zephyr_add_prj_conf("HWINFO", True)
# gdb thread support
zephyr_add_prj_conf("DEBUG_THREAD_INFO", True)
+3 -1
View File
@@ -56,7 +56,6 @@ from esphome.const import (
from esphome.core import CORE, EsphomeError, HexInt
from esphome.core.config import BOARD_MAX_LENGTH
from esphome.coroutine import CoroPriority, coroutine_with_priority
from esphome.espidf import sbom
from esphome.espidf.component import IDFComponent, generate_idf_components
import esphome.final_validate as fv
from esphome.helpers import copy_file_if_changed, rmtree, write_file_if_changed
@@ -4033,6 +4032,9 @@ def copy_files():
CORE.relative_build_path("version.txt"),
__version__,
)
# Imported here: sbom loads git and urllib helpers that config validation does not need
from esphome.espidf import sbom
sbom.write_manifests()
# Remote extra build files are fetched into the shared download cache in
+15 -18
View File
@@ -347,26 +347,23 @@ bool FUSB302B::read_message_(PdMsg &msg, bool &is_sop) {
bool FUSB302B::send_message(const PdMsg &msg) {
// Register address, then the TX tokens; written in one transfer without the copy write_register() makes
uint8_t buf[1 + 4 + 1 + 2 + PD_MAX_NUM_DATA_OBJECTS * 4 + 4];
size_t len = 0;
uint8_t *p = buf;
buf[len++] = FUSB_FIFOS;
buf[len++] = FUSB_TX_TOKEN_SYNC1;
buf[len++] = FUSB_TX_TOKEN_SYNC1;
buf[len++] = FUSB_TX_TOKEN_SYNC1;
buf[len++] = FUSB_TX_TOKEN_SYNC2;
buf[len++] = FUSB_TX_TOKEN_PACKSYM | (msg.num_of_obj * 4 + 2);
put_le16(buf + len, msg.get_coded_header());
len += 2;
for (uint8_t i = 0; i < msg.num_of_obj; i++) {
put_le32(buf + len, msg.data_objects[i]);
len += 4;
}
buf[len++] = FUSB_TX_TOKEN_JAM_CRC;
buf[len++] = FUSB_TX_TOKEN_EOP;
buf[len++] = FUSB_TX_TOKEN_TXOFF;
buf[len++] = FUSB_TX_TOKEN_TXON;
*p++ = FUSB_FIFOS;
*p++ = FUSB_TX_TOKEN_SYNC1;
*p++ = FUSB_TX_TOKEN_SYNC1;
*p++ = FUSB_TX_TOKEN_SYNC1;
*p++ = FUSB_TX_TOKEN_SYNC2;
*p++ = FUSB_TX_TOKEN_PACKSYM | (msg.num_of_obj * 4 + 2);
p = put_le16(p, msg.get_coded_header());
for (uint8_t i = 0; i < msg.num_of_obj; i++)
p = put_le32(p, msg.data_objects[i]);
*p++ = FUSB_TX_TOKEN_JAM_CRC;
*p++ = FUSB_TX_TOKEN_EOP;
*p++ = FUSB_TX_TOKEN_TXOFF;
*p++ = FUSB_TX_TOKEN_TXON;
i2c::ErrorCode err = this->write(buf, len);
i2c::ErrorCode err = this->write(buf, p - buf);
if (err != i2c::ERROR_OK) {
ESP_LOGW(TAG, "Sending message type %u failed (error %d)", msg.type, static_cast<int>(err));
return false;
+2 -12
View File
@@ -28,18 +28,6 @@ DOMAIN = "mpr121"
mpr121_ns = cg.esphome_ns.namespace("mpr121")
MPR121Component = mpr121_ns.class_("MPR121Component", cg.Component, i2c.I2CDevice)
# Both run only for channels: the thresholds once at setup, the states on each loop
THRESHOLDS_PUBLISHER = cg.HubFunction(
"thresholds", cg.void, [], define="USE_MPR121_CHANNEL_PUBLISHERS"
)
CHANNELS_PUBLISHER = cg.HubFunction(
"channels",
cg.void,
[(cg.uint16, "touched")],
define="USE_MPR121_CHANNEL_PUBLISHERS",
)
TOUCHED = CHANNELS_PUBLISHER.param("touched")
MPR121GPIOPin = mpr121_ns.class_(
"MPR121GPIOPin", cg.GPIOPin, cg.Parented.template(MPR121Component)
)
@@ -90,6 +78,8 @@ async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
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]))
cg.add(var.set_max_touch_channel(config[CONF_MAX_TOUCH_CHANNEL]))
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
@@ -1,25 +1,21 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_CHANNEL
import esphome.final_validate as fv
from esphome.types import ConfigType
from .. import (
CHANNELS_PUBLISHER,
CONF_MPR121_ID,
CONF_RELEASE_THRESHOLD,
CONF_TOUCH_THRESHOLD,
THRESHOLDS_PUBLISHER,
TOUCHED,
MPR121Component,
mpr121_ns,
)
DEPENDENCIES = ["mpr121"]
MPR121BinarySensor = mpr121_ns.class_("MPR121BinarySensor", binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
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),
@@ -29,26 +25,13 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
)
def _final_validate(config: ConfigType) -> None:
"""Take the thresholds the channel does not set from its hub."""
hub = fv.get_declaring_config(config[CONF_MPR121_ID])
for key in (CONF_TOUCH_THRESHOLD, CONF_RELEASE_THRESHOLD):
config.setdefault(key, hub[key])
FINAL_VALIDATE_SCHEMA = _final_validate
async def to_code(config: ConfigType) -> None:
var = await binary_sensor.new_binary_sensor(config)
hub = await cg.get_variable(config[CONF_MPR121_ID])
channel = config[CONF_CHANNEL]
THRESHOLDS_PUBLISHER.add(
hub,
hub.write_thresholds(
channel, config[CONF_TOUCH_THRESHOLD], config[CONF_RELEASE_THRESHOLD]
),
)
CHANNELS_PUBLISHER.add(
hub, mpr121_ns.publish_channel(parent_ref(var), TOUCHED, channel)
)
cg.add(var.set_channel(config[CONF_CHANNEL]))
await cg.register_parented(var, hub)
cg.add_optional(var.set_touch_threshold, config.get(CONF_TOUCH_THRESHOLD))
cg.add_optional(var.set_release_threshold, config.get(CONF_RELEASE_THRESHOLD))
cg.add(hub.register_channel(var))
@@ -0,0 +1,18 @@
#include "mpr121_binary_sensor.h"
namespace esphome::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 esphome::mpr121
@@ -0,0 +1,26 @@
#pragma once
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "../mpr121.h"
namespace esphome::mpr121 {
class MPR121BinarySensor final : 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 esphome::mpr121
+9 -18
View File
@@ -17,10 +17,10 @@ void MPR121Component::setup() {
this->set_timeout(100, [this]() {
if (!this->setup_check(this->write_byte(MPR121_ECR, 0x0)))
return;
#ifdef USE_MPR121_CHANNEL_PUBLISHERS
if (this->thresholds_publisher_ != nullptr)
this->thresholds_publisher_();
#endif
// set touch sensitivity for all 12 channels
for (auto *channel : this->channels_) {
channel->setup();
}
this->write_byte(MPR121_MHDR, 0x01);
this->write_byte(MPR121_NHDR, 0x01);
this->write_byte(MPR121_NCLR, 0x0E);
@@ -65,25 +65,16 @@ void MPR121Component::set_release_debounce(uint8_t debounce) {
this->debounce_ |= mask;
};
#ifdef USE_MPR121_CHANNEL_PUBLISHERS
void MPR121Component::write_thresholds(uint8_t channel, uint8_t touch, uint8_t release) {
this->write_byte(MPR121_TOUCHTH_0 + 2 * channel, touch);
this->write_byte(MPR121_RELEASETH_0 + 2 * channel, release);
}
#endif
void MPR121Component::dump_config() {
ESP_LOGCONFIG(TAG, "MPR121:");
LOG_I2C_DEVICE(this);
}
void MPR121Component::loop() {
#ifdef USE_MPR121_CHANNEL_PUBLISHERS
if (this->channels_publisher_ != nullptr) {
uint16_t val = 0;
this->read_byte_16_le(MPR121_TOUCHSTATUS_L, &val);
this->channels_publisher_(val);
}
#endif
uint16_t val = 0;
this->read_byte_16_le(MPR121_TOUCHSTATUS_L, &val);
for (auto *channel : this->channels_)
channel->process(val);
this->read_byte(MPR121_GPIODATA, &this->gpio_input_);
}
+15 -10
View File
@@ -3,11 +3,12 @@
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/components/gpio_expander/expander_gpio_pin.h"
#include "esphome/components/i2c/i2c.h"
#include <vector>
namespace esphome::mpr121 {
enum {
@@ -51,20 +52,21 @@ enum {
MPR121_SOFTRESET = 0x80,
};
// A template on the sensor type keeps binary_sensor.h out of this hub header
template<typename T> inline void publish_channel(T *sensor, uint16_t touched, uint8_t channel) {
sensor->publish_state((touched >> channel) & 1);
}
class MPR121Channel {
public:
virtual void setup() = 0;
virtual void process(uint16_t data) = 0;
};
class MPR121Component final : public Component, public i2c::I2CDevice {
public:
#ifdef USE_MPR121_CHANNEL_PUBLISHERS
HUB_PUBLISHER(thresholds)
HUB_PUBLISHER(channels, uint16_t)
void write_thresholds(uint8_t channel, uint8_t touch, uint8_t release);
#endif
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 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() const { return this->touch_threshold_; };
uint8_t get_release_threshold() const { return this->release_threshold_; };
void setup() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::IO; }
@@ -78,7 +80,10 @@ class MPR121Component final : public Component, public i2c::I2CDevice {
void pin_mode(uint8_t pin, gpio::Flags flags);
protected:
std::vector<MPR121Channel *> channels_{};
uint8_t debounce_{0};
uint8_t touch_threshold_{};
uint8_t release_threshold_{};
uint8_t max_touch_channel_{3};
bool flush_gpio_();
+303
View File
@@ -0,0 +1,303 @@
from collections.abc import Callable
from esphome import automation
import esphome.codegen as cg
from esphome.components import i2c
from esphome.components.const import CONF_CHARGE_CURRENT, CONF_ENABLED
import esphome.config_validation as cv
from esphome.const import CONF_DURATION, CONF_ID, CONF_MODE, CONF_VOLTAGE
from esphome.types import ConfigType
CODEOWNERS = ["@tomaszduda23"]
DEPENDENCIES = ["i2c"]
DOMAIN = "npm1300"
MULTI_CONF = True
CONF_NPM1300_ID = "npm1300_id"
CONF_VBUS_CURRENT_LIMIT = "vbus_current_limit"
CONF_THERMISTOR_RESISTANCE = "thermistor_resistance"
CONF_THERMISTOR_BETA = "thermistor_beta"
CONF_CHARGER = "charger"
CONF_TERMINATION_VOLTAGE = "termination_voltage"
CONF_WARM_TERMINATION_VOLTAGE = "warm_termination_voltage"
CONF_TERMINATION_CURRENT = "termination_current"
CONF_TRICKLE_VOLTAGE = "trickle_voltage"
CONF_DISCHARGE_CURRENT_LIMIT = "discharge_current_limit"
CONF_RECHARGE = "recharge"
CONF_BUCK1 = "buck1"
CONF_BUCK2 = "buck2"
CONF_LDO1 = "ldo1"
CONF_LDO2 = "ldo2"
npm1300_ns = cg.esphome_ns.namespace("npm1300")
NPM1300Component = npm1300_ns.class_(
"NPM1300Component", cg.PollingComponent, i2c.I2CDevice
)
NPM1300Output = npm1300_ns.enum("NPM1300Output", is_class=True)
NPM1300OutputConfig = npm1300_ns.struct("NPM1300OutputConfig")
NPM1300ChargerConfig = npm1300_ns.struct("NPM1300ChargerConfig")
NPM1300Config = npm1300_ns.struct("NPM1300Config")
NPM1300_CHILD_SCHEMA = cv.Schema(
{cv.GenerateID(CONF_NPM1300_ID): cv.use_id(NPM1300Component)}
)
# Status registers read on each update: VBUSINSTATUS, BCHGCHARGESTATUS and BCHGERRREASON
STATUS_PUBLISHER = cg.HubFunction(
"status",
cg.void,
[
(cg.uint8, "vbus_status"),
(cg.uint8, "charge_status"),
(cg.uint8, "error_reason"),
],
define="USE_NPM1300_STATUS_PUBLISHER",
)
VBUS_STATUS = STATUS_PUBLISHER.param("vbus_status")
CHARGE_STATUS = STATUS_PUBLISHER.param("charge_status")
ERROR_REASON = STATUS_PUBLISHER.param("error_reason")
# The ADC results and the amperes per code of the battery current
DATA_PUBLISHER = cg.HubFunction(
"data",
cg.void,
[(cg.uint8.operator("const").operator("ptr"), "adc"), (cg.float_, "ibat_scale")],
define="USE_NPM1300_DATA_PUBLISHER",
)
ADC = DATA_PUBLISHER.param("adc")
IBAT_SCALE = DATA_PUBLISHER.param("ibat_scale")
# The dump lines of the children decoded by the two functions above
DUMP = cg.HubFunction("dump", cg.void, [(cg.const_char_ptr, "tag")])
DUMP_TAG = DUMP.param("tag")
# Config key of each regulator and its NPM1300Output enumerator
OUTPUTS = NPM1300Output.options(
CONF_BUCK1, CONF_BUCK2, CONF_LDO1, CONF_LDO2, prefix="NPM1300_OUTPUT_"
)
# Written for a field the YAML leaves out
NPM1300_UNSET = 0xFF
# ADCNTCRSEL codes, by kiloohms
THERMISTOR_RESISTANCES = {10: 1, 47: 2, 100: 3}
LDO_MODES = {"load_switch": 0, "ldo": 1}
TERMINATION_CURRENTS = {"10%": 0, "20%": 1}
# BCHGVTRICKLESEL, by millivolts
TRICKLE_VOLTAGES = {2900: 0, 2500: 1}
# BCHGISETDISCHARGE codes, by milliamperes
DISCHARGE_CURRENT_LIMITS = {200: 84, 1000: 415}
# BCHGVTERM: 3.50 V to 3.65 V, then 4.00 V to 4.45 V, both in 50 mV steps
TERMINATION_VOLTAGES = [3500 + 50 * i for i in range(4)] + [
4000 + 50 * i for i in range(10)
]
# The wake-up timer counts 16 ms ticks in a 24-bit register
HIBERNATE_TICK_MS = 16
HIBERNATE_MAX_TICKS = 0xFFFFFF
def _millis(value: float) -> int:
return round(value * 1000)
def _one_of_units(
validator: Callable[[object], float],
allowed: list[int],
unit: str,
scale: float = 1000,
) -> Callable[[object], float]:
"""Validate a quantity with `validator` and accept only the listed values, given in units * `scale`."""
def validate(value: object) -> float:
value = validator(value)
if round(value * scale) not in allowed:
options = ", ".join(f"{v / scale:g}{unit}" for v in allowed)
raise cv.Invalid(f"Must be one of {options}")
return value
return validate
def _stepped(
validator: Callable[[object], float],
minimum: int,
maximum: int,
step: int,
unit: str,
) -> Callable[[object], float]:
"""Validate a quantity in a range of milli-units, in steps of `step`."""
allowed = list(range(minimum, maximum + 1, step))
in_range = cv.All(validator, cv.Range(min=minimum / 1000, max=maximum / 1000))
def validate(value: object) -> float:
value = in_range(value)
if _millis(value) not in allowed:
raise cv.Invalid(f"Must be a multiple of {step}m{unit}")
return value
return validate
OUTPUT_VOLTAGE = _stepped(cv.voltage, 1000, 3300, 100, "V")
VBUS_CURRENT_LIMIT = _stepped(cv.current, 100, 1500, 100, "A")
BUCK_SCHEMA = cv.Schema(
{
cv.Optional(CONF_VOLTAGE): OUTPUT_VOLTAGE,
cv.Optional(CONF_ENABLED): cv.boolean,
}
)
LDO_SCHEMA = BUCK_SCHEMA.extend(
{
cv.Optional(CONF_MODE): cv.enum(LDO_MODES, lower=True),
}
)
CHARGER_SCHEMA = cv.Schema(
{
cv.Required(CONF_CHARGE_CURRENT): _stepped(cv.current, 32, 800, 2, "A"),
cv.Optional(CONF_TERMINATION_VOLTAGE, default="4.2V"): _one_of_units(
cv.voltage, TERMINATION_VOLTAGES, "V"
),
cv.Optional(CONF_WARM_TERMINATION_VOLTAGE): _one_of_units(
cv.voltage, TERMINATION_VOLTAGES, "V"
),
cv.Optional(CONF_TERMINATION_CURRENT, default="10%"): cv.enum(
TERMINATION_CURRENTS
),
cv.Optional(CONF_TRICKLE_VOLTAGE, default="2.9V"): _one_of_units(
cv.voltage, list(TRICKLE_VOLTAGES), "V"
),
cv.Optional(CONF_DISCHARGE_CURRENT_LIMIT, default="1000mA"): _one_of_units(
cv.current, list(DISCHARGE_CURRENT_LIMITS), "A"
),
cv.Optional(CONF_RECHARGE, default=True): cv.boolean,
cv.Optional(CONF_ENABLED, default=True): cv.boolean,
}
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(NPM1300Component),
cv.Optional(CONF_VBUS_CURRENT_LIMIT): VBUS_CURRENT_LIMIT,
cv.Optional(CONF_THERMISTOR_RESISTANCE): _one_of_units(
cv.resistance, list(THERMISTOR_RESISTANCES), "kOhm", scale=0.001
),
cv.Optional(CONF_THERMISTOR_BETA, default=3380): cv.int_range(
min=1, max=65535
),
cv.Optional(CONF_CHARGER): CHARGER_SCHEMA,
cv.Optional(CONF_BUCK1): BUCK_SCHEMA,
cv.Optional(CONF_BUCK2): BUCK_SCHEMA,
cv.Optional(CONF_LDO1): LDO_SCHEMA,
cv.Optional(CONF_LDO2): LDO_SCHEMA,
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x6B))
)
def _termination_voltage_code(volts: float) -> int:
return TERMINATION_VOLTAGES.index(_millis(volts))
def _vbus_current_limit_code(amps: float) -> int:
"""VBUSINILIM0: 100 mA per step of the code."""
return _millis(amps) // 100
def _charger_config(config: ConfigType) -> cg.StructInitializer:
termination = _termination_voltage_code(config[CONF_TERMINATION_VOLTAGE])
warm = config.get(CONF_WARM_TERMINATION_VOLTAGE)
return cg.StructInitializer(
NPM1300ChargerConfig,
("current", _millis(config[CONF_CHARGE_CURRENT]) // 2),
(
"discharge_limit",
DISCHARGE_CURRENT_LIMITS[_millis(config[CONF_DISCHARGE_CURRENT_LIMIT])],
),
("termination_voltage", termination),
(
"warm_termination_voltage",
termination if warm is None else _termination_voltage_code(warm),
),
("trickle_voltage", TRICKLE_VOLTAGES[_millis(config[CONF_TRICKLE_VOLTAGE])]),
("termination_current", TERMINATION_CURRENTS[config[CONF_TERMINATION_CURRENT]]),
("recharge", config[CONF_RECHARGE]),
("enabled", config[CONF_ENABLED]),
)
def _output_config(config: ConfigType | None) -> cg.StructInitializer:
config = config or {}
voltage = config.get(CONF_VOLTAGE)
mode = config.get(CONF_MODE)
enabled = config.get(CONF_ENABLED)
return cg.StructInitializer(
NPM1300OutputConfig,
(
"voltage",
NPM1300_UNSET if voltage is None else (_millis(voltage) - 1000) // 100,
),
("ldo_mode", NPM1300_UNSET if mode is None else LDO_MODES[mode]),
("enabled", NPM1300_UNSET if enabled is None else int(enabled)),
)
def _config(config: ConfigType) -> cg.StructInitializer:
charger = config.get(CONF_CHARGER)
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
return cg.StructInitializer(
NPM1300Config,
(
"outputs",
cg.ArrayInitializer(*(_output_config(config.get(key)) for key in OUTPUTS)),
),
("charger", None if charger is None else _charger_config(charger)),
("thermistor_beta", config[CONF_THERMISTOR_BETA]),
(
"thermistor",
NPM1300_UNSET
if resistance is None
else THERMISTOR_RESISTANCES[round(resistance / 1000)],
),
("charger_configured", charger is not None),
)
async def to_code(config: ConfigType) -> None:
# In flash: only setup() and dump_config() read it
table = cg.shared_progmem_array(
"npm1300_config", NPM1300Config, cg.ArrayInitializer(_config(config))
)
var = await i2c.new_i2c_component(config, table)
if (limit := config.get(CONF_VBUS_CURRENT_LIMIT)) is not None:
cg.add(var.set_vbus_current_limit(_vbus_current_limit_code(limit)))
automation.register_apply_action(
"npm1300.enter_ship_mode",
automation.maybe_simple_id({cv.Required(CONF_ID): cv.use_id(NPM1300Component)}),
automation.ApplyCall("enter_ship_mode()"),
)
automation.register_apply_action(
"npm1300.hibernate",
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NPM1300Component),
cv.Required(CONF_DURATION): cv.templatable(
cv.All(
cv.positive_time_period_milliseconds,
cv.Range(
min=cv.TimePeriod(milliseconds=HIBERNATE_TICK_MS),
max=cv.TimePeriod(
milliseconds=HIBERNATE_TICK_MS * HIBERNATE_MAX_TICKS
),
),
)
),
}
),
automation.ApplyField(CONF_DURATION, "hibernate", cg.uint32),
)
+106
View File
@@ -0,0 +1,106 @@
from dataclasses import dataclass
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import (
DEVICE_CLASS_BATTERY_CHARGING,
DEVICE_CLASS_POWER,
DEVICE_CLASS_PROBLEM,
ENTITY_CATEGORY_DIAGNOSTIC,
)
from esphome.cpp_generator import HexIntLiteral
from esphome.types import ConfigType
from . import (
CHARGE_STATUS,
CONF_NPM1300_ID,
DUMP,
DUMP_TAG,
ERROR_REASON,
NPM1300_CHILD_SCHEMA,
STATUS_PUBLISHER,
VBUS_STATUS,
)
DEPENDENCIES = ["npm1300"]
@dataclass(frozen=True, kw_only=True)
class _StatusBits:
"""On when any bit of mask is set in the status register."""
key: str
register: cg.MockObj
mask: int
name: str
device_class: str = cv.UNDEFINED
entity_category: str = cv.UNDEFINED
_FIELDS = (
_StatusBits(
key="vbus_present",
register=VBUS_STATUS,
mask=0x01,
name="VBUS Present",
device_class=DEVICE_CLASS_POWER,
),
_StatusBits(
key="vbus_current_limit_active",
register=VBUS_STATUS,
mask=0x02,
name="VBUS Current Limit Active",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
# Trickle, constant current or constant voltage charging
_StatusBits(
key="charging",
register=CHARGE_STATUS,
mask=0x1C,
name="Charging",
device_class=DEVICE_CLASS_BATTERY_CHARGING,
),
_StatusBits(
key="charge_complete",
register=CHARGE_STATUS,
mask=0x02,
name="Charge Complete",
),
_StatusBits(
key="charger_error",
register=ERROR_REASON,
mask=0xFF,
name="Charger Error",
device_class=DEVICE_CLASS_PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
)
CONFIG_SCHEMA = NPM1300_CHILD_SCHEMA.extend(
{
cv.Optional(field.key): binary_sensor.binary_sensor_schema(
device_class=field.device_class, entity_category=field.entity_category
)
for field in _FIELDS
}
)
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_NPM1300_ID])
sensors = binary_sensor.sub_binary_sensors(config)
for field in _FIELDS:
if (var := await sensors(field.key, None)) is not None:
state = (field.register & HexIntLiteral(field.mask)) != 0
STATUS_PUBLISHER.add(hub, parent_ref(var).publish_state(state))
DUMP.add(
hub,
binary_sensor.binary_sensor_ns.log_binary_sensor(
DUMP_TAG,
" ",
cg.RawExpression(f'LOG_STR_LITERAL("{field.name}")'),
parent_ref(var),
),
)
+483
View File
@@ -0,0 +1,483 @@
#include "npm1300.h"
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#ifdef USE_TEXT_SENSOR
#include "esphome/core/progmem.h"
#endif
namespace esphome::npm1300 {
ESPHOME_LOG_TAG(TAG, "npm1300");
// Registers: the base address of the peripheral in the high byte, the offset in the low byte
static constexpr uint16_t REG_VBUSIN_TASK_UPDATE_ILIM = 0x0200;
static constexpr uint16_t REG_VBUSIN_ILIM0 = 0x0201;
static constexpr uint16_t REG_VBUSIN_ILIM_STARTUP = 0x0202;
static constexpr uint16_t REG_VBUSIN_STATUS = 0x0207;
static constexpr uint16_t REG_BCHG_TASK_RELEASE_ERR = 0x0300;
static constexpr uint16_t REG_BCHG_TASK_CLEAR_CHG_ERR = 0x0301;
static constexpr uint16_t REG_BCHG_ENABLE_SET = 0x0304;
static constexpr uint16_t REG_BCHG_ENABLE_CLR = 0x0305;
static constexpr uint16_t REG_BCHG_DISABLE_SET = 0x0306;
static constexpr uint16_t REG_BCHG_DISABLE_CLR = 0x0307;
static constexpr uint16_t REG_BCHG_ISET_MSB = 0x0308;
static constexpr uint16_t REG_BCHG_ISET_DISCHARGE_MSB = 0x030A;
static constexpr uint16_t REG_BCHG_VTERM = 0x030C;
static constexpr uint16_t REG_BCHG_VTERM_R = 0x030D;
static constexpr uint16_t REG_BCHG_VTRICKLE_SEL = 0x030E;
static constexpr uint16_t REG_BCHG_ITERM_SEL = 0x030F;
static constexpr uint16_t REG_BCHG_CHARGE_STATUS = 0x0334;
static constexpr uint16_t REG_BCHG_ERR_REASON = 0x0336;
static constexpr uint16_t REG_BUCK1_ENA_SET = 0x0400;
static constexpr uint16_t REG_BUCK1_NORM_VOUT = 0x0408;
static constexpr uint16_t REG_BUCK_SW_CTRL_SEL = 0x040F;
static constexpr uint16_t REG_BUCK_STATUS = 0x0434;
static constexpr uint16_t REG_ADC_TASK_VBAT = 0x0500;
static constexpr uint16_t REG_ADC_TASK_VBUS = 0x0507;
static constexpr uint16_t REG_ADC_NTC_R_SEL = 0x050A;
static constexpr uint16_t REG_ADC_TASK_AUTO_TIM_UPDATE = 0x050C;
static constexpr uint16_t REG_ADC_RESULTS = 0x0510;
static constexpr uint16_t REG_ADC_IBAT_MEAS_EN = 0x0524;
static constexpr uint16_t REG_TIMER_TARGET_STROBE = 0x0703;
static constexpr uint16_t REG_TIMER_CONFIG = 0x0705;
static constexpr uint16_t REG_TIMER_HI_BYTE = 0x0708;
static constexpr uint16_t REG_LDSW1_TASK_SET = 0x0800;
static constexpr uint16_t REG_LDSW_STATUS = 0x0804;
static constexpr uint16_t REG_LDSW1_LDO_SEL = 0x0808;
static constexpr uint16_t REG_LDSW1_VOUT_SEL = 0x080C;
static constexpr uint16_t REG_SHIP_TASK_HIBERNATE = 0x0B00;
static constexpr uint16_t REG_SHIP_TASK_SHIP_MODE = 0x0B02;
// VBUSINSTATUS
static constexpr uint8_t VBUS_PRESENT = 0x01;
// BCHGCHARGESTATUS; bit 0 (battery detected) is reserved since C00 silicon
static constexpr uint8_t CHARGE_STATUS_COMPLETED = 0x02;
static constexpr uint8_t CHARGE_STATUS_TRICKLE = 0x04;
static constexpr uint8_t CHARGE_STATUS_CONSTANT_CURRENT = 0x08;
static constexpr uint8_t CHARGE_STATUS_CONSTANT_VOLTAGE = 0x10;
static constexpr uint8_t CHARGE_STATUS_DIE_TEMP_HIGH = 0x40;
// BCHGDISABLESET and BCHGDISABLECLR
static constexpr uint8_t CHARGER_DISABLE_RECHARGE = 0x01;
static constexpr uint8_t CHARGER_DISABLE_NTC = 0x02;
// BUCKSTATUS power good, LDSWSTATUS powered up as load switch or as LDO
static constexpr uint8_t BUCK_POWER_GOOD[2] = {0x04, 0x40};
static constexpr uint8_t LDSW_POWERED[2] = {0x03, 0x0C};
// TIMERCONFIG: wake-up timer with the 16 ms prescaler
static constexpr uint8_t TIMER_CONFIG_WAKEUP = 0x04;
static constexpr uint32_t TIMER_TICK_MS = 16;
static constexpr uint32_t TIMER_MAX_TICKS = 0xFFFFFF;
// BCHGISETDISCHARGE codes up to this one select the 200 mA discharge limit, the others 1000 mA
static constexpr uint16_t DISCHARGE_LIMIT_200MA_CODE = 84;
static uint16_t discharge_limit_ma(uint16_t code) { return code <= DISCHARGE_LIMIT_200MA_CODE ? 200 : 1000; }
// BCHGISET and BCHGISETDISCHARGE hold a 9-bit code: the high 8 bits, then bit 0 in the next register
static void split_9bit(uint16_t code, uint8_t *out) {
out[0] = code >> 1;
out[1] = code & 1;
}
#ifdef USE_NPM1300_DATA_PUBLISHER
static uint16_t join_9bit(const uint8_t *in) { return (in[0] << 1) | (in[1] & 1); }
// ADCIBATMEASSTATUS
static constexpr uint8_t IBAT_MEAS_INVALID = 0x10;
static constexpr uint8_t IBAT_MODE_SHIFT = 2;
static constexpr uint8_t IBAT_MODE_DISCHARGE = 0x01;
static constexpr uint8_t IBAT_MODE_CHARGE = 0x03;
// Full scale of the battery current measurement per milliampere of the charge and discharge limits
static constexpr float CHARGE_FULL_SCALE_FACTOR = 1.25f;
static constexpr float DISCHARGE_FULL_SCALE_FACTOR = 1.12f;
static constexpr float ADC_MAX_CODE = 1023.0f;
// Five conversions of 250 us each; the loop runs the timeout later than this anyway
static constexpr uint32_t CONVERSION_TIME_MS = 2;
static constexpr uint32_t READ_TIMEOUT_ID = 0;
float ntc_temperature(uint16_t code, float inv_beta) {
if (code == 0)
return NAN;
// The NTC divider gives code = 1024 * R / (R + R_nominal)
return 1.0f / (1.0f / 298.15f - logf(1024.0f / code - 1.0f) * inv_beta) - 273.15f;
}
#endif
#ifdef USE_TEXT_SENSOR
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Error", "Paused, Die Too Hot", "Charge Complete", "Trickle Charge",
"Constant Current", "Constant Voltage", "Discharging", "Not Charging");
// BCHGCHARGESTATUS bits in order of priority; bit i is reported as string i + 1
static const uint8_t CHARGER_STATUS_BITS[] PROGMEM = {CHARGE_STATUS_DIE_TEMP_HIGH, CHARGE_STATUS_COMPLETED,
CHARGE_STATUS_TRICKLE, CHARGE_STATUS_CONSTANT_CURRENT,
CHARGE_STATUS_CONSTANT_VOLTAGE};
static uint8_t charger_status_index(uint8_t vbus_status, uint8_t charge_status, uint8_t error_reason) {
if (error_reason != 0)
return 0;
for (size_t i = 0; i < sizeof(CHARGER_STATUS_BITS); i++) {
if ((charge_status & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
return i + 1;
}
return (vbus_status & VBUS_PRESENT) == 0 ? 6 : 7;
}
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
uint8_t error_reason) {
const uint8_t index = charger_status_index(vbus_status, charge_status, error_reason);
if (index == last)
return;
last = index;
sensor->publish_state(ChargerStatusStrings::get_progmem_str(index, ChargerStatusStrings::LAST_INDEX));
}
#endif
NPM1300Config NPM1300Component::load_config_() const {
NPM1300Config config;
progmem_memcpy(&config, this->config_, sizeof(config));
return config;
}
void NPM1300Component::setup() {
uint8_t status;
if (!this->setup_check(this->read_byte_reg16(REG_BCHG_CHARGE_STATUS, &status)))
return;
// Raise the VBUS current limit before anything turns on a regulator: at the power-on limit of 100 mA the
// inrush current of a regulator can trip the over-current protection and brown out the board
if (this->vbus_current_limit_ != NPM1300_UNSET &&
!this->setup_check(this->write_byte_reg16(REG_VBUSIN_ILIM_STARTUP, this->vbus_current_limit_) &&
this->apply_vbus_current_limit_()))
return;
const NPM1300Config config = this->load_config_();
if (config.thermistor != NPM1300_UNSET &&
!this->setup_check(this->write_byte_reg16(REG_ADC_NTC_R_SEL, config.thermistor)))
return;
if (config.charger_configured && !this->setup_check(this->configure_charger_(config)))
return;
for (uint8_t i = 0; i < NPM1300_OUTPUT_COUNT; i++) {
if (!this->setup_check(this->configure_output_(i, config.outputs[i])))
return;
}
#ifdef USE_NPM1300_DATA_PUBLISHER
if (this->data_publisher_ != nullptr) {
// The battery current is measured after every battery voltage measurement
if (!this->setup_check(this->write_byte_reg16(REG_ADC_IBAT_MEAS_EN, 1)))
return;
if (config.charger_configured) {
this->charge_code_ = config.charger.current;
this->discharge_limit_high_ = discharge_limit_ma(config.charger.discharge_limit) == 1000;
} else if (!this->setup_check(this->read_charge_currents_())) {
return;
}
}
#endif
this->update();
}
bool NPM1300Component::apply_vbus_current_limit_() {
return this->write_byte_reg16(REG_VBUSIN_ILIM0, this->vbus_current_limit_) &&
this->write_byte_reg16(REG_VBUSIN_TASK_UPDATE_ILIM, 1);
}
bool NPM1300Component::configure_charger_(const NPM1300Config &config) {
const NPM1300ChargerConfig &charger = config.charger;
// The charge settings are only taken while the charger is off
if (!this->write_byte_reg16(REG_BCHG_ENABLE_CLR, 1))
return false;
uint8_t current[2];
uint8_t discharge[2];
split_9bit(charger.current, current);
split_9bit(charger.discharge_limit, discharge);
uint8_t disable = charger.recharge ? 0 : CHARGER_DISABLE_RECHARGE;
// Without a thermistor the charger reports an NTC error and does not charge
if (config.thermistor == NPM1300_UNSET)
disable |= CHARGER_DISABLE_NTC;
const uint8_t enable = (CHARGER_DISABLE_RECHARGE | CHARGER_DISABLE_NTC) & ~disable;
if (this->write_register16(REG_BCHG_ISET_MSB, current, sizeof(current)) != i2c::ERROR_OK ||
this->write_register16(REG_BCHG_ISET_DISCHARGE_MSB, discharge, sizeof(discharge)) != i2c::ERROR_OK ||
!this->write_byte_reg16(REG_BCHG_VTERM, charger.termination_voltage) ||
!this->write_byte_reg16(REG_BCHG_VTERM_R, charger.warm_termination_voltage) ||
!this->write_byte_reg16(REG_BCHG_VTRICKLE_SEL, charger.trickle_voltage) ||
!this->write_byte_reg16(REG_BCHG_ITERM_SEL, charger.termination_current) ||
!this->write_byte_reg16(REG_BCHG_DISABLE_SET, disable) || !this->write_byte_reg16(REG_BCHG_DISABLE_CLR, enable) ||
// Measure the battery temperature automatically while charging
!this->write_byte_reg16(REG_ADC_TASK_AUTO_TIM_UPDATE, 1))
return false;
return !charger.enabled || this->set_charger_enabled(true);
}
bool NPM1300Component::configure_output_(uint8_t index, const NPM1300OutputConfig &config) {
const uint8_t channel = index & 1;
if (index < 2) {
// The software voltage replaces the one set by the VSET pin only once its SWCTRLSEL bit is set
if (config.voltage != NPM1300_UNSET &&
(!this->write_byte_reg16(REG_BUCK1_NORM_VOUT + channel * 2, config.voltage) ||
!this->update_register16(REG_BUCK_SW_CTRL_SEL, 1 << channel, 1 << channel)))
return false;
} else if ((config.voltage != NPM1300_UNSET &&
!this->write_byte_reg16(REG_LDSW1_VOUT_SEL + channel, config.voltage)) ||
(config.ldo_mode != NPM1300_UNSET &&
!this->write_byte_reg16(REG_LDSW1_LDO_SEL + channel, config.ldo_mode))) {
return false;
}
return config.enabled == NPM1300_UNSET ||
this->set_output_enabled(static_cast<NPM1300Output>(index), config.enabled != 0);
}
#ifdef USE_NPM1300_DATA_PUBLISHER
bool NPM1300Component::read_charge_currents_() {
uint8_t charge[2];
uint8_t discharge[2];
if (this->read_register16(REG_BCHG_ISET_MSB, charge, sizeof(charge)) != i2c::ERROR_OK ||
this->read_register16(REG_BCHG_ISET_DISCHARGE_MSB, discharge, sizeof(discharge)) != i2c::ERROR_OK)
return false;
this->charge_code_ = join_9bit(charge);
this->discharge_limit_high_ = discharge_limit_ma(join_9bit(discharge)) == 1000;
return true;
}
#endif
bool NPM1300Component::set_charger_enabled(bool enabled) {
if (this->is_failed())
return false;
if (!enabled)
return this->write_byte_reg16(REG_BCHG_ENABLE_CLR, 1);
// A charger error stops the charger until the error is cleared and the charger released
return this->write_byte_reg16(REG_BCHG_TASK_CLEAR_CHG_ERR, 1) &&
this->write_byte_reg16(REG_BCHG_TASK_RELEASE_ERR, 1) && this->write_byte_reg16(REG_BCHG_ENABLE_SET, 1);
}
bool NPM1300Component::set_output_enabled(NPM1300Output output, bool enabled) {
if (this->is_failed())
return false;
const uint8_t index = static_cast<uint8_t>(output);
// A pair of SET and CLR task registers per output
const uint16_t base = index < 2 ? REG_BUCK1_ENA_SET : REG_LDSW1_TASK_SET;
const uint16_t reg = base + (index & 1) * 2 + (enabled ? 0 : 1);
if (!this->write_byte_reg16(reg, 1)) {
ESP_LOGW(TAG, "Turning output %u %s failed", index, enabled ? LOG_STR_LITERAL("on") : LOG_STR_LITERAL("off"));
return false;
}
return true;
}
void NPM1300Component::enter_ship_mode() {
ESP_LOGI(TAG, "Entering ship mode");
this->write_byte_reg16(REG_SHIP_TASK_SHIP_MODE, 1);
}
void NPM1300Component::hibernate(uint32_t duration_ms) {
// A zero period would never wake the chip
const uint32_t ticks = std::clamp<uint32_t>(duration_ms / TIMER_TICK_MS, 1, TIMER_MAX_TICKS);
uint8_t timer[3];
put_be24(timer, ticks);
ESP_LOGI(TAG, "Hibernating for %" PRIu32 " ms", ticks * TIMER_TICK_MS);
if (!this->write_byte_reg16(REG_TIMER_CONFIG, TIMER_CONFIG_WAKEUP) ||
this->write_register16(REG_TIMER_HI_BYTE, timer, sizeof(timer)) != i2c::ERROR_OK ||
!this->write_byte_reg16(REG_TIMER_TARGET_STROBE, 1)) {
ESP_LOGW(TAG, "Setting the wake-up timer failed");
return;
}
// The timer needs a moment to load the new period
delay(1);
this->write_byte_reg16(REG_SHIP_TASK_HIBERNATE, 1);
}
void NPM1300Component::update() {
uint8_t vbus_status;
uint8_t charge_status;
uint8_t error_reason;
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &vbus_status) &&
this->read_byte_reg16(REG_BCHG_CHARGE_STATUS, &charge_status) &&
this->read_byte_reg16(REG_BCHG_ERR_REASON, &error_reason)))
return;
// The chip goes back to its default VBUS current limit whenever VBUS is removed
const bool vbus_present = (vbus_status & VBUS_PRESENT) != 0;
if (vbus_present && !this->vbus_present_ && this->vbus_current_limit_ != NPM1300_UNSET &&
!this->status_comm_check(this->apply_vbus_current_limit_()))
return;
this->vbus_present_ = vbus_present;
if (error_reason != this->last_error_reason_) {
this->last_error_reason_ = error_reason;
if (error_reason != 0) {
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", error_reason);
}
}
#ifdef USE_NPM1300_STATUS_PUBLISHER
if (this->status_publisher_ != nullptr)
this->status_publisher_(vbus_status, charge_status, error_reason);
#endif
#ifdef USE_SWITCH
if (!this->status_comm_check(this->publish_switches_()))
return;
#endif
#ifdef USE_SENSOR
if (this->vbus_current_limit_sensor_ != nullptr) {
uint8_t code;
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_ILIM0, &code)))
return;
// 100 mA per step of the code, but code 0 is 500 mA
const uint16_t milliamps = code == 0 ? 500 : code * 100;
this->vbus_current_limit_sensor_->publish_state(milliamps / 1000.0f);
}
#endif
#ifdef USE_NPM1300_DATA_PUBLISHER
if (this->data_publisher_ != nullptr) {
// Start the battery voltage (followed by the battery current), NTC, die temperature and system voltage
// conversions, then the VBUS one
static constexpr uint8_t START[4] = {1, 1, 1, 1};
if (!this->status_comm_check(this->write_register16(REG_ADC_TASK_VBAT, START, sizeof(START)) == i2c::ERROR_OK &&
this->write_byte_reg16(REG_ADC_TASK_VBUS, 1)))
return;
this->set_timeout(READ_TIMEOUT_ID, CONVERSION_TIME_MS, [this]() { this->read_measurements_(); });
return;
}
#endif
this->status_comm_result(true);
}
#ifdef USE_SWITCH
bool NPM1300Component::publish_switches_() {
uint8_t value;
bool ok = true;
if (this->charger_switch_ != nullptr) {
if (this->read_byte_reg16(REG_BCHG_ENABLE_SET, &value)) {
this->charger_switch_->publish_state((value & 1) != 0);
} else {
ok = false;
}
}
switch_::Switch *const bucks[2] = {this->buck1_switch_, this->buck2_switch_};
if (bucks[0] != nullptr || bucks[1] != nullptr) {
if (this->read_byte_reg16(REG_BUCK_STATUS, &value)) {
for (uint8_t i = 0; i < 2; i++)
PUBLISH_STATE(bucks[i], (value & BUCK_POWER_GOOD[i]) != 0);
} else {
ok = false;
}
}
switch_::Switch *const ldos[2] = {this->ldo1_switch_, this->ldo2_switch_};
if (ldos[0] != nullptr || ldos[1] != nullptr) {
if (this->read_byte_reg16(REG_LDSW_STATUS, &value)) {
for (uint8_t i = 0; i < 2; i++)
PUBLISH_STATE(ldos[i], (value & LDSW_POWERED[i]) != 0);
} else {
ok = false;
}
}
return ok;
}
#endif
#ifdef USE_NPM1300_DATA_PUBLISHER
void NPM1300Component::read_measurements_() {
// ADCIBATMEASSTATUS, then the VBAT, NTC, TEMP and VSYS results, their low bits, VBAT0 to VBAT3 (VBAT2 holds the
// battery current and VBAT3 the VBUS voltage) and their low bits
uint8_t adc[11];
if (!this->status_comm_result(this->read_register16(REG_ADC_RESULTS, adc, sizeof(adc)) == i2c::ERROR_OK))
return;
// Amperes per code of the battery current, positive while charging and negative while discharging
float ibat_scale = NAN;
// Any other mode means the system runs from VBUS and the battery current is not measured
const uint8_t mode = (adc[0] >> IBAT_MODE_SHIFT) & 0x03;
if ((adc[0] & IBAT_MEAS_INVALID) == 0) {
if (mode == IBAT_MODE_CHARGE) {
ibat_scale = this->charge_code_ * (2 * CHARGE_FULL_SCALE_FACTOR) / (ADC_MAX_CODE * 1000.0f);
} else if (mode == IBAT_MODE_DISCHARGE) {
ibat_scale = -(this->discharge_limit_high_ ? 1000 : 200) * DISCHARGE_FULL_SCALE_FACTOR / (ADC_MAX_CODE * 1000.0f);
}
}
if (this->data_publisher_ != nullptr)
this->data_publisher_(adc, ibat_scale);
}
#endif
// BCHGVTERM: 3.50 V to 3.65 V, then 4.00 V to 4.45 V, both in 50 mV steps
static uint16_t termination_voltage_mv(uint8_t code) { return code < 4 ? 3500 + 50 * code : 4000 + 50 * (code - 4); }
static const LogString *output_name(uint8_t index) {
switch (static_cast<NPM1300Output>(index)) {
case NPM1300Output::NPM1300_OUTPUT_BUCK1:
return LOG_STR("BUCK1");
case NPM1300Output::NPM1300_OUTPUT_BUCK2:
return LOG_STR("BUCK2");
case NPM1300Output::NPM1300_OUTPUT_LDO1:
return LOG_STR("LDO1");
default:
return LOG_STR("LDO2");
}
}
void NPM1300Component::dump_config() {
ESP_LOGCONFIG(TAG, "nPM1300:");
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
if (this->vbus_current_limit_ != NPM1300_UNSET) {
// 100 mA per step of the code
ESP_LOGCONFIG(TAG, " VBUS current limit: %u mA", this->vbus_current_limit_ * 100u);
}
const NPM1300Config config = this->load_config_();
if (config.charger_configured) {
const NPM1300ChargerConfig &charger = config.charger;
ESP_LOGCONFIG(TAG,
" Charger:\n"
" Current: %u mA\n"
" Discharge limit: %u mA\n"
" Termination voltage: %u mV\n"
" Warm termination voltage: %u mV\n"
" Termination current: %u%%\n"
" Trickle voltage: %u mV\n"
" Enabled: %s\n"
" Recharge: %s",
charger.current * 2u, discharge_limit_ma(charger.discharge_limit),
termination_voltage_mv(charger.termination_voltage),
termination_voltage_mv(charger.warm_termination_voltage), charger.termination_current ? 20u : 10u,
charger.trickle_voltage ? 2500u : 2900u, YESNO(charger.enabled), YESNO(charger.recharge));
}
for (uint8_t i = 0; i < NPM1300_OUTPUT_COUNT; i++) {
const NPM1300OutputConfig &output = config.outputs[i];
if (output.voltage == NPM1300_UNSET && output.ldo_mode == NPM1300_UNSET && output.enabled == NPM1300_UNSET)
continue;
ESP_LOGCONFIG(TAG, " %s:", LOG_STR_ARG(output_name(i)));
if (output.voltage != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Voltage: %u mV", 1000u + output.voltage * 100u);
}
if (output.ldo_mode != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Mode: %s",
output.ldo_mode != 0 ? LOG_STR_LITERAL("LDO") : LOG_STR_LITERAL("load switch"));
}
if (output.enabled != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Enabled at boot: %s", YESNO(output.enabled != 0));
}
}
if (config.thermistor != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Thermistor B constant: %u", config.thermistor_beta);
}
#ifdef USE_SENSOR
LOG_SENSOR(" ", "VBUS Current Limit", this->vbus_current_limit_sensor_);
#endif
#ifdef USE_SWITCH
LOG_SWITCH(" ", "Charger", this->charger_switch_);
LOG_SWITCH(" ", "BUCK1", this->buck1_switch_);
LOG_SWITCH(" ", "BUCK2", this->buck2_switch_);
LOG_SWITCH(" ", "LDO1", this->ldo1_switch_);
LOG_SWITCH(" ", "LDO2", this->ldo2_switch_);
#endif
#if defined(USE_NPM1300_STATUS_PUBLISHER) || defined(USE_NPM1300_DATA_PUBLISHER)
if (this->dump_publisher_ != nullptr)
this->dump_publisher_(TAG);
#endif
}
} // namespace esphome::npm1300
+145
View File
@@ -0,0 +1,145 @@
#pragma once
// Product specification: https://docs.nordicsemi.com/bundle/ps_npm1300/page/keyfeatures_html5.html
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
#ifdef USE_TEXT_SENSOR
#include "esphome/components/text_sensor/text_sensor.h"
#endif
#ifdef USE_SWITCH
#include "esphome/components/switch/switch.h"
#endif
namespace esphome::npm1300 {
/// The regulators, in the order of their enable task registers
enum class NPM1300Output : uint8_t {
NPM1300_OUTPUT_BUCK1 = 0,
NPM1300_OUTPUT_BUCK2 = 1,
NPM1300_OUTPUT_LDO1 = 2,
NPM1300_OUTPUT_LDO2 = 3,
};
static constexpr uint8_t NPM1300_OUTPUT_COUNT = 4;
/// A configuration field the YAML leaves out, so the chip keeps its own value
static constexpr uint8_t NPM1300_UNSET = 0xFF;
/// Register codes for one regulator, NPM1300_UNSET where the YAML leaves the field out
struct NPM1300OutputConfig {
uint8_t voltage; // VOUT: (millivolts - 1000) / 100
uint8_t ldo_mode; // LDSWxLDOSEL, LDO1 and LDO2 only: 1 LDO, 0 load switch
uint8_t enabled; // turned on (1) or off (0) at boot
};
/// Register codes for the battery charger
struct NPM1300ChargerConfig {
uint16_t current; // BCHGISET: milliamperes / 2
uint16_t discharge_limit; // BCHGISETDISCHARGE: 84 for 200 mA, 415 for 1000 mA
uint8_t termination_voltage; // BCHGVTERM
uint8_t warm_termination_voltage; // BCHGVTERMR
uint8_t trickle_voltage; // BCHGVTRICKLESEL: 0 for 2.9 V, 1 for 2.5 V
uint8_t termination_current; // BCHGITERMSEL: 0 for 10 %, 1 for 20 %
bool recharge;
bool enabled;
};
/// The settings applied in setup(), built by code generation into a PROGMEM table
struct NPM1300Config {
NPM1300OutputConfig outputs[NPM1300_OUTPUT_COUNT];
NPM1300ChargerConfig charger; // only applied when charger_configured
uint16_t thermistor_beta;
uint8_t thermistor; // ADCNTCRSEL code: 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
bool charger_configured;
};
#ifdef USE_NPM1300_DATA_PUBLISHER
/// The 10-bit ADC result whose high 8 bits are adc[msb] and low 2 bits are at `shift` in adc[lsbs]
inline uint16_t adc_code(const uint8_t *adc, uint8_t msb, uint8_t lsbs, uint8_t shift) {
return (adc[msb] << 2) | ((adc[lsbs] >> shift) & 0x03);
}
/// Battery temperature in degrees Celsius from the NTC code and 1 / B constant of the thermistor
float ntc_temperature(uint16_t code, float inv_beta);
#endif
#ifdef USE_TEXT_SENSOR
/// Publish the charger status text, only when it differs from `last`
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
uint8_t error_reason);
#endif
class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
#ifdef USE_SENSOR
SUB_SENSOR(vbus_current_limit)
#endif
#ifdef USE_SWITCH
SUB_SWITCH(charger)
SUB_SWITCH(buck1)
SUB_SWITCH(buck2)
SUB_SWITCH(ldo1)
SUB_SWITCH(ldo2)
#endif
public:
/// `config` points into PROGMEM
explicit NPM1300Component(const NPM1300Config *config) : config_(config) {}
void setup() override;
void update() override;
void dump_config() override;
// Right after the I2C bus: the regulators may power other peripherals
float get_setup_priority() const override { return setup_priority::BUS - 1.0f; }
#ifdef USE_NPM1300_STATUS_PUBLISHER
HUB_PUBLISHER(status, uint8_t, uint8_t, uint8_t)
#endif
#ifdef USE_NPM1300_DATA_PUBLISHER
HUB_PUBLISHER(data, const uint8_t *, float)
#endif
#if defined(USE_NPM1300_STATUS_PUBLISHER) || defined(USE_NPM1300_DATA_PUBLISHER)
HUB_PUBLISHER(dump, const char *)
#endif
/// VBUSINILIM0 code: 100 mA per step (code 0, the reset value, is 500 mA)
void set_vbus_current_limit(uint8_t code) { this->vbus_current_limit_ = code; }
/// Turn the battery charger on or off; turning it on also clears a charger error
bool set_charger_enabled(bool enabled);
/// Turn a regulator on or off
bool set_output_enabled(NPM1300Output output, bool enabled);
/// Enter ship mode: every output is turned off until the SHPHLD button is pressed or VBUS is connected
void enter_ship_mode();
/// Enter hibernate mode: every output is turned off for `duration_ms`, or until the SHPHLD button is
/// pressed or VBUS is connected
void hibernate(uint32_t duration_ms);
protected:
NPM1300Config load_config_() const;
bool apply_vbus_current_limit_();
bool configure_charger_(const NPM1300Config &config);
bool configure_output_(uint8_t index, const NPM1300OutputConfig &config);
#ifdef USE_NPM1300_DATA_PUBLISHER
bool read_charge_currents_();
void read_measurements_();
#endif
#ifdef USE_SWITCH
/// Returns false when a status read failed
bool publish_switches_();
#endif
const NPM1300Config *config_;
#ifdef USE_NPM1300_DATA_PUBLISHER
// BCHGISET code and discharge limit, for the full scale of the battery current measurement
uint16_t charge_code_{0};
bool discharge_limit_high_{false};
#endif
uint8_t vbus_current_limit_{NPM1300_UNSET};
uint8_t last_error_reason_{0};
bool vbus_present_{false};
};
} // namespace esphome::npm1300
+151
View File
@@ -0,0 +1,151 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_BATTERY_VOLTAGE,
CONF_ID,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLTAGE,
ENTITY_CATEGORY_DIAGNOSTIC,
STATE_CLASS_MEASUREMENT,
UNIT_AMPERE,
UNIT_CELSIUS,
UNIT_VOLT,
)
from esphome.core import CORE
import esphome.final_validate as fv
from esphome.types import ConfigType
from . import (
ADC,
CONF_NPM1300_ID,
CONF_THERMISTOR_BETA,
CONF_THERMISTOR_RESISTANCE,
CONF_VBUS_CURRENT_LIMIT,
DATA_PUBLISHER,
DOMAIN,
DUMP,
DUMP_TAG,
IBAT_SCALE,
NPM1300_CHILD_SCHEMA,
npm1300_ns,
)
DEPENDENCIES = ["npm1300"]
CONF_BATTERY_CURRENT = "battery_current"
CONF_BATTERY_TEMPERATURE = "battery_temperature"
CONF_DIE_TEMPERATURE = "die_temperature"
CONF_SYSTEM_VOLTAGE = "system_voltage"
CONF_VBUS_VOLTAGE = "vbus_voltage"
ADC_MAX_CODE = 1023
def _adc(msb: int, lsbs: int, shift: int) -> cg.MockObj:
"""The 10-bit result in adc[msb] and bits shift..shift+1 of adc[lsbs]."""
return npm1300_ns.adc_code(ADC, msb, lsbs, shift)
# The decoded value and the dump name of each ADC sensor but the battery temperature, which needs the hub config.
# Voltages scale by their full scale (product specification, section ADC).
STATES = {
CONF_BATTERY_VOLTAGE: (_adc(1, 5, 0) * (5.0 / ADC_MAX_CODE), "Battery Voltage"),
CONF_BATTERY_CURRENT: (_adc(8, 10, 4) * IBAT_SCALE, "Battery Current"),
# Product specification: T = 394.67 - 0.7926 * code
CONF_DIE_TEMPERATURE: (394.67 - 0.7926 * _adc(3, 5, 4), "Die Temperature"),
CONF_SYSTEM_VOLTAGE: (_adc(4, 5, 6) * (6.375 / ADC_MAX_CODE), "System Voltage"),
CONF_VBUS_VOLTAGE: (_adc(9, 10, 6) * (7.5 / ADC_MAX_CODE), "VBUS Voltage"),
}
def _voltage_schema() -> cv.Schema:
return sensor.sensor_schema(
unit_of_measurement=UNIT_VOLT,
accuracy_decimals=3,
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
)
def _temperature_schema(**extra: str) -> cv.Schema:
return sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
**extra,
)
CONFIG_SCHEMA = NPM1300_CHILD_SCHEMA.extend(
{
cv.Optional(CONF_BATTERY_VOLTAGE): _voltage_schema(),
cv.Optional(CONF_BATTERY_CURRENT): sensor.sensor_schema(
unit_of_measurement=UNIT_AMPERE,
accuracy_decimals=3,
device_class=DEVICE_CLASS_CURRENT,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_BATTERY_TEMPERATURE): _temperature_schema(),
cv.Optional(CONF_DIE_TEMPERATURE): _temperature_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC
),
cv.Optional(CONF_SYSTEM_VOLTAGE): _voltage_schema(),
cv.Optional(CONF_VBUS_VOLTAGE): _voltage_schema(),
cv.Optional(CONF_VBUS_CURRENT_LIMIT): sensor.sensor_schema(
unit_of_measurement=UNIT_AMPERE,
accuracy_decimals=1,
device_class=DEVICE_CLASS_CURRENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
)
def _final_validate(config: ConfigType) -> None:
if CONF_BATTERY_TEMPERATURE not in config:
return
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
if CONF_THERMISTOR_RESISTANCE not in hub:
raise cv.Invalid(
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' "
"set on the npm1300 component",
path=[CONF_BATTERY_TEMPERATURE],
)
FINAL_VALIDATE_SCHEMA = _final_validate
def _add_adc_sensor(
hub: cg.MockObj, var: cg.MockObj, state: cg.MockObj, name: str
) -> None:
DATA_PUBLISHER.add(hub, parent_ref(var).publish_state(state))
DUMP.add(
hub,
sensor.sensor_ns.log_sensor(
DUMP_TAG,
" ",
cg.RawExpression(f'LOG_STR_LITERAL("{name}")'),
parent_ref(var),
),
)
async def to_code(config: ConfigType) -> None:
hub_id = config[CONF_NPM1300_ID]
hub = await cg.get_variable(hub_id)
sensors = sensor.sub_sensors(config)
for key, (state, name) in STATES.items():
if (var := await sensors(key, None)) is not None:
_add_adc_sensor(hub, var, state, name)
if (var := await sensors(CONF_BATTERY_TEMPERATURE, None)) is not None:
hub_config = next(c for c in CORE.config[DOMAIN] if c[CONF_ID] == hub_id)
state = npm1300_ns.ntc_temperature(
_adc(2, 5, 2), 1.0 / hub_config[CONF_THERMISTOR_BETA]
)
_add_adc_sensor(hub, var, state, "Battery Temperature")
await sensors(CONF_VBUS_CURRENT_LIMIT, hub.set_vbus_current_limit_sensor)
+95
View File
@@ -0,0 +1,95 @@
import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import ENTITY_CATEGORY_CONFIG
import esphome.final_validate as fv
from esphome.types import ConfigType
from . import (
CONF_BUCK1,
CONF_BUCK2,
CONF_CHARGER,
CONF_LDO1,
CONF_LDO2,
CONF_NPM1300_ID,
CONF_THERMISTOR_RESISTANCE,
NPM1300_CHILD_SCHEMA,
OUTPUTS,
)
DEPENDENCIES = ["npm1300"]
def _switch_schema() -> cv.Schema:
# The switches show the state of the chip, so nothing is restored at boot
return switch.switch_schema(
switch.ApplySwitch,
default_restore_mode="DISABLED",
entity_category=ENTITY_CATEGORY_CONFIG,
)
CONFIG_SCHEMA = NPM1300_CHILD_SCHEMA.extend(
{
cv.Optional(CONF_CHARGER): _switch_schema(),
cv.Optional(CONF_BUCK1): _switch_schema(),
cv.Optional(CONF_BUCK2): _switch_schema(),
cv.Optional(CONF_LDO1): _switch_schema(),
cv.Optional(CONF_LDO2): _switch_schema(),
}
)
def _final_validate(config: ConfigType) -> None:
if CONF_CHARGER not in config:
return
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
# Only the charger block turns off the NTC check, without which the charger never starts
if CONF_CHARGER not in hub and CONF_THERMISTOR_RESISTANCE not in hub:
raise cv.Invalid(
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block or "
f"'{CONF_THERMISTOR_RESISTANCE}' on the npm1300 component",
path=[CONF_CHARGER],
)
FINAL_VALIDATE_SCHEMA = _final_validate
def _output_apply(key: str) -> str:
return f"set_output_enabled({OUTPUTS[key]}, state)"
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_NPM1300_ID])
switches = switch.sub_switches(config, parent=hub)
await switches(
CONF_CHARGER,
hub.set_charger_switch,
apply="set_charger_enabled(state)",
publish="on_success",
)
await switches(
CONF_BUCK1,
hub.set_buck1_switch,
apply=_output_apply(CONF_BUCK1),
publish="on_success",
)
await switches(
CONF_BUCK2,
hub.set_buck2_switch,
apply=_output_apply(CONF_BUCK2),
publish="on_success",
)
await switches(
CONF_LDO1,
hub.set_ldo1_switch,
apply=_output_apply(CONF_LDO1),
publish="on_success",
)
await switches(
CONF_LDO2,
hub.set_ldo2_switch,
apply=_output_apply(CONF_LDO2),
publish="on_success",
)
+63
View File
@@ -0,0 +1,63 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import ICON_BATTERY
from esphome.cpp_generator import (
AssignmentExpression,
HexIntLiteral,
VariableDeclarationExpression,
)
from esphome.types import ConfigType
from . import (
CHARGE_STATUS,
CONF_NPM1300_ID,
DUMP,
DUMP_TAG,
ERROR_REASON,
NPM1300_CHILD_SCHEMA,
STATUS_PUBLISHER,
VBUS_STATUS,
npm1300_ns,
)
DEPENDENCIES = ["npm1300"]
CONF_CHARGER_STATUS = "charger_status"
CONFIG_SCHEMA = NPM1300_CHILD_SCHEMA.extend(
{
cv.Optional(CONF_CHARGER_STATUS): text_sensor.text_sensor_schema(
icon=ICON_BATTERY,
),
}
)
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_NPM1300_ID])
text_sensors = text_sensor.sub_text_sensors(config)
if (var := await text_sensors(CONF_CHARGER_STATUS, None)) is None:
return
last = cg.MockObj(f"esphome__{var}__last")
# 0xFF so the first update publishes
declaration = VariableDeclarationExpression(cg.uint8, "", last, static=True)
STATUS_PUBLISHER.add(
hub, AssignmentExpression(None, None, declaration, HexIntLiteral(0xFF))
)
STATUS_PUBLISHER.add(
hub,
npm1300_ns.publish_charger_status(
parent_ref(var), last, VBUS_STATUS, CHARGE_STATUS, ERROR_REASON
),
)
DUMP.add(
hub,
text_sensor.text_sensor_ns.log_text_sensor(
DUMP_TAG,
" ",
cg.RawExpression('LOG_STR_LITERAL("Charger Status")'),
parent_ref(var),
),
)
-9
View File
@@ -13,15 +13,6 @@ MULTI_CONF = True
rdm6300_ns = cg.esphome_ns.namespace("rdm6300")
RDM6300Component = rdm6300_ns.class_("RDM6300Component", cg.Component, uart.UARTDevice)
# Publishes the card binary sensors for each tag read; returns whether one matched
CARDS_PUBLISHER = cg.HubFunction(
"cards",
cg.bool_,
[(cg.uint32, "tag")],
define="USE_RDM6300_CARDS_PUBLISHER",
)
TAG = CARDS_PUBLISHER.param("tag")
_AUTOMATION_LISTS = (
automation.AutomationList(CONF_ON_TAG, "RDM6300_ON_TAG_COUNT", [(cg.uint32, "x")]),
)
+8 -8
View File
@@ -1,19 +1,19 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import binary_sensor, rdm6300
import esphome.config_validation as cv
from esphome.const import CONF_UID
from esphome.types import ConfigType
from . import CARDS_PUBLISHER, TAG, rdm6300_ns
from . import rdm6300_ns
DEPENDENCIES = ["rdm6300"]
CONF_RDM6300_ID = "rdm6300_id"
RDM6300BinarySensor = rdm6300_ns.class_(
"RDM6300BinarySensor", binary_sensor.BinarySensor
)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(
binary_sensor.BinarySensorInitiallyOff
).extend(
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(RDM6300BinarySensor).extend(
{
cv.GenerateID(CONF_RDM6300_ID): cv.use_id(rdm6300.RDM6300Component),
cv.Required(CONF_UID): cv.uint32_t,
@@ -23,7 +23,7 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(
async def to_code(config: ConfigType) -> None:
var = await binary_sensor.new_binary_sensor(config)
hub = await cg.get_variable(config[CONF_RDM6300_ID])
CARDS_PUBLISHER.add(
hub, rdm6300_ns.publish_card(parent_ref(var), TAG, config[CONF_UID])
)
cg.add(hub.register_card(var))
cg.add(var.set_id(config[CONF_UID]))
+5 -4
View File
@@ -48,10 +48,11 @@ void rdm6300::RDM6300Component::loop() {
this->status_clear_warning();
const uint32_t result = encode_uint32(this->buffer_[1], this->buffer_[2], this->buffer_[3], this->buffer_[4]);
bool report = result != last_id_;
#ifdef USE_RDM6300_CARDS_PUBLISHER
if (this->cards_publisher_ != nullptr && this->cards_publisher_(result))
report = false;
#endif
for (auto *card : this->cards_) {
if (card->process(result)) {
report = false;
}
}
#ifdef RDM6300_ON_TAG_COUNT
this->tag_automations_.trigger(result);
#endif
+23 -14
View File
@@ -2,31 +2,21 @@
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/uart/uart.h"
#include <cinttypes>
#include <vector>
namespace esphome::rdm6300 {
// A template on the sensor type keeps binary_sensor.h out of this hub header
template<typename T> inline bool publish_card(T *sensor, uint32_t tag, uint32_t card) {
if (tag != card)
return false;
sensor->publish_state(true);
yield();
sensor->publish_state(false);
return true;
}
class RDM6300BinarySensor;
class RDM6300Component final : public Component, public uart::UARTDevice {
public:
void loop() override;
#ifdef USE_RDM6300_CARDS_PUBLISHER
HUB_PUBLISHER_RETURNING(bool, cards, uint32_t)
#endif
void register_card(RDM6300BinarySensor *obj) { this->cards_.push_back(obj); }
#ifdef RDM6300_ON_TAG_COUNT
void add_on_tag_automation(Automation<uint32_t> *automation) { this->tag_automations_.add(automation); }
#endif
@@ -34,10 +24,29 @@ class RDM6300Component final : public Component, public uart::UARTDevice {
protected:
int8_t read_state_{-1};
uint8_t buffer_[6]{};
std::vector<RDM6300BinarySensor *> cards_;
#ifdef RDM6300_ON_TAG_COUNT
AutomationList<RDM6300_ON_TAG_COUNT, uint32_t> tag_automations_;
#endif
uint32_t last_id_{0};
};
class RDM6300BinarySensor final : public binary_sensor::BinarySensorInitiallyOff {
public:
void set_id(uint32_t id) { id_ = id; }
bool process(uint32_t id) {
if (this->id_ == id) {
this->publish_state(true);
yield();
this->publish_state(false);
return true;
}
return false;
}
protected:
uint32_t id_;
};
} // namespace esphome::rdm6300
@@ -291,25 +291,21 @@ bool ShellyDimmer::send_command_(uint8_t cmd, const uint8_t *const payload, uint
}
size_t ShellyDimmer::frame_command_(uint8_t *data, uint8_t cmd, const uint8_t *const payload, size_t len) {
size_t pos = 0;
// Generate a frame.
data[0] = SHELLY_DIMMER_PROTO_START_BYTE;
data[1] = ++this->seq_;
data[2] = cmd;
data[3] = len;
pos += 4;
uint8_t *p = data;
*p++ = SHELLY_DIMMER_PROTO_START_BYTE;
*p++ = ++this->seq_;
*p++ = cmd;
*p++ = len;
if (payload != nullptr) {
std::memcpy(data + 4, payload, len);
pos += len;
std::memcpy(p, payload, len);
p += len;
}
// Calculate checksum for the payload.
put_be16(data + pos, shelly_dimmer_checksum(data + 1, 3 + len));
pos += 2;
data[pos++] = SHELLY_DIMMER_PROTO_END_BYTE;
return pos;
// Checksum over the sequence, command, length and payload bytes
p = put_be16(p, shelly_dimmer_checksum(data + 1, 3 + len));
*p++ = SHELLY_DIMMER_PROTO_END_BYTE;
return p - data;
}
int ShellyDimmer::handle_byte_(uint8_t c) {
-9
View File
@@ -30,15 +30,6 @@ DOMAIN = "sx1509"
MULTI_CONF = True
sx1509_ns = cg.esphome_ns.namespace("sx1509")
# Publishes the keypad binary sensors from the key data read on each scan
KEYPAD_PUBLISHER = cg.HubFunction(
"keypad",
cg.void,
[(cg.uint16, "key_data")],
define="USE_SX1509_KEYPAD_PUBLISHER",
)
KEY_DATA = KEYPAD_PUBLISHER.param("key_data")
LedDriverFrequency = sx1509_ns.enum("LedDriverFrequency")
# The PWM frequency is shared by all output pins; the values are nominal (see sx1509.h).
@@ -1,16 +1,16 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_COL, CONF_ROW
from esphome.core import HexInt
from esphome.types import ConfigType
from .. import CONF_SX1509_ID, KEY_DATA, KEYPAD_PUBLISHER, SX1509Component, sx1509_ns
from .. import CONF_SX1509_ID, SX1509Component, sx1509_ns
DEPENDENCIES = ["sx1509"]
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
SX1509BinarySensor = sx1509_ns.class_("SX1509BinarySensor", binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(SX1509BinarySensor).extend(
{
cv.GenerateID(CONF_SX1509_ID): cv.use_id(SX1509Component),
cv.Required(CONF_ROW): cv.int_range(min=0, max=7),
@@ -22,6 +22,6 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
async def to_code(config: ConfigType) -> None:
var = await binary_sensor.new_binary_sensor(config)
hub = await cg.get_variable(config[CONF_SX1509_ID])
# The key data has the column bit in the high byte and the row bit in the low byte
key = HexInt((1 << (config[CONF_COL] + 8)) | (1 << config[CONF_ROW]))
KEYPAD_PUBLISHER.add(hub, sx1509_ns.publish_key(parent_ref(var), KEY_DATA, key))
cg.add(var.set_row_col(config[CONF_ROW], config[CONF_COL]))
cg.add(hub.register_keypad_binary_sensor(var))
@@ -0,0 +1,17 @@
#pragma once
#include "esphome/components/sx1509/sx1509.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
namespace esphome::sx1509 {
class SX1509BinarySensor final : public sx1509::SX1509Processor, public binary_sensor::BinarySensor {
public:
void set_row_col(uint8_t row, uint8_t col) { this->key_ = (1 << (col + 8)) | (1 << row); }
void process(uint16_t data) override { this->publish_state(static_cast<bool>(data == key_)); }
protected:
uint16_t key_{0};
};
} // namespace esphome::sx1509
+2 -4
View File
@@ -40,10 +40,8 @@ void SX1509Component::loop() {
return;
this->last_loop_timestamp_ = millis();
uint16_t key_data = this->read_key_data();
#ifdef USE_SX1509_KEYPAD_PUBLISHER
if (this->keypad_publisher_ != nullptr)
this->keypad_publisher_(key_data);
#endif
for (auto *binary_sensor : this->keypad_binary_sensors_)
binary_sensor->process(key_data);
if (this->keys_ == nullptr)
return;
if (key_data == 0) {
+11 -8
View File
@@ -6,10 +6,11 @@
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/components/gpio_expander/expander_gpio_pin.h"
#include "sx1509_registers.h"
#include <vector>
namespace esphome::sx1509 {
// These are used for clock config:
@@ -35,10 +36,11 @@ const uint8_t REG_I_ON[16] = {REG_I_ON_0, REG_I_ON_1, REG_I_ON_2, REG_I_ON_3,
REG_I_ON_6, REG_I_ON_7, REG_I_ON_8, REG_I_ON_9, REG_I_ON_10, REG_I_ON_11,
REG_I_ON_12, REG_I_ON_13, REG_I_ON_14, REG_I_ON_15};
// A template on the sensor type keeps binary_sensor.h out of this hub header
template<typename T> inline void publish_key(T *sensor, uint16_t key_data, uint16_t key) {
sensor->publish_state(key_data == key);
}
// for all components that implement the process(uint16_t data )
class SX1509Processor {
public:
virtual void process(uint16_t data) {};
};
class SX1509Component final : public Component,
public i2c::I2CDevice,
@@ -65,9 +67,9 @@ class SX1509Component final : public Component,
void set_sleep_time(uint16_t sleep_time) { this->sleep_time_ = sleep_time; };
void set_scan_time(uint8_t scan_time) { this->scan_time_ = scan_time; };
void set_debounce_time(uint8_t debounce_time = 1) { this->debounce_time_ = debounce_time; };
#ifdef USE_SX1509_KEYPAD_PUBLISHER
HUB_PUBLISHER(keypad, uint16_t)
#endif
void register_keypad_binary_sensor(SX1509Processor *binary_sensor) {
this->keypad_binary_sensors_.push_back(binary_sensor);
}
#ifdef SX1509_ON_KEY_COUNT
void add_on_key_automation(Automation<uint8_t> *automation) { this->key_automations_.add(automation); }
#endif
@@ -93,6 +95,7 @@ class SX1509Component final : public Component,
uint8_t scan_time_ = 1;
uint8_t debounce_time_ = 1;
uint8_t last_key_ = 0;
std::vector<SX1509Processor *> keypad_binary_sensors_;
#ifdef SX1509_ON_KEY_COUNT
AutomationList<SX1509_ON_KEY_COUNT, uint8_t> key_automations_;
#endif
@@ -12,15 +12,6 @@ ttp229_bsf_ns = cg.esphome_ns.namespace("ttp229_bsf")
TTP229BSFComponent = ttp229_bsf_ns.class_("TTP229BSFComponent", cg.Component)
# Publishes the channel binary sensors from the touch status read on each loop
CHANNELS_PUBLISHER = cg.HubFunction(
"channels",
cg.void,
[(cg.uint16, "touched")],
define="USE_TTP229_BSF_CHANNELS_PUBLISHER",
)
TOUCHED = CHANNELS_PUBLISHER.param("touched")
MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema(
{
+6 -13
View File
@@ -1,21 +1,15 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_CHANNEL
from esphome.types import ConfigType
from . import (
CHANNELS_PUBLISHER,
CONF_TTP229_ID,
TOUCHED,
TTP229BSFComponent,
ttp229_bsf_ns,
)
from . import CONF_TTP229_ID, TTP229BSFComponent, ttp229_bsf_ns
DEPENDENCIES = ["ttp229_bsf"]
TTP229BSFChannel = ttp229_bsf_ns.class_("TTP229BSFChannel", binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(TTP229BSFChannel).extend(
{
cv.GenerateID(CONF_TTP229_ID): cv.use_id(TTP229BSFComponent),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=15),
@@ -25,8 +19,7 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
async def to_code(config: ConfigType) -> None:
var = await binary_sensor.new_binary_sensor(config)
cg.add(var.set_channel(config[CONF_CHANNEL]))
hub = await cg.get_variable(config[CONF_TTP229_ID])
CHANNELS_PUBLISHER.add(
hub,
ttp229_bsf_ns.publish_channel(parent_ref(var), TOUCHED, config[CONF_CHANNEL]),
)
cg.add(hub.register_channel(var))
+16 -12
View File
@@ -2,22 +2,26 @@
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include <vector>
namespace esphome::ttp229_bsf {
// A template on the sensor type keeps binary_sensor.h out of this hub header
template<typename T> inline void publish_channel(T *sensor, uint16_t touched, uint8_t channel) {
sensor->publish_state((touched >> channel) & 1);
}
class TTP229BSFChannel final : public binary_sensor::BinarySensor {
public:
void set_channel(uint8_t channel) { channel_ = channel; }
void process(uint16_t data) { this->publish_state(data & (1 << this->channel_)); }
protected:
uint8_t channel_;
};
class TTP229BSFComponent final : public Component {
public:
void set_sdo_pin(GPIOPin *sdo_pin) { sdo_pin_ = sdo_pin; }
void set_scl_pin(GPIOPin *scl_pin) { scl_pin_ = scl_pin; }
#ifdef USE_TTP229_BSF_CHANNELS_PUBLISHER
HUB_PUBLISHER(channels, uint16_t)
#endif
void register_channel(TTP229BSFChannel *channel) { this->channels_.push_back(channel); }
void setup() override;
void dump_config() override;
void loop() override {
@@ -35,15 +39,15 @@ class TTP229BSFComponent final : public Component {
touched |= uint16_t(bitval) << i;
}
#ifdef USE_TTP229_BSF_CHANNELS_PUBLISHER
if (this->channels_publisher_ != nullptr)
this->channels_publisher_(touched);
#endif
for (auto *channel : this->channels_) {
channel->process(touched);
}
}
protected:
GPIOPin *sdo_pin_;
GPIOPin *scl_pin_;
std::vector<TTP229BSFChannel *> channels_{};
};
} // namespace esphome::ttp229_bsf
@@ -14,15 +14,6 @@ TTP229LSFComponent = ttp229_lsf_ns.class_(
"TTP229LSFComponent", cg.Component, i2c.I2CDevice
)
# Publishes the channel binary sensors from the touch status read on each loop
CHANNELS_PUBLISHER = cg.HubFunction(
"channels",
cg.void,
[(cg.uint16, "touched")],
define="USE_TTP229_LSF_CHANNELS_PUBLISHER",
)
TOUCHED = CHANNELS_PUBLISHER.param("touched")
MULTI_CONF = True
CONFIG_SCHEMA = (
cv.Schema(
+6 -13
View File
@@ -1,21 +1,15 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_CHANNEL
from esphome.types import ConfigType
from . import (
CHANNELS_PUBLISHER,
CONF_TTP229_ID,
TOUCHED,
TTP229LSFComponent,
ttp229_lsf_ns,
)
from . import CONF_TTP229_ID, TTP229LSFComponent, ttp229_lsf_ns
DEPENDENCIES = ["ttp229_lsf"]
TTP229Channel = ttp229_lsf_ns.class_("TTP229Channel", binary_sensor.BinarySensor)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(TTP229Channel).extend(
{
cv.GenerateID(CONF_TTP229_ID): cv.use_id(TTP229LSFComponent),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=15),
@@ -25,8 +19,7 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema().extend(
async def to_code(config: ConfigType) -> None:
var = await binary_sensor.new_binary_sensor(config)
cg.add(var.set_channel(config[CONF_CHANNEL]))
hub = await cg.get_variable(config[CONF_TTP229_ID])
CHANNELS_PUBLISHER.add(
hub,
ttp229_lsf_ns.publish_channel(parent_ref(var), TOUCHED, config[CONF_CHANNEL]),
)
cg.add(hub.register_channel(var))
+3 -4
View File
@@ -20,10 +20,9 @@ void TTP229LSFComponent::loop() {
return;
touched = i2c::i2ctohs(touched);
touched = reverse_bits(touched);
#ifdef USE_TTP229_LSF_CHANNELS_PUBLISHER
if (this->channels_publisher_ != nullptr)
this->channels_publisher_(touched);
#endif
for (auto *channel : this->channels_) {
channel->process(touched);
}
}
} // namespace esphome::ttp229_lsf
+15 -8
View File
@@ -1,24 +1,31 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/i2c/i2c.h"
#include <vector>
namespace esphome::ttp229_lsf {
// A template on the sensor type keeps binary_sensor.h out of this hub header
template<typename T> inline void publish_channel(T *sensor, uint16_t touched, uint8_t channel) {
sensor->publish_state((touched >> channel) & 1);
}
class TTP229Channel final : public binary_sensor::BinarySensor {
public:
void set_channel(uint8_t channel) { channel_ = channel; }
void process(uint16_t data) { this->publish_state(data & (1 << this->channel_)); }
protected:
uint8_t channel_;
};
class TTP229LSFComponent final : public Component, public i2c::I2CDevice {
public:
#ifdef USE_TTP229_LSF_CHANNELS_PUBLISHER
HUB_PUBLISHER(channels, uint16_t)
#endif
void register_channel(TTP229Channel *channel) { this->channels_.push_back(channel); }
void setup() override;
void dump_config() override;
void loop() override;
protected:
std::vector<TTP229Channel *> channels_{};
};
} // namespace esphome::ttp229_lsf
+2 -6
View File
@@ -70,7 +70,6 @@
#define USE_BUTTON
#define USE_CAMERA
#define USE_CANBUS_FD
#define USE_CAP1188_CHANNELS_PUBLISHER
#define USE_CC1101_LONG_PACKET
#define USE_CLIMATE
#define USE_CLIMATE_VISUAL_OVERRIDES
@@ -225,7 +224,6 @@
#define MODBUS_ON_REQUEST_COUNT 1
#define MODBUS_ON_RESPONSE_COUNT 1
#define USE_MOTION_DATA_PUBLISHER
#define USE_MPR121_CHANNEL_PUBLISHERS
#define NFC_TAG_LISTENER_COUNT 1
#define PN532_BINARY_SENSOR_COUNT 1
#define PN532_ON_TAG_REMOVED_TRIGGER_COUNT 1
@@ -239,7 +237,6 @@
#define RC522_ON_TAG_COUNT 1
#define RC522_ON_TAG_REMOVED_COUNT 1
#define RDM6300_ON_TAG_COUNT 1
#define USE_RDM6300_CARDS_PUBLISHER
#define REMOTE_BASE_DUMPER_COUNT 1
#define REMOTE_BASE_LISTENER_COUNT 1
#define SX1509_ON_KEY_COUNT 1
@@ -323,6 +320,8 @@
#define USE_NEXTION_TRIGGER_CUSTOM_SWITCH
#define USE_NEXTION_TRIGGER_CUSTOM_TEXT_SENSOR
#define USE_NEXTION_WAVEFORM
#define USE_NPM1300_DATA_PUBLISHER
#define USE_NPM1300_STATUS_PUBLISHER
#define USE_NUMBER
#define USE_OTA_STATE_LISTENER
#define USE_OUTPUT
@@ -365,7 +364,6 @@
#define USE_STATUS_SENSOR
#define USE_SWITCH
#define USE_SWITCH_STORAGE
#define USE_SX1509_KEYPAD_PUBLISHER
#define USE_SY6970_DATA_PUBLISHER
#define USE_TCA8418_KEY_PUBLISHER
#define USE_TEXT
@@ -381,8 +379,6 @@
#define USE_TMP102_TEXT_SENSOR
#define USE_TOSHIBA_RAS_25J2KVSG_ND
#define USE_TOUCHSCREEN
#define USE_TTP229_BSF_CHANNELS_PUBLISHER
#define USE_TTP229_LSF_CHANNELS_PUBLISHER
#define USE_UART_DEBUGGER
#define USE_UART_WAKE_LOOP_ON_RX
#define USE_UFM01_CLEAR_ACCUMULATED_FLOW_ACTION
+2 -1
View File
@@ -18,7 +18,6 @@ from pathlib import Path
import re
from typing import Any, NamedTuple
from urllib.parse import urlparse
from urllib.request import url2pathname
from esphome import loader, yaml_util
from esphome.components.external_components import ExternalSource, get_sources
@@ -201,6 +200,8 @@ def _esphome_vcs() -> _Source | None:
if commit:
return _Source(strip_url_credentials(url), commit)
if "dir_info" in info and url.startswith("file:"):
from urllib.request import url2pathname
# An editable or local install may be a fork or a patched clone
if (
source := _checkout_vcs(Path(url2pathname(urlparse(url).path)))
+3 -1
View File
@@ -17,7 +17,6 @@ import threading
import time
from typing import IO, TYPE_CHECKING
from urllib.parse import urlparse
from urllib.request import url2pathname
from esphome.helpers import ProgressBar, get_usable_cpu_count, rmtree
from esphome.net_retry import (
@@ -1363,6 +1362,9 @@ def local_path_from_url(url: str) -> Path:
Handles ``file:///home/me/x`` and ``file:///C:/x`` (no host), ``file://localhost/x``
and ``file://server/share/x`` (a UNC path).
"""
# urllib.request loads http.client and the email parser, which config validation does not need
from urllib.request import url2pathname
from esphome.core import EsphomeError
try:
+2 -1
View File
@@ -26,7 +26,6 @@ from pathlib import Path, PurePosixPath
import re
from typing import Any
from urllib.parse import urlsplit, urlunsplit
from urllib.request import url2pathname
from esphome import git
from esphome.core import CORE, EsphomeError, Library
@@ -961,6 +960,8 @@ def _node_key(
f"{scheme}:// library URL '{repository}' must be an absolute "
f"directory path, e.g. {scheme}:///path/to/lib"
)
from urllib.request import url2pathname
path = url2pathname(url_path)
return (name or PurePosixPath(url_path).name), "local", (path, None)
key = str(split_result.path).strip("/").removesuffix(".git")
@@ -1,24 +0,0 @@
esphome:
name: test
esp32:
board: esp32dev
i2c:
sda: GPIO21
scl: GPIO22
cap1188:
- id: hub
- id: idle_hub
address: 0x28
binary_sensor:
- platform: cap1188
cap1188_id: hub
id: key0
channel: 0
- platform: cap1188
cap1188_id: hub
id: key7
channel: 7
@@ -1,21 +0,0 @@
"""Tests for the function that publishes the cap1188 channel binary sensors."""
from collections.abc import Callable
from pathlib import Path
def test_channels_publish_through_hub_function(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("hub_function.yaml"))
assert (
"static void esphome__hub__channels_fn(uint8_t esphome__arg_touched) {\n"
" cap1188::publish_channel(::key0, esphome__arg_touched, 0);\n"
" cap1188::publish_channel(::key7, esphome__arg_touched, 7);\n"
"}"
) in main_cpp
assert "hub->set_channels_publisher(esphome__hub__channels_fn);" in main_cpp
# A hub without channels keeps a null publisher
assert "idle_hub->set_channels_publisher" not in main_cpp
Whitespace-only changes.
@@ -1,27 +0,0 @@
esphome:
name: test
esp32:
board: esp32dev
i2c:
sda: GPIO21
scl: GPIO22
mpr121:
- id: hub
touch_threshold: 0x10
release_threshold: 0x08
- id: idle_hub
address: 0x5B
binary_sensor:
- platform: mpr121
mpr121_id: hub
id: key0
channel: 0
- platform: mpr121
mpr121_id: hub
id: key3
channel: 3
touch_threshold: 0x20
@@ -1,29 +0,0 @@
"""Tests for the function that publishes the mpr121 channel binary sensors."""
from collections.abc import Callable
from pathlib import Path
def test_channels_write_thresholds_and_publish_through_hub_functions(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("hub_function.yaml"))
# A channel without its own thresholds takes the hub's (16 and 8)
assert (
"static void esphome__hub__thresholds_fn() {\n"
" hub->write_thresholds(0, 16, 8);\n"
" hub->write_thresholds(3, 32, 8);\n"
"}"
) in main_cpp
assert (
"static void esphome__hub__channels_fn(uint16_t esphome__arg_touched) {\n"
" mpr121::publish_channel(::key0, esphome__arg_touched, 0);\n"
" mpr121::publish_channel(::key3, esphome__arg_touched, 3);\n"
"}"
) in main_cpp
assert "hub->set_thresholds_publisher(esphome__hub__thresholds_fn);" in main_cpp
assert "hub->set_channels_publisher(esphome__hub__channels_fn);" in main_cpp
assert "idle_hub->set_thresholds_publisher" not in main_cpp
assert "idle_hub->set_channels_publisher" not in main_cpp
@@ -0,0 +1,114 @@
"""Tests for the npm1300 register encoders and validators."""
import pytest
from esphome import config_validation as cv
from esphome.components.const import CONF_ENABLED
from esphome.components.npm1300 import (
CHARGER_SCHEMA,
NPM1300_UNSET,
OUTPUT_VOLTAGE,
VBUS_CURRENT_LIMIT,
_charger_config,
_output_config,
_termination_voltage_code,
_vbus_current_limit_code,
)
from esphome.const import CONF_MODE, CONF_VOLTAGE
from esphome.cpp_generator import StructInitializer
from esphome.types import ConfigType
def _fields(init: StructInitializer) -> dict[str, str]:
return {key: str(value) for key, value in init.args.items()}
def _charger(config: ConfigType) -> dict[str, str]:
return _fields(_charger_config(CHARGER_SCHEMA(config)))
@pytest.mark.parametrize(
("current", "code"), [("32mA", 16), ("150mA", 75), ("800mA", 400)]
)
def test_charge_current_code(current: str, code: int) -> None:
assert _charger({"charge_current": current})["current"] == str(code)
@pytest.mark.parametrize("current", ["30mA", "802mA", "151mA"])
def test_charge_current_rejected(current: str) -> None:
with pytest.raises(cv.Invalid):
CHARGER_SCHEMA({"charge_current": current})
@pytest.mark.parametrize(
("voltage", "code"),
[(3.5, 0), (3.65, 3), (4.0, 4), (4.2, 8), (4.45, 13)],
)
def test_termination_voltage_code(voltage: float, code: int) -> None:
assert _termination_voltage_code(voltage) == code
@pytest.mark.parametrize("voltage", ["3.7V", "3.95V", "4.5V"])
def test_termination_voltage_gap_rejected(voltage: str) -> None:
with pytest.raises(cv.Invalid):
CHARGER_SCHEMA({"charge_current": "100mA", "termination_voltage": voltage})
def test_warm_termination_defaults_to_termination() -> None:
fields = _charger({"charge_current": "100mA", "termination_voltage": "4.1V"})
assert fields["termination_voltage"] == "6"
assert fields["warm_termination_voltage"] == "6"
def test_warm_termination_code() -> None:
fields = _charger(
{
"charge_current": "100mA",
"termination_voltage": "4.2V",
"warm_termination_voltage": "3.6V",
}
)
assert fields["termination_voltage"] == "8"
assert fields["warm_termination_voltage"] == "2"
@pytest.mark.parametrize(("voltage", "code"), [("1.0V", 0), ("1.8V", 8), ("3.3V", 23)])
def test_output_voltage_code(voltage: str, code: int) -> None:
config = {CONF_VOLTAGE: OUTPUT_VOLTAGE(voltage)}
assert _fields(_output_config(config))["voltage"] == str(code)
@pytest.mark.parametrize("voltage", ["0.9V", "3.4V", "1.85V"])
def test_output_voltage_rejected(voltage: str) -> None:
with pytest.raises(cv.Invalid):
OUTPUT_VOLTAGE(voltage)
@pytest.mark.parametrize("config", [None, {}])
def test_unset_output_fields(config: ConfigType | None) -> None:
unset = str(NPM1300_UNSET)
assert _fields(_output_config(config)) == {
"voltage": unset,
"ldo_mode": unset,
"enabled": unset,
}
def test_output_mode_and_enabled() -> None:
fields = _fields(_output_config({CONF_MODE: "load_switch", CONF_ENABLED: True}))
assert fields["ldo_mode"] == "0"
assert fields["enabled"] == "1"
assert fields["voltage"] == str(NPM1300_UNSET)
@pytest.mark.parametrize(
("limit", "code"), [("100mA", 1), ("300mA", 3), ("500mA", 5), ("1.5A", 15)]
)
def test_vbus_current_limit_code(limit: str, code: int) -> None:
assert _vbus_current_limit_code(VBUS_CURRENT_LIMIT(limit)) == code
@pytest.mark.parametrize("limit", ["50mA", "1600mA", "550mA"])
def test_vbus_current_limit_rejected(limit: str) -> None:
with pytest.raises(cv.Invalid):
VBUS_CURRENT_LIMIT(limit)
Whitespace-only changes.
@@ -1,29 +0,0 @@
esphome:
name: test
esp32:
board: esp32dev
uart:
- id: uart_reader
rx_pin: GPIO16
baud_rate: 9600
- id: uart_idle
rx_pin: GPIO17
baud_rate: 9600
rdm6300:
- id: reader
uart_id: uart_reader
- id: idle_reader
uart_id: uart_idle
binary_sensor:
- platform: rdm6300
rdm6300_id: reader
id: card_a
uid: 7616525
- platform: rdm6300
rdm6300_id: reader
id: card_b
uid: 4294967295
@@ -1,27 +0,0 @@
"""Tests for the function that publishes the rdm6300 card binary sensors."""
from collections.abc import Callable
from pathlib import Path
def test_cards_publish_through_hub_function(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("hub_function.yaml"))
# A bool function: every card is checked, and it returns whether one matched
assert (
"static bool esphome__reader__cards_fn(uint32_t esphome__arg_tag) {\n"
" bool esphome__found = false;\n"
" (esphome__found |= rdm6300::publish_card(::card_a, esphome__arg_tag, 7616525));\n"
" (esphome__found |= rdm6300::publish_card(::card_b, esphome__arg_tag, 4294967295UL));\n"
" return esphome__found;\n"
"}"
) in main_cpp
assert "reader->set_cards_publisher(esphome__reader__cards_fn);" in main_cpp
# Cards start OFF, as before; they only pulse ON when their tag is read
for card in ("card_a", "card_b"):
assert f"new({card}) binary_sensor::BinarySensorInitiallyOff();" in main_cpp
# A reader without cards keeps a null publisher
assert "idle_reader->set_cards_publisher" not in main_cpp
@@ -1,29 +0,0 @@
esphome:
name: test
esp32:
board: esp32dev
i2c:
sda: GPIO21
scl: GPIO22
sx1509:
- id: hub
keypad:
key_rows: 2
key_columns: 2
- id: idle_hub
address: 0x3F
binary_sensor:
- platform: sx1509
sx1509_id: hub
id: key_r0_c0
row: 0
col: 0
- platform: sx1509
sx1509_id: hub
id: key_r1_c1
row: 1
col: 1
@@ -1,21 +0,0 @@
"""Tests for the function that publishes the sx1509 keypad binary sensors."""
from collections.abc import Callable
from pathlib import Path
def test_keypad_publishes_through_hub_function(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("hub_function.yaml"))
# The key data has the column bit in the high byte and the row bit in the low byte
assert (
"static void esphome__hub__keypad_fn(uint16_t esphome__arg_key_data) {\n"
" sx1509::publish_key(::key_r0_c0, esphome__arg_key_data, 0x101);\n"
" sx1509::publish_key(::key_r1_c1, esphome__arg_key_data, 0x202);\n"
"}"
) in main_cpp
assert "hub->set_keypad_publisher(esphome__hub__keypad_fn);" in main_cpp
assert "idle_hub->set_keypad_publisher" not in main_cpp
Whitespace-only changes.
@@ -1,19 +0,0 @@
esphome:
name: test
esp32:
board: esp32dev
ttp229_bsf:
- id: hub
sdo_pin: GPIO16
scl_pin: GPIO17
- id: idle_hub
sdo_pin: GPIO18
scl_pin: GPIO19
binary_sensor:
- platform: ttp229_bsf
ttp229_id: hub
id: key3
channel: 3
@@ -1,19 +0,0 @@
"""Tests for the function that publishes the ttp229_bsf channel binary sensors."""
from collections.abc import Callable
from pathlib import Path
def test_channels_publish_through_hub_function(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("hub_function.yaml"))
assert (
"static void esphome__hub__channels_fn(uint16_t esphome__arg_touched) {\n"
" ttp229_bsf::publish_channel(::key3, esphome__arg_touched, 3);\n"
"}"
) in main_cpp
assert "hub->set_channels_publisher(esphome__hub__channels_fn);" in main_cpp
assert "idle_hub->set_channels_publisher" not in main_cpp
Whitespace-only changes.
@@ -1,24 +0,0 @@
esphome:
name: test
esp32:
board: esp32dev
i2c:
sda: GPIO21
scl: GPIO22
ttp229_lsf:
- id: hub
- id: idle_hub
address: 0x58
binary_sensor:
- platform: ttp229_lsf
ttp229_id: hub
id: key0
channel: 0
- platform: ttp229_lsf
ttp229_id: hub
id: key15
channel: 15
@@ -1,20 +0,0 @@
"""Tests for the function that publishes the ttp229_lsf channel binary sensors."""
from collections.abc import Callable
from pathlib import Path
def test_channels_publish_through_hub_function(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("hub_function.yaml"))
assert (
"static void esphome__hub__channels_fn(uint16_t esphome__arg_touched) {\n"
" ttp229_lsf::publish_channel(::key0, esphome__arg_touched, 0);\n"
" ttp229_lsf::publish_channel(::key15, esphome__arg_touched, 15);\n"
"}"
) in main_cpp
assert "hub->set_channels_publisher(esphome__hub__channels_fn);" in main_cpp
assert "idle_hub->set_channels_publisher" not in main_cpp
+6
View File
@@ -0,0 +1,6 @@
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
# touchscreen.h includes display.h
manifest.dependencies = manifest.dependencies + ["display"]
+50
View File
@@ -0,0 +1,50 @@
#pragma once
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include "esphome/components/cst3240/touchscreen/cst3240_touchscreen.h"
#include "esphome/core/helpers.h"
namespace esphome::cst3240::testing {
struct Write {
uint16_t reg;
size_t size;
};
// Answers 16-bit register reads from a map; every write succeeds and is recorded.
class RegisterBus final : public i2c::I2CBus {
public:
std::map<uint16_t, std::vector<uint8_t>> registers;
std::vector<Write> writes;
i2c::ErrorCode write_readv(uint8_t address, const uint8_t *data, size_t size, uint8_t *out,
size_t out_size) override {
if (size < 2)
return i2c::ERROR_NOT_ACKNOWLEDGED;
const uint16_t reg = encode_uint16(data[0], data[1]);
if (out_size == 0) {
this->writes.push_back({reg, size});
return i2c::ERROR_OK;
}
auto &value = this->registers[reg];
value.resize(std::max(value.size(), out_size));
std::memcpy(out, value.data(), out_size);
return i2c::ERROR_OK;
}
// Lists `count` fingers with ids 0..count-1, each with the given event bits.
void set_report(uint8_t count, uint8_t sync = cstxxx::SYNC_BYTE, uint8_t event = TOUCH_EVENT_PRESSED) {
std::vector<uint8_t> data(CST3240_REPORT_SIZE);
for (uint8_t i = 0; i < std::min(count, CST3240_MAX_TOUCHES); i++)
data[i * 5 + (i == 0 ? 0 : 2)] = i << 4 | event;
data[cstxxx::FINGER_COUNT_IDX] = count;
data[cstxxx::SYNC_IDX] = sync;
this->registers[cstxxx::REG_TOUCH_DATA] = data;
}
};
} // namespace esphome::cst3240::testing
+18
View File
@@ -0,0 +1,18 @@
display:
- platform: ssd1306_i2c
i2c_id: i2c_bus
id: cst3240_ssd1306_i2c_display
model: SSD1306_128X64
reset_pin: ${display_reset_pin}
pages:
- id: cst3240_page1
lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height());
touchscreen:
- platform: cst3240
i2c_id: i2c_bus
id: cst3240_touchscreen
display: cst3240_ssd1306_i2c_display
interrupt_pin: ${interrupt_pin}
reset_pin: ${reset_pin}
@@ -0,0 +1,8 @@
substitutions:
display_reset_pin: "4"
interrupt_pin: "20"
reset_pin: "21"
packages:
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
cst3240: !include common.yaml
@@ -0,0 +1,103 @@
#include <gtest/gtest.h>
#include "esphome/components/cst3240/touchscreen/cst3240_touchscreen.h"
#include "esphome/core/gpio.h"
#include "../common.h"
namespace esphome::cst3240::testing {
class TestableCST3240 : public CST3240Touchscreen {
public:
using CST3240Touchscreen::chip_id_;
using CST3240Touchscreen::is_touched_;
using CST3240Touchscreen::setup_complete_;
using CST3240Touchscreen::skip_update_;
using CST3240Touchscreen::update_touches;
using CST3240Touchscreen::x_raw_max_;
using CST3240Touchscreen::y_raw_max_;
};
class CST3240Test : public ::testing::Test {
protected:
void SetUp() override {
this->bus_.registers[cstxxx::REG_CHIP_INFO] = {0x01, 0x00, 0x40, 0x32};
this->bus_.registers[cstxxx::REG_RESOLUTION] = {0x40, 0x01, 0xF0, 0x00};
this->hub_.set_i2c_bus(&this->bus_);
this->hub_.setup();
this->bus_.writes.clear();
}
void update_() {
this->hub_.skip_update_ = false;
this->hub_.is_touched_ = false;
this->hub_.update_touches();
}
bool cleared_() const {
return this->bus_.writes.size() == 1 && this->bus_.writes[0].reg == cstxxx::REG_TOUCH_DATA &&
this->bus_.writes[0].size == 2u + CST3240_REPORT_SIZE;
}
RegisterBus bus_;
TestableCST3240 hub_;
};
TEST_F(CST3240Test, SetupReadsTheChipAndResolutionWithoutAFirmwareCheck) {
EXPECT_TRUE(this->hub_.setup_complete_);
EXPECT_FALSE(this->hub_.is_failed());
EXPECT_EQ(this->hub_.chip_id_, CST3240_CHIP_ID);
EXPECT_EQ(this->hub_.x_raw_max_, 320);
EXPECT_EQ(this->hub_.y_raw_max_, 240);
}
TEST_F(CST3240Test, ReportWithoutTheSyncByteIsSkippedAndCleared) {
this->bus_.set_report(1, 0x55);
this->update_();
EXPECT_TRUE(this->hub_.skip_update_);
EXPECT_FALSE(this->hub_.is_touched_);
EXPECT_TRUE(this->cleared_());
}
TEST_F(CST3240Test, IdleBlockIsNotRewritten) {
this->bus_.set_report(0, 0);
this->update_();
EXPECT_TRUE(this->hub_.skip_update_);
EXPECT_TRUE(this->bus_.writes.empty());
}
TEST_F(CST3240Test, ReleasedRecordIsNotATouch) {
this->bus_.set_report(1, cstxxx::SYNC_BYTE, 0);
this->update_();
EXPECT_FALSE(this->hub_.skip_update_);
EXPECT_FALSE(this->hub_.is_touched_);
EXPECT_TRUE(this->hub_.get_touches().empty());
}
TEST_F(CST3240Test, PollingClearsTheReport) {
this->bus_.set_report(1);
this->update_();
EXPECT_TRUE(this->hub_.is_touched_);
EXPECT_TRUE(this->cleared_());
}
TEST_F(CST3240Test, InterruptAcknowledgesTheReport) {
InternalGPIOPin pin;
this->hub_.set_interrupt_pin(&pin);
this->bus_.set_report(1);
this->update_();
ASSERT_EQ(this->bus_.writes.size(), 2u);
EXPECT_EQ(this->bus_.writes[0].reg, cstxxx::REG_FINGER_COUNT);
EXPECT_EQ(this->bus_.writes[1].reg, cstxxx::REG_TOUCH_DATA);
EXPECT_EQ(this->bus_.writes[1].size, 3u);
}
TEST_F(CST3240Test, ReportsUpToFiveFingers) {
this->bus_.set_report(5);
this->update_();
EXPECT_EQ(this->hub_.get_touches().size(), 5u);
this->bus_.set_report(6);
this->update_();
EXPECT_FALSE(this->hub_.is_touched_);
}
} // namespace esphome::cst3240::testing
+84
View File
@@ -0,0 +1,84 @@
npm1300:
id: pmic
i2c_id: i2c_bus
vbus_current_limit: 500mA
thermistor_resistance: 10kOhm
thermistor_beta: 3380
charger:
charge_current: 150mA
termination_voltage: 4.2V
warm_termination_voltage: 4.0V
termination_current: 20%
trickle_voltage: 2.5V
discharge_current_limit: 1000mA
recharge: true
buck2:
voltage: 3.3V
ldo1:
voltage: 3.3V
mode: ldo
enabled: true
ldo2:
mode: load_switch
enabled: false
sensor:
- platform: npm1300
battery_voltage:
name: Battery Voltage
battery_current:
name: Battery Current
battery_temperature:
name: Battery Temperature
die_temperature:
name: PMIC Temperature
system_voltage:
name: System Voltage
vbus_voltage:
name: VBUS Voltage
vbus_current_limit:
name: VBUS Current Limit
binary_sensor:
- platform: npm1300
vbus_present:
name: VBUS Present
vbus_current_limit_active:
name: VBUS Current Limit Active
charging:
name: Charging
charge_complete:
name: Charge Complete
charger_error:
name: Charger Error
text_sensor:
- platform: npm1300
charger_status:
name: Charger Status
switch:
- platform: npm1300
charger:
name: Charger
buck1:
name: BUCK1
ldo1:
name: LDO1
ldo2:
name: LDO2
button:
- platform: template
name: Ship Mode
on_press:
- npm1300.enter_ship_mode: pmic
- platform: template
name: Hibernate
on_press:
- npm1300.hibernate:
id: pmic
duration: 1h
- npm1300.hibernate:
id: pmic
duration: !lambda return 60000;
@@ -0,0 +1,3 @@
packages:
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
npm1300: !include common.yaml
+3
View File
@@ -0,0 +1,3 @@
packages:
i2c: !include ../../test_build_components/common/i2c/host.yaml
npm1300: !include common.yaml
@@ -0,0 +1,3 @@
packages:
i2c: !include ../../test_build_components/common/i2c/nrf52.yaml
npm1300: !include common.yaml