Improved display printing, added extra debug.

This commit is contained in:
Nahuel Lofeudo 2026-05-22 05:06:46 +01:00
parent c13411065a
commit e185b38222
3 changed files with 38 additions and 15 deletions

View File

@ -4,6 +4,7 @@ mod utils;
pub mod structs; pub mod structs;
use chrono::{DateTime, Local, Timelike}; use chrono::{DateTime, Local, Timelike};
use log::{debug}; use log::{debug};
use sdl3::sys::pixels::SDL_ArrayOrder;
use std::{ use std::{
collections::{HashMap, HashSet}, fs::File, io::Error collections::{HashMap, HashSet}, fs::File, io::Error
}; };
@ -66,7 +67,6 @@ impl Gtfs {
for (_id, stop_time) in self.stop_times.iter() { for (_id, stop_time) in self.stop_times.iter() {
if trips.contains_key(&stop_time.trip_id) { if trips.contains_key(&stop_time.trip_id) {
let stop_timestamp = stop_time.departure_time.or(stop_time.arrival_time)?; let stop_timestamp = stop_time.departure_time.or(stop_time.arrival_time)?;
debug!("Stop timestamp {} current timestamp {}", stop_timestamp, current_timestamp);
let trip= &self.trips.get(&stop_time.trip_id).unwrap(); let trip= &self.trips.get(&stop_time.trip_id).unwrap();
if current_timestamp < stop_timestamp.into() { if current_timestamp < stop_timestamp.into() {
let arrival: Arrival = Arrival { let arrival: Arrival = Arrival {
@ -76,6 +76,7 @@ impl Gtfs {
trip: &trip, trip: &trip,
departure_time: stop_timestamp.into() departure_time: stop_timestamp.into()
}; };
debug!("Arrival to {:#?} for trip ID {:#?}.", arrival.trip.trip_headsign.as_ref().unwrap(), arrival.trip.id);
arrivals.push(arrival); arrivals.push(arrival);
} }
} }

View File

@ -83,7 +83,7 @@ fn main() {
// Create preferences structures from config // Create preferences structures from config
let gtfs_prefs = gtfs::structs::Preferences { let gtfs_prefs = gtfs::structs::Preferences {
route_numbers: HashSet::from([String::from("15A"), String::from("F1"), String::from("F2"), String::from("F3")]), route_numbers: HashSet::from([String::from("15A"), String::from("F1"), String::from("F2"), String::from("F3")]),
stop_codes: HashSet::from([String::from("1117")]) stop_codes: HashSet::from([String::from("1114")])
}; };
let screen_prefs = renderer::structs::Prefs { let screen_prefs = renderer::structs::Prefs {

View File

@ -1,7 +1,7 @@
pub mod structs; pub mod structs;
use std::cmp::min; use std::{cmp::min};
use log::{error, warn};
use sdl3::{Sdl, pixels::Color, rect::Rect, ttf::FontStyle}; use sdl3::{Sdl, pixels::Color, rect::Rect};
use structs::{Prefs, DisplayData}; use structs::{Prefs, DisplayData};
use crate::renderer::structs::Screen; use crate::renderer::structs::Screen;
@ -11,6 +11,7 @@ const COLOR_LCD_AMBER : Color = Color::RGB(0xf4, 0xcb, 0x60);
const COLOR_LCD_GREEN : Color = Color::RGB(0xb3, 0xff, 0x00); const COLOR_LCD_GREEN : Color = Color::RGB(0xb3, 0xff, 0x00);
const COLOR_LCD_RED : Color = Color::RGB(0xff, 0x3a, 0x4a); const COLOR_LCD_RED : Color = Color::RGB(0xff, 0x3a, 0x4a);
const COLOR_BACKGROUND : Color = Color::RGB(0x0, 0x0, 0x0 ); const COLOR_BACKGROUND : Color = Color::RGB(0x0, 0x0, 0x0 );
const COLOR_TEXT_BG : Color = Color::RGBA(0x0, 0x0, 0x0, 0x0);
const TEXT_SIZE: f32 = 160.0; // Size of the font in pixels const TEXT_SIZE: f32 = 160.0; // Size of the font in pixels
// Offsets of each part within a line // Offsets of each part within a line
@ -45,8 +46,16 @@ impl Screen<'_> {
pub fn update_information(&mut self, display_data: &DisplayData) { pub fn update_information(&mut self, display_data: &DisplayData) {
self.do_clear(); self.do_clear();
// TODO: Add header // Print status first
if display_data.status.is_some() {
// If the update has some information text, show it
self.do_print_at(5, display_data.status.as_ref().unwrap(), 0);
} else {
// Display date and time otherwise
self.do_print_at(5, &format!("TODO: DATE/TIME GOES HERE").to_string(), 0);
}
// Then data lines from the bottom up
let num_arrivals: i32 = min(LINE_COUNT, display_data.lines.len() as i32); let num_arrivals: i32 = min(LINE_COUNT, display_data.lines.len() as i32);
for index in 0..num_arrivals { for index in 0..num_arrivals {
let line: u32 = ((num_arrivals - 1) - index) as u32; let line: u32 = ((num_arrivals - 1) - index) as u32;
@ -62,10 +71,6 @@ impl Screen<'_> {
self.color = due_color; self.color = due_color;
self.do_print_at(line, &due_text, XOFFSEET_TIME_LEFT); self.do_print_at(line, &due_text, XOFFSEET_TIME_LEFT);
}; };
if display_data.status.is_some() {
self.do_print_at(5, display_data.status.as_ref().unwrap(), 0);
}
self.do_update(); self.do_update();
} }
@ -76,14 +81,31 @@ impl Screen<'_> {
} }
fn do_print_at(&mut self, line: u32, text: &str, left: u32) -> u32 { fn do_print_at(&mut self, line: u32, text: &str, left: u32) {
let rendered_text = self.font.render(text).lcd(self.color, COLOR_BACKGROUND).unwrap(); if text.len() == 0 {
warn!("do_print_at called with a 0-length string");
return;
}
let render_result = self.font.render(text).lcd(self.color, COLOR_BACKGROUND);
if render_result.is_err() {
error!("Error rendering text \"{}\": {:#?}", text, render_result.err());
return;
}
let rendered_text = render_result.unwrap();
let texture_creator = self.canvas.texture_creator(); let texture_creator = self.canvas.texture_creator();
let texture = rendered_text.as_texture(&texture_creator).unwrap(); let texture = rendered_text.as_texture(&texture_creator);
let _= self.canvas.copy(&texture,
if texture.is_err() {
error!("Error creating texture from rendered text: {:#?}", texture.err());
return;
}
let _= self.canvas.copy(&texture.unwrap(),
Rect::new(0, 0, rendered_text.width(), rendered_text.height()), Rect::new(0, 0, rendered_text.width(), rendered_text.height()),
Rect::new(left.try_into().unwrap(), (line * (rendered_text.height() - INTER_LINE_OVERLAP)).try_into().unwrap(), rendered_text.width(), rendered_text.height())); Rect::new(left.try_into().unwrap(), (line * (rendered_text.height() - INTER_LINE_OVERLAP)).try_into().unwrap(), rendered_text.width(), rendered_text.height()));
return left + rendered_text.width();
} }