Handle switch input

The `input` enum is extended with `click` and `hold` values and all
dependencies are adapted to that. In order to handle the `click` input
an interrupt is used. Further, for handling held button input with the
switch the Timer/Counter1 compare match interrupt with OCR1A is used.
This commit is contained in:
finga 2021-09-09 20:19:40 +02:00
parent e099b0f94a
commit 6746d34b2f

View file

@ -58,7 +58,7 @@ static const struct symbol sym_9 = { 5, { 0xF8, 0xFC, 0x8C, 0xFC, 0xF8,
static const struct symbol sym_setup = { 19, { 0xF8, 0x98, 0xB8, 0x00, 0xF8, 0x98, 0x18, 0x00, 0x18, 0xF8, 0x18, 0x00, 0xF8, 0x00, 0xF8, 0x00, 0xF8, 0x98, 0xF8,
0x1D, 0x19, 0x1F, 0x00, 0x1F, 0x19, 0x18, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x18, 0x1F, 0x00, 0x1F, 0x01, 0x01 } };
enum input {cw, ccw};
enum input {cw, ccw, click, hold};
static enum state {home} current_state = home;
static enum home_state {ch1, ch2, ch3, setup} current_home_state = ch1;
@ -253,6 +253,10 @@ static void update_home(enum input event) {
current_home_state = setup;
lcd_home();
break;
case click:
break;
case hold:
break;
}
}
@ -310,12 +314,45 @@ ISR(PCINT0_vect) {
break;
}
// TODO: proper debounce and dechattering
// TODO: proper dechattering
_delay_us(100);
sei();
}
// encoder button interrupt
ISR(PCINT1_vect) {
cli();
if (PINC & (1 << PC0)) { // release
TCCR1B &= (0 << CS11) & (0 << CS10); // Disable Timer/Counter1
if (TCNT1 != 0) {
update_state(click); // Switch to selected state
TCNT1 = 0;
}
} else { // press
if (!(TCCR1B & (1 << CS10))) {
TCCR1B |= (1 << CS11) | (1 << CS10); // Enable Timer/Counter1
}
}
// TODO: proper debouncing
_delay_us(70);
sei();
}
// Timer/Counter1 compare match A
ISR(TIMER1_COMPA_vect) {
cli();
TCCR1B &= (0 << CS11) & (0 << CS10); // Disable Timer/Counter1
TCNT1 = 0;
update_state(hold);
sei();
}
int main(void) {
// FastPWM: 1.25kHz
// TODO: Try to get the backlit even more dim
@ -334,6 +371,15 @@ int main(void) {
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT6) | (1 << PCINT7);
// Encoder switch setup
PORTC |= (1 << PC0);
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT8);
// Timer1 setup to recognize held button
OCR1A = 8192;
TIMSK1 |= (1 << OCIE1A); // Enable match compare A
// Show splash screen and load the menu
lcd_splash();
_delay_ms(250);