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