Checkpoint

This commit is contained in:
2026-05-13 20:41:15 +01:00
parent 10dee971b9
commit b3f251e4cc
4 changed files with 106 additions and 9 deletions
+67 -5
View File
@@ -1,32 +1,94 @@
pub mod structs;
use std::cmp::min;
use sdl3::{Sdl, pixels::Color, rect::Rect};
use structs::{Prefs};
use structs::{Prefs, DisplayData};
use crate::renderer::structs::Screen;
const LINE_COUNT: i32 = 6;
const COLOR_LCD_AMBER : Color = Color::RGB(0xf4, 0xcb, 0x60);
const COLOR_LCD_GREEN : Color = Color::RGB(0xb3, 0xff, 0x00);
const COLOR_LCD_RED : Color = Color::RGB(0xff, 0x3a, 0x4a);
//const COLOR_BACKGROUND = pygame.Color(0, 0, 0)
const UPDATE_INTERVAL_SECONDS: u32 = 62;
const TEXT_SIZE: u32 = 160; // Size of the font in pixels
// Offsets of each part within a line
const XOFFSET_ROUTE: u32 = 24;
const XOFFSET_DESTINATION: u32 = 300;
const XOFFSEET_TIME_LEFT: u32 = 1606;
const INTER_LINE_SPACE: i32 = -15;
impl Screen<'_> {
pub fn get_context(&self) -> &Sdl {
return &self.context;
}
fn color_for(&self, due_in: i32) -> Color {
if due_in > 15 { return COLOR_LCD_GREEN };
if due_in > 10 { return COLOR_LCD_AMBER };
return COLOR_LCD_RED;
}
pub fn update_information(&mut self, display_data: &DisplayData) {
self.do_clear();
let num_arrivals: i32 = min(if display_data.status.is_some() {LINE_COUNT - 1} else {LINE_COUNT}, display_data.lines.len().try_into().unwrap());
for line in 0..num_arrivals {
// Compose a line of text with all the information
let entry = display_data.lines.get(line as usize).unwrap();
let line: u32 = line.try_into().unwrap();
let due_in_mins = (entry.due_in / 60) as i32;
let arrival_color: Color = self.color_for(due_in_mins);
self.do_print_at(line, entry.route, XOFFSET_ROUTE);
self.do_print_at(line, entry.destination, XOFFSET_DESTINATION);
self.do_print_at(line, &due_in_mins.to_string(), XOFFSEET_TIME_LEFT);
};
if display_data.status.is_none() {
self.do_print_at(5, display_data.status.as_ref().unwrap(), 0);
}
self.do_update();
}
pub fn print(&mut self, line: u32, text: &str) {
self.do_print_at(line, text, 0);
self.do_update();
}
fn do_print_at(&mut self, line: u32, text: &str, left: u32) -> u32 {
let rendered_text = self.font.render(text).solid(Color::RED).unwrap();
let texture_creator = self.canvas.texture_creator();
let texture = rendered_text.as_texture(&texture_creator).unwrap();
let _ = self.canvas.copy(&texture,
self.canvas.copy(&texture,
Rect::new(0, 0, rendered_text.width(), rendered_text.height()),
Rect::new(0, (line * rendered_text.height()).try_into().unwrap(), rendered_text.width(), rendered_text.height()));
self.canvas.present();
Rect::new(left.try_into().unwrap(), (line * rendered_text.height()).try_into().unwrap(), rendered_text.width(), rendered_text.height()));
return left + rendered_text.width();
}
pub fn clear(&mut self) {
self.do_clear();
self.do_update();
}
fn do_update (&mut self) {
self.canvas.present();
}
fn do_clear(&mut self) {
self.canvas.set_draw_color(Color::BLACK);
self.canvas.clear();
self.canvas.present();
}
/// Initialize video, allocate buffers and load fonts