mirror of
https://github.com/esphome/esphome.git
synced 2026-11-17 18:46:08 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8270d7870 | ||
|
|
e7aa736732 | ||
|
|
ac1b5036ba | ||
|
|
61f26c0ebf |
No files matched your search
@@ -61,14 +61,10 @@ RADAR_ALLOWED_CUR_CONSUMPTION = [
|
||||
78e-6,
|
||||
]
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(AT581XComponent),
|
||||
}
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
CONFIG_SCHEMA.extend(i2c.i2c_device_schema(0x28)).extend(cv.COMPONENT_SCHEMA)
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema({cv.GenerateID(): cv.declare_id(AT581XComponent)})
|
||||
.extend(i2c.i2c_device_schema(0x28))
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
)
|
||||
|
||||
|
||||
@@ -94,6 +90,16 @@ def _microamps(value: float) -> int:
|
||||
return round(value * 1000000)
|
||||
|
||||
|
||||
def _time_ms(min_ms: int) -> cv.All:
|
||||
return cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(
|
||||
min=core.TimePeriod(milliseconds=min_ms),
|
||||
max=core.TimePeriod(milliseconds=65535),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
RADAR_SETTINGS_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(AT581XComponent),
|
||||
@@ -105,51 +111,18 @@ RADAR_SETTINGS_SCHEMA = cv.Schema(
|
||||
cv.int_range(min=0, max=1023)
|
||||
),
|
||||
cv.Optional(CONF_POWERON_SELFCHECK_TIME, default="2000ms"): cv.templatable(
|
||||
cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(max=core.TimePeriod(milliseconds=65535)),
|
||||
)
|
||||
_time_ms(0)
|
||||
),
|
||||
cv.Optional(CONF_POWER_CONSUMPTION, default="70uA"): cv.templatable(
|
||||
cv.All(cv.current, cv.one_of(*RADAR_ALLOWED_CUR_CONSUMPTION), _microamps)
|
||||
),
|
||||
cv.Optional(CONF_PROTECT_TIME, default="1000ms"): cv.templatable(
|
||||
cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(
|
||||
min=core.TimePeriod(milliseconds=1),
|
||||
max=core.TimePeriod(milliseconds=65535),
|
||||
),
|
||||
)
|
||||
),
|
||||
cv.Optional(CONF_TRIGGER_BASE, default="500ms"): cv.templatable(
|
||||
cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(
|
||||
min=core.TimePeriod(milliseconds=1),
|
||||
max=core.TimePeriod(milliseconds=65535),
|
||||
),
|
||||
)
|
||||
),
|
||||
cv.Optional(CONF_TRIGGER_KEEP, default="1500ms"): cv.templatable(
|
||||
cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(
|
||||
min=core.TimePeriod(milliseconds=1),
|
||||
max=core.TimePeriod(milliseconds=65535),
|
||||
),
|
||||
)
|
||||
),
|
||||
cv.Optional(CONF_PROTECT_TIME, default="1000ms"): cv.templatable(_time_ms(1)),
|
||||
cv.Optional(CONF_TRIGGER_BASE, default="500ms"): cv.templatable(_time_ms(1)),
|
||||
cv.Optional(CONF_TRIGGER_KEEP, default="1500ms"): cv.templatable(_time_ms(1)),
|
||||
cv.Optional(CONF_STAGE_GAIN, default=3): cv.templatable(
|
||||
cv.int_range(min=0, max=12)
|
||||
),
|
||||
}
|
||||
).add_extra(
|
||||
cv.has_at_least_one_key(
|
||||
CONF_HW_FRONTEND_RESET,
|
||||
CONF_FREQUENCY,
|
||||
CONF_SENSING_DISTANCE,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,201 +1,125 @@
|
||||
#include "at581x.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
/* Select gain for AT581X (3dB per step for level1, 6dB per step for level 2), high value = small gain. (p12) */
|
||||
const uint8_t GAIN_ADDR_TABLE[] = {0x5c, 0x63};
|
||||
const uint8_t GAIN5C_TABLE[] = {0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78, 0x88, 0x98, 0xa8, 0xb8, 0xc8};
|
||||
const uint8_t GAIN63_TABLE[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
|
||||
const uint8_t GAIN61_VALUE = 0xCA; // 0xC0 | 0x02 (freq present) | 0x08 (gain present)
|
||||
|
||||
/*!< Power consumption configuration table (p12). */
|
||||
const uint8_t POWER_TABLE[] = {48, 56, 63, 70, 77, 91, 105, 115, 40, 44, 47, 51, 54, 61, 68, 78};
|
||||
const uint8_t POWER67_TABLE[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7};
|
||||
const uint8_t POWER68_TABLE[] = {0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8,
|
||||
24, 24, 24, 24, 24, 24, 24, 24}; // See Page 12, shift by 3 bits
|
||||
|
||||
/*!< Frequency Configuration table (p14/15 of datasheet). */
|
||||
const uint8_t FREQ_ADDR = 0x61;
|
||||
const uint16_t FREQ_TABLE[] = {5696, 5715, 5730, 5748, 5765, 5784, 5800, 5819, 5836, 5851, 5869, 5888};
|
||||
const uint8_t FREQ5F_TABLE[] = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x40, 0x41, 0x42, 0x43};
|
||||
const uint8_t FREQ60_TABLE[] = {0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9e, 0x9e, 0x9e, 0x9e};
|
||||
|
||||
/*!< Value for RF and analog modules switch (p10). */
|
||||
const uint8_t RF_OFF_TABLE[] = {0x46, 0xaa, 0x50};
|
||||
const uint8_t RF_ON_TABLE[] = {0x45, 0x55, 0xA0};
|
||||
const uint8_t RF_REG_ADDR[] = {0x5d, 0x62, 0x51};
|
||||
|
||||
/*!< Registers of Lighting delay time. Unit: ms, min 2s (p8) */
|
||||
const uint8_t HIGH_LEVEL_DELAY_CONTROL_ADDR = 0x41; /*!< Time_flag_out_ctrl 0x01 */
|
||||
const uint8_t HIGH_LEVEL_DELAY_VALUE_ADDR = 0x42; /*!< Time_flag_out_1 Bit<7:0> */
|
||||
|
||||
const uint8_t RESET_ADDR = 0x00;
|
||||
|
||||
/*!< Sensing distance address */
|
||||
const uint8_t SIGNAL_DETECTION_THRESHOLD_ADDR_LO = 0x10;
|
||||
const uint8_t SIGNAL_DETECTION_THRESHOLD_ADDR_HI = 0x11;
|
||||
|
||||
/*!< Bit field value for power registers */
|
||||
const uint8_t POWER_THRESHOLD_ADDR_HI = 0x68;
|
||||
const uint8_t POWER_THRESHOLD_ADDR_LO = 0x67;
|
||||
const uint8_t PWR_WORK_TIME_EN = 8; // Reg 0x67
|
||||
const uint8_t PWR_BURST_TIME_EN = 32; // Reg 0x68
|
||||
const uint8_t PWR_THRESH_EN = 64; // Reg 0x68
|
||||
const uint8_t PWR_THRESH_VAL_EN = 128; // Reg 0x67
|
||||
|
||||
/*!< Times */
|
||||
const uint8_t TRIGGER_BASE_TIME_ADDR = 0x3D; // 4 bytes, so up to 0x40
|
||||
const uint8_t PROTECT_TIME_ADDR = 0x4E; // 2 bytes, up to 0x4F
|
||||
const uint8_t TRIGGER_KEEP_TIME_ADDR = 0x42; // 4 bytes, so up to 0x45
|
||||
const uint8_t TIME41_VALUE = 1;
|
||||
const uint8_t SELF_CHECK_TIME_ADDR = 0x38; // 2 bytes, up to 0x39
|
||||
#include <iterator>
|
||||
|
||||
namespace esphome::at581x {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "at581x");
|
||||
|
||||
bool AT581XComponent::i2c_write_reg(uint8_t addr, uint8_t data) {
|
||||
return this->write_register(addr, &data, 1) == esphome::i2c::NO_ERROR;
|
||||
}
|
||||
bool AT581XComponent::i2c_write_reg(uint8_t addr, uint32_t data) {
|
||||
return this->i2c_write_reg(addr + 0, uint8_t(data & 0xFF)) &&
|
||||
this->i2c_write_reg(addr + 1, uint8_t((data >> 8) & 0xFF)) &&
|
||||
this->i2c_write_reg(addr + 2, uint8_t((data >> 16) & 0xFF)) &&
|
||||
this->i2c_write_reg(addr + 3, uint8_t((data >> 24) & 0xFF));
|
||||
}
|
||||
bool AT581XComponent::i2c_write_reg(uint8_t addr, uint16_t data) {
|
||||
return this->i2c_write_reg(addr, uint8_t(data & 0xFF)) && this->i2c_write_reg(addr + 1, uint8_t((data >> 8) & 0xFF));
|
||||
/*!< Frequency points in MHz (p14/15 of datasheet). */
|
||||
static constexpr uint16_t FREQ_TABLE[] PROGMEM = {5696, 5715, 5730, 5748, 5765, 5784,
|
||||
5800, 5819, 5836, 5851, 5869, 5888};
|
||||
static constexpr uint8_t FREQ_MODE_ADDR = 0x61;
|
||||
static constexpr uint8_t FREQ_MODE_VALUE = 0xCA; // 0xC0 | 0x02 (freq present) | 0x08 (gain present)
|
||||
static constexpr uint8_t FREQ_LO_ADDR = 0x5F;
|
||||
static constexpr uint8_t FREQ_HI_ADDR = 0x60;
|
||||
|
||||
/*!< Power consumption points in µA (p12). */
|
||||
static constexpr uint8_t POWER_TABLE[] PROGMEM = {48, 56, 63, 70, 77, 91, 105, 115, 40, 44, 47, 51, 54, 61, 68, 78};
|
||||
static constexpr uint8_t POWER_LO_ADDR = 0x67;
|
||||
static constexpr uint8_t POWER_HI_ADDR = 0x68;
|
||||
static constexpr uint8_t PWR_WORK_TIME_EN = 8; // Reg 0x67
|
||||
static constexpr uint8_t PWR_BURST_TIME_EN = 32; // Reg 0x68
|
||||
static constexpr uint8_t PWR_THRESH_EN = 64; // Reg 0x68
|
||||
static constexpr uint8_t PWR_THRESH_VAL_EN = 128; // Reg 0x67
|
||||
|
||||
/* Gain for AT581X (3dB per step for level1, 6dB per step for level 2), high value = small gain. (p12) */
|
||||
static constexpr uint8_t GAIN_LEVEL1_ADDR = 0x5C;
|
||||
static constexpr uint8_t GAIN_LEVEL2_ADDR = 0x63;
|
||||
static constexpr uint8_t MAX_GAIN = 12;
|
||||
|
||||
static constexpr uint8_t SIGNAL_DETECTION_THRESHOLD_ADDR = 0x10; // 2 bytes, up to 0x11
|
||||
static constexpr uint8_t SELF_CHECK_TIME_ADDR = 0x38; // 2 bytes, up to 0x39
|
||||
static constexpr uint8_t TRIGGER_BASE_TIME_ADDR = 0x3D; // 4 bytes, up to 0x40
|
||||
static constexpr uint8_t TIME_ENABLE_ADDR = 0x41;
|
||||
static constexpr uint8_t TRIGGER_KEEP_TIME_ADDR = 0x42; // 4 bytes, up to 0x45
|
||||
static constexpr uint8_t PROTECT_TIME_ADDR = 0x4E; // 2 bytes, up to 0x4F
|
||||
// Don't know why it's required in other code, it's not in datasheet
|
||||
static constexpr uint8_t ENABLE_ADDR = 0x55;
|
||||
static constexpr uint8_t RESET_ADDR = 0x00;
|
||||
|
||||
/*!< Values for RF and analog modules switch (p10). */
|
||||
static constexpr i2c::I2CRegValue RF_ON[] PROGMEM = {{0x5d, 0x45}, {0x62, 0x55}, {0x51, 0xA0}};
|
||||
static constexpr i2c::I2CRegValue RF_OFF[] PROGMEM = {{0x5d, 0x46}, {0x62, 0xaa}, {0x51, 0x50}};
|
||||
static constexpr i2c::I2CRegValue RESET_SEQUENCE[] PROGMEM = {{RESET_ADDR, 0}, {RESET_ADDR, 1}};
|
||||
|
||||
void AT581XComponent::setup() {
|
||||
#ifdef USE_SWITCH
|
||||
if (this->rf_power_switch_ != nullptr)
|
||||
this->rf_power_switch_->restore(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool AT581XComponent::i2c_read_reg(uint8_t addr, uint8_t &data) {
|
||||
return this->read_register(addr, &data, 1) == esphome::i2c::NO_ERROR;
|
||||
}
|
||||
|
||||
void AT581XComponent::setup() {}
|
||||
void AT581XComponent::dump_config() { LOG_I2C_DEVICE(this); }
|
||||
#define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
|
||||
bool AT581XComponent::i2c_write_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"Writing new config for AT581X\n"
|
||||
" Frequency: %dMHz\n"
|
||||
" Sensing distance: %d\n"
|
||||
" Power: %dµA\n"
|
||||
" Gain: %d\n"
|
||||
" Trigger base time: %dms\n"
|
||||
" Trigger keep time: %dms\n"
|
||||
" Protect time: %dms\n"
|
||||
" Self check time: %dms",
|
||||
this->freq_, this->delta_, this->power_, this->gain_, this->trigger_base_time_ms_,
|
||||
this->trigger_keep_time_ms_, this->protect_time_ms_, this->self_check_time_ms_);
|
||||
|
||||
// Set frequency point
|
||||
if (!this->i2c_write_reg(FREQ_ADDR, GAIN61_VALUE)) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X Freq mode");
|
||||
return false;
|
||||
}
|
||||
// Find the current frequency from the table to know what value to write
|
||||
for (size_t i = 0; i < ARRAY_SIZE(FREQ_TABLE) + 1; i++) {
|
||||
if (i == ARRAY_SIZE(FREQ_TABLE)) {
|
||||
ESP_LOGE(TAG, "Set frequency not found");
|
||||
return false;
|
||||
}
|
||||
if (FREQ_TABLE[i] == this->freq_) {
|
||||
if (!this->i2c_write_reg(0x5F, FREQ5F_TABLE[i]) || !this->i2c_write_reg(0x60, FREQ60_TABLE[i])) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X Freq value");
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Set distance
|
||||
if (!this->i2c_write_reg(SIGNAL_DETECTION_THRESHOLD_ADDR_LO, (uint8_t) (this->delta_ & 0xFF)) ||
|
||||
!this->i2c_write_reg(SIGNAL_DETECTION_THRESHOLD_ADDR_HI, (uint8_t) (this->delta_ >> 8))) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X sensing distance low");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set power setting
|
||||
uint8_t pwr67 = PWR_THRESH_VAL_EN | PWR_WORK_TIME_EN, pwr68 = PWR_BURST_TIME_EN | PWR_THRESH_EN;
|
||||
for (size_t i = 0; i < ARRAY_SIZE(POWER_TABLE) + 1; i++) {
|
||||
if (i == ARRAY_SIZE(POWER_TABLE)) {
|
||||
ESP_LOGE(TAG, "Set power not found");
|
||||
return false;
|
||||
}
|
||||
if (POWER_TABLE[i] == this->power_) {
|
||||
pwr67 |= POWER67_TABLE[i];
|
||||
pwr68 |= POWER68_TABLE[i]; // See Page 12
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!this->i2c_write_reg(POWER_THRESHOLD_ADDR_LO, pwr67) || !this->i2c_write_reg(POWER_THRESHOLD_ADDR_HI, pwr68)) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X power registers");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set gain
|
||||
if (this->gain_ < 0 || static_cast<size_t>(this->gain_) >= ARRAY_SIZE(GAIN5C_TABLE) ||
|
||||
static_cast<size_t>(this->gain_ >> 1) >= ARRAY_SIZE(GAIN63_TABLE)) {
|
||||
ESP_LOGE(TAG, "AT581X gain index out of range: %d", this->gain_);
|
||||
return false;
|
||||
}
|
||||
if (!this->i2c_write_reg(GAIN_ADDR_TABLE[0], GAIN5C_TABLE[this->gain_]) ||
|
||||
!this->i2c_write_reg(GAIN_ADDR_TABLE[1], GAIN63_TABLE[this->gain_ >> 1])) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X gain registers");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set times
|
||||
if (!this->i2c_write_reg(TRIGGER_BASE_TIME_ADDR, (uint32_t) this->trigger_base_time_ms_)) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X trigger base time registers");
|
||||
return false;
|
||||
}
|
||||
if (!this->i2c_write_reg(TRIGGER_KEEP_TIME_ADDR, (uint32_t) this->trigger_keep_time_ms_)) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X trigger keep time registers");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this->i2c_write_reg(PROTECT_TIME_ADDR, (uint16_t) this->protect_time_ms_)) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X protect time registers");
|
||||
return false;
|
||||
}
|
||||
if (!this->i2c_write_reg(SELF_CHECK_TIME_ADDR, (uint16_t) this->self_check_time_ms_)) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X self check time registers");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this->i2c_write_reg(0x41, TIME41_VALUE)) {
|
||||
ESP_LOGE(TAG, "Failed to enable AT581X time registers");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Don't know why it's required in other code, it's not in datasheet
|
||||
if (!this->i2c_write_reg(0x55, (uint8_t) 0x04)) {
|
||||
ESP_LOGE(TAG, "Failed to enable AT581X");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ok, config is written, let's reset the chip so it's using the new config
|
||||
return this->reset_hardware_frontend();
|
||||
void AT581XComponent::set_frequency(int frequency) {
|
||||
this->freq_index_ = 0;
|
||||
while (this->freq_index_ < std::size(FREQ_TABLE) && progmem_read_uint16(&FREQ_TABLE[this->freq_index_]) != frequency)
|
||||
this->freq_index_++;
|
||||
}
|
||||
|
||||
// float AT581XComponent::get_setup_priority() const { return 0; }
|
||||
bool AT581XComponent::reset_hardware_frontend() {
|
||||
if (!this->i2c_write_reg(RESET_ADDR, (uint8_t) 0) || !this->i2c_write_reg(RESET_ADDR, (uint8_t) 1)) {
|
||||
ESP_LOGE(TAG, "Failed to reset AT581X hardware frontend");
|
||||
return false;
|
||||
void AT581XComponent::set_power_consumption(int value) {
|
||||
this->power_index_ = 0;
|
||||
while (this->power_index_ < std::size(POWER_TABLE) && progmem_read_byte(&POWER_TABLE[this->power_index_]) != value)
|
||||
this->power_index_++;
|
||||
}
|
||||
|
||||
bool AT581XComponent::write_le_(uint8_t addr, uint32_t value, uint8_t size) {
|
||||
for (uint8_t i = 0; i < size; i++) {
|
||||
if (!this->write_byte(addr + i, static_cast<uint8_t>(value >> (8 * i))))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void AT581XComponent::set_rf_mode(bool enable) {
|
||||
const uint8_t *p = enable ? &RF_ON_TABLE[0] : &RF_OFF_TABLE[0];
|
||||
for (size_t i = 0; i < ARRAY_SIZE(RF_REG_ADDR); i++) {
|
||||
if (!this->i2c_write_reg(RF_REG_ADDR[i], p[i])) {
|
||||
ESP_LOGE(TAG, "Failed to write AT581X RF mode");
|
||||
return;
|
||||
}
|
||||
bool AT581XComponent::i2c_write_config() {
|
||||
if (this->freq_index_ >= std::size(FREQ_TABLE) || this->power_index_ >= std::size(POWER_TABLE) ||
|
||||
this->gain_ > MAX_GAIN) {
|
||||
ESP_LOGE(TAG, "Unsupported frequency, power consumption or gain");
|
||||
return false;
|
||||
}
|
||||
ESP_LOGD(TAG,
|
||||
"Writing config\n"
|
||||
" Frequency: %uMHz\n"
|
||||
" Sensing distance: %u\n"
|
||||
" Power: %uµA\n"
|
||||
" Gain: %u\n"
|
||||
" Trigger base time: %ums\n"
|
||||
" Trigger keep time: %ums\n"
|
||||
" Protect time: %ums\n"
|
||||
" Self check time: %ums",
|
||||
progmem_read_uint16(&FREQ_TABLE[this->freq_index_]), 1023 - this->delta_,
|
||||
progmem_read_byte(&POWER_TABLE[this->power_index_]), this->gain_, this->trigger_base_time_ms_,
|
||||
this->trigger_keep_time_ms_, this->protect_time_ms_, this->self_check_time_ms_);
|
||||
|
||||
// The second half of each table repeats the register pattern with the high bit set
|
||||
const bool high_freq = this->freq_index_ >= 8;
|
||||
const bool high_power = this->power_index_ >= 8;
|
||||
const bool ok = this->write_byte(FREQ_MODE_ADDR, FREQ_MODE_VALUE) &&
|
||||
this->write_byte(FREQ_LO_ADDR, 0x40 | (this->freq_index_ & 0x07)) &&
|
||||
this->write_byte(FREQ_HI_ADDR, high_freq ? 0x9e : 0x9d) &&
|
||||
this->write_le_(SIGNAL_DETECTION_THRESHOLD_ADDR, this->delta_, 2) &&
|
||||
this->write_byte(POWER_LO_ADDR, PWR_THRESH_VAL_EN | PWR_WORK_TIME_EN | (this->power_index_ & 0x07)) &&
|
||||
this->write_byte(POWER_HI_ADDR, PWR_BURST_TIME_EN | PWR_THRESH_EN | (high_power ? 0x18 : 0x08)) &&
|
||||
this->write_byte(GAIN_LEVEL1_ADDR, (this->gain_ << 4) | 0x08) &&
|
||||
this->write_byte(GAIN_LEVEL2_ADDR, this->gain_ >> 1) &&
|
||||
this->write_le_(TRIGGER_BASE_TIME_ADDR, this->trigger_base_time_ms_, 4) &&
|
||||
this->write_le_(TRIGGER_KEEP_TIME_ADDR, this->trigger_keep_time_ms_, 4) &&
|
||||
this->write_le_(PROTECT_TIME_ADDR, this->protect_time_ms_, 2) &&
|
||||
this->write_le_(SELF_CHECK_TIME_ADDR, this->self_check_time_ms_, 2) &&
|
||||
this->write_byte(TIME_ENABLE_ADDR, 1) && this->write_byte(ENABLE_ADDR, 0x04);
|
||||
// Reset the chip so it uses the new config
|
||||
return this->status_comm_result(ok) && this->reset_hardware_frontend();
|
||||
}
|
||||
|
||||
bool AT581XComponent::reset_hardware_frontend() {
|
||||
return this->status_comm_result(this->write_registers(RESET_SEQUENCE));
|
||||
}
|
||||
|
||||
bool AT581XComponent::set_rf_mode(bool enable) {
|
||||
return this->status_comm_result(enable ? this->write_registers(RF_ON) : this->write_registers(RF_OFF));
|
||||
}
|
||||
|
||||
} // namespace esphome::at581x
|
||||
@@ -1,40 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#ifdef USE_SWITCH
|
||||
#include "esphome/components/switch/switch.h"
|
||||
#endif
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace esphome::at581x {
|
||||
|
||||
class AT581XComponent final : public Component, public i2c::I2CDevice {
|
||||
public:
|
||||
#ifdef USE_SWITCH
|
||||
void set_rf_power_switch(switch_::Switch *s) {
|
||||
this->rf_power_switch_ = s;
|
||||
s->turn_on();
|
||||
}
|
||||
SUB_SWITCH(rf_power)
|
||||
#endif
|
||||
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
// float get_setup_priority() const override;
|
||||
|
||||
// Frequency in MHz and power consumption in µA, as listed in the datasheet; other values fail the next write
|
||||
void set_frequency(int frequency);
|
||||
void set_power_consumption(int value);
|
||||
void set_sensing_distance(int distance) { this->delta_ = 1023 - distance; }
|
||||
|
||||
void set_rf_mode(bool enabled);
|
||||
void set_frequency(int frequency) { this->freq_ = frequency; }
|
||||
void set_poweron_selfcheck_time(int value) { this->self_check_time_ms_ = value; }
|
||||
void set_protect_time(int value) { this->protect_time_ms_ = value; }
|
||||
void set_trigger_base(int value) { this->trigger_base_time_ms_ = value; }
|
||||
void set_trigger_keep(int value) { this->trigger_keep_time_ms_ = value; }
|
||||
void set_stage_gain(int value) { this->gain_ = value; }
|
||||
void set_power_consumption(int value) { this->power_ = value; }
|
||||
// Times in ms; the schema caps them at 65535, and larger lambda values are clamped
|
||||
void set_poweron_selfcheck_time(int value) { this->self_check_time_ms_ = std::clamp(value, 0, 65535); }
|
||||
void set_protect_time(int value) { this->protect_time_ms_ = std::clamp(value, 0, 65535); }
|
||||
void set_trigger_base(int value) { this->trigger_base_time_ms_ = std::clamp(value, 0, 65535); }
|
||||
void set_trigger_keep(int value) { this->trigger_keep_time_ms_ = std::clamp(value, 0, 65535); }
|
||||
// Out-of-range values, negative ones included, become 255 so the next write rejects them
|
||||
void set_stage_gain(int value) { this->gain_ = std::min<unsigned>(value, UINT8_MAX); }
|
||||
|
||||
bool i2c_write_config();
|
||||
bool reset_hardware_frontend();
|
||||
@@ -42,23 +38,20 @@ class AT581XComponent final : public Component, public i2c::I2CDevice {
|
||||
if (reset)
|
||||
this->reset_hardware_frontend();
|
||||
}
|
||||
bool i2c_write_reg(uint8_t addr, uint8_t data);
|
||||
bool i2c_write_reg(uint8_t addr, uint32_t data);
|
||||
bool i2c_write_reg(uint8_t addr, uint16_t data);
|
||||
bool i2c_read_reg(uint8_t addr, uint8_t &data);
|
||||
bool set_rf_mode(bool enable);
|
||||
|
||||
protected:
|
||||
#ifdef USE_SWITCH
|
||||
switch_::Switch *rf_power_switch_{nullptr};
|
||||
#endif
|
||||
int freq_;
|
||||
int self_check_time_ms_; /*!< Power-on self-test time, range: 0 ~ 65536 ms */
|
||||
int protect_time_ms_; /*!< Protection time, recommended 1000 ms */
|
||||
int trigger_base_time_ms_; /*!< Default: 500 ms */
|
||||
int trigger_keep_time_ms_; /*!< Total trig time = TRIGGER_BASE_TIME + DEF_TRIGGER_KEEP_TIME, minimum: 1 */
|
||||
int delta_; /*!< Delta value: 0 ~ 1023, the larger the value, the shorter the distance */
|
||||
int gain_; /*!< Default: 9dB */
|
||||
int power_; /*!< In µA */
|
||||
bool write_le_(uint8_t addr, uint32_t value, uint8_t size);
|
||||
|
||||
// Defaults match the at581x.settings defaults
|
||||
uint16_t self_check_time_ms_{2000};
|
||||
uint16_t protect_time_ms_{1000};
|
||||
uint16_t trigger_base_time_ms_{500};
|
||||
uint16_t trigger_keep_time_ms_{1500};
|
||||
uint16_t delta_{1023 - 823}; // 0 to 1023, the larger the value, the shorter the distance
|
||||
uint8_t freq_index_{6}; // 5800 MHz
|
||||
uint8_t power_index_{3}; // 70 µA
|
||||
uint8_t gain_{3};
|
||||
};
|
||||
|
||||
} // namespace esphome::at581x
|
||||
@@ -13,17 +13,15 @@ CONFIG_SCHEMA = switch.switch_schema(
|
||||
switch.ApplySwitch,
|
||||
device_class=DEVICE_CLASS_SWITCH,
|
||||
icon=ICON_WIFI,
|
||||
).extend(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_AT581X_ID): cv.use_id(AT581XComponent),
|
||||
}
|
||||
)
|
||||
)
|
||||
default_restore_mode="ALWAYS_ON",
|
||||
).extend({cv.GenerateID(CONF_AT581X_ID): cv.use_id(AT581XComponent)})
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
hub = await cg.get_variable(config[CONF_AT581X_ID])
|
||||
await switch.sub_switches(config, parent=hub).create(
|
||||
config, hub.set_rf_power_switch, apply="set_rf_mode(state)", publish="before"
|
||||
config,
|
||||
hub.set_rf_power_switch,
|
||||
apply="set_rf_mode(state)",
|
||||
publish="on_success",
|
||||
)
|
||||
@@ -1,4 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
at581x: !include common.yaml
|
||||
@@ -1,4 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
at581x: !include common.yaml
|
||||
@@ -1,4 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/rp2040-ard.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
at581x: !include common.yaml
|
||||
Reference in new issue
Block a user