mirror of
https://github.com/esphome/esphome.git
synced 2025-03-14 04:25:15 +01:00
Cleaning the code a bit
This commit is contained in:
parent
86e87ea0ed
commit
578c1f8d2c
4 changed files with 8 additions and 171 deletions
|
@ -5,15 +5,11 @@ import esphome.codegen as cg
|
|||
import esphome.final_validate as fv
|
||||
|
||||
from esphome.components import esp32
|
||||
from esphome.components.i2c import I2CDevice, i2c_ns, I2CBus, CONFIG_SCHEMA as I2C_CONFIG_SCHEMA
|
||||
from esphome.core import CORE
|
||||
from esphome.components.i2c import I2CDevice, i2c_ns, I2CBus
|
||||
|
||||
|
||||
from esphome.const import CONF_ID, CONF_BOARD
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
DEPENDENCIES = ["esp32"]
|
||||
|
||||
|
@ -83,12 +79,6 @@ async def to_code(config):
|
|||
submodules=["components/esp-sr", "components/esp-adf-libs"],
|
||||
)
|
||||
|
||||
# esp32.add_idf_component(
|
||||
# name="esp-dsp",
|
||||
# repo="https://github.com/espressif/esp-dsp",
|
||||
# ref="v1.4.0",
|
||||
# )
|
||||
|
||||
cg.add_platformio_option(
|
||||
"board_build.embed_txtfiles", "components/dueros_service/duer_profile"
|
||||
)
|
||||
|
@ -118,9 +108,6 @@ ADFI2CBus = i2c_ns.class_("ADFI2CBus", I2CBus, cg.Component)
|
|||
def _patch_idfi2cbus(config):
|
||||
from esphome.cpp_generator import MockObjClass
|
||||
|
||||
if not CORE.is_esp32:
|
||||
raise cv.Invalid("Not supported on other CPU that ESP32")
|
||||
|
||||
if "i2c" in fv.full_config.get():
|
||||
for i2c_inst in fv.full_config.get()["i2c"]:
|
||||
i2c_inst["id"].type = MockObjClass("i2c::ADFI2CBus", parents = i2c_inst["id"].type._parents)
|
||||
|
|
|
@ -33,6 +33,10 @@ void ADFI2CBus::setup() {
|
|||
ESP_LOGE(TAG, "Too many I2C buses configured"); this->mark_failed(); return;
|
||||
}
|
||||
|
||||
// Recovery is disabled by default in adf since any esp-adf component that needed the I2C bus
|
||||
// might have run its setup and create the bus already. In that case, it would break the bus
|
||||
// this->recover_();
|
||||
this->recovery_result_ = RECOVERY_COMPLETED;
|
||||
|
||||
i2c_config_t conf{};
|
||||
memset(&conf, 0, sizeof(conf));
|
||||
|
@ -48,12 +52,12 @@ void ADFI2CBus::setup() {
|
|||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
if (i2c_bus_run_cb(this->handle_, &recover_i2c_hard, this) != ESP_OK) {
|
||||
/* if (i2c_bus_run_cb(this->handle_, &recover_i2c_hard, this) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "i2c_bus_recover failed");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
*/
|
||||
if (this->scan_) {
|
||||
ESP_LOGV(TAG, "Scanning i2c bus for active devices...");
|
||||
this->i2c_scan_();
|
||||
|
|
|
@ -4,9 +4,8 @@
|
|||
/* Tried and failed with:
|
||||
esp_peripherals/driver/i2c_bus/i2c_bus.h
|
||||
driver/i2c_bus/i2c_bus.h
|
||||
|
||||
Note the conflicting i2c_bus.h header name that exists in both path
|
||||
*/
|
||||
|
||||
#include "../components/esp_peripherals/driver/i2c_bus/i2c_bus.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
|
|
@ -1,153 +0,0 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
import esphome.final_validate as fv
|
||||
from esphome import pins
|
||||
from esphome.const import (
|
||||
CONF_FREQUENCY,
|
||||
CONF_ID,
|
||||
CONF_INPUT,
|
||||
CONF_OUTPUT,
|
||||
CONF_SCAN,
|
||||
CONF_SCL,
|
||||
CONF_SDA,
|
||||
CONF_ADDRESS,
|
||||
CONF_I2C_ID,
|
||||
PLATFORM_ESP32,
|
||||
PLATFORM_ESP8266,
|
||||
PLATFORM_RP2040,
|
||||
)
|
||||
from esphome.core import coroutine_with_priority, CORE
|
||||
from esphome.components.i2c import I2CDevice, i2c_ns, I2CBus
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
CODEOWNERS = ["@esphome/X-Ryl669"]
|
||||
ADFI2CBus = i2c_ns.class_("ADFI2CBus", I2CBus, cg.Component)
|
||||
|
||||
|
||||
CONF_SDA_PULLUP_ENABLED = "sda_pullup_enabled"
|
||||
CONF_SCL_PULLUP_ENABLED = "scl_pullup_enabled"
|
||||
MULTI_CONF = True
|
||||
|
||||
|
||||
def _bus_declare_type(value):
|
||||
if CORE.using_arduino:
|
||||
raise cv.Invalid(f"Not supported on Arduino platform")
|
||||
if CORE.using_esp_idf:
|
||||
return cv.declare_id(ADFI2CBus)(value)
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
pin_with_input_and_output_support = cv.All(
|
||||
pins.internal_gpio_pin_number({CONF_INPUT: True}),
|
||||
pins.internal_gpio_pin_number({CONF_OUTPUT: True}),
|
||||
)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): _bus_declare_type,
|
||||
cv.Optional(CONF_SDA, default="SDA"): pin_with_input_and_output_support,
|
||||
cv.SplitDefault(CONF_SDA_PULLUP_ENABLED, esp32_idf=True): cv.All(
|
||||
cv.only_with_esp_idf, cv.boolean
|
||||
),
|
||||
cv.Optional(CONF_SCL, default="SCL"): pin_with_input_and_output_support,
|
||||
cv.SplitDefault(CONF_SCL_PULLUP_ENABLED, esp32_idf=True): cv.All(
|
||||
cv.only_with_esp_idf, cv.boolean
|
||||
),
|
||||
cv.Optional(CONF_FREQUENCY, default="100kHz"): cv.All(
|
||||
cv.frequency, cv.Range(min=0, min_included=False)
|
||||
),
|
||||
cv.Optional(CONF_SCAN, default=True): cv.boolean,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA),
|
||||
cv.only_on([PLATFORM_ESP32]),
|
||||
)
|
||||
|
||||
|
||||
@coroutine_with_priority(1.0)
|
||||
async def to_code(config):
|
||||
cg.add_global(i2c_ns.using)
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_sda_pin(config[CONF_SDA]))
|
||||
if CONF_SDA_PULLUP_ENABLED in config:
|
||||
cg.add(var.set_sda_pullup_enabled(config[CONF_SDA_PULLUP_ENABLED]))
|
||||
cg.add(var.set_scl_pin(config[CONF_SCL]))
|
||||
if CONF_SCL_PULLUP_ENABLED in config:
|
||||
cg.add(var.set_scl_pullup_enabled(config[CONF_SCL_PULLUP_ENABLED]))
|
||||
|
||||
cg.add(var.set_frequency(int(config[CONF_FREQUENCY])))
|
||||
cg.add(var.set_scan(config[CONF_SCAN]))
|
||||
|
||||
|
||||
def i2c_device_schema(default_address):
|
||||
"""Create a schema for a i2c device.
|
||||
|
||||
:param default_address: The default address of the i2c device, can be None to represent
|
||||
a required option.
|
||||
:return: The i2c device schema, `extend` this in your config schema.
|
||||
"""
|
||||
schema = {
|
||||
cv.GenerateID(CONF_I2C_ID): cv.use_id(I2CBus),
|
||||
cv.Optional("multiplexer"): cv.invalid(
|
||||
"This option has been removed, please see "
|
||||
"the tca9584a docs for the updated way to use multiplexers"
|
||||
),
|
||||
}
|
||||
if default_address is None:
|
||||
schema[cv.Required(CONF_ADDRESS)] = cv.i2c_address
|
||||
else:
|
||||
schema[cv.Optional(CONF_ADDRESS, default=default_address)] = cv.i2c_address
|
||||
return cv.Schema(schema)
|
||||
|
||||
|
||||
async def register_i2c_device(var, config):
|
||||
"""Register an i2c device with the given config.
|
||||
|
||||
Sets the i2c bus to use and the i2c address.
|
||||
|
||||
This is a coroutine, you need to await it with a 'yield' expression!
|
||||
"""
|
||||
parent = await cg.get_variable(config[CONF_I2C_ID])
|
||||
cg.add(var.set_i2c_bus(parent))
|
||||
cg.add(var.set_i2c_address(config[CONF_ADDRESS]))
|
||||
|
||||
|
||||
def final_validate_device_schema(
|
||||
name: str, *, min_frequency: cv.frequency = None, max_frequency: cv.frequency = None
|
||||
):
|
||||
hub_schema = {}
|
||||
if min_frequency is not None:
|
||||
hub_schema[cv.Required(CONF_FREQUENCY)] = cv.Range(
|
||||
min=cv.frequency(min_frequency),
|
||||
min_included=True,
|
||||
msg=f"Component {name} requires a minimum frequency of {min_frequency} for the I2C bus",
|
||||
)
|
||||
|
||||
if max_frequency is not None:
|
||||
hub_schema[cv.Required(CONF_FREQUENCY)] = cv.Range(
|
||||
max=cv.frequency(max_frequency),
|
||||
max_included=True,
|
||||
msg=f"Component {name} cannot be used with a frequency of over {max_frequency} for the I2C bus",
|
||||
)
|
||||
|
||||
return cv.Schema(
|
||||
{cv.Required(CONF_I2C_ID): fv.id_declaration_match_schema(hub_schema)},
|
||||
extra=cv.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
def validate_not_idfbus(config):
|
||||
if not CORE.is_esp32:
|
||||
raise cv.Invalid("Not supported on other CPU that ESP32")
|
||||
|
||||
if (
|
||||
"i2c" in fv.full_config.get()
|
||||
# and fv.full_config.get()["i2c"][0]["id"].type == "IDFI2CBus"
|
||||
):
|
||||
raise cv.Invalid("Can't be used with default i2c component, remove it first to use i2c_adf")
|
||||
return config
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = validate_not_idfbus
|
Loading…
Add table
Reference in a new issue