Skip to content

Commit dae6de7

Browse files
committed
Improve co-existence of usermods sharing I2C
Improve co-existence of several popular usermods with respect to shared I2C bus. - ensure that i2c_sda and i2c_scl are used when defined - ensure that HW_PIN_SDA / HW_PIN_SCL are not overwritten - ensure that Wire.begin()nis always called with user-defined pins (remove rogue Wire.begin() without parameters) - ensure that set.cpp / cfg.cpp use esp32-specific global Wire objects.
1 parent 6385bf4 commit dae6de7

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

usermods/BH1750_v2/usermod_bh1750.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
#pragma once
55

6-
#include "wled.h"
6+
#include <Arduino.h> // WLEDMM: make sure that I2C drivers have the "right" Wire Object
77
#include <Wire.h>
8+
9+
#include "wled.h"
810
#include <BH1750.h>
911

1012
// the max frequency to check photoresistor, 10 seconds
@@ -53,9 +55,13 @@ class Usermod_BH1750 : public Usermod
5355
static const char _HomeAssistantDiscovery[];
5456

5557
// set the default pins based on the architecture, these get overridden by Usermod menu settings
56-
#ifdef ARDUINO_ARCH_ESP32 // ESP32 boards
57-
#define HW_PIN_SCL 22
58-
#define HW_PIN_SDA 21
58+
#ifdef ARDUINO_ARCH_ESP32 // ESP32 boards -- WLEDMM: don't override already defined HW pins
59+
#ifndef HW_PIN_SDA
60+
#define HW_PIN_SCL 22
61+
#endif
62+
#ifndef HW_PIN_SDA
63+
#define HW_PIN_SDA 21
64+
#endif
5965
#else // ESP8266 boards
6066
#define HW_PIN_SCL 5
6167
#define HW_PIN_SDA 4
@@ -121,11 +127,20 @@ class Usermod_BH1750 : public Usermod
121127
{
122128
bool HW_Pins_Used = (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA); // note whether architecture-based hardware SCL/SDA pins used
123129
PinOwner po = PinOwner::UM_BH1750; // defaults to being pinowner for SCL/SDA pins
124-
PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true } }; // allocate pins
125130
if (HW_Pins_Used) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
131+
if ((i2c_scl >= 0) && (i2c_sda >=0)) { // WLEDMM: make sure that global HW pins are used if defined
132+
ioPin[0] = i2c_scl;
133+
ioPin[1] = i2c_sda;
134+
po = PinOwner::HW_I2C;
135+
}
136+
PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true } }; // allocate pins // WLEDMM: after selecting final pins
126137
if (!pinManager.allocateMultiplePins(pins, 2, po)) return;
127138

128-
Wire.begin(ioPin[1], ioPin[0]);
139+
#if defined(ARDUINO_ARCH_ESP32)
140+
Wire.begin(pins[1].pin, pins[0].pin); // WLEDMM this might silently fail, which is OK as it just means that I2C bus is already running.
141+
#else
142+
Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed.
143+
#endif
129144

130145
sensorFound = lightMeter.begin();
131146
initDone = true;
@@ -136,6 +151,7 @@ class Usermod_BH1750 : public Usermod
136151
if ((!enabled) || strip.isUpdating())
137152
return;
138153

154+
if (!sensorFound) return; // WLEDMM bugfix
139155
unsigned long now = millis();
140156

141157
// check to see if we are due for taking a measurement
@@ -179,6 +195,8 @@ class Usermod_BH1750 : public Usermod
179195

180196
void addToJsonInfo(JsonObject &root)
181197
{
198+
if ((!enabled) || (!sensorFound)) return; // WLEDMM bugfix
199+
182200
JsonObject user = root[F("u")];
183201
if (user.isNull())
184202
user = root.createNestedObject(F("u"));

usermods/RTC/usermod_rtc.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include <Arduino.h> // WLEDMM: make sure that I2C drivers have the "right" Wire Object
4+
#include <Wire.h>
5+
36
#include "src/dependencies/time/DS1307RTC.h"
47
#include "wled.h"
58

@@ -13,7 +16,17 @@ class RTCUsermod : public Usermod {
1316

1417
void setup() {
1518
PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } };
19+
#if defined(ARDUINO_ARCH_ESP32) && defined(HW_PIN_SDA) && defined(HW_PIN_SCL)
20+
if (pins[0].pin < 0) pins[0].pin = HW_PIN_SCL; //WLEDMM
21+
if (pins[1].pin < 0) pins[1].pin = HW_PIN_SDA; //WLEDMM
22+
#endif
1623
if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { disabled = true; return; }
24+
#if defined(ARDUINO_ARCH_ESP32)
25+
Wire.begin(pins[1].pin, pins[0].pin); // WLEDMM this might silently fail, which is OK as it just means that I2C bus is already running.
26+
#else
27+
Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed.
28+
#endif
29+
1730
RTC.begin();
1831
time_t rtcTime = RTC.get();
1932
if (rtcTime) {

usermods/mpu6050_imu/usermod_mpu6050_imu.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include <Arduino.h> // WLEDMM: make sure that I2C drivers have the "right" Wire Object
4+
#include <Wire.h>
5+
36
#include "wled.h"
47

58
// #define MPU6050_INT_GPIO 13 // WLEDMM - better choice on ESP32
@@ -44,9 +47,12 @@
4447
5. Wire up the MPU6050 as detailed above.
4548
*/
4649

47-
#include "I2Cdev.h"
50+
// WLEDMM: make sure that the "standard" Wire object is used
51+
#define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE
52+
53+
#include <I2Cdev.h>
4854

49-
#include "MPU6050_6Axis_MotionApps20.h"
55+
#include <MPU6050_6Axis_MotionApps20.h>
5056

5157
// WLEDMM - need to re-define WLED DEBUG_PRINT maros, because the were overwritten by MPU6050_6Axis_MotionApps20.h
5258
#undef DEBUG_PRINT
@@ -132,7 +138,12 @@ class MPU6050Driver : public Usermod {
132138

133139
// join I2C bus (I2Cdev library doesn't do this automatically)
134140
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
135-
Wire.begin(); // WLEDMM fixme - this completely ignores any PINS
141+
#if defined(ARDUINO_ARCH_ESP32)
142+
Wire.begin(pins[1].pin, pins[0].pin); // WLEDMM fix - need to use proper pins, in case that Wire was not started yet. Call will silently fail if Wire is initialized already.
143+
#else
144+
Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed.
145+
#endif
146+
136147
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
137148
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
138149
Fastwire::setup(400, true);

usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#pragma once
22

3+
#include <Arduino.h> // WLEDMM: make sure that I2C drivers have the "right" Wire Object
4+
#include <Wire.h>
5+
#undef U8X8_NO_HW_I2C // WLEDMM: we do want I2C hardware drivers - if possible
6+
//#define WIRE_INTERFACES_COUNT 2 // experimental - tell U8x8Lib that there is a econd Wire unit
7+
38
#include "wled.h"
49
#include <U8x8lib.h> // from https://github.com/olikraus/u8g2/
510
#include "4LD_wled_fonts.c"

wled00/cfg.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <Arduino.h> // WLEDMM: make sure that I2C drivers have the "right" Wire Object
2+
#include <Wire.h>
3+
14
#include "wled.h"
25
#include "wled_ethernet.h"
36

wled00/set.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <Arduino.h> // WLEDMM: make sure that I2C drivers have the "right" Wire Object
2+
#include <Wire.h>
3+
14
#include "wled.h"
25

36
/*

wled00/src/dependencies/time/DS1307RTC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DS1307RTC
1414
// user-accessible "public" interface
1515
public:
1616
DS1307RTC() {}
17-
static void begin() { Wire.begin(); }
17+
static void begin() { /*Wire.begin();*/ }
1818
static time_t get();
1919
static bool set(time_t t);
2020
static bool read(tmElements_t &tm);

0 commit comments

Comments
 (0)