Various fixes for the main state machine

This commit is contained in:
Thomas Kolb 2024-05-18 00:16:22 +02:00
parent 0d3ca18885
commit deca973854

View file

@ -63,33 +63,33 @@ static void show_time(uint32_t time_ms)
{ {
bool blinking_dot_on = (time_ms % 1000) < 500; bool blinking_dot_on = (time_ms % 1000) < 500;
if(time_ms >= 100 * ONE_DAY) { // more than 100 days if(time_ms >= 99 * ONE_DAY) { // more than 99 days
// time too large to display // time too large to display
sseg_set_char(0, 'i', false); sseg_set_char(0, 'i', false);
sseg_set_char(1, 'n', false); sseg_set_char(1, 'n', false);
sseg_set_char(2, 'f', false); sseg_set_char(2, 'f', false);
} else if(time_ms >= 10 * ONE_DAY) { // 10 .. 100 days } else if(time_ms >= 99 * ONE_DAY / 10) { // 9.9 .. 99 days
// show days without decimal // show days without decimal
uint32_t days = (time_ms + ONE_DAY/2) / ONE_DAY; uint32_t days = (time_ms + ONE_DAY/2) / ONE_DAY;
sseg_set_char(0, '0' + days / 10, false); sseg_set_char(0, '0' + days / 10, false);
sseg_set_char(1, '0' + days % 10, blinking_dot_on); sseg_set_char(1, '0' + days % 10, blinking_dot_on);
sseg_set_char(2, 'd', false); sseg_set_char(2, 'd', false);
} else if(time_ms >= 10 * ONE_HOUR) { // 10 hours .. 10 days } else if(time_ms >= 99 * ONE_HOUR / 10) { // 9.9 hours .. 9.9 days
// show days with one decimal // show days with one decimal
uint32_t decidays = (time_ms + ONE_DAY/20) * 10 / ONE_DAY; uint32_t decidays = (time_ms + ONE_DAY/20) * 10 / ONE_DAY;
sseg_set_char(0, '0' + decidays / 10, blinking_dot_on); sseg_set_char(0, '0' + decidays / 10, blinking_dot_on);
sseg_set_char(1, '0' + decidays % 10, false); sseg_set_char(1, '0' + decidays % 10, false);
sseg_set_char(2, 'd', false); sseg_set_char(2, 'd', false);
} else if(time_ms >= 1000 * ONE_SECOND) { // 1000 seconds (0.27 hours) .. 10 hours } else if(time_ms >= 1000 * ONE_SECOND) { // 1000 seconds (0.27 hours) .. 9.9 hours
// show hours with one decimal // show hours with one decimal
uint32_t decihours = (time_ms + ONE_HOUR/20) * 10 / ONE_HOUR; uint32_t decihours = (time_ms + ONE_HOUR/20) * 10 / ONE_HOUR;
sseg_set_char(0, '0' + decihours / 10, blinking_dot_on); sseg_set_char(0, '0' + decihours / 10, blinking_dot_on);
sseg_set_char(1, '0' + decihours % 10, false); sseg_set_char(1, '0' + decihours % 10, false);
sseg_set_char(2, 'H', false); sseg_set_char(2, 'H', false);
} else if(time_ms >= 100 * ONE_SECOND) { // 100 .. 1000 seconds } else if(time_ms >= 99 * ONE_SECOND) { // 99 .. 1000 seconds
// show seconds // show seconds
uint32_t seconds = (time_ms + ONE_SECOND/2) / ONE_SECOND; uint32_t seconds = (time_ms + ONE_SECOND/2) / ONE_SECOND;
@ -124,7 +124,8 @@ static void update_countdown_fsm(uint32_t time_ms)
case COUNTDOWN_STOPPED: case COUNTDOWN_STOPPED:
if(state_entered) { if(state_entered) {
// countdown has been stopped -> switch off the load // countdown has been stopped -> switch off the load
gpio_put(OUTPUT_ENABLE_PIN, true); gpio_put(OUTPUT_ENABLE_PIN, false);
buzzer_off();
sseg_set_char(0, 'o', false); sseg_set_char(0, 'o', false);
sseg_set_char(1, 'f', false); sseg_set_char(1, 'f', false);
@ -188,7 +189,7 @@ static void update_countdown_fsm(uint32_t time_ms)
m_countdown_off_time = time_ms + 14 * ONE_DAY; m_countdown_off_time = time_ms + 14 * ONE_DAY;
} else { } else {
// default mode // default mode
m_countdown_off_time = time_ms + 30 * ONE_MINUTE; m_countdown_off_time = time_ms + 10 * ONE_MINUTE;
} }
m_countdown_state = COUNTDOWN_RUNNING; m_countdown_state = COUNTDOWN_RUNNING;
@ -200,6 +201,7 @@ static void update_countdown_fsm(uint32_t time_ms)
if(state_entered) { if(state_entered) {
// countdown has been started -> switch on the load // countdown has been started -> switch on the load
gpio_put(OUTPUT_ENABLE_PIN, true); gpio_put(OUTPUT_ENABLE_PIN, true);
buzzer_off();
} }
countdown_time_remaining = m_countdown_off_time - time_ms; countdown_time_remaining = m_countdown_off_time - time_ms;
@ -207,14 +209,23 @@ static void update_countdown_fsm(uint32_t time_ms)
show_time(countdown_time_remaining); show_time(countdown_time_remaining);
// off button pressed -> go to stop procedure // off button pressed -> go to stop procedure
if(buttons_get_events(BTN_ON) & BTN_EVENT_PRESSED) { if(buttons_get_events(BTN_OFF) & BTN_EVENT_PRESSED) {
m_countdown_state = COUNTDOWN_STOPPING; m_countdown_state = COUNTDOWN_STOPPING;
} }
// extend time when on button is pressed again // extend time when on button is pressed again
if(buttons_get_events(BTN_ON) & BTN_EVENT_PRESSED) { if(buttons_get_events(BTN_ON) & BTN_EVENT_PRESSED
if(countdown_time_remaining < 10 * ONE_HOUR) { || (buttons_is_pressed(BTN_ON)
m_countdown_off_time += 30 * ONE_MINUTE; && ((time_ms - buttons_get_last_change_time(BTN_ON)) > 500)
&& ((time_ms - buttons_get_last_change_time(BTN_ON)) % 500 == 0))) {
if(m_countdown_off_time < 10 * ONE_MINUTE) {
m_countdown_off_time += 10 * ONE_MINUTE;
} else {
m_countdown_off_time *= 2;
}
if(m_countdown_off_time > 10 * ONE_HOUR) {
m_countdown_off_time = 10 * ONE_HOUR;
} }
} }
@ -276,7 +287,7 @@ static void update_countdown_fsm(uint32_t time_ms)
break; break;
case 500: case 500:
sseg_set_char(0, '|', false); sseg_set_char(0, '1', false);
buzzer_on(4000); buzzer_on(4000);
break; break;
@ -286,7 +297,7 @@ static void update_countdown_fsm(uint32_t time_ms)
break; break;
case 1500: case 1500:
sseg_set_char(1, '|', false); sseg_set_char(1, '1', false);
buzzer_on(2000); buzzer_on(2000);
break; break;
@ -296,7 +307,7 @@ static void update_countdown_fsm(uint32_t time_ms)
break; break;
case 2500: case 2500:
sseg_set_char(2, '|', false); sseg_set_char(2, '1', false);
buzzer_on(1000); buzzer_on(1000);
break; break;
@ -331,6 +342,11 @@ int main()
buzzer_init(); buzzer_init();
buttons_init(); buttons_init();
sseg_init();
sseg_set_char(0, 'Z', false);
sseg_set_char(1, 'A', false);
sseg_set_char(2, 'M', false);
buzzer_on(1000); buzzer_on(1000);
@ -348,9 +364,7 @@ int main()
buzzer_off(); buzzer_off();
sleep_ms(2400); sleep_ms(400);
sseg_init();
configure_ms_trig_alarm(); configure_ms_trig_alarm();
arm_ms_trig_alarm(); arm_ms_trig_alarm();
@ -370,9 +384,6 @@ int main()
update_countdown_fsm(ms_counter); update_countdown_fsm(ms_counter);
sseg_set_char(2, '0' + (ms_counter / 100) % 10, false);
sseg_set_char(1, '0' + (ms_counter / 1000) % 10, true);
sseg_set_char(0, '0' + (ms_counter / 10000) % 10, false);
} }
__asm("wfi"); __asm("wfi");