Display the arrival time correctly
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
use log::debug;
|
||||
use gtfs_structures::{Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
|
||||
use serde::de::{DeserializeOwned, value::MapAccessDeserializer};
|
||||
use serde::de::{DeserializeOwned};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
fs::File,
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ mod arrival;
|
||||
mod loader;
|
||||
mod utils;
|
||||
pub mod structs;
|
||||
use chrono::{DateTime, Local, NaiveTime, Timelike};
|
||||
use chrono::{DateTime, Local, Timelike};
|
||||
use log::{debug};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet}, fs::File, io::Error
|
||||
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use chrono::NaiveTime;
|
||||
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
|
||||
|
||||
// This is to store the preferences for the GTFS(-R) side of the code.
|
||||
@@ -32,7 +31,7 @@ pub struct Gtfs {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Arrival<'a> {
|
||||
pub departure_time: i64,
|
||||
pub departure_time: u32,
|
||||
pub route: &'a Route,
|
||||
pub stop: &'a Stop,
|
||||
pub stop_time: &'a RawStopTime,
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
use std::collections::HashSet;
|
||||
use crate::gtfs::Gtfs;
|
||||
|
||||
pub fn stop_ids_from_codes(gtfs: &Gtfs, stop_codes: &HashSet<String>) -> HashSet<String> {
|
||||
pub(crate) fn stop_ids_from_codes(gtfs: &Gtfs, stop_codes: &HashSet<String>) -> HashSet<String> {
|
||||
let mut ids: HashSet<String> = HashSet::new();
|
||||
|
||||
for stop in >fs.stops {
|
||||
@@ -13,7 +13,7 @@ pub fn stop_ids_from_codes(gtfs: &Gtfs, stop_codes: &HashSet<String>) -> HashSet
|
||||
return ids;
|
||||
}
|
||||
|
||||
pub fn route_ids_from_numbers(gtfs: &Gtfs, route_numbers: &HashSet<String>) -> HashSet<String> {
|
||||
pub(crate) fn route_ids_from_numbers(gtfs: &Gtfs, route_numbers: &HashSet<String>) -> HashSet<String> {
|
||||
let mut ids: HashSet<String> = HashSet::new();
|
||||
|
||||
for route in >fs.routes {
|
||||
|
||||
+7
-7
@@ -1,13 +1,15 @@
|
||||
mod gtfs;
|
||||
mod renderer;
|
||||
use std::{collections::HashSet, ops::Add, os::unix::process::ExitStatusExt, process, thread::Builder, time::SystemTime};
|
||||
use chrono::{DateTime, Duration, Local, NaiveTime, Timelike};
|
||||
use log::{Metadata, Record, debug, error, info};
|
||||
use std::{collections::HashSet, ops::Add, process, thread::Builder, time::SystemTime};
|
||||
use chrono::{DateTime, Duration, Local, NaiveTime};
|
||||
use log::{Metadata, Record, error, info};
|
||||
use sdl3::event::Event;
|
||||
use crate::{gtfs::structs::{Arrival, Gtfs}, renderer::structs::{DisplayData, DisplayEntry, Screen}};
|
||||
|
||||
const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip";
|
||||
const NUM_ARRIVALS: usize = 4;
|
||||
const UPDATE_INTERVAL_SECONDS: u64 = 62;
|
||||
|
||||
|
||||
// Custom Event to signal data refresh
|
||||
#[derive(Debug)]
|
||||
@@ -31,8 +33,6 @@ fn refresh_schedule<'a>(gtfs: &'a Gtfs, screen : &mut Screen<'a>) -> Option<Vec<
|
||||
next_arrivals.sort();
|
||||
|
||||
// Create the DisplayData structure to render the information to screen
|
||||
let now =Local::now();
|
||||
let current_time: i64 = (now.hour() * 3600 + now.minute() * 60 + now.second()).into();
|
||||
let mut display_data: DisplayData = DisplayData {
|
||||
lines: Vec::<DisplayEntry>::new(),
|
||||
status: None
|
||||
@@ -46,7 +46,7 @@ fn refresh_schedule<'a>(gtfs: &'a Gtfs, screen : &mut Screen<'a>) -> Option<Vec<
|
||||
.or(Option::Some(String::from("Unknown")
|
||||
))).unwrap(),
|
||||
route: arrival.route.short_name.clone().or(arrival.route.long_name.clone()).unwrap(),
|
||||
due_in: (arrival.departure_time - current_time) / 60,
|
||||
departure_time: arrival.departure_time,
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -117,7 +117,7 @@ fn main() {
|
||||
.name("updater".to_string())
|
||||
.spawn(move || {
|
||||
loop {
|
||||
std::thread::sleep(std::time::Duration::new(60,0));
|
||||
std::thread::sleep(std::time::Duration::new(UPDATE_INTERVAL_SECONDS,0));
|
||||
let event = RefreshDataEvent {};
|
||||
let send_result = event_sender.push_custom_event(event);
|
||||
if send_result.is_err() {
|
||||
|
||||
+27
-20
@@ -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),
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user