Skip to content

Commit

Permalink
[I2C] Code improvements and UI issues fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
tonhuisman committed Feb 1, 2025
1 parent 1eb141c commit 1cc8d79
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/_P017_PN532.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/_P036_FrameOLED.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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>(P036_RESOLUTION), // OLED index
(P036_ROTATE == 2), // 1 = Normal, 2 = Rotated
P036_CONTRAST,
Expand Down
6 changes: 6 additions & 0 deletions src/src/DataStructs_templ/SettingsStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,12 @@ void SettingsStruct_tmpl<N_TASKS>::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;
Expand Down
11 changes: 8 additions & 3 deletions src/src/Helpers/I2C_Plugin_Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 7 additions & 2 deletions src/src/PluginStructs/P109_data_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/src/WebServer/AdvancedConfigPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
15 changes: 13 additions & 2 deletions src/src/WebServer/DevicesPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/src/WebServer/HardwarePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &amp; MCP Direct I/O"));
const uint8_t i2cBus = Settings.getI2CInterfacePCFMCP();
I2CInterfaceSelector(F("I2C Interface"),
Expand Down

0 comments on commit 1cc8d79

Please sign in to comment.