Display the arrival time correctly

This commit is contained in:
2026-05-21 07:40:01 +01:00
parent cef6faa05f
commit c13411065a
9 changed files with 173 additions and 35 deletions
+27 -20
View File
@@ -1,20 +1,17 @@
pub mod structs;
use std::cmp::min;
use sdl3::{Sdl, pixels::Color, rect::Rect};
use sdl3::{Sdl, pixels::Color, rect::Rect, ttf::FontStyle};
use structs::{Prefs, DisplayData};
use crate::renderer::structs::Screen;
const LINE_COUNT: i32 = 6;
const LINE_COUNT: i32 = 5;
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
const COLOR_BACKGROUND : Color = Color::RGB(0x0, 0x0, 0x0 );
const TEXT_SIZE: f32 = 160.0; // Size of the font in pixels
// Offsets of each part within a line
const XOFFSET_ROUTE: u32 = 24;
@@ -22,7 +19,6 @@ const XOFFSET_DESTINATION: u32 = 300;
const XOFFSEET_TIME_LEFT: u32 = 1606;
const INTER_LINE_OVERLAP: u32 = 15;
impl Screen<'_> {
pub fn get_context(&self) -> &Sdl {
@@ -35,25 +31,36 @@ impl Screen<'_> {
return COLOR_LCD_RED;
}
fn format_due_for(&self, due_in: i32, departure_time: u32) -> String {
if due_in < 60 {
return String::from("due");
}
if due_in < 3600 {
return ((due_in / 60) as i32).to_string() + "min";
}
return format!("{:02}:{:02}", (departure_time / 3600) as i32, ((departure_time % 3600) / 60) as i32);
}
pub fn update_information(&mut self, display_data: &DisplayData) {
self.do_clear();
// TODO: Add header
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 {
let num_arrivals: i32 = min(LINE_COUNT, display_data.lines.len() as i32);
for index in 0..num_arrivals {
let line: u32 = ((num_arrivals - 1) - index) as u32;
// Compose a line of text with all the information
let entry = display_data.lines.get(line as usize).unwrap();
let line: u32 = (line + 1).try_into().unwrap();
let due_in_mins = entry.due_in as i32;
let arrival_color: Color = self.color_for(due_in_mins);
let due_in_mins = (entry.departure_time / 60) as i32;
let due_color: Color = self.color_for(due_in_mins);
let due_text = self.format_due_for(due_in_mins, entry.departure_time);
self.color = COLOR_LCD_AMBER;
self.do_print_at(line, &entry.route, XOFFSET_ROUTE);
self.do_print_at(line, &entry.destination, XOFFSET_DESTINATION);
self.color = arrival_color;
self.do_print_at(line, &due_in_mins.to_string(), XOFFSEET_TIME_LEFT);
self.color = due_color;
self.do_print_at(line, &due_text, XOFFSEET_TIME_LEFT);
};
if display_data.status.is_some() {
@@ -63,14 +70,14 @@ impl Screen<'_> {
}
pub fn print(&mut self, line: u32, text: &str) {
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(self.color).unwrap();
let rendered_text = self.font.render(text).lcd(self.color, COLOR_BACKGROUND).unwrap();
let texture_creator = self.canvas.texture_creator();
let texture = rendered_text.as_texture(&texture_creator).unwrap();
let _= self.canvas.copy(&texture,
@@ -92,7 +99,7 @@ impl Screen<'_> {
fn do_clear(&mut self) {
self.canvas.set_draw_color(Color::BLACK);
self.canvas.set_draw_color(COLOR_BACKGROUND);
self.canvas.clear();
}
@@ -112,8 +119,8 @@ impl Screen<'_> {
// Load font
let ttf_context = sdl3::ttf::init().unwrap();
let font = ttf_context.load_font(&prefs.font_path, 128.0).unwrap();
let font = ttf_context.load_font(&prefs.font_path, TEXT_SIZE).unwrap();
let mut screen: Screen = Screen {
canvas: Box::new(window.into_canvas()),
font: Box::new(font),
+1 -1
View File
@@ -18,7 +18,7 @@ pub struct Prefs {
pub struct DisplayEntry {
pub route: String,
pub destination: String,
pub due_in: i64,
pub departure_time: u32,
}
pub struct DisplayData {