Skip to content

Commit 1d4487b

Browse files
henrygabAircoookie
andauthored
Ethernet configuration fix, improve PinManager (wled#2123)
* Improved pin manager, ethernet config * Ethernet is configured prior even to LED pins * Pin Manager allocation / deallocation functions now take an "ownership" tag parameter, helping avoid accidentally free'ing pins that were allocated by other code * Pin Manager now has ability to allocate multiple pins at once; Simplifies error handling * Fix operator precedence error Bitwise AND has lower precedence than the relational "greater than" operator. * PinManager update for some user modules * don't build everything... * Final step to reduce RAM overhead * update comment * remove macros * Remove leftover allocated * Init ethernet after settings saved Co-authored-by: Christian Schwinne <[email protected]>
1 parent ff8145b commit 1d4487b

File tree

19 files changed

+450
-206
lines changed

19 files changed

+450
-206
lines changed

platformio.ini

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
# Please uncomment one of the lines below to select your board(s)
99
# ------------------------------------------------------------------------------
1010

11-
# Travis CI binaries (comment this out with a ';' when building for your own board)
12-
;default_envs = travis_esp8266, travis_esp32
11+
# Travis CI binaries (use `platformio_override.ini` when building for your own board; see `platformio_override.ini.sample` for an example)
12+
; default_envs = travis_esp8266, travis_esp32
1313

1414
# Release binaries
1515
default_envs = nodemcuv2, esp01_1m_full, esp32dev, esp32_eth
1616

17+
# Build everything
18+
; default_envs = esp32dev, esp8285_4CH_MagicHome, esp8285_4CH_H801, codm-controller-0.6-rev2, codm-controller-0.6, esp32s2_saola, d1_mini_5CH_Shojo_PCB, d1_mini, sp501e, travis_esp8266, travis_esp32, nodemcuv2, esp32_eth, anavi_miracle_controller, esp07, esp01_1m_full, m5atom, h803wf, d1_mini_ota, heltec_wifi_kit_8, esp8285_5CH_H801, d1_mini_debug, wemos_shield_esp32, elekstube_ips
19+
1720
# Single binaries (uncomment your board)
1821
; default_envs = elekstube_ips
1922
; default_envs = nodemcuv2
@@ -399,7 +402,7 @@ build_flags = ${common.build_flags_esp32}
399402
-D TEMPERATURE_PIN=23
400403
lib_deps = ${esp32.lib_deps}
401404
OneWire@~2.3.5
402-
U8g2@~2.28.11
405+
olikraus/U8g2 @ ^2.28.8
403406

404407
[env:m5atom]
405408
board = esp32dev
@@ -482,4 +485,6 @@ build_flags = ${common.build_flags_esp32} -D WLED_DISABLE_BROWNOUT_DET -D WLED_D
482485
-D SPI_FREQUENCY=40000000
483486
-D USER_SETUP_LOADED
484487
monitor_filters = esp32_exception_decoder
485-
lib_deps = ${esp32.lib_deps} TFT_eSPI
488+
lib_deps =
489+
${esp32.lib_deps}
490+
TFT_eSPI @ ^2.3.70

usermods/Animated_Staircase/Animated_Staircase.h

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -306,22 +306,26 @@ class Animated_Staircase : public Usermod {
306306

307307
public:
308308
void setup() {
309+
// standardize invalid pin numbers to -1
310+
if (topPIRorTriggerPin < 0) topPIRorTriggerPin = -1;
311+
if (topEchoPin < 0) topEchoPin = -1;
312+
if (bottomPIRorTriggerPin < 0) bottomPIRorTriggerPin = -1;
313+
if (bottomEchoPin < 0) bottomEchoPin = -1;
309314
// allocate pins
310-
if (topPIRorTriggerPin >= 0) {
311-
if (!pinManager.allocatePin(topPIRorTriggerPin,useUSSensorTop))
312-
topPIRorTriggerPin = -1;
313-
}
314-
if (topEchoPin >= 0) {
315-
if (!pinManager.allocatePin(topEchoPin,false))
316-
topEchoPin = -1;
317-
}
318-
if (bottomPIRorTriggerPin >= 0) {
319-
if (!pinManager.allocatePin(bottomPIRorTriggerPin,useUSSensorBottom))
320-
bottomPIRorTriggerPin = -1;
321-
}
322-
if (bottomEchoPin >= 0) {
323-
if (!pinManager.allocatePin(bottomEchoPin,false))
324-
bottomEchoPin = -1;
315+
PinManagerPinType pins[4] = {
316+
{ topPIRorTriggerPin, useUSSensorTop },
317+
{ topEchoPin, false },
318+
{ bottomPIRorTriggerPin, useUSSensorBottom },
319+
{ bottomEchoPin, false },
320+
};
321+
// NOTE: this *WILL* return TRUE if all the pins are set to -1.
322+
// this is *BY DESIGN*.
323+
if (!pinManager.allocateMultiplePins(pins, 4, PinOwner::UM_AnimatedStaircase)) {
324+
topPIRorTriggerPin = -1;
325+
topEchoPin = -1;
326+
bottomPIRorTriggerPin = -1;
327+
bottomEchoPin = -1;
328+
enabled = false;
325329
}
326330
enable(enabled);
327331
initDone = true;
@@ -480,10 +484,10 @@ class Animated_Staircase : public Usermod {
480484
(oldBottomAPin != bottomPIRorTriggerPin) ||
481485
(oldBottomBPin != bottomEchoPin)) {
482486
changed = true;
483-
pinManager.deallocatePin(oldTopAPin);
484-
pinManager.deallocatePin(oldTopBPin);
485-
pinManager.deallocatePin(oldBottomAPin);
486-
pinManager.deallocatePin(oldBottomBPin);
487+
pinManager.deallocatePin(oldTopAPin, PinOwner::UM_AnimatedStaircase);
488+
pinManager.deallocatePin(oldTopBPin, PinOwner::UM_AnimatedStaircase);
489+
pinManager.deallocatePin(oldBottomAPin, PinOwner::UM_AnimatedStaircase);
490+
pinManager.deallocatePin(oldBottomBPin, PinOwner::UM_AnimatedStaircase);
487491
}
488492
if (changed) setup();
489493
}

usermods/EleksTube_IPS/ChipSelect.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ class ChipSelect {
5858
void setMinutesTens() { setDigit(MINUTES_TENS); }
5959
void setHoursOnes() { setDigit(HOURS_ONES); }
6060
void setHoursTens() { setDigit(HOURS_TENS); }
61-
bool isSecondsOnes() { return (digits_map&SECONDS_ONES_MAP > 0); }
62-
bool isSecondsTens() { return (digits_map&SECONDS_TENS_MAP > 0); }
63-
bool isMinutesOnes() { return (digits_map&MINUTES_ONES_MAP > 0); }
64-
bool isMinutesTens() { return (digits_map&MINUTES_TENS_MAP > 0); }
65-
bool isHoursOnes() { return (digits_map&HOURS_ONES_MAP > 0); }
66-
bool isHoursTens() { return (digits_map&HOURS_TENS_MAP > 0); }
61+
bool isSecondsOnes() { return ((digits_map & SECONDS_ONES_MAP) > 0); }
62+
bool isSecondsTens() { return ((digits_map & SECONDS_TENS_MAP) > 0); }
63+
bool isMinutesOnes() { return ((digits_map & MINUTES_ONES_MAP) > 0); }
64+
bool isMinutesTens() { return ((digits_map & MINUTES_TENS_MAP) > 0); }
65+
bool isHoursOnes() { return ((digits_map & HOURS_ONES_MAP) > 0); }
66+
bool isHoursTens() { return ((digits_map & HOURS_TENS_MAP) > 0); }
6767
};
6868

6969

usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,14 @@ class PIRsensorSwitch : public Usermod
201201
{
202202
if (enabled) {
203203
// pin retrieved from cfg.json (readFromConfig()) prior to running setup()
204-
if (PIRsensorPin >= 0 && pinManager.allocatePin(PIRsensorPin,false)) {
204+
if (PIRsensorPin >= 0 && pinManager.allocatePin(PIRsensorPin, false, PinOwner::UM_PIR)) {
205205
// PIR Sensor mode INPUT_PULLUP
206206
pinMode(PIRsensorPin, INPUT_PULLUP);
207207
sensorPinState = digitalRead(PIRsensorPin);
208208
} else {
209-
if (PIRsensorPin >= 0) DEBUG_PRINTLN(F("PIRSensorSwitch pin allocation failed."));
209+
if (PIRsensorPin >= 0) {
210+
DEBUG_PRINTLN(F("PIRSensorSwitch pin allocation failed."));
211+
}
210212
PIRsensorPin = -1; // allocation failed
211213
enabled = false;
212214
}
@@ -367,8 +369,8 @@ class PIRsensorSwitch : public Usermod
367369
if (oldPin != PIRsensorPin && oldPin >= 0) {
368370
// if we are changing pin in settings page
369371
// deallocate old pin
370-
pinManager.deallocatePin(oldPin);
371-
if (pinManager.allocatePin(PIRsensorPin,false)) {
372+
pinManager.deallocatePin(oldPin, PinOwner::UM_PIR);
373+
if (pinManager.allocatePin(PIRsensorPin, false, PinOwner::UM_PIR)) {
372374
pinMode(PIRsensorPin, INPUT_PULLUP);
373375
} else {
374376
// allocation failed

usermods/Temperature/usermod_temperature.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,19 @@ class UsermodTemperature : public Usermod {
115115
// config says we are enabled
116116
DEBUG_PRINTLN(F("Allocating temperature pin..."));
117117
// pin retrieved from cfg.json (readFromConfig()) prior to running setup()
118-
if (temperaturePin >= 0 && pinManager.allocatePin(temperaturePin)) {
118+
if (temperaturePin >= 0 && pinManager.allocatePin(temperaturePin, true, PinOwner::UM_Temperature)) {
119119
oneWire = new OneWire(temperaturePin);
120-
if (!oneWire->reset())
120+
if (!oneWire->reset()) {
121121
sensorFound = false; // resetting 1-Wire bus yielded an error
122-
else
123-
while ((sensorFound=findSensor()) && retries--) delay(25); // try to find sensor
122+
} else {
123+
while ((sensorFound=findSensor()) && retries--) {
124+
delay(25); // try to find sensor
125+
}
126+
}
124127
} else {
125-
if (temperaturePin >= 0) DEBUG_PRINTLN(F("Temperature pin allocation failed."));
128+
if (temperaturePin >= 0) {
129+
DEBUG_PRINTLN(F("Temperature pin allocation failed."));
130+
}
126131
temperaturePin = -1; // allocation failed
127132
sensorFound = false;
128133
}
@@ -273,7 +278,7 @@ class UsermodTemperature : public Usermod {
273278
DEBUG_PRINTLN(F("Re-init temperature."));
274279
// deallocate pin and release memory
275280
delete oneWire;
276-
pinManager.deallocatePin(temperaturePin);
281+
pinManager.deallocatePin(temperaturePin, PinOwner::UM_Temperature);
277282
temperaturePin = newTemperaturePin;
278283
// initialise
279284
setup();

usermods/multi_relay/usermod_multi_relay.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class MultiRelay : public Usermod {
258258
// pins retrieved from cfg.json (readFromConfig()) prior to running setup()
259259
for (uint8_t i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
260260
if (_relay[i].pin<0) continue;
261-
if (!pinManager.allocatePin(_relay[i].pin,true)) {
261+
if (!pinManager.allocatePin(_relay[i].pin,true, PinOwner::UM_MultiRelay)) {
262262
_relay[i].pin = -1; // allocation failed
263263
} else {
264264
switchRelay(i, _relay[i].state = (bool)bri);
@@ -380,12 +380,14 @@ class MultiRelay : public Usermod {
380380
// deallocate all pins 1st
381381
for (uint8_t i=0; i<MULTI_RELAY_MAX_RELAYS; i++)
382382
if (oldPin[i]>=0) {
383-
pinManager.deallocatePin(oldPin[i]);
383+
pinManager.deallocatePin(oldPin[i], PinOwner::UM_MultiRelay);
384384
}
385385
// allocate new pins
386386
for (uint8_t i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
387-
if (_relay[i].pin>=0 && pinManager.allocatePin(_relay[i].pin,true)) {
388-
if (!_relay[i].external) switchRelay(i, _relay[i].state = (bool)bri);
387+
if (_relay[i].pin>=0 && pinManager.allocatePin(_relay[i].pin, true, PinOwner::UM_MultiRelay)) {
388+
if (!_relay[i].external) {
389+
switchRelay(i, _relay[i].state = (bool)bri);
390+
}
389391
} else {
390392
_relay[i].pin = -1;
391393
}

usermods/rgb-rotary-encoder/rgb-rotary-encoder.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class RgbRotaryEncoderUsermod : public Usermod
1313
BusDigital *ledBus;
1414
/*
1515
* Green - eb - Q4 - 32
16-
* Red - ea - Q1 - 15
16+
* Red - ea - Q1 - 15
1717
* Black - sw - Q2 - 12
1818
*/
1919
ESPRotary *rotaryEncoder;
@@ -39,13 +39,10 @@ class RgbRotaryEncoderUsermod : public Usermod
3939

4040
void initRotaryEncoder()
4141
{
42-
if (!pinManager.allocatePin(eaIo, false)) {
42+
PinManagerPinType pins[2] = { { eaIo, false }, { ebIo, false } };
43+
if (!pinManager.allocateMultiplePins(pins, 2, UM_RGBRotaryEncoder)) {
4344
eaIo = -1;
44-
}
45-
if (!pinManager.allocatePin(ebIo, false)) {
4645
ebIo = -1;
47-
}
48-
if (eaIo == -1 || ebIo == -1) {
4946
cleanup();
5047
return;
5148
}
@@ -111,10 +108,12 @@ class RgbRotaryEncoderUsermod : public Usermod
111108
{
112109
// Only deallocate pins if we allocated them ;)
113110
if (eaIo != -1) {
114-
pinManager.deallocatePin(eaIo);
111+
pinManager.deallocatePin(eaIo, PinOwner::UM_RGBRotaryEncoder);
112+
eaIo = -1;
115113
}
116114
if (ebIo != -1) {
117-
pinManager.deallocatePin(ebIo);
115+
pinManager.deallocatePin(ebIo, PinOwner::UM_RGBRotaryEncoder);
116+
ebIo = -1;
118117
}
119118

120119
delete rotaryEncoder;
@@ -304,8 +303,8 @@ class RgbRotaryEncoderUsermod : public Usermod
304303
}
305304

306305
if (eaIo != oldEaIo || ebIo != oldEbIo || stepsPerClick != oldStepsPerClick || incrementPerClick != oldIncrementPerClick) {
307-
pinManager.deallocatePin(oldEaIo);
308-
pinManager.deallocatePin(oldEbIo);
306+
pinManager.deallocatePin(oldEaIo, PinOwner::UM_RGBRotaryEncoder);
307+
pinManager.deallocatePin(oldEbIo, PinOwner::UM_RGBRotaryEncoder);
309308

310309
delete rotaryEncoder;
311310
initRotaryEncoder();

usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,13 @@ class FourLineDisplayUsermod : public Usermod {
168168
// network here
169169
void setup() {
170170
if (type == NONE) return;
171-
bool allocated = false;
172171
byte i;
173172
if (type == SSD1306_SPI || type == SSD1306_SPI64) {
174-
for (i=0; i<5; i++) if (!pinManager.allocatePin(ioPin[i])) { allocated=true; break; }
175-
if (i<5 && allocated) { for (byte i=0; i<5; i++) pinManager.deallocatePin(ioPin[i]); type=NONE; return; }
173+
PinManagerPinType pins[5] = { { ioPin[0], true }, { ioPin[1], true}, { ioPin[2], true }, { ioPin[3], true}, { ioPin[4], true }};
174+
if (!pinManager.allocateMultiplePins(pins, 5, PinOwner::UM_FourLineDisplay)) { type=NONE; return; }
176175
} else {
177-
for (i=0; i<2; i++) if (!pinManager.allocatePin(ioPin[i])) { allocated=true; break; }
178-
if (i<2 && allocated) { for (byte i=0; i<5; i++) pinManager.deallocatePin(ioPin[i]); type=NONE; return; }
176+
PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true} };
177+
if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::UM_FourLineDisplay)) { type=NONE; return; }
179178
}
180179
DEBUG_PRINTLN(F("Allocating display."));
181180
switch (type) {
@@ -240,18 +239,20 @@ class FourLineDisplayUsermod : public Usermod {
240239
break;
241240
default:
242241
u8x8 = nullptr;
242+
}
243+
if (nullptr == u8x8) {
244+
DEBUG_PRINTLN(F("Display init failed."));
245+
pinManager.deallocatePin(sclPin, PinOwner::UM_FourLineDisplay);
246+
pinManager.deallocatePin(sdaPin, PinOwner::UM_FourLineDisplay);
247+
sclPin = -1;
248+
sdaPin = -1;
243249
type = NONE;
244250
return;
245251
}
252+
246253
initDone = true;
247-
if (u8x8 != nullptr) {
248-
DEBUG_PRINTLN(F("Starting display."));
249-
(static_cast<U8X8*>(u8x8))->begin();
250-
} else {
251-
DEBUG_PRINTLN(F("Display init failed."));
252-
type = NONE;
253-
return;
254-
}
254+
DEBUG_PRINTLN(F("Starting display."));
255+
(static_cast<U8X8*>(u8x8))->begin(); // why a static cast here? variable is of this type...
255256
setFlipMode(flip);
256257
setContrast(contrast); //Contrast setup will help to preserve OLED lifetime. In case OLED need to be brighter increase number up to 255
257258
setPowerSave(0);
@@ -727,15 +728,17 @@ class FourLineDisplayUsermod : public Usermod {
727728
if (pinsChanged || type!=newType) {
728729
if (type != NONE) delete (static_cast<U8X8*>(u8x8));
729730
for (byte i=0; i<5; i++) {
730-
if (ioPin[i]>=0) pinManager.deallocatePin(ioPin[i]);
731+
if (ioPin[i]>=0) pinManager.deallocatePin(ioPin[i], PinOwner::UM_FourLineDisplay);
731732
ioPin[i] = newPin[i];
732733
}
733734
if (ioPin[0]<0 || ioPin[1]<0) { // data & clock must be > -1
734735
type = NONE;
735736
return true;
736737
} else type = newType;
737738
setup();
738-
needsRedraw |= true;
739+
if (sclPin >= 0 && sdaPin >= 0) {
740+
needsRedraw |= true;
741+
}
739742
}
740743
setContrast(contrast);
741744
setFlipMode(flip);

usermods/usermod_v2_rotary_encoder_ui/usermod_v2_rotary_encoder_ui.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class RotaryEncoderUIUsermod : public Usermod {
7676
unsigned char Enc_B;
7777
unsigned char Enc_A_prev = 0;
7878

79-
bool currentEffectAndPaleeteInitialized = false;
79+
bool currentEffectAndPaletteInitialized = false;
8080
uint8_t effectCurrentIndex = 0;
8181
uint8_t effectPaletteIndex = 0;
8282

@@ -97,9 +97,17 @@ class RotaryEncoderUIUsermod : public Usermod {
9797
*/
9898
void setup()
9999
{
100-
if (!pinManager.allocatePin(pinA)) { enabled = false; return;}
101-
if (!pinManager.allocatePin(pinB)) { pinManager.deallocatePin(pinA); enabled = false; return; }
102-
if (!pinManager.allocatePin(pinC)) { pinManager.deallocatePin(pinA); pinManager.deallocatePin(pinB); enabled = false; return; }
100+
PinManagerPinType pins[3] = { { pinA, false }, { pinB, false }, { pinC, false } };
101+
if (!pinManager.allocateMultiplePins(pins, 3, PinOwner::UM_RotaryEncoderUI)) {
102+
// BUG: configuring this usermod with conflicting pins
103+
// will cause it to de-allocate pins it does not own
104+
// (at second config)
105+
// This is the exact type of bug solved by pinManager
106+
// tracking the owner tags....
107+
pinA = pinB = pinC = -1;
108+
enabled = false;
109+
return;
110+
}
103111

104112
pinMode(pinA, INPUT_PULLUP);
105113
pinMode(pinB, INPUT_PULLUP);
@@ -152,7 +160,7 @@ class RotaryEncoderUIUsermod : public Usermod {
152160
// Initialize effectCurrentIndex and effectPaletteIndex to
153161
// current state. We do it here as (at least) effectCurrent
154162
// is not yet initialized when setup is called.
155-
if (!currentEffectAndPaleeteInitialized) {
163+
if (!currentEffectAndPaletteInitialized) {
156164
findCurrentEffectAndPalette();
157165
}
158166

@@ -248,7 +256,7 @@ class RotaryEncoderUIUsermod : public Usermod {
248256
}
249257

250258
void findCurrentEffectAndPalette() {
251-
currentEffectAndPaleeteInitialized = true;
259+
currentEffectAndPaletteInitialized = true;
252260
for (uint8_t i = 0; i < strip.getModeCount(); i++) {
253261
//byte value = modes_alpha_indexes[i];
254262
if (modes_alpha_indexes[i] == effectCurrent) {
@@ -455,9 +463,9 @@ class RotaryEncoderUIUsermod : public Usermod {
455463
DEBUG_PRINTLN(F(" config (re)loaded."));
456464
// changing parameters from settings page
457465
if (pinA!=newDTpin || pinB!=newCLKpin || pinC!=newSWpin) {
458-
pinManager.deallocatePin(pinA);
459-
pinManager.deallocatePin(pinB);
460-
pinManager.deallocatePin(pinC);
466+
pinManager.deallocatePin(pinA, PinOwner::UM_RotaryEncoderUI);
467+
pinManager.deallocatePin(pinB, PinOwner::UM_RotaryEncoderUI);
468+
pinManager.deallocatePin(pinC, PinOwner::UM_RotaryEncoderUI);
461469
pinA = newDTpin;
462470
pinB = newCLKpin;
463471
pinC = newSWpin;

0 commit comments

Comments
 (0)