Skip to content

Commit

Permalink
feat(base): update base class (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lzw655 authored Jan 24, 2025
2 parents 61740fb + cbd89f6 commit 61fbf44
Show file tree
Hide file tree
Showing 26 changed files with 350 additions and 254 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# ChangeLog

## v1.0.1 - 2024-12-11
## v1.0.1 - 2025-01-23

### Enhancements:

* feat(base): update base class

### Bug Fixes:

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ idf_component_register(
driver
)

target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-missing-field-initializers)
target_compile_options(${COMPONENT_LIB} PUBLIC -Wno-missing-field-initializers)
1 change: 0 additions & 1 deletion examples/ch422g/ch422g.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
Since the input/output mode of CH422G's IO0-7 must remain consistent, the driver will only set IO0-7 to
input mode when it determines that all pins are configured as input.
Using pinMode and multiPinMode will be invalid. You can only set the pin working mode through enableAllIO_Input, enableAllIO_Output, enableOC_PushPull and enableOC_OpenDrain
*
*/

#include <Arduino.h>
Expand Down
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ issues: https://github.com/esp-arduino-libs/ESP32_IO_Expander/issues
dependencies:
idf: ">=5.1"
espressif/esp-lib-utils:
version: ">=0.1.0,<=0.2.0"
version: "0.1.*"
public: true
6 changes: 6 additions & 0 deletions micropython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ target_sources(usermod_esp_io_expander INTERFACE ${SRCS_C} ${SRCS_CXX})
# Add the current directory as an include directory.
target_include_directories(usermod_esp_io_expander INTERFACE ${SRC_DIR})

# Add compile options. Since the target is not created by `idf_component_register()`, we need to add the `ESP_PLATFORM` define manually.
target_compile_options(usermod_esp_io_expander
INTERFACE
-Wno-missing-field-initializers -DESP_PLATFORM $<$<COMPILE_LANGUAGE:CXX>:-std=gnu++2b>
)

# Link our INTERFACE library to the usermod target.
target_link_libraries(usermod INTERFACE usermod_esp_io_expander)
1 change: 0 additions & 1 deletion src/ESP_IOExpander_Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

/**
* This file is just to keep the compatibility with the old version of the library. Please use the file `esp_io_expander.hpp` instead.
*
*/

#pragma once
Expand Down
175 changes: 151 additions & 24 deletions src/chip/esp_expander_base.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -13,20 +13,138 @@

namespace esp_expander {

void Base::Config::convertPartialToFull(void)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

if (isHostConfigValid() && std::holds_alternative<HostPartialConfig>(host.value())) {
#if ESP_UTILS_CONF_LOG_LEVEL == ESP_UTILS_LOG_LEVEL_DEBUG
printHostConfig();
#endif // ESP_UTILS_LOG_LEVEL_DEBUG
auto &config = std::get<HostPartialConfig>(host.value());
host = HostFullConfig{
.mode = I2C_MODE_MASTER,
.sda_io_num = config.sda_io_num,
.scl_io_num = config.scl_io_num,
.sda_pullup_en = config.sda_pullup_en,
.scl_pullup_en = config.scl_pullup_en,
.master = {
.clk_speed = static_cast<uint32_t>(config.clk_speed),
},
.clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL,
};
}

ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
}

void Base::Config::printHostConfig(void) const
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

if (!isHostConfigValid()) {
ESP_UTILS_LOGI("\n\t{Host config}[skipped]");
goto end;
}

if (std::holds_alternative<HostFullConfig>(host.value())) {
auto &config = std::get<HostFullConfig>(host.value());
ESP_UTILS_LOGI(
"\n\t{Host config}[full]\n"
"\t\t-> [host_id]: %d\n"
"\t\t-> [mode]: %d\n"
"\t\t-> [sda_io_num]: %d\n"
"\t\t-> [scl_io_num]: %d\n"
"\t\t-> [sda_pullup_en]: %d\n"
"\t\t-> [scl_pullup_en]: %d\n"
"\t\t-> [master.clk_speed]: %d\n"
"\t\t-> [clk_flags]: %d"
, host_id
, config.mode
, config.sda_io_num
, config.scl_io_num
, config.sda_pullup_en
, config.scl_pullup_en
, config.master.clk_speed
, config.clk_flags
);
} else {
auto &config = std::get<HostPartialConfig>(host.value());
ESP_UTILS_LOGI(
"\n\t{Host config}[partial]\n"
"\t\t-> [host_id]: %d\n"
"\t\t-> [sda_io_num]: %d\n"
"\t\t-> [scl_io_num]: %d\n"
"\t\t-> [sda_pullup_en]: %d\n"
"\t\t-> [scl_pullup_en]: %d\n"
"\t\t-> [clk_speed]: %d"
, host_id
, config.sda_io_num
, config.scl_io_num
, config.sda_pullup_en
, config.scl_pullup_en
, config.clk_speed
);
}

end:
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
}

void Base::Config::printDeviceConfig(void) const
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_LOGI(
"\n\t{Device config}[partial]\n"
"\t\t-> [host_id]: %d\n"
"\t\t-> [address]: 0x%02X"
, host_id
, device.address
);

ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
}

bool Base::configHostSkipInit(bool skip_init)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(!isOverState(State::INIT), false, "Should be called before `init()`");

_is_host_skip_init = skip_init;

ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();

return true;
}

bool Base::init(void)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(!checkIsInit(), false, "Already initialized");
ESP_UTILS_CHECK_FALSE_RETURN(!isOverState(State::INIT), false, "Already initialized");

// Convert the partial configuration to full configuration
_config.convertPartialToFull();
#if ESP_UTILS_CONF_LOG_LEVEL == ESP_UTILS_LOG_LEVEL_DEBUG
_config.printHostConfig();
_config.printDeviceConfig();
#endif // ESP_UTILS_LOG_LEVEL_DEBUG

if (!checkIsSkipInitHost()) {
ESP_UTILS_CHECK_ERROR_RETURN(i2c_param_config(getHostID(), &_host_config), false, "I2C param config failed");
// Initialize the I2C host if not skipped
if (!isHostSkipInit()) {
i2c_port_t host_id = static_cast<i2c_port_t>(getConfig().host_id);
ESP_UTILS_CHECK_ERROR_RETURN(
i2c_driver_install(getHostID(), _host_config.mode, 0, 0, 0), false, "I2C driver install failed"
i2c_param_config(host_id, getHostFullConfig()), false, "I2C param config failed"
);
ESP_UTILS_LOGI("Init I2C host(%d)", _host_id);
ESP_UTILS_CHECK_ERROR_RETURN(
i2c_driver_install(host_id, getHostFullConfig()->mode, 0, 0, 0), false, "I2C driver install failed"
);
ESP_UTILS_LOGD("Init I2C host(%d)", static_cast<int>(host_id));
}
_flags.is_init = true;

setState(State::INIT);

ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();

Expand All @@ -37,7 +155,7 @@ bool Base::reset(void)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_reset(device_handle), false, "Reset failed");

Expand All @@ -50,20 +168,20 @@ bool Base::del(void)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

if (checkIsBegun()) {
if (device_handle != nullptr) {
ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_del(device_handle), false, "Delete failed");
device_handle = nullptr;
ESP_UTILS_LOGD("Delete IO expander(@%p)", device_handle);
ESP_UTILS_LOGD("Delete @%p", device_handle);
}

if (checkIsInit()) {
if (!checkIsSkipInitHost()) {
ESP_UTILS_CHECK_ERROR_RETURN(i2c_driver_delete(getHostID()), false, "I2C driver delete failed");
ESP_UTILS_LOGI("Delete I2C host(%d)", _host_id);
}
_flags.is_init = false;
if (isOverState(State::INIT) && !isHostSkipInit()) {
i2c_port_t host_id = static_cast<i2c_port_t>(getConfig().host_id);
ESP_UTILS_CHECK_ERROR_RETURN(i2c_driver_delete(host_id), false, "I2C driver delete failed");
ESP_UTILS_LOGD("Delete I2C host(%d)", static_cast<int>(host_id));
}

setState(State::DEINIT);

ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();

return true;
Expand All @@ -73,7 +191,7 @@ bool Base::pinMode(uint8_t pin, uint8_t mode)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_LOGD("Param: pin(%d), mode(%d)", pin, mode);
ESP_UTILS_CHECK_FALSE_RETURN(IS_VALID_PIN(pin), false, "Invalid pin");
Expand All @@ -91,7 +209,7 @@ bool Base::digitalWrite(uint8_t pin, uint8_t value)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_LOGD("Param: pin(%d), value(%d)", pin, value);
ESP_UTILS_CHECK_FALSE_RETURN(IS_VALID_PIN(pin), false, "Invalid pin");
Expand All @@ -109,7 +227,7 @@ int Base::digitalRead(uint8_t pin)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_LOGD("Param: pin(%d)", pin);
ESP_UTILS_CHECK_FALSE_RETURN(IS_VALID_PIN(pin), -1, "Invalid pin");
Expand All @@ -128,7 +246,7 @@ bool Base::multiPinMode(uint32_t pin_mask, uint8_t mode)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_LOGD("Param: pin_mask(%0x), mode(%d)", pin_mask, mode);
ESP_UTILS_CHECK_FALSE_RETURN((mode == INPUT) || (mode == OUTPUT), false, "Invalid mode");
Expand All @@ -145,7 +263,7 @@ bool Base::multiDigitalWrite(uint32_t pin_mask, uint8_t value)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_LOGD("Param: pin_mask(%0x), value(%d)", pin_mask, value);

Expand All @@ -160,7 +278,7 @@ int64_t Base::multiDigitalRead(uint32_t pin_mask)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_LOGD("Param: pin_mask(%0x)", pin_mask);

Expand All @@ -172,11 +290,11 @@ int64_t Base::multiDigitalRead(uint32_t pin_mask)
return level;
}

bool Base::printStatus(void)
bool Base::printStatus(void) const
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_print_state(device_handle), false, "Print state failed");

Expand All @@ -185,4 +303,13 @@ bool Base::printStatus(void)
return true;
}

Base::HostFullConfig *Base::getHostFullConfig()
{
if (std::holds_alternative<HostPartialConfig>(_config.host.value())) {
_config.convertPartialToFull();
}

return &std::get<HostFullConfig>(_config.host.value());
}

} // namespace esp_expander
Loading

0 comments on commit 61fbf44

Please sign in to comment.