Skip to content

Conversation

@Kflin01
Copy link

@Kflin01 Kflin01 commented Jun 26, 2025

to turn it on/off. Have a mixture of Quad and Half step encoders and wanted an easy way to use either without recompiling.

Summary by CodeRabbit

  • New Features

    • Added support for a "half step" mode for rotary encoder controls, allowing for more precise input handling.
    • The selected mode (half step or full step) is now saved and restored automatically.
  • Bug Fixes

    • Improved detection of rotary encoder direction for both half step and full step modes.

to turn it on/off.  Have a mixture of Quad and Half step encoders and
wanted an easy way to use either without recompiling.
@coderabbitai
Copy link

coderabbitai bot commented Jun 26, 2025

Walkthrough

This change adds a "half step" mode to the rotary encoder UI usermod. It introduces a new configuration flag, updates encoder reading logic to support both half-step and full-step decoding, and ensures the mode is saved and loaded from persistent configuration. No other features or behaviors are modified.

Changes

File(s) Change Summary
usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h Added HalfStep and Enc_B_prev members, _HalfStep config key, and updated logic for half-step; modified config serialization/deserialization methods to support the new flag.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant RotaryEncoder
    participant RotaryEncoderUIUsermod
    participant ConfigStorage

    User ->> RotaryEncoder: Rotate knob
    RotaryEncoder ->> RotaryEncoderUIUsermod: Signal pin A/B changes
    RotaryEncoderUIUsermod ->> RotaryEncoderUIUsermod: Check HalfStep flag
    alt HalfStep mode
        RotaryEncoderUIUsermod ->> RotaryEncoderUIUsermod: Detect edge on pin A or B
        RotaryEncoderUIUsermod ->> RotaryEncoderUIUsermod: Determine direction (CW/CCW)
    else FullStep mode
        RotaryEncoderUIUsermod ->> RotaryEncoderUIUsermod: Detect falling edge on pin A
        RotaryEncoderUIUsermod ->> RotaryEncoderUIUsermod: Check pin B for direction
    end
    RotaryEncoderUIUsermod ->> ConfigStorage: Save/Load HalfStep flag
Loading

Poem

In circuits where the encoders spin,
A rabbit grins at change within—
Half steps now, or full, you choose,
Turning knobs for any use.
Saved in memory, swift and neat,
This hopping code is quite a feat!
🐇✨


📜 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 11945e1 and 61e75af.

📒 Files selected for processing (1)
  • usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h (9 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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: 2

🧹 Nitpick comments (1)
usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h (1)

640-640: Remove misleading comment.

The comment "kkf check for half steps" is confusing as it appears in the full-step code branch.

-// kkf check for half steps
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4df16b4 and 11945e1.

📒 Files selected for processing (1)
  • usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h (9 hunks)
🔇 Additional comments (4)
usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h (4)

152-152: LGTM! Member variables properly added for half-step support.

The additions of Enc_B_prev, HalfStep, and _HalfStep are correct and necessary for implementing the half-step encoder functionality.

Also applies to: 166-166, 180-180


336-336: String constant properly defined.

The PROGMEM string definition follows the established pattern for configuration keys.


469-469: Encoder pin B state properly initialized.

Initializing Enc_B_prev along with Enc_A_prev ensures proper edge detection from the start.


1141-1141: Configuration reading properly implemented.

The HalfStep configuration value is correctly read using the established pattern.

Comment on lines +593 to +636
if (HalfStep) { // WLEDMM: use half step for encoder
if (Enc_A != Enc_A_prev) { // WLEDMM: use rising edge detection
if ((Enc_B == HIGH && Enc_A == LOW) || (Enc_B == LOW && Enc_A == HIGH)) {
// Serial.println("A changed +");
// Clockwise
switch(select_state) {
case 0: changeBrightness(true); break;
case 1: changeEffectSpeed(true); break;
case 2: changeEffectIntensity(true); break;
case 3: changePalette(true); break;
case 4: changeEffect(true); break;
case 5: changeHue(true); break;
case 6: changeSat(true); break;
case 7: changeCCT(true); break;
case 8: changePreset(true); break;
case 9: changeCustom(1,true); break;
case 10: changeCustom(2,true); break;
case 11: changeCustom(3,true); break;
}
}
}
else if (Enc_B == HIGH)
{ // B is low so counter-clockwise
switch(select_state) {
case 0: changeBrightness(false); break;
case 1: changeEffectSpeed(false); break;
case 2: changeEffectIntensity(false); break;
case 3: changePalette(false); break;
case 4: changeEffect(false); break;
case 5: changeHue(false); break;
case 6: changeSat(false); break;
case 7: changeCCT(false); break;
case 8: changePreset(false); break;
case 9: changeCustom(1,false); break;
case 10: changeCustom(2,false); break;
case 11: changeCustom(3,false); break;
else {
if (Enc_B != Enc_B_prev) {
if ((Enc_A == HIGH && Enc_B == LOW) || (Enc_A == LOW && Enc_B == HIGH)) {
// Counter-clockwise
// Serial.println("B changed -");
switch(select_state) {
case 0: changeBrightness(false); break;
case 1: changeEffectSpeed(false); break;
case 2: changeEffectIntensity(false); break;
case 3: changePalette(false); break;
case 4: changeEffect(false); break;
case 5: changeHue(false); break;
case 6: changeSat(false); break;
case 7: changeCCT(false); break;
case 8: changePreset(false); break;
case 9: changeCustom(1,false); break;
case 10: changeCustom(2,false); break;
case 11: changeCustom(3,false); break;
}
}
}
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Incorrect half-step encoder logic implementation.

The half-step implementation has a fundamental logic error. Both the A-change and B-change branches use equivalent conditions, which would cause the same encoder state to trigger both clockwise and counter-clockwise actions.

Current logic:

  • A change: (Enc_B == HIGH && Enc_A == LOW) || (Enc_B == LOW && Enc_A == HIGH)
  • B change: (Enc_A == HIGH && Enc_B == LOW) || (Enc_A == LOW && Enc_B == HIGH)

These conditions are logically equivalent (just with A and B swapped), so they would both evaluate to true for the same encoder state.

The correct implementation should detect edge transitions and use the state of the other pin to determine direction:

    if (HalfStep) { // WLEDMM: use half step for encoder
-      if (Enc_A != Enc_A_prev) { // WLEDMM: use rising edge detection
-        if ((Enc_B == HIGH && Enc_A == LOW) || (Enc_B == LOW && Enc_A == HIGH)) {
+      if (Enc_A != Enc_A_prev) { // A pin changed
+        if ((Enc_A == HIGH && Enc_B == LOW) || (Enc_A == LOW && Enc_B == HIGH)) {
 //          Serial.println("A changed +");
           // Clockwise
           switch(select_state) {
             case  0: changeBrightness(true);      break;
             case  1: changeEffectSpeed(true);     break;
             case  2: changeEffectIntensity(true); break;
             case  3: changePalette(true);         break;
             case  4: changeEffect(true);          break;
             case  5: changeHue(true);             break;
             case  6: changeSat(true);             break;
             case  7: changeCCT(true);             break;
             case  8: changePreset(true);          break;
             case  9: changeCustom(1,true);        break;
             case 10: changeCustom(2,true);        break;
             case 11: changeCustom(3,true);        break;
           }
+        } else {
+          // Counter-clockwise
+          switch(select_state) {
+            case  0: changeBrightness(false);      break;
+            case  1: changeEffectSpeed(false);     break;
+            case  2: changeEffectIntensity(false); break;
+            case  3: changePalette(false);         break;
+            case  4: changeEffect(false);          break;
+            case  5: changeHue(false);             break;
+            case  6: changeSat(false);             break;
+            case  7: changeCCT(false);             break;
+            case  8: changePreset(false);          break;
+            case  9: changeCustom(1,false);        break;
+            case 10: changeCustom(2,false);        break;
+            case 11: changeCustom(3,false);        break;
+          }
         }
       }
-      else {
-        if (Enc_B != Enc_B_prev) {
-          if ((Enc_A == HIGH && Enc_B == LOW) || (Enc_A == LOW && Enc_B == HIGH)) {
+      if (Enc_B != Enc_B_prev) { // B pin changed
+        if ((Enc_B == HIGH && Enc_A == HIGH) || (Enc_B == LOW && Enc_A == LOW)) {
           // Counter-clockwise
 //            Serial.println("B changed -");
             switch(select_state) {
               case  0: changeBrightness(false);      break;
               case  1: changeEffectSpeed(false);     break;
               case  2: changeEffectIntensity(false); break;
               case  3: changePalette(false);         break;
               case  4: changeEffect(false);          break;
               case  5: changeHue(false);             break;
               case  6: changeSat(false);             break;
               case  7: changeCCT(false);             break;
               case  8: changePreset(false);          break;
               case  9: changeCustom(1,false);        break;
               case 10: changeCustom(2,false);        break;
               case 11: changeCustom(3,false);        break;
             }
+          } else {
+            // Clockwise
+            switch(select_state) {
+              case  0: changeBrightness(true);      break;
+              case  1: changeEffectSpeed(true);     break;
+              case  2: changeEffectIntensity(true); break;
+              case  3: changePalette(true);         break;
+              case  4: changeEffect(true);          break;
+              case  5: changeHue(true);             break;
+              case  6: changeSat(true);             break;
+              case  7: changeCCT(true);             break;
+              case  8: changePreset(true);          break;
+              case  9: changeCustom(1,true);        break;
+              case 10: changeCustom(2,true);        break;
+              case 11: changeCustom(3,true);        break;
+            }
           }
-        }
       }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h
between lines 593 and 636, the half-step encoder logic incorrectly uses
equivalent conditions for both A and B pin changes, causing both clockwise and
counter-clockwise actions to trigger simultaneously. To fix this, revise the
conditions so that on an A pin change, the direction is determined by the state
of the B pin, and on a B pin change, the direction is determined by the state of
the A pin, ensuring distinct and correct detection of rotation direction based
on edge transitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant