Skip to content

Commit 67fd84f

Browse files
committed
Change encoder code.
1 parent 9c6824f commit 67fd84f

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

firmware/front-display/avr/encoder_hd.cpp

+8-18
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,14 @@ void octoglow::front_display::encoder::pool() {
4444
}
4545
}
4646

47-
/*
48-
* Code borrowed from https://www.circuitsathome.com/mcu/rotary-encoder-interrupt-service-routine-for-avr-micros/
49-
*/
5047
ISR(PCINT2_vect) {
51-
static uint8_t old_AB = 3;
52-
static int8_t encval = 0;
53-
static const int8_t enc_states[] PROGMEM = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
54-
/**/
55-
old_AB <<= 2; //remember previous state
56-
old_AB |= (PIN(ENC_PORT) & (_BV(ENC_A_PIN) | _BV(ENC_B_PIN)))
57-
>> 1; // >> 1 shift is necessary because in original code the encoder was connected to 0 and 1 pins
58-
encval += pgm_read_byte(&(enc_states[(old_AB & 0x0f)]));
59-
/* post "Navigation forward/reverse" event */
60-
if (encval > 3) { //four steps forward
61-
_currentEncoderSteps++;
62-
encval = 0;
63-
} else if (encval < -3) { //four steps backwards
64-
_currentEncoderSteps--;
65-
encval = 0;
48+
static uint8_t old = 0;
49+
uint8_t current = 0b11 & ((PIN(ENC_PORT) & (_BV(ENC_A_PIN) | _BV(ENC_B_PIN))) >> 1);
50+
51+
if (old == 0b00) {
52+
if (current == 0b10) { _currentEncoderSteps++; }
53+
if (current == 0b01) { _currentEncoderSteps--; }
6654
}
55+
56+
old = current;
6757
}

0 commit comments

Comments
 (0)