Skip to content

Commit 7ac8f6d

Browse files
committed
improvements for new MCU support (-S3/-S2/-C3)
- switch off debug messages to USBCDC, if WLED_DEBUG is not set - ensure that analogread pins are valid - invalid pins cause lots of error messages and finally lead to watchdog reset on some MCUs.
1 parent cafa78c commit 7ac8f6d

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

wled00/cfg.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,28 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
203203
int8_t pin = btn["pin"][0] | -1;
204204
if (pin > -1 && pinManager.allocatePin(pin, false, PinOwner::Button)) {
205205
btnPin[s] = pin;
206-
if (disablePullUp) {
207-
pinMode(btnPin[s], INPUT);
208-
} else {
209-
#ifdef ESP32
210-
pinMode(btnPin[s], buttonType[s]==BTN_TYPE_PUSH_ACT_HIGH ? INPUT_PULLDOWN : INPUT_PULLUP);
211-
#else
212-
pinMode(btnPin[s], INPUT_PULLUP);
213-
#endif
206+
#ifdef ARDUINO_ARCH_ESP32
207+
// ESP32 only: check that analog button pin is a valid ADC gpio
208+
if (((buttonType[s] == BTN_TYPE_ANALOG) || (buttonType[s] == BTN_TYPE_ANALOG_INVERTED)) && (digitalPinToAnalogChannel(btnPin[s]) < 0))
209+
{
210+
// not an ADC analog pin
211+
DEBUG_PRINTF("PIN ALLOC error: GPIO%d for analog button #%d is not an analog pin!\n", btnPin[s], s);
212+
btnPin[s] = -1;
213+
pinManager.deallocatePin(pin,PinOwner::Button);
214+
}
215+
else
216+
#endif
217+
{
218+
if (disablePullUp) {
219+
pinMode(btnPin[s], INPUT);
220+
} else {
221+
#ifdef ESP32
222+
// first check that analog button is valid
223+
pinMode(btnPin[s], buttonType[s]==BTN_TYPE_PUSH_ACT_HIGH ? INPUT_PULLDOWN : INPUT_PULLUP);
224+
#else
225+
pinMode(btnPin[s], INPUT_PULLUP);
226+
#endif
227+
}
214228
}
215229
} else {
216230
btnPin[s] = -1;

wled00/set.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,27 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
169169
if (pinManager.allocatePin(hw_btn_pin,false,PinOwner::Button)) {
170170
btnPin[i] = hw_btn_pin;
171171
buttonType[i] = request->arg(be).toInt();
172-
if (disablePullUp) {
173-
pinMode(btnPin[i], INPUT);
174-
} else {
175-
#ifdef ESP32
176-
pinMode(btnPin[i], buttonType[i]==BTN_TYPE_PUSH_ACT_HIGH ? INPUT_PULLDOWN : INPUT_PULLUP);
177-
#else
178-
pinMode(btnPin[i], INPUT_PULLUP);
179-
#endif
172+
#ifdef ARDUINO_ARCH_ESP32
173+
// ESP32 only: check that analog button pin is a valid ADC gpio
174+
if (((buttonType[i] == BTN_TYPE_ANALOG) || (buttonType[i] == BTN_TYPE_ANALOG_INVERTED)) && (digitalPinToAnalogChannel(btnPin[i]) < 0))
175+
{
176+
// not an ADC analog pin
177+
DEBUG_PRINTF("PIN ALLOC error: GPIO%d for analog button #%d is not an analog pin!\n", btnPin[i], i);
178+
btnPin[i] = -1;
179+
pinManager.deallocatePin(hw_btn_pin,PinOwner::Button);
180+
}
181+
else
182+
#endif
183+
{
184+
if (disablePullUp) {
185+
pinMode(btnPin[i], INPUT);
186+
} else {
187+
#ifdef ESP32
188+
pinMode(btnPin[i], buttonType[i]==BTN_TYPE_PUSH_ACT_HIGH ? INPUT_PULLDOWN : INPUT_PULLUP);
189+
#else
190+
pinMode(btnPin[i], INPUT_PULLUP);
191+
#endif
192+
}
180193
}
181194
} else {
182195
btnPin[i] = -1;

wled00/wled.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ void WLED::setup()
272272
#if defined(WLED_DEBUG) && defined(ARDUINO_ARCH_ESP32) && (defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || ARDUINO_USB_CDC_ON_BOOT)
273273
delay(2500); // allow CDC USB serial to initialise
274274
#endif
275+
#if !defined(WLED_DEBUG) && defined(ARDUINO_ARCH_ESP32) && !defined(WLED_DEBUG_HOST) && ARDUINO_USB_CDC_ON_BOOT
276+
Serial.setDebugOutput(false); // switch off kernel messages when using USBCDC
277+
#endif
275278
DEBUG_PRINTLN();
276279
DEBUG_PRINT(F("---WLED "));
277280
DEBUG_PRINT(versionString);

0 commit comments

Comments
 (0)