Skip to content

Conversation

@rommo911
Copy link

@rommo911 rommo911 commented Jul 28, 2025

after a while searching, i found out that USB Serial CDC is not working for ADA light.
it seems there are incompatibility in isPinOK when checking for pins are used in case of pins of native USB.
this fixes the issue and now the ada light is working fine.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Enhanced USB CDC serial communication handling to properly detect and enable serial capabilities when using USB CDC boot mode, improving serial communication reliability across different hardware configurations.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 28, 2025

Walkthrough

Added conditional logic in WLED::setup to set serial capability flags (serialCanRX and serialCanTX) based on ARDUINO_USB_CDC_ON_BOOT definition. When USB CDC boot mode is active, both flags are forced true; otherwise, existing pin-allocation logic is preserved.

Changes

Cohort / File(s) Summary
Serial capability flag initialization
wled00/wled.cpp
Added conditional block to handle serialCanRX and serialCanTX assignment based on USB CDC boot mode configuration.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify the conditional logic correctly distinguishes between USB CDC boot and standard pin allocation scenarios
  • Confirm that forcing both serial flags to true under USB CDC boot mode is the intended behavior
  • Check that the fallback logic for non-USB CDC boards preserves existing RX/TX pin allocation semantics

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and specifically describes the main change: fixing serial conditions for USB CDC on certain ESP32 variants (C3/S2/S3).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8a26210 and 8a09156.

📒 Files selected for processing (1)
  • wled00/wled.cpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • wled00/wled.cpp

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
wled00/wled_serial.cpp (1)

72-74: LGTM! Logic simplification aligns with USB CDC fix.

The removal of the runtime Serial check is correct since serialCanRX is now properly set during setup based on board type. This fixes the USB CDC issue for ESP32 C3/S2/S3.

Minor style nitpick: remove the semicolon after the closing brace.

-  if (serialCanRX == false) {
-    return;
-  }; // arduino docs: `if (Serial)` indicates whether or not the USB CDC serial connection is open. For all non-USB CDC ports, this will always return true
+  if (serialCanRX == false) {
+    return;
+  } // arduino docs: `if (Serial)` indicates whether or not the USB CDC serial connection is open. For all non-USB CDC ports, this will always return true
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 591dbe3 and 82d099f.

📒 Files selected for processing (2)
  • wled00/wled.cpp (1 hunks)
  • wled00/wled_serial.cpp (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: blazoncek
PR: wled/WLED#4482
File: wled00/udp.cpp:147-149
Timestamp: 2025-02-19T12:43:34.199Z
Learning: ESP8266 and ESP32 platforms have different maximum segment name lengths in WLED, which can cause truncation when syncing segment names between devices. This platform difference affects the user experience when using the segment name sync feature.
wled00/wled_serial.cpp (1)

Learnt from: blazoncek
PR: #4667
File: usermods/user_fx/user_fx.cpp:27-30
Timestamp: 2025-04-30T05:41:03.633Z
Learning: In WLED, the Segment::allocateData() method already includes optimization to check if data is allocated and sufficiently sized, handling buffer reuse to reduce memory fragmentation. Adding an external check like if (SEGENV.data == nullptr && !SEGENV.allocateData(dataSize)) is unnecessary and could be problematic, as it bypasses proper size verification.

wled00/wled.cpp (4)

Learnt from: blazoncek
PR: #4667
File: usermods/user_fx/user_fx.cpp:27-30
Timestamp: 2025-04-30T05:41:03.633Z
Learning: In WLED, the Segment::allocateData() method already includes optimization to check if data is allocated and sufficiently sized, handling buffer reuse to reduce memory fragmentation. Adding an external check like if (SEGENV.data == nullptr && !SEGENV.allocateData(dataSize)) is unnecessary and could be problematic, as it bypasses proper size verification.

Learnt from: blazoncek
PR: #4700
File: wled00/wled_server.cpp:409-414
Timestamp: 2025-05-26T16:09:34.325Z
Learning: In wled00/wled_server.cpp, the OTA update access control logic intentionally allows OTA updates from different subnets when otaSameSubnet is true AND a PIN is set. This was a conscious design decision by blazoncek to provide flexibility for remote OTA updates with PIN protection, though they acknowledged it may not be the optimal security model.

Learnt from: KrX3D
PR: #4664
File: wled00/syslog.cpp:50-51
Timestamp: 2025-04-26T12:51:04.993Z
Learning: In the WLED codebase, syslog configuration variables (syslogHost, syslogPort, syslogProtocol, syslogFacility, syslogSeverity) are defined as global variables in wled.h and are accessed directly in the SyslogPrinter implementation, rather than being class members.

Learnt from: blazoncek
PR: #4658
File: wled00/const.h:140-141
Timestamp: 2025-04-26T19:19:07.600Z
Learning: In WLED, the WLED_MAX_PANELS macro is intentionally defined as a fixed constant value (18) with no redefinition mechanism, making it "unoverridable" - there's no need for a static assertion to check its maximum value.

🔇 Additional comments (1)
wled00/wled.cpp (1)

456-464: Excellent fix for USB CDC boards!

This change correctly addresses the root cause by bypassing pin allocation checks for USB CDC boards. The logic is sound:

  • USB CDC boards use D+/D- pins for serial, not GPIO 1/3
  • Pin allocator incorrectly reports these as unavailable
  • Forcing serialCanRX and serialCanTX to true bypasses this issue
  • Maintains backward compatibility for non-USB CDC boards

The clear comment explains the reasoning well.

@softhack007 softhack007 requested a review from blazoncek November 7, 2025 20:43
void handleSerial()
{
if (!(serialCanRX && Serial)) return; // arduino docs: `if (Serial)` indicates whether or not the USB CDC serial connection is open. For all non-USB CDC ports, this will always return true
if (serialCanRX == false && Serial) {
Copy link
Member

@softhack007 softhack007 Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rommo911, could it be that your proposed change does not lead to the same behaviour when not having USB-CDC ?

old condition truth table:

canRX Serial result
false - true = return
true false true = return
true true false = continue

new condition truth table:

canRX Serial result
false true true = return
false false false = continue
true - false = continue

As the new condition you propose seems to be broken - how about just removing the "early exit" for the case of USB_CDC ?

#if ARDUINO_USB_CDC_ON_BOOT
   if (!Serial) return;
#else
   if (!(serialCanRX && Serial)) return;
#endif

@blazoncek not sure if serialCanRX is used in other places in the code, so we still need the change in wled.cpp?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serialCanRX is only used to determine if ADA/Improv can be used. Nothing else AFAIK.

Copy link
Member

@softhack007 softhack007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new condition in wled_serial.cpp does not preserve the same behaviour when not using USB-CDC.
Please correct.

@softhack007 softhack007 changed the title fixed ADA light Serial conditions for USB CDC (aka ESP32 C3 / S2 / S3 ) fixed ADA light Serial conditions for USB CDC (some ESP32 C3 / S2 / S3 ) Nov 7, 2025
@softhack007
Copy link
Member

softhack007 commented Nov 7, 2025

Additionally, the logic for handling serial input is simplified by modifying the early return condition in the serial handler to focus on RX pin availability when Serial is active.

@coderabbitai it seems that rabbits are not good at logic ;-)
You missed that the new "simplified" condition in wled_serial.cpp does not preserve previous behaviour in case that ARDUINO_USB_CDC_ON_BOOT is not defined. So this PR has unwanted side-effects for other configurations.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 7, 2025

Tip

For best results, initiate chat on the files or code changes.

@softhack007 You're absolutely right, and I apologize for missing this logic error! 🙈

Let me break down why the new condition does NOT preserve the previous behavior:

Previous condition:

if (!(serialCanRX && Serial)) return;

Returns early if: serialCanRX is false OR Serial is false

New condition:

if (serialCanRX == false && Serial) { return; }

Returns early if: serialCanRX is false AND Serial is true

Truth table showing the difference:

serialCanRX Serial Old: !(serialCanRX && Serial) New: serialCanRX == false && Serial Match?
false false ✅ Returns ❌ Does NOT return ❌ BROKEN
false true ✅ Returns ✅ Returns
true false ✅ Returns ❌ Does NOT return ❌ BROKEN
true true Continues Continues

The issue: When Serial is false (which happens on boards without USB CDC when the serial port isn't connected), the new condition will NOT return early and will proceed to handle serial operations, which is incorrect behavior.

Correct condition to preserve original behavior:

if (!serialCanRX || !Serial) return;

or keep the original:

if (!(serialCanRX && Serial)) return;

This is a critical bug that affects all non-USB-CDC configurations.

 /\_/\
( o.o )  *oops*
 > ^ <
/|   |\

(| |)


🧠 Learnings used
Learnt from: willmmiles
Repo: wled/WLED PR: 4939
File: wled00/FX_fcn.cpp:1183-1194
Timestamp: 2025-09-18T03:17:30.107Z
Learning: In WLED bus management code, dead code branches in error handling (like unreachable else breaks) are intentionally preserved to accommodate potential future API changes, even when currently unreachable.

Learnt from: DedeHai
Repo: wled/WLED PR: 4939
File: wled00/FX_fcn.cpp:1176-1187
Timestamp: 2025-09-16T18:08:42.848Z
Learning: In WLED finalizeInit() bus creation (wled00/FX_fcn.cpp), intentionally allowing memory overruns when bus configurations exceed MAX_LED_MEMORY is a deliberate design choice. The trade-off prioritizes creating buses with reduced LED counts over completely failing to create buses, which would cause no LED output and UI failures. This approach forces users to update configurations after migrating to version 0.16 while maintaining basic functionality.

Learnt from: DedeHai
Repo: wled/WLED PR: 4798
File: wled00/FX.cpp:7531-7533
Timestamp: 2025-08-26T11:51:21.817Z
Learning: In WLED PR #4798, DedeHai confirmed that certain gamma-related calls in FX.cpp/FX_fcn.cpp/particle systems are intentional for effect-level shaping (e.g., brightness curves, TV sim, Pride 2015 pre-mix), distinct from final output gamma. Do not flag or remove these in future reviews; add comments when feasible to clarify intent.

@wled wled deleted a comment from coderabbitai bot Nov 7, 2025
@blazoncek
Copy link
Collaborator

I do not know if forcing serialCanRX or serialCanTX to true is ok. It does not seem so to me.

@netmindz netmindz added bug connectivity Issue regarding protocols, WiFi connection or availability of interfaces labels Nov 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug connectivity Issue regarding protocols, WiFi connection or availability of interfaces

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants