Revert "Additional debounce control parameters for rotary encoder settings (f…" (#2846)

This reverts commit bf18851b6b.
This commit is contained in:
gullradriel
2025-10-28 10:34:14 +01:00
committed by GitHub
parent 3b57672dda
commit 0f9d9e589d
8 changed files with 64 additions and 144 deletions
+6 -19
View File
@@ -163,28 +163,15 @@ uint8_t EncoderDebounce::rotation_rate() {
// Returns TRUE if encoder position phase bits changed (after debouncing)
bool EncoderDebounce::feed(const uint8_t phase_bits) {
// Shift in new 2-bit sample into 32-bit history (16 samples total)
history_ = (history_ << 2) | phase_bits;
// For very noisy encoders: require BOTH bits stable for N consecutive ticks
// Get configurable debounce window (4-32ms)
uint8_t debounce_samples = portapack::persistent_memory::encoder_debounce_ms();
// If both inputs have been stable for the last 4 ticks, history_ should equal 0x00, 0x55, 0xAA, or 0xFF.
uint8_t expected_stable_history = phase_bits * 0b01010101;
// Build expected pattern: phase_bits repeated debounce_samples times
// For phase_bits=0b00: 0x00000000, 0b01: 0x55555555, 0b10: 0xAAAAAAAA, 0b11: 0xFFFFFFFF
uint32_t expected_stable_history = 0;
for (uint8_t i = 0; i < debounce_samples; i++) {
expected_stable_history = (expected_stable_history << 2) | phase_bits;
}
// Create mask for the number of samples we're checking
uint32_t mask = 0;
for (uint8_t i = 0; i < debounce_samples; i++) {
mask = (mask << 2) | 0x3;
}
// Require exact match - both bits must be stable for configured ms
if ((history_ & mask) == expected_stable_history) {
// But, checking for equal seems to cause issues with at least 1 user's encoder, so we're treating the input
// as "stable" if at least ONE input bit is consistent for 4 ticks...
uint8_t diff = (history_ ^ expected_stable_history);
if (((diff & 0b01010101) == 0) || ((diff & 0b10101010) == 0)) {
// Has the debounced input value changed?
if (state_ != phase_bits) {
state_ = phase_bits;
+1 -1
View File
@@ -78,7 +78,7 @@ class EncoderDebounce {
uint8_t rotation_rate(); // returns last rotation rate
private:
uint32_t history_{0}; // shift register of previous reads from encoder (16 samples @ 2 bits each)
uint8_t history_{0}; // shift register of previous reads from encoder
uint8_t state_{0}; // actual encoder output state (after debounce logic)
+13 -33
View File
@@ -62,41 +62,21 @@ int_fast8_t Encoder::update(const uint_fast8_t phase_bits) {
int_fast8_t direction = transition_map[state];
// Decrement cooldown timer
if (direction_cooldown > 0) {
direction_cooldown--;
// Require 2 state changes in same direction to register movement -- for additional level of contact switch debouncing
if (direction == prev_direction) {
if ((sensitivity_map[portapack::persistent_memory::encoder_dial_sensitivity()] & (1 << state)) == 0)
return 0;
// false: normal, true: reverse
if (portapack::persistent_memory::encoder_dial_direction())
direction = -direction;
return direction;
}
// Require N consecutive state changes in same direction (configurable for noisy encoders)
if (direction == prev_direction && direction != 0) {
direction_stability_count++;
// Need N consecutive same-direction changes AND cooldown expired
uint8_t required_hits = portapack::persistent_memory::encoder_consecutive_hits();
if (direction_stability_count >= required_hits && direction_cooldown == 0) {
if ((sensitivity_map[portapack::persistent_memory::encoder_dial_sensitivity()] & (1 << state)) == 0)
return 0;
// Successfully registered movement - reset stability and set cooldown
direction_stability_count = 0;
direction_cooldown = portapack::persistent_memory::encoder_cooldown_ms();
// false: normal, true: reverse
if (portapack::persistent_memory::encoder_dial_direction())
direction = -direction;
return direction;
}
} else if (direction != 0 && direction != prev_direction) {
// Direction changed - only accept if cooldown expired (prevents bounce-induced reversals)
if (direction_cooldown == 0) {
prev_direction = direction;
direction_stability_count = 1; // Start counting this new direction
} else {
// During cooldown, completely ignore opposite direction - reset stability count
direction_stability_count = 0;
}
}
// It's normal for transition map to return 0 between every +1/-1, so discarding those
if (direction != 0)
prev_direction = direction;
return 0;
}
-2
View File
@@ -32,8 +32,6 @@ class Encoder {
private:
uint_fast8_t state{0};
int_fast8_t prev_direction{0};
uint8_t direction_stability_count{0}; // count consecutive same-direction changes
uint8_t direction_cooldown{0}; // prevent rapid direction reversals
};
#endif /*__ENCODER_H__*/