Schematic (using C6 and C7):
All modifications are made to matrix.c.
Code to add near top of file:
uint8_t state;
int32_t position;
static void init_encoder(void)
{
DDRC &= ~(1<<6 | 1<<7); // C6 and C7 are the two ports being used
PORTC |= (1<<6 | 1<<7); // This sets the ports up for input
uint8_t s = 0;
_delay_ms(1);
if (PINC&(1<<6)) s |= 1; // PINC&(1<<6) reads the value of the C6 port (0 or 1)
if (PINC&(1<<7)) s |= 2;
state = s;
position = 0;
}
void read_encoder(void)
{
uint8_t s = state & 3;
if (PINC&(1<<6)) s |= 4;
if (PINC&(1<<7)) s |= 8;
state = (s >> 2);
switch (s) {
case 1: case 7: case 8: case 14:
position++;
break;
case 2: case 4: case 11: case 13:
position--;
break;
case 3: case 12:
position += 2;
break;
case 6: case 9:
position -= 2;
break;
}
}
Add init_encoder();
to your matrix_init(void)
method.
Code to add to your matrix_scan method:
read_encoder();
if (position >= 2) { // 2 may need to be changed depending on your encoder
register_code(KC_AUDIO_VOL_UP); // Any keycode should work here
unregister_code(KC_AUDIO_VOL_UP);
position = 0;
} else if (position <= -2) { // Change this value as well
register_code(KC_AUDIO_VOL_DOWN);
unregister_code(KC_AUDIO_VOL_DOWN);
position = 0;
}