-
-
Notifications
You must be signed in to change notification settings - Fork 111
Added code to handle Half-step encoders along with a config option #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: mdev
Are you sure you want to change the base?
Conversation
to turn it on/off. Have a mixture of Quad and Half step encoders and wanted an easy way to use either without recompiling.
WalkthroughThis 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
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
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
📒 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_HalfStepare 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_prevalong withEnc_A_prevensures proper edge detection from the start.
1141-1141: Configuration reading properly implemented.The
HalfStepconfiguration value is correctly read using the established pattern.
usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h
Outdated
Show resolved
Hide resolved
| 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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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
Bug Fixes