94 lines
2.0 KiB
C++
94 lines
2.0 KiB
C++
// src/render/loop.cpp
|
|
#include "loop.h"
|
|
#include "../background_dark_inverted.h"
|
|
#include "background.h"
|
|
#include "mbed.h"
|
|
#include "player.h"
|
|
#include <cstring>
|
|
|
|
extern BufferedSerial serial_port;
|
|
extern DigitalOut led;
|
|
|
|
#define BUFFER_SIZE 64
|
|
|
|
static char rx_buffer[BUFFER_SIZE];
|
|
static char message[BUFFER_SIZE];
|
|
static bool message_active = false;
|
|
|
|
void draw_mask(const char *unused_filename, int shift, const char *text);
|
|
|
|
static bool read_uart() {
|
|
bool changed = false;
|
|
|
|
// cita spravu z uartu
|
|
if (serial_port.readable()) {
|
|
memset(rx_buffer, 0, sizeof(rx_buffer));
|
|
ssize_t num = serial_port.read(rx_buffer, sizeof(rx_buffer) - 1);
|
|
if (num > 0) {
|
|
// toogle ledky
|
|
led = !led;
|
|
strncpy(message, rx_buffer, sizeof(message) - 1);
|
|
message_active = true;
|
|
changed = true;
|
|
}
|
|
}
|
|
|
|
// casovac na 1s zobrazenia spravy
|
|
static Timer msg_timer;
|
|
static bool timer_started = false;
|
|
if (!timer_started) {
|
|
msg_timer.start();
|
|
timer_started = true;
|
|
}
|
|
|
|
// po jednu sekundu sa sprava zobrazi
|
|
if (message_active && msg_timer.elapsed_time() > 1s) {
|
|
message_active = false;
|
|
memset(message, 0, sizeof(message));
|
|
msg_timer.reset();
|
|
changed = true;
|
|
}
|
|
|
|
return changed;
|
|
}
|
|
|
|
static bool update_animation(Timer &anim_timer, int &shift, int speed) {
|
|
if (anim_timer.elapsed_time() >= 200ms) {
|
|
// speed determines scroll steps per tick
|
|
shift += speed;
|
|
anim_timer.reset();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void render_loop(int speed) {
|
|
Timer msg_timer;
|
|
Timer anim_timer;
|
|
msg_timer.start();
|
|
anim_timer.start();
|
|
|
|
int shift = 0;
|
|
const char *bg_file = "background_dark_inverted.txt";
|
|
bool need_redraw = false;
|
|
|
|
while (true) {
|
|
need_redraw = false;
|
|
|
|
if (read_uart()) {
|
|
need_redraw = true;
|
|
}
|
|
|
|
if (update_animation(anim_timer, shift, speed))
|
|
need_redraw = true;
|
|
|
|
if (need_redraw) {
|
|
draw_mask(bg_file, shift, message_active ? message : nullptr);
|
|
draw_player(18, 16);
|
|
ThisThread::sleep_for(50ms);
|
|
}
|
|
|
|
ThisThread::sleep_for(25ms);
|
|
}
|
|
}
|