Refactor state handling

In order to improve the handling of states the `state` enum is also
used for saving the `home_state`, which was previously called
`current_home_state`. All dependent functions were adapted to the
change.

The `volatile` keyword was added to some variables.
This commit is contained in:
finga 2021-09-09 20:40:50 +02:00
parent 6746d34b2f
commit 9c496e765d

View file

@ -60,10 +60,10 @@ static const struct symbol sym_setup = { 19, { 0xF8, 0x98, 0xB8, 0x00, 0xF8, 0x9
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;
static volatile enum state {home, ch1, ch2, ch3, setup} current_state = home;
static volatile enum state home_state = ch1;
static uint8_t enc = 0;
static volatile uint8_t enc = 0;
void spi_init(void) {
SPI_DDR |= (1 << SPI_SCK) | (1 << SPI_MOSI) | (1 << SPI_SS);
@ -169,7 +169,7 @@ static void lcd_home(void) {
bool ch3_selected = false;
bool setup_selected = false;
switch(current_home_state) {
switch(home_state) {
case ch1:
ch1_selected = true;
break;
@ -239,21 +239,39 @@ static void lcd_home(void) {
}
}
static void change_state(enum state new_state) {
switch(new_state) {
case home:
lcd_home();
current_state = home;
break;
case ch1:
break;
case ch2:
break;
case ch3:
break;
case setup:
break;
}
}
static void update_home(enum input event) {
switch (event) {
case cw:
current_home_state++;
if (current_home_state > setup)
current_home_state = ch1;
home_state++;
if (home_state > setup)
home_state = ch1;
lcd_home();
break;
case ccw:
current_home_state--;
if (current_home_state > setup)
current_home_state = setup;
home_state--;
if (home_state > setup)
home_state = setup;
lcd_home();
break;
case click:
change_state(home_state);
break;
case hold:
break;
@ -265,14 +283,13 @@ static void update_state(enum input event) {
case home:
update_home(event);
break;
}
}
static void change_state(enum state new_state) {
switch(new_state) {
case home:
lcd_home();
current_state = home;
case ch1:
break;
case ch2:
break;
case ch3:
break;
case setup:
break;
}
}