From 1cc8d7961ab4e7a4c74eebda5726c54c16917ba8 Mon Sep 17 00:00:00 2001 From: Ton Huisman Date: Sat, 1 Feb 2025 22:50:24 +0100 Subject: [PATCH] [I2C] Code improvements and UI issues fixed --- src/_P017_PN532.ino | 9 +++++++-- src/_P036_FrameOLED.ino | 9 +++++++-- src/src/DataStructs_templ/SettingsStruct.cpp | 6 ++++++ src/src/Helpers/I2C_Plugin_Helper.cpp | 11 ++++++++--- src/src/PluginStructs/P109_data_struct.cpp | 9 +++++++-- src/src/WebServer/AdvancedConfigPage.cpp | 4 ++-- src/src/WebServer/DevicesPage.cpp | 15 +++++++++++++-- src/src/WebServer/HardwarePage.cpp | 9 ++++++++- 8 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/_P017_PN532.ino b/src/_P017_PN532.ino index 8634fea1be..63cdb4db1f 100644 --- a/src/_P017_PN532.ino +++ b/src/_P017_PN532.ino @@ -242,8 +242,13 @@ bool P017_handle_timer_in(struct EventStruct *event) # endif // ifdef P017_DEBUG_LOGIC_ANALYZER_PIN // TODO: Clock stretching issue https://github.com/esp8266/Arduino/issues/1541 - if (Settings.isI2CEnabled(Settings.getI2CInterface(event->TaskIndex)) - && ((DIRECT_pinRead(Settings.Pin_i2c_sda) == 0) || (DIRECT_pinRead(Settings.Pin_i2c_scl) == 0))) + #if FEATURE_I2C_MULTIPLE + const uint8_t i2cBus = Settings.getI2CInterface(event->TaskIndex); + #else + const uint8_t i2cBus = 0; + #endif // if FEATURE_I2C_MULTIPLE + if (Settings.isI2CEnabled(i2cBus) + && ((DIRECT_pinRead(Settings.getI2CSdaPin(i2cBus)) == 0) || (DIRECT_pinRead(Settings.getI2CSclPin(i2cBus)) == 0))) { addLog(LOG_LEVEL_ERROR, F("PN532: BUS error")); Plugin_017_Init(CONFIG_PIN3); diff --git a/src/_P036_FrameOLED.ino b/src/_P036_FrameOLED.ino index 6070a33377..2337ab862a 100644 --- a/src/_P036_FrameOLED.ino +++ b/src/_P036_FrameOLED.ino @@ -747,12 +747,17 @@ boolean Plugin_036(uint8_t function, struct EventStruct *event, String& string) P036_CheckHeap(F("_INIT: Before P036_data->init()")); # endif // P036_CHECK_HEAP + #if FEATURE_I2C_MULTIPLE + const uint8_t i2cBus = Settings.getI2CInterface(event->TaskIndex); + #else + const uint8_t i2cBus = 0; + #endif // if FEATURE_I2C_MULTIPLE if (!(P036_data->init(event->TaskIndex, get4BitFromUL(P036_FLAGS_0, P036_FLAG_SETTINGS_VERSION), // Bit23-20 Version CustomTaskSettings P036_CONTROLLER, // Type P036_ADR, // I2C address - Settings.Pin_i2c_sda, - Settings.Pin_i2c_scl, + Settings.getI2CSdaPin(i2cBus), + Settings.getI2CSclPin(i2cBus), static_cast(P036_RESOLUTION), // OLED index (P036_ROTATE == 2), // 1 = Normal, 2 = Rotated P036_CONTRAST, diff --git a/src/src/DataStructs_templ/SettingsStruct.cpp b/src/src/DataStructs_templ/SettingsStruct.cpp index 8e9aaa1d05..5106b7e005 100644 --- a/src/src/DataStructs_templ/SettingsStruct.cpp +++ b/src/src/DataStructs_templ/SettingsStruct.cpp @@ -578,6 +578,12 @@ void SettingsStruct_tmpl::clearMisc() { Pin_status_led_Inversed = DEFAULT_PIN_STATUS_LED_INVERSED; Pin_sd_cs = -1; #ifdef ESP32 + #if FEATURE_I2C_MULTIPLE + Pin_i2c2_sda = DEFAULT_PIN_I2C2_SDA; + Pin_i2c2_scl = DEFAULT_PIN_I2C2_SCL; + Pin_i2c3_sda = DEFAULT_PIN_I2C3_SDA; + Pin_i2c3_scl = DEFAULT_PIN_I2C3_SCL; + #endif // Ethernet related settings are never used on ESP8266 ETH_Phy_Addr = DEFAULT_ETH_PHY_ADDR; ETH_Pin_mdc_cs = DEFAULT_ETH_PIN_MDC; diff --git a/src/src/Helpers/I2C_Plugin_Helper.cpp b/src/src/Helpers/I2C_Plugin_Helper.cpp index 86b2393743..2f92f3b571 100644 --- a/src/src/Helpers/I2C_Plugin_Helper.cpp +++ b/src/src/Helpers/I2C_Plugin_Helper.cpp @@ -9,14 +9,19 @@ **********************************************************************/ bool checkI2CConfigValid_toHtml(taskIndex_t taskIndex, bool outputToHtml) { - if ((Settings.Pin_i2c_sda == -1) || (Settings.Pin_i2c_scl == -1)) { + #if FEATURE_I2C_MULTIPLE + const uint8_t i2cBus = Settings.getI2CInterface(taskIndex); + #else + const uint8_t i2cBus = 0; + #endif // if FEATURE_I2C_MULTIPLE + if ((Settings.getI2CSdaPin(i2cBus) == -1) || (Settings.getI2CSclPin(i2cBus) == -1)) { if (outputToHtml) { addHtml(F("Incomplete I2C configuration.")); } return false; } #if FEATURE_I2CMULTIPLEXER - if ((Settings.I2C_Multiplexer_Type != I2C_MULTIPLEXER_NONE) && - (Settings.I2C_Multiplexer_Addr == -1)) { // Multiplexer selected, but no port configured + if ((Settings.getI2CMultiplexerType(i2cBus) != I2C_MULTIPLEXER_NONE) && + (Settings.getI2CMultiplexerAddr(i2cBus) == -1)) { // Multiplexer selected, but no port configured if (outputToHtml) { addHtml(F("Incomplete I2C Multiplexer configuration.")); } return false; } diff --git a/src/src/PluginStructs/P109_data_struct.cpp b/src/src/PluginStructs/P109_data_struct.cpp index b43220bad6..fc4a398c75 100644 --- a/src/src/PluginStructs/P109_data_struct.cpp +++ b/src/src/PluginStructs/P109_data_struct.cpp @@ -87,10 +87,15 @@ bool P109_data_struct::plugin_init(struct EventStruct *event) { _relayInverted = P109_GET_RELAY_INVERT; _setpointTimeout = P109_CONFIG_SETPOINT_DELAY - P109_SETPOINT_OFFSET; + #if FEATURE_I2C_MULTIPLE + const uint8_t i2cBus = Settings.getI2CInterface(event->TaskIndex); + #else + const uint8_t i2cBus = 0; + #endif // if FEATURE_I2C_MULTIPLE if (P109_CONFIG_DISPLAYTYPE == 1) { - _display = new (std::nothrow) SSD1306Wire(P109_CONFIG_I2CADDRESS, Settings.Pin_i2c_sda, Settings.Pin_i2c_scl); + _display = new (std::nothrow) SSD1306Wire(P109_CONFIG_I2CADDRESS, Settings.getI2CSdaPin(i2cBus), Settings.getI2CSclPin(i2cBus)); } else { - _display = new (std::nothrow) SH1106Wire(P109_CONFIG_I2CADDRESS, Settings.Pin_i2c_sda, Settings.Pin_i2c_scl); + _display = new (std::nothrow) SH1106Wire(P109_CONFIG_I2CADDRESS, Settings.getI2CSdaPin(i2cBus), Settings.getI2CSclPin(i2cBus)); } if (nullptr == _display) { diff --git a/src/src/WebServer/AdvancedConfigPage.cpp b/src/src/WebServer/AdvancedConfigPage.cpp index 8f5167356b..880a9a1abe 100644 --- a/src/src/WebServer/AdvancedConfigPage.cpp +++ b/src/src/WebServer/AdvancedConfigPage.cpp @@ -338,12 +338,12 @@ void handle_advanced() { addUnit(F("1/80 usec")); #endif #if FEATURE_I2C_MULTIPLE - if (getI2CBusCount() >= 2) { + if ((getI2CBusCount() >= 2) && Settings.isI2CEnabled(1)) { addFormNumericBox(concat(F("I2C ClockStretchLimit"), F(" Interface 2")), F("wire2stretch"), Settings.Wire2ClockStretchLimit); // TODO define limits addUnit(F("1/80 usec")); } #if FEATURE_I2C_INTERFACE_3 - if (getI2CBusCount() >= 3) { + if ((getI2CBusCount() >= 3) && Settings.isI2CEnabled(2)) { addFormNumericBox(concat(F("I2C ClockStretchLimit"), F(" Interface 3")), F("wire3stretch"), Settings.Wire3ClockStretchLimit); // TODO define limits addUnit(F("1/80 usec")); } diff --git a/src/src/WebServer/DevicesPage.cpp b/src/src/WebServer/DevicesPage.cpp index 86dbc3545a..ab92fc9eed 100644 --- a/src/src/WebServer/DevicesPage.cpp +++ b/src/src/WebServer/DevicesPage.cpp @@ -834,6 +834,12 @@ void format_I2C_port_description(taskIndex_t x) # if FEATURE_I2CMULTIPLEXER #if FEATURE_I2C_MULTIPLE const uint8_t i2cBus = Settings.getI2CInterface(x); + if (i2cBus > 0) { + html_BR(); + addHtml(F("I2C Interface")); + addHtml(' '); + addHtmlInt(i2cBus + 1); + } #else const uint8_t i2cBus = 0; #endif // if FEATURE_I2C_MULTIPLE @@ -876,10 +882,15 @@ void format_SPI_port_description(int8_t spi_gpios[3]) void format_I2C_pin_description(taskIndex_t x) { + #if FEATURE_I2C_MULTIPLE + const uint8_t i2cBus = Settings.getI2CInterface(x); + #else + const uint8_t i2cBus = 0; + #endif // if FEATURE_I2C_MULTIPLE if (checkI2CConfigValid_toHtml(x)) { - Label_Gpio_toHtml(F("SDA"), formatGpioLabel(Settings.Pin_i2c_sda, false)); + Label_Gpio_toHtml(F("SDA"), formatGpioLabel(Settings.getI2CSdaPin(i2cBus), false)); html_BR(); - Label_Gpio_toHtml(F("SCL"), formatGpioLabel(Settings.Pin_i2c_scl, false)); + Label_Gpio_toHtml(F("SCL"), formatGpioLabel(Settings.getI2CSclPin(i2cBus), false)); } } diff --git a/src/src/WebServer/HardwarePage.cpp b/src/src/WebServer/HardwarePage.cpp index 3d431a9d16..a496e2ab3d 100644 --- a/src/src/WebServer/HardwarePage.cpp +++ b/src/src/WebServer/HardwarePage.cpp @@ -250,7 +250,14 @@ void handle_hardware() { #endif // if FEATURE_I2CMULTIPLEXER } #if FEATURE_I2C_MULTIPLE - if (getI2CBusCount() >= 2) { + const uint8_t i2cMaxBusCount = (getI2CBusCount() >= 2 + ? ((Settings.isI2CEnabled(1) ? 1 : 0) + # if FEATURE_I2C_INTERFACE_3 + + (Settings.isI2CEnabled(2) ? 1 : 0) + # endif // if FEATURE_I2C_INTERFACE_3 + ) + : 0) + (Settings.isI2CEnabled(0) ? 1 : 0); + if (i2cMaxBusCount > 1) { addFormSubHeader(F("PCF & MCP Direct I/O")); const uint8_t i2cBus = Settings.getI2CInterfacePCFMCP(); I2CInterfaceSelector(F("I2C Interface"),