diff --git a/semestralka1/src/render/loop.cpp b/semestralka1/src/render/loop.cpp index be6c30f..162338b 100644 --- a/semestralka1/src/render/loop.cpp +++ b/semestralka1/src/render/loop.cpp @@ -75,6 +75,8 @@ static bool update_animation(Timer &anim_timer, int &shift, int speed) { void render_loop(int speed) { Timer msg_timer; Timer anim_timer; + Timer crawl_timer; + msg_timer.start(); anim_timer.start(); @@ -82,31 +84,49 @@ void render_loop(int speed) { const char *bg_file = "background_dark_inverted.txt"; bool need_redraw = false; + bool crawling = false; // are we in 1s crawl state? + bool crawl_timer_started = false; + const auto CRAWL_DURATION = 300ms; + while (true) { need_redraw = false; int uart_state = read_uart(); // returns 0/1/2 if (uart_state == 1) - need_redraw = true; + need_redraw = true; - bool triggered = (uart_state == 2); + if (uart_state == 2) { + // start crawl frame for 1 second + crawling = true; + crawl_timer.reset(); + crawl_timer.start(); + crawl_timer_started = true; + } + + // stop crawling after 1 second + if (crawling && crawl_timer_started && + crawl_timer.elapsed_time() >= CRAWL_DURATION) { + crawling = false; + crawl_timer.stop(); + crawl_timer_started = false; + } if (update_animation(anim_timer, shift, speed)) need_redraw = true; - if (need_redraw || triggered) { - draw_mask(bg_file, shift, message_active ? message : nullptr); + if (need_redraw || crawling) { + draw_mask(bg_file, shift, message_active ? message : nullptr); - static int frame = 0; - if (triggered) { - // Display triggered frame once - draw_player(18, 13, 2); - } else { - draw_player(18, 13, frame); - frame = (frame + 1) % 2; // alternate between frame 0 and 1 - } + static int frame = 0; + if (crawling) { + // Display crawl frame during 1s crawl state + draw_player(18, 13, 2); + } else { + draw_player(18, 13, frame); + frame = (frame + 1) % 2; // alternate between frame 0 and 1 + } - ThisThread::sleep_for(50ms); + ThisThread::sleep_for(50ms); } ThisThread::sleep_for(25ms);