added all available chip info in dump_config and text_sensors

This commit is contained in:
Gábor Poczkodi 2024-10-23 22:10:32 +02:00
parent db44e50bf2
commit 64c8b4a23b
4 changed files with 96 additions and 21 deletions

View file

@ -127,6 +127,11 @@ CONF_GPIO2 = "gpio2"
CONF_GPIO3 = "gpio3"
# sensor
CONF_CHIP_ID = "chip_id"
CONF_CHIP_REVISION = "chip_revision"
CONF_CHIP_FIRMWARE = "chip_firmware"
CONF_CHIP_COMPONENT = "chip_component"
CONF_CHIP_LIBRARY = "chip_library"
CONF_CHIP_PATCH = "chip_patch"
# CONF_FREQUENCY = "frequency"
# CONF_POWER = "power"
# CONF_ANTCAP = "antcap"
@ -310,6 +315,26 @@ SENSOR_SCHEMA = cv.Schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon=ICON_CHIP,
),
cv.Optional(CONF_CHIP_REVISION): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon=ICON_CHIP,
),
cv.Optional(CONF_CHIP_FIRMWARE): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon=ICON_CHIP,
),
cv.Optional(CONF_CHIP_COMPONENT): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon=ICON_CHIP,
),
cv.Optional(CONF_CHIP_LIBRARY): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon=ICON_CHIP,
),
cv.Optional(CONF_CHIP_PATCH): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon=ICON_CHIP,
),
cv.Optional(CONF_FREQUENCY): sensor.sensor_schema(
unit_of_measurement=UNIT_MEGA_HERTZ,
# entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
@ -448,6 +473,11 @@ VARIABLES = {
SENSORS = {
CONF_SENSOR: [
[CONF_CHIP_ID, "text_sensor"],
[CONF_CHIP_REVISION, "text_sensor"],
[CONF_CHIP_FIRMWARE, "text_sensor"],
[CONF_CHIP_COMPONENT, "text_sensor"],
[CONF_CHIP_LIBRARY, "text_sensor"],
[CONF_CHIP_PATCH, "text_sensor"],
[CONF_FREQUENCY, "sensor"],
[CONF_POWER, "sensor"],
[CONF_ANTCAP, "sensor"],

View file

@ -109,35 +109,31 @@ bool Si4713Component::power_up_() {
CmdPowerUp cmd;
cmd.OPMODE = (uint8_t) this->op_mode_;
cmd.FUNC = 2; // transmit
cmd.FUNC = (uint8_t) PowerUpFunc::FUNC_QUERY_LIBRARY_ID;
cmd.XOSCEN = this->op_mode_ == OpMode::OPMODE_ANALOG ? 1 : 0; // auto-enable(?) xtal for analog mode
cmd.GPO2OEN = 0; // we do this later
cmd.CTSIEN = 0; // no interrupts
return this->send_cmd_(cmd);
}
bool Si4713Component::power_down_() { return this->send_cmd_(CmdPowerDown()); }
bool Si4713Component::detect_chip_id_() {
ResGetRev res;
if (!this->send_cmd_(CmdGetRev(), res)) {
if (!this->send_cmd_(cmd, this->library_)) {
ESP_LOGE(TAG, "query library id failed");
return false;
}
char buff[32] = {0};
snprintf(buff, sizeof(buff), "Si47%02d Rev %d", res.PN, res.CHIPREV);
this->chip_id_ = buff;
cmd.FUNC = (uint8_t) PowerUpFunc::FUNC_TRANSMIT; // transmit
if (!this->send_cmd_(cmd)) {
ESP_LOGE(TAG, "power up failed");
return false;
}
// TODO: support all transmitters 10/11/12/13/20/21, mask unsupported features
if (res.PN != 10 && res.PN != 11 && res.PN != 12 && res.PN != 13) {
ESP_LOGE(TAG, "Si47%02d is not supported", res.PN);
if (this->library_.PN != 10 && this->library_.PN != 11 && this->library_.PN != 12 && this->library_.PN != 13) {
ESP_LOGE(TAG, "Si47%02d is not supported", this->library_.PN);
return false;
}
return true;
}
bool Si4713Component::power_down_() { return this->send_cmd_(CmdPowerDown()); }
bool Si4713Component::tune_freq_(uint16_t freq) { return this->send_cmd_(CmdTxTuneFreq(freq)) && this->stc_wait_(); }
bool Si4713Component::tune_power_(uint8_t power, uint8_t antcap) {
@ -165,7 +161,7 @@ void Si4713Component::setup() {
return;
}
if (!this->detect_chip_id_()) {
if (!this->send_cmd_(CmdGetRev(), this->revision_)) {
this->mark_failed();
return;
}
@ -289,7 +285,12 @@ void Si4713Component::dump_config() {
if (this->is_failed()) {
ESP_LOGE(TAG, "failed!");
}
ESP_LOGCONFIG(TAG, " Chip: %s", this->chip_id_.c_str());
ESP_LOGCONFIG(TAG, " Chip Id: %s", this->get_chip_id_text_sensor().c_str());
ESP_LOGCONFIG(TAG, " Revision: %s", this->get_chip_revision_text_sensor().c_str());
ESP_LOGCONFIG(TAG, " Firmware: %s", this->get_chip_firmware_text_sensor().c_str());
ESP_LOGCONFIG(TAG, " Component: %s", this->get_chip_component_text_sensor().c_str());
ESP_LOGCONFIG(TAG, " Library: %s", this->get_chip_library_text_sensor().c_str());
ESP_LOGCONFIG(TAG, " Patch: %s", this->get_chip_patch_text_sensor().c_str());
ESP_LOGCONFIG(TAG, " Frequency: %.2f MHz", this->get_tuner_frequency());
ESP_LOGCONFIG(TAG, " RDS station: %s", this->rds_station_.c_str());
ESP_LOGCONFIG(TAG, " RDS text: %s", this->rds_text_.c_str());
@ -815,7 +816,41 @@ void Si4713Component::set_output_gpio3(bool value) {
bool Si4713Component::get_output_gpio3() { return this->gpio_[2] != 0; }
std::string Si4713Component::get_chip_id_text_sensor() { return this->chip_id_; }
std::string Si4713Component::get_chip_id_text_sensor() {
char buff[16];
snprintf(buff, sizeof(buff), "Si47%02d", this->library_.PN);
return std::string(buff);
}
std::string Si4713Component::get_chip_revision_text_sensor() {
char buff[16];
snprintf(buff, sizeof(buff), "%c", (char) this->library_.CHIPREV);
return std::string(buff);
}
std::string Si4713Component::get_chip_firmware_text_sensor() {
char buff[16];
snprintf(buff, sizeof(buff), "%c.%c", (char) this->library_.FWMAJOR, (char) this->library_.FWMINOR);
return std::string(buff);
}
std::string Si4713Component::get_chip_component_text_sensor() {
char buff[16];
snprintf(buff, sizeof(buff), "%c.%c", (char) this->revision_.CMPMAJOR, (char) this->revision_.CMPMINOR);
return std::string(buff);
}
std::string Si4713Component::get_chip_library_text_sensor() {
char buff[16];
snprintf(buff, sizeof(buff), "R%d", this->library_.LIBRARYID);
return std::string(buff);
}
std::string Si4713Component::get_chip_patch_text_sensor() {
char buff[16];
snprintf(buff, sizeof(buff), "%d", ((uint16_t) this->revision_.PATCHH << 8) | this->revision_.PATCHL);
return std::string(buff);
}
float Si4713Component::get_frequency_sensor() {
return (float) ((this->tune_status_.READFREQH << 8) | this->tune_status_.READFREQL) / 100;

View file

@ -19,9 +19,10 @@ namespace esphome {
namespace si4713_i2c {
class Si4713Component : public PollingComponent, public i2c::I2CDevice {
std::string chip_id_;
InternalGPIOPin *reset_pin_;
bool reset_;
ResPowerUpQueryLibrary library_;
ResGetRev revision_;
ResTxTuneStatus tune_status_;
ResTxAsqStatus asq_status_;
@ -88,7 +89,6 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice {
bool device_reset_();
bool power_up_();
bool power_down_();
bool detect_chip_id_();
bool tune_freq_(uint16_t freq);
bool tune_power_(uint8_t power, uint8_t antcap = 0);
bool stc_wait_();
@ -153,6 +153,11 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice {
SI4713_SUB_SWITCH(output_gpio2)
SI4713_SUB_SWITCH(output_gpio3)
SI4713_SUB_TEXT_SENSOR(chip_id)
SI4713_SUB_TEXT_SENSOR(chip_revision)
SI4713_SUB_TEXT_SENSOR(chip_firmware)
SI4713_SUB_TEXT_SENSOR(chip_component)
SI4713_SUB_TEXT_SENSOR(chip_library)
SI4713_SUB_TEXT_SENSOR(chip_patch)
SI4713_SUB_SENSOR(frequency)
SI4713_SUB_SENSOR(power)
SI4713_SUB_SENSOR(antcap)

View file

@ -287,6 +287,11 @@ struct ResBase {
ResBase() { this->STATUS = 0; }
};
enum class PowerUpFunc : uint8_t {
FUNC_TRANSMIT = 2,
FUNC_QUERY_LIBRARY_ID = 15,
};
struct CmdPowerUp : CmdBase {
union {
uint8_t ARG[2];