Skip to content

Commit 114161a

Browse files
committed
[I2C] Add more getters to SettingsStruct to simplify code
1 parent f8cba3c commit 114161a

File tree

17 files changed

+68
-61
lines changed

17 files changed

+68
-61
lines changed

src/_P017_PN532.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ bool P017_handle_timer_in(struct EventStruct *event)
242242
# endif // ifdef P017_DEBUG_LOGIC_ANALYZER_PIN
243243

244244
// TODO: Clock stretching issue https://github.com/esp8266/Arduino/issues/1541
245-
if (Settings.isI2CEnabled(get3BitFromUL(Settings.I2C_Flags[event->TaskIndex], I2C_FLAGS_BUS_NUMBER))
245+
if (Settings.isI2CEnabled(Settings.getI2CInterface(event->TaskIndex))
246246
&& ((DIRECT_pinRead(Settings.Pin_i2c_sda) == 0) || (DIRECT_pinRead(Settings.Pin_i2c_scl) == 0)))
247247
{
248248
addLog(LOG_LEVEL_ERROR, F("PN532: BUS error"));

src/_P139_AXP2101.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ boolean Plugin_139(uint8_t function, struct EventStruct *event, String& string)
257257

258258
case PLUGIN_INIT:
259259
{
260-
if (Settings.isI2CEnabled(get3BitFromUL(Settings.I2C_Flags[event->TaskIndex], I2C_FLAGS_BUS_NUMBER))) { // FIXME
260+
if (Settings.isI2CEnabled(Settings.getI2CInterface(event->TaskIndex))) { // FIXME
261261
P139_data_struct *P139_init = static_cast<P139_data_struct *>(getPluginTaskData(event->TaskIndex));
262262

263263
if (nullptr != P139_init) {

src/_P166_GP8403.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ boolean Plugin_166(uint8_t function, struct EventStruct *event, String& string)
214214

215215
case PLUGIN_INIT:
216216
{
217-
if (Settings.isI2CEnabled(get3BitFromUL(Settings.I2C_Flags[event->TaskIndex], I2C_FLAGS_BUS_NUMBER))) {
217+
if (Settings.isI2CEnabled(Settings.getI2CInterface(event->TaskIndex))) {
218218
initPluginTaskData(event->TaskIndex,
219219
new (std::nothrow) P166_data_struct(P166_I2C_ADDRESS,
220220
static_cast<DFRobot_GP8403::eOutPutRange_t>(P166_MAX_VOLTAGE)));

src/src/Commands/GPIO.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ bool getGPIOPinStateValues(String& str) {
12241224
#endif // if FEATURE_PINSTATE_EXTENDED
12251225
#if FEATURE_I2C_MULTIPLE
12261226
if (getI2CBusCount() >= 2) {
1227-
I2CSelectHighClockSpeed(get3BitFromUL(Settings.I2C_peripheral_bus, I2C_PERIPHERAL_BUS_PCFMCP));
1227+
I2CSelectHighClockSpeed(Settings.getI2CInterfacePCFMCP());
12281228
}
12291229
#endif // if FEATURE_I2C_MULTIPLE
12301230
str = GPIO_MCP_Read(par1);
@@ -1242,7 +1242,7 @@ bool getGPIOPinStateValues(String& str) {
12421242
#endif // if FEATURE_PINSTATE_EXTENDED
12431243
#if FEATURE_I2C_MULTIPLE
12441244
if (getI2CBusCount() >= 2) {
1245-
I2CSelectHighClockSpeed(get3BitFromUL(Settings.I2C_peripheral_bus, I2C_PERIPHERAL_BUS_PCFMCP));
1245+
I2CSelectHighClockSpeed(Settings.getI2CInterfacePCFMCP());
12461246
}
12471247
#endif // if FEATURE_I2C_MULTIPLE
12481248
str = GPIO_PCF_Read(par1);

src/src/DataStructs/DeviceStruct.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
#define I2C_FLAGS_MUX_MULTICHANNEL 1 // Allow multiple multiplexer channels when set
4141
#define I2C_FLAGS_BUS_NUMBER 2 // 3 bits. The I2C bus number to use (ESP32 only), 3 bits allow for future expansion
4242

43+
#if FEATURE_I2C_MULTIPLE
44+
#define I2C_PERIPHERAL_BUS_CLOCK 0 // bit-offset for I2C bus used for the RTC clock device
45+
#define I2C_PERIPHERAL_BUS_WDT 3 // bit-offset for I2C bus used for the watchdog timer
46+
#define I2C_PERIPHERAL_BUS_PCFMCP 6 // bit-offset for I2C bus used for PCF & MCP direct access
47+
// #define I2C_PERIPHERAL_BUS_??? 9 // bit-offset for I2C bus used for the ???
48+
#endif // if FEATURE_I2C_MULTIPLE
4349

4450

4551
/*********************************************************************************************\

src/src/DataStructs/SettingsStruct.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,19 @@ class SettingsStruct_tmpl
317317
// Return true if I2C settings are correct
318318
bool isI2CEnabled(uint8_t i2cBus) const;
319319

320+
uint8_t getI2CInterface(taskIndex_t TaskIndex) const;
320321
int8_t getI2CSdaPin(uint8_t i2cBus) const;
321322
int8_t getI2CSclPin(uint8_t i2cBus) const;
322323
uint32_t getI2CClockSpeed(uint8_t i2cBus) const;
323324
uint32_t getI2CClockSpeedSlow(uint8_t i2cBus) const;
324325
uint32_t getI2CClockStretch(uint8_t i2cBus) const;
325326

327+
#if FEATURE_I2C_MULTIPLE
328+
uint8_t getI2CInterfaceRTC() const;
329+
uint8_t getI2CInterfaceWDT() const;
330+
uint8_t getI2CInterfacePCFMCP() const;
331+
#endif // if FEATURE_I2C_MULTIPLE
332+
326333
#if FEATURE_I2CMULTIPLEXER
327334
int8_t getI2CMultiplexerType(uint8_t i2cBus) const;
328335
int8_t getI2CMultiplexerAddr(uint8_t i2cBus) const;

src/src/DataStructs_templ/SettingsStruct.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -966,39 +966,25 @@ bool SettingsStruct_tmpl<N_TASKS>::isSPI_valid() const {
966966
template<unsigned int N_TASKS>
967967
bool SettingsStruct_tmpl<N_TASKS>::isI2C_pin(int8_t pin) const {
968968
if (pin < 0) { return false; }
969-
return Pin_i2c_sda == pin || Pin_i2c_scl == pin
970-
#if FEATURE_I2C_MULTIPLE
971-
|| ((getI2CBusCount() >= 2) && (Pin_i2c2_sda == pin || Pin_i2c2_scl == pin))
972-
#if FEATURE_I2C_INTERFACE_3
973-
|| ((getI2CBusCount() >= 3) && (Pin_i2c3_sda == pin || Pin_i2c3_scl == pin))
974-
#endif // if FEATURE_I2C_INTERFACE_3
975-
#endif // if FEATURE_I2C_MULTIPLE
976-
;
969+
for (uint8_t i2cBus = 0; i2cBus < getI2CBusCount(); ++i2cBus) {
970+
if ((getI2CSdaPin(i2cBus) == pin) || (getI2CSclPin(i2cBus) == pin)) {
971+
return true;
972+
}
973+
}
974+
return false;
977975
}
978976

979977
template<unsigned int N_TASKS>
980978
bool SettingsStruct_tmpl<N_TASKS>::isI2CEnabled(uint8_t i2cBus) const {
981-
if (0 == i2cBus) {
982-
return (Pin_i2c_sda != -1) &&
983-
(Pin_i2c_scl != -1) &&
984-
(I2C_clockSpeed > 0) &&
985-
(I2C_clockSpeed_Slow > 0);
986-
#if FEATURE_I2C_MULTIPLE
987-
} else if (1 == i2cBus) {
988-
return (Pin_i2c2_sda != -1) &&
989-
(Pin_i2c2_scl != -1) &&
990-
(I2C2_clockSpeed > 0) &&
991-
(I2C2_clockSpeed_Slow > 0);
992-
#if FEATURE_I2C_INTERFACE_3
993-
} else {
994-
return (Pin_i2c3_sda != -1) &&
995-
(Pin_i2c3_scl != -1) &&
996-
(I2C3_clockSpeed > 0) &&
997-
(I2C3_clockSpeed_Slow > 0);
998-
#endif // if FEATURE_I2C_INTERFACE_3
999-
#endif // if FEATURE_I2C_MULTIPLE
1000-
}
1001-
return false;
979+
return (getI2CSdaPin(i2cBus) != -1) &&
980+
(getI2CSclPin(i2cBus) != -1) &&
981+
(getI2CClockSpeed(i2cBus) > 0) &&
982+
(getI2CClockSpeedSlow(i2cBus) > 0);
983+
}
984+
985+
template<unsigned int N_TASKS>
986+
uint8_t SettingsStruct_tmpl<N_TASKS>::getI2CInterface(taskIndex_t TaskIndex) const {
987+
return get3BitFromUL(I2C_Flags[TaskIndex], I2C_FLAGS_BUS_NUMBER);
1002988
}
1003989

1004990
template<unsigned int N_TASKS>
@@ -1081,6 +1067,23 @@ uint32_t SettingsStruct_tmpl<N_TASKS>::getI2CClockStretch(uint8_t i2cBus) const
10811067
return 0u;
10821068
}
10831069

1070+
#if FEATURE_I2C_MULTIPLE
1071+
template<unsigned int N_TASKS>
1072+
uint8_t SettingsStruct_tmpl<N_TASKS>::getI2CInterfaceRTC() const {
1073+
return get3BitFromUL(I2C_peripheral_bus, I2C_PERIPHERAL_BUS_CLOCK);
1074+
}
1075+
1076+
template<unsigned int N_TASKS>
1077+
uint8_t SettingsStruct_tmpl<N_TASKS>::getI2CInterfaceWDT() const {
1078+
return get3BitFromUL(I2C_peripheral_bus, I2C_PERIPHERAL_BUS_WDT);
1079+
}
1080+
1081+
template<unsigned int N_TASKS>
1082+
uint8_t SettingsStruct_tmpl<N_TASKS>::getI2CInterfacePCFMCP() const {
1083+
return get3BitFromUL(I2C_peripheral_bus, I2C_PERIPHERAL_BUS_PCFMCP);
1084+
}
1085+
#endif // if FEATURE_I2C_MULTIPLE
1086+
10841087
#if FEATURE_I2CMULTIPLEXER
10851088
template<unsigned int N_TASKS>
10861089
int8_t SettingsStruct_tmpl<N_TASKS>::getI2CMultiplexerType(uint8_t i2cBus) const {

src/src/Globals/Plugins.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pluginID_t getPluginID_from_TaskIndex(taskIndex_t taskIndex) {
9393
bool isPluginI2CPowerManager_from_TaskIndex(taskIndex_t taskIndex, uint8_t i2cBus) {
9494
if (validTaskIndex(taskIndex)) {
9595
#if FEATURE_I2C_MULTIPLE
96-
if (get3BitFromUL(Settings.I2C_Flags[taskIndex], I2C_FLAGS_BUS_NUMBER) != i2cBus) {
96+
if (Settings.getI2CInterface(taskIndex) != i2cBus) {
9797
return false;
9898
}
9999
#endif // if FEATURE_I2C_MULTIPLE
@@ -219,7 +219,7 @@ bool prepare_I2C_by_taskIndex(taskIndex_t taskIndex, deviceIndex_t DeviceIndex)
219219
}
220220

221221
#if FEATURE_I2C_MULTIPLE
222-
const uint8_t i2cBus = get3BitFromUL(Settings.I2C_Flags[taskIndex], I2C_FLAGS_BUS_NUMBER);
222+
const uint8_t i2cBus = Settings.getI2CInterface(taskIndex);
223223
#else
224224
const uint8_t i2cBus = 0;
225225
#endif // if FEATURE_I2C_MULTIPLE
@@ -249,7 +249,7 @@ void post_I2C_by_taskIndex(taskIndex_t taskIndex, deviceIndex_t DeviceIndex) {
249249
return;
250250
}
251251
#if FEATURE_I2C_MULTIPLE
252-
const uint8_t i2cBus = get3BitFromUL(Settings.I2C_Flags[taskIndex], I2C_FLAGS_BUS_NUMBER);
252+
const uint8_t i2cBus = Settings.getI2CInterface(taskIndex);
253253
#else
254254
const uint8_t i2cBus = 0;
255255
#endif // ifdef ESP32

src/src/Helpers/ESPEasy_time.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ bool ESPEasy_time::ExtRTC_get(uint32_t& unixtime)
920920
{
921921
unixtime = 0;
922922
#if FEATURE_I2C_MULTIPLE
923-
const uint8_t i2cBus = get3BitFromUL(Settings.I2C_peripheral_bus, I2C_PERIPHERAL_BUS_CLOCK);
923+
const uint8_t i2cBus = Settings.getI2CInterfaceRTC();
924924
#else
925925
const uint8_t i2cBus = 0;
926926
#endif // if FEATURE_I2C_MULTIPLE
@@ -1021,7 +1021,7 @@ bool ESPEasy_time::ExtRTC_set(uint32_t unixtime)
10211021
}
10221022
bool timeAdjusted = false;
10231023
#if FEATURE_I2C_MULTIPLE
1024-
const uint8_t i2cBus = get3BitFromUL(Settings.I2C_peripheral_bus, I2C_PERIPHERAL_BUS_CLOCK);
1024+
const uint8_t i2cBus = Settings.getI2CInterfaceRTC();
10251025
#else
10261026
const uint8_t i2cBus = 0;
10271027
#endif // if FEATURE_I2C_MULTIPLE

src/src/Helpers/Hardware_I2C.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void initI2C() {
5555
delay(500);
5656

5757
#if FEATURE_I2C_MULTIPLE
58-
I2CSelectHighClockSpeed(get3BitFromUL(Settings.I2C_peripheral_bus, I2C_PERIPHERAL_BUS_WDT));
58+
I2CSelectHighClockSpeed(Settings.getI2CInterfaceWDT());
5959
#endif // if FEATURE_I2C_MULTIPLE
6060

6161
if (I2C_write8_reg(Settings.WDI2CAddress,
@@ -236,7 +236,7 @@ void I2CMultiplexerSelectByTaskIndex(taskIndex_t taskIndex) {
236236
uint8_t toWrite = 0;
237237

238238
# if FEATURE_I2C_MULTIPLE
239-
const uint8_t i2cBus = get3BitFromUL(Settings.I2C_Flags[taskIndex], I2C_FLAGS_BUS_NUMBER);
239+
const uint8_t i2cBus = Settings.getI2CInterface(taskIndex);
240240
# else // if FEATURE_I2C_MULTIPLE
241241
const uint8_t i2cBus = 0;
242242
# endif // if FEATURE_I2C_MULTIPLE
@@ -294,7 +294,7 @@ bool I2CMultiplexerPortSelectedForTask(taskIndex_t taskIndex) {
294294
if (!validTaskIndex(taskIndex)) { return false; }
295295

296296
# if FEATURE_I2C_MULTIPLE
297-
const uint8_t i2cBus = get3BitFromUL(Settings.I2C_Flags[taskIndex], I2C_FLAGS_BUS_NUMBER);
297+
const uint8_t i2cBus = Settings.getI2CInterface(taskIndex);
298298
# else // if FEATURE_I2C_MULTIPLE
299299
const uint8_t i2cBus = 0;
300300
# endif // if FEATURE_I2C_MULTIPLE

0 commit comments

Comments
 (0)