Compare commits
No commits in common. "main" and "Render-data" have entirely different histories.
main
...
Render-dat
28
Cargo.toml
28
Cargo.toml
|
|
@ -5,34 +5,16 @@ edition = "2024"
|
||||||
host = "x86_64-unknown-linux-gnu"
|
host = "x86_64-unknown-linux-gnu"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4"
|
|
||||||
csv = "1.4"
|
|
||||||
gtfs-structures = "0.47"
|
|
||||||
log = "0.4"
|
|
||||||
reqwest = { version = "0.11", features = ["blocking"] }
|
|
||||||
sdl3 = {version = "0.17", features = ["ttf"]}
|
sdl3 = {version = "0.17", features = ["ttf"]}
|
||||||
serde = { version = "1.0", features = ["derive"]}
|
serde = "1.0"
|
||||||
time-format = "1.2"
|
gtfs-structures = "0.47"
|
||||||
yaml_serde = "0.10"
|
chrono = "0.4"
|
||||||
|
log = "0.4"
|
||||||
zip = "8.3"
|
zip = "8.3"
|
||||||
|
csv = "1.4"
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 1
|
opt-level = 1
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
|
|
||||||
[target.aarch64-unknown-linux-gnu]
|
|
||||||
cc = "aarch64-linux-gnu-gcc"
|
|
||||||
cxx = "aarch64-linux-gnu-g++"
|
|
||||||
ar = "aarch64-linux-gnu-ar"
|
|
||||||
# ranlib = "aarch64-linux-gnu-ranlib"
|
|
||||||
linker = "aarch64-linux-gnu-gcc"
|
|
||||||
|
|
||||||
|
|
||||||
[target.arm-unknown-linux-gnueabihf]
|
|
||||||
cc = "arm-linux-gnueabihf-gcc"
|
|
||||||
cxx = "arm-linux-gnueabihf-g++"
|
|
||||||
ar = "arm-linux-gnueabihf-ar"
|
|
||||||
# ranlib = "arm-linux-gnueabihf-ranlib"
|
|
||||||
linker = "arm-linux-gnueabihf-gcc"
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
[build]
|
||||||
|
default-target = "aarch64-unknown-linux-gnu" # use this target if none is explicitly provided
|
||||||
|
pre-build = [ # additional commands to run prior to building the package
|
||||||
|
"dpkg --add-architecture $CROSS_DEB_ARCH",
|
||||||
|
"apt update",
|
||||||
|
"apt --assume-yes install apt-utils:$CROSS_DEB_ARCH",
|
||||||
|
"apt --assume-yes install libsdl3-dev:$CROSS_DEB_ARCH"
|
||||||
|
]
|
||||||
31
readme.md
31
readme.md
|
|
@ -6,28 +6,21 @@
|
||||||
|
|
||||||
## Install ARM toolchains:
|
## Install ARM toolchains:
|
||||||
|
|
||||||
```bash
|
```
|
||||||
$ rustup target add aarch64-unknown-linux-gnu
|
cargo install cross
|
||||||
$ rustup target add arm-unknown-linux-gnueabihf
|
rustup target add aarch64-unknown-linux-gnu # 64-bit Pi OS
|
||||||
|
rustup target add armv7-unknown-linux-gnueabihf # 32-bit Pi 2/3/4 OS
|
||||||
|
rustup target add arm-unknown-linux-gnueabihf # ARMv6 (Pi Zero/1)
|
||||||
|
|
||||||
|
sudo apt-get update
|
||||||
$ sudo dpkg --add-architecture arm64
|
sudo apt-get install gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf
|
||||||
$ sudo dpkg --add-architecture armhf
|
|
||||||
|
|
||||||
$ sudo apt update
|
|
||||||
$ sudo apt-get install gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf \
|
|
||||||
libc6-arm64-cross libc6-dev-arm64-cross gcc-aarch64-linux-gnu \
|
|
||||||
libc6-armhf-cross libc6-dev-armhf-cross gcc-arm-linux-gnueabihf \
|
|
||||||
libssl-dev libssl-dev:arm64 libsdl3-dev:arm64 libssl-dev:armhf libsdl3-dev:armhf
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Cross-compile
|
## Cross-compile
|
||||||
|
|
||||||
ARM64
|
```
|
||||||
```bash
|
# 2) Build
|
||||||
$ export PKG_CONFIG_SYSROOT_DIR=/usr/aarch64-linux-gnu/
|
~/.cargo/bin/cross build --target aarch64-unknown-linux-gnu --release
|
||||||
$ cargo build --target aarch64-unknown-linux-gnu --release
|
~/.cargo/bin/cross build --target armv7-unknown-linux-gnueabihf --release
|
||||||
|
~/.cargo/bin/cross build --target arm-unknown-linux-gnueabihf --release
|
||||||
$ export PKG_CONFIG_SYSROOT_DIR=/usr/arm-linux-gnueabihf/
|
|
||||||
$ cargo build --target arm-unknown-linux-gnueabihf --release
|
|
||||||
```
|
```
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
screen:
|
|
||||||
width: 1920
|
|
||||||
height: 720
|
|
||||||
font-path: "resources/jd_lcd_rounded/jd-lcd-rounded.ttf"
|
|
||||||
|
|
||||||
gtfs:
|
|
||||||
routes:
|
|
||||||
- 15A
|
|
||||||
- F1
|
|
||||||
- F2
|
|
||||||
stops:
|
|
||||||
- 1114
|
|
||||||
- 2410
|
|
||||||
data-folder: "/home/nahuel/Downloads"
|
|
||||||
gtfs-url: "https://www.transportforireland.ie/transitData/Data/GTFS_Realtime.zip"
|
|
||||||
realtime-url: "https://api.nationaltransport.ie/gtfsr/v2/gtfsr"
|
|
||||||
realtime-api-key: "Not defined"
|
|
||||||
refresh-seconds: 61
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
use crate::{gtfs, renderer};
|
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
use std::fs;
|
|
||||||
use yaml_serde;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
||||||
pub struct Config {
|
|
||||||
#[serde(rename = "gtfs")]
|
|
||||||
pub gtfs_prefs: gtfs::structs::Preferences,
|
|
||||||
|
|
||||||
#[serde(rename = "screen")]
|
|
||||||
pub screen_prefs: renderer::structs::Prefs,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive (Debug)]
|
|
||||||
pub struct Error {
|
|
||||||
pub message: String
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl From<std::io::Error> for Error {
|
|
||||||
fn from(value: std::io::Error) -> Self {
|
|
||||||
return Error {
|
|
||||||
message: value.to_string(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<yaml_serde::Error> for Error {
|
|
||||||
fn from(value: yaml_serde::Error) -> Self {
|
|
||||||
return Error {
|
|
||||||
message: value.to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<sdl3::Error> for Error {
|
|
||||||
fn from(value: sdl3::Error) -> Self {
|
|
||||||
return Error {
|
|
||||||
message: value.to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn load_config(config_name: String) -> Result<Config, Error> {
|
|
||||||
let config_text = fs::read_to_string(config_name)?;
|
|
||||||
let config = yaml_serde::from_str(config_text.as_ref())?;
|
|
||||||
return Ok(config);
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use gtfs_structures::{Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
|
use gtfs_structures::{Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
|
||||||
use serde::de::{DeserializeOwned};
|
use serde::de::DeserializeOwned;
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
fs::File,
|
fs::File,
|
||||||
|
|
@ -14,14 +14,14 @@ use crate::gtfs::{
|
||||||
};
|
};
|
||||||
|
|
||||||
trait Filter<T> {
|
trait Filter<T> {
|
||||||
fn accept(&self, v: &T) -> Option<bool>;
|
fn accept(&self, v: &T) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No filter on loaded records
|
// No filter on loaded records
|
||||||
struct LoadAll {}
|
struct LoadAll {}
|
||||||
impl<T> Filter<T> for LoadAll {
|
impl<T> Filter<T> for LoadAll {
|
||||||
fn accept(&self, _: &T) -> Option<bool> {
|
fn accept(&self, _: &T) -> bool {
|
||||||
return Some(true);
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -29,9 +29,9 @@ struct LoadRoutes<'a> {
|
||||||
routes: &'a HashSet<String>,
|
routes: &'a HashSet<String>,
|
||||||
}
|
}
|
||||||
impl Filter<Route> for LoadRoutes<'_> {
|
impl Filter<Route> for LoadRoutes<'_> {
|
||||||
fn accept(&self, r: &Route) -> Option<bool> {
|
fn accept(&self, r: &Route) -> bool {
|
||||||
let short_name = &r.short_name;
|
let short_name = &r.short_name;
|
||||||
return Some(short_name.is_some() && self.routes.contains(short_name.as_ref()?));
|
return short_name.is_some() && self.routes.contains(short_name.as_ref().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,9 +39,9 @@ struct LoadStops<'a> {
|
||||||
stops: &'a HashSet<String>,
|
stops: &'a HashSet<String>,
|
||||||
}
|
}
|
||||||
impl Filter<Stop> for LoadStops<'_> {
|
impl Filter<Stop> for LoadStops<'_> {
|
||||||
fn accept(&self, s: &Stop) -> Option<bool> {
|
fn accept(&self, s: &Stop) -> bool {
|
||||||
let stop_code = &s.code;
|
let stop_code = &s.code;
|
||||||
return Some(stop_code.is_some() && self.stops.contains(s.code.as_ref()?));
|
return stop_code.is_some() && self.stops.contains(s.code.as_ref().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -49,9 +49,9 @@ struct LoadTrips<'a> {
|
||||||
route_ids: &'a HashSet<String>,
|
route_ids: &'a HashSet<String>,
|
||||||
}
|
}
|
||||||
impl Filter<RawTrip> for LoadTrips<'_> {
|
impl Filter<RawTrip> for LoadTrips<'_> {
|
||||||
fn accept(&self, t: &RawTrip) -> Option<bool> {
|
fn accept(&self, t: &RawTrip) -> bool {
|
||||||
let route_id = &t.route_id;
|
let route_id = &t.route_id;
|
||||||
return Some(self.route_ids.contains(route_id));
|
return self.route_ids.contains(route_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,8 +60,8 @@ struct LoadStopTimes<'a> {
|
||||||
stop_ids: &'a HashSet<String>,
|
stop_ids: &'a HashSet<String>,
|
||||||
}
|
}
|
||||||
impl Filter<RawStopTime> for LoadStopTimes<'_> {
|
impl Filter<RawStopTime> for LoadStopTimes<'_> {
|
||||||
fn accept(&self, st: &RawStopTime) -> Option<bool> {
|
fn accept(&self, st: &RawStopTime) -> bool {
|
||||||
return Some(self.stop_ids.contains(&st.stop_id) && self.trip_ids.contains(&st.trip_id));
|
return self.stop_ids.contains(&st.stop_id) && self.trip_ids.contains(&st.trip_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,15 +70,14 @@ fn load_vector<T: serde::de::DeserializeOwned>(
|
||||||
destination: &mut Vec<T>,
|
destination: &mut Vec<T>,
|
||||||
zip_reader: &mut ZipArchive<File>,
|
zip_reader: &mut ZipArchive<File>,
|
||||||
table_name: &str,
|
table_name: &str,
|
||||||
) -> Option<bool> {
|
) {
|
||||||
let file_reader = zip_reader.by_name(table_name);
|
let file_reader = zip_reader.by_name(table_name).unwrap();
|
||||||
let mut rdr = csv::Reader::from_reader(file_reader.ok()?);
|
let mut rdr = csv::Reader::from_reader(file_reader);
|
||||||
|
|
||||||
for row in rdr.deserialize() {
|
for row in rdr.deserialize() {
|
||||||
let record: T = row.ok()?;
|
let record: T = row.unwrap();
|
||||||
destination.push(record);
|
destination.push(record);
|
||||||
}
|
}
|
||||||
return Some(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loads a HashMap of the selected type, using the provided index function as the key
|
// Loads a HashMap of the selected type, using the provided index function as the key
|
||||||
|
|
@ -88,30 +87,27 @@ fn load_map<K, V, IndexFn, FilterT>(
|
||||||
table_name: &str,
|
table_name: &str,
|
||||||
index: IndexFn,
|
index: IndexFn,
|
||||||
filter: FilterT,
|
filter: FilterT,
|
||||||
) -> Option<bool>
|
) where
|
||||||
where
|
|
||||||
K: Eq + Hash,
|
K: Eq + Hash,
|
||||||
V: DeserializeOwned,
|
V: DeserializeOwned,
|
||||||
IndexFn: Fn(&V) -> K,
|
IndexFn: Fn(&V) -> K,
|
||||||
FilterT: Filter<V>
|
FilterT: Filter<V>,
|
||||||
{
|
{
|
||||||
let file_reader = (zip_reader.by_name(table_name)).ok()?;
|
let file_reader = zip_reader.by_name(table_name).unwrap();
|
||||||
let mut rdr = csv::Reader::from_reader(file_reader);
|
let mut rdr = csv::Reader::from_reader(file_reader);
|
||||||
|
|
||||||
for row in rdr.deserialize() {
|
for row in rdr.deserialize() {
|
||||||
if row.is_ok() {
|
if row.is_ok() {
|
||||||
let record: V = row.ok()?;
|
let record: V = row.unwrap();
|
||||||
let accepted = filter.accept(&record);
|
if filter.accept(&record) {
|
||||||
if accepted.is_some() && accepted? {
|
|
||||||
let idx: K = index(&record);
|
let idx: K = index(&record);
|
||||||
destination.insert(idx, record);
|
destination.insert(idx, record);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print!("Row failed to deserialize row {:#?}", row.err());
|
print!("Row failed to deserialize row {:#?}", row.err());
|
||||||
return None;
|
panic!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Some(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loads a HashMap of a vector of the selected type, using the provided index function as the key
|
// Loads a HashMap of a vector of the selected type, using the provided index function as the key
|
||||||
|
|
@ -122,25 +118,22 @@ fn load_vector_map<'a, K, V, IndexFn, FilterT>(
|
||||||
table_name: &str,
|
table_name: &str,
|
||||||
index: IndexFn,
|
index: IndexFn,
|
||||||
filter: FilterT,
|
filter: FilterT,
|
||||||
) -> Option<bool>
|
) where
|
||||||
where
|
|
||||||
K: Eq + Hash,
|
K: Eq + Hash,
|
||||||
V: DeserializeOwned,
|
V: DeserializeOwned,
|
||||||
IndexFn: Fn(&V) -> K,
|
IndexFn: Fn(&V) -> K,
|
||||||
FilterT: Filter<V>,
|
FilterT: Filter<V>,
|
||||||
{
|
{
|
||||||
let file_reader = zip_reader.by_name(table_name).ok()?;
|
let file_reader = zip_reader.by_name(table_name).unwrap();
|
||||||
let mut rdr = csv::Reader::from_reader(file_reader);
|
let mut rdr = csv::Reader::from_reader(file_reader);
|
||||||
|
|
||||||
for row in rdr.deserialize() {
|
for row in rdr.deserialize() {
|
||||||
let record: V = row.ok()?;
|
let record: V = row.unwrap();
|
||||||
let accepted = filter.accept(&record);
|
if filter.accept(&record) {
|
||||||
if accepted.is_some() && accepted? {
|
|
||||||
let idx = index(&record);
|
let idx = index(&record);
|
||||||
destination.entry(idx).or_insert_with(Vec::new).push(record);
|
destination.entry(idx).or_insert_with(Vec::new).push(record);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Some(true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_gtfs(
|
pub fn load_gtfs(
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,21 @@
|
||||||
mod arrival;
|
mod arrival;
|
||||||
mod loader;
|
mod loader;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod refresher;
|
|
||||||
pub mod structs;
|
pub mod structs;
|
||||||
use chrono::{DateTime, Local, Timelike};
|
use chrono::{DateTime, Local, NaiveTime, Timelike};
|
||||||
use log::{debug, trace};
|
use log::{debug};
|
||||||
use std::{collections::{HashMap, HashSet}, fs::File };
|
use std::{
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
fs::File,
|
||||||
|
};
|
||||||
use gtfs_structures::{Exception, RawTrip};
|
use gtfs_structures::{Exception, RawTrip};
|
||||||
use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences, Error}};
|
|
||||||
|
use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences}};
|
||||||
|
|
||||||
|
|
||||||
impl Gtfs {
|
impl Gtfs {
|
||||||
|
|
||||||
pub fn get_next_arrivals_for(&self, target_datetime: &DateTime<Local>) -> Option<Vec<Arrival<'_>>> {
|
pub fn get_next_arrivals_for(&self, target_datetime: &DateTime<Local>) -> Vec<Arrival<'_>> {
|
||||||
let naive_target = target_datetime.naive_local();
|
let naive_target = target_datetime.naive_local();
|
||||||
let target_date = naive_target.date();
|
let target_date = naive_target.date();
|
||||||
|
|
||||||
|
|
@ -63,39 +66,31 @@ 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).unwrap();
|
||||||
|
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 {
|
||||||
route: self.routes.get(&self.trips.get(&stop_time.trip_id)?.route_id)?,
|
route: self.routes.get(&trip.route_id).unwrap(),
|
||||||
stop: self.stops.get(&stop_time.stop_id)?,
|
stop: self.stops.get(&stop_time.stop_id).unwrap(),
|
||||||
stop_time: stop_time,
|
stop_time: stop_time,
|
||||||
trip: &trip,
|
trip: &trip,
|
||||||
departure_time: stop_timestamp.into()
|
departure_time: NaiveTime::from_num_seconds_from_midnight_opt(stop_timestamp, 0).unwrap()
|
||||||
};
|
};
|
||||||
trace!("{:#?}: Arrival to {:#?} for trip ID {:#?}.",
|
|
||||||
format!("{:02}:{:02}", (arrival.departure_time/3600) as u32, ((arrival.departure_time / 60) % 60) as u32),
|
|
||||||
arrival.trip.trip_headsign.as_ref().unwrap(),
|
|
||||||
arrival.trip.id);
|
|
||||||
arrivals.push(arrival);
|
arrivals.push(arrival);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
arrivals.sort();
|
|
||||||
debug!("Found {} arrivals", arrivals.len());
|
debug!("Found {} arrivals", arrivals.len());
|
||||||
|
|
||||||
return Some(arrivals);
|
return arrivals;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Load a GTFS structure from a zip file
|
/// Load a GTFS structure from a zip file
|
||||||
pub fn load(prefs: &Preferences) -> Result<Gtfs, Error> {
|
pub fn load(src_file: &str, prefs: &Preferences) -> Gtfs {
|
||||||
|
|
||||||
_ = refresher::refresh(prefs);
|
|
||||||
|
|
||||||
// Open zip file
|
// Open zip file
|
||||||
let zip_file = File::open(prefs.gtfs_file_path()?)?;
|
let mut zip_reader = zip::ZipArchive::new(File::open(src_file).unwrap()).unwrap();
|
||||||
let mut zip_reader = zip::ZipArchive::new(zip_file)?;
|
|
||||||
|
|
||||||
let mut gtfs: Gtfs = Gtfs {
|
let mut gtfs: Gtfs = Gtfs {
|
||||||
agencies: Vec::new(),
|
agencies: Vec::new(),
|
||||||
|
|
@ -110,6 +105,6 @@ impl Gtfs {
|
||||||
|
|
||||||
load_gtfs(&mut gtfs, &mut zip_reader, &prefs.route_numbers, &prefs.stop_codes);
|
load_gtfs(&mut gtfs, &mut zip_reader, &prefs.route_numbers, &prefs.stop_codes);
|
||||||
|
|
||||||
return Ok(gtfs);
|
return gtfs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
use log::{debug, info};
|
|
||||||
use time_format::format_common_utc;
|
|
||||||
use std::{fs::{self}, time::{Duration, SystemTime, UNIX_EPOCH}};
|
|
||||||
use crate::gtfs::structs::{Error, Preferences};
|
|
||||||
use reqwest::{StatusCode, blocking::Client};
|
|
||||||
|
|
||||||
|
|
||||||
pub(crate) fn refresh(prefs: &Preferences) -> Result<(), Error> {
|
|
||||||
|
|
||||||
let mut modified_timestamp: SystemTime = SystemTime::UNIX_EPOCH;
|
|
||||||
let gtfs_file_name = prefs.gtfs_file_path()?;
|
|
||||||
|
|
||||||
// Obtain the GTFS zip's creation time.
|
|
||||||
let metadata = fs::metadata(>fs_file_name);
|
|
||||||
if metadata.is_ok() {
|
|
||||||
modified_timestamp = metadata?.modified()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let modified_seconds = modified_timestamp.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64;
|
|
||||||
let modified_header = format_common_utc(modified_seconds, time_format::DateFormat::HTTP).unwrap();
|
|
||||||
|
|
||||||
debug!("Using if-modified-since: {}", modified_header);
|
|
||||||
|
|
||||||
// request updates from the server if-modified-since last time
|
|
||||||
let client = Client::builder()
|
|
||||||
.timeout(Duration::from_secs(30))
|
|
||||||
.connect_timeout(Duration::from_secs(5))
|
|
||||||
.build()?;
|
|
||||||
|
|
||||||
let mut response = client
|
|
||||||
.get(&prefs.gtfs_url)
|
|
||||||
.header(reqwest::header::IF_MODIFIED_SINCE, modified_header)
|
|
||||||
.send()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
if response.status() == StatusCode::NOT_MODIFIED {
|
|
||||||
debug!("GTFS data is still up-to-date");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
if response.status() == StatusCode::OK {
|
|
||||||
info!("Refreshing GTFS data");
|
|
||||||
// Stream the response data into a temp file and swap them
|
|
||||||
|
|
||||||
let tmp_full_file_name = format!("{}.new", >fs_file_name);
|
|
||||||
let mut tmp_file = fs::File::create(&tmp_full_file_name)?;
|
|
||||||
|
|
||||||
_ = response.copy_to(&mut tmp_file)?;
|
|
||||||
drop(tmp_file);
|
|
||||||
|
|
||||||
if fs::exists(>fs_file_name)? {
|
|
||||||
_ = fs::remove_file(>fs_file_name);
|
|
||||||
}
|
|
||||||
_ = fs::rename(&tmp_full_file_name, >fs_file_name);
|
|
||||||
info!("GTFS data refreshed");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
let errmsg = format!("GET on {} returned result code {}", prefs.gtfs_url, response.status());
|
|
||||||
return Err(Error { _message: errmsg });
|
|
||||||
}
|
|
||||||
|
|
@ -1,74 +1,12 @@
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
use chrono::NaiveTime;
|
||||||
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
|
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
|
||||||
use log::error;
|
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
use zip::result::ZipError;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Error {
|
|
||||||
pub(crate) _message: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<std::io::Error> for Error {
|
|
||||||
fn from(value: std::io::Error) -> Self {
|
|
||||||
return Error { _message: value.to_string() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<reqwest::Error> for Error {
|
|
||||||
fn from(value: reqwest::Error) -> Self {
|
|
||||||
return Error { _message: value.to_string() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ZipError> for Error {
|
|
||||||
fn from(value: ZipError) -> Self {
|
|
||||||
return Error { _message: value.to_string() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// This is to store the preferences for the GTFS(-R) side of the code.
|
// This is to store the preferences for the GTFS(-R) side of the code.
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
||||||
pub struct Preferences {
|
pub struct Preferences {
|
||||||
#[serde(rename = "routes")]
|
|
||||||
pub route_numbers: HashSet<String>,
|
pub route_numbers: HashSet<String>,
|
||||||
|
|
||||||
#[serde(rename = "stops")]
|
|
||||||
pub stop_codes: HashSet<String>,
|
pub stop_codes: HashSet<String>,
|
||||||
|
|
||||||
#[serde(rename = "data-folder")]
|
|
||||||
pub data_folder: String,
|
|
||||||
|
|
||||||
#[serde(rename = "gtfs-url")]
|
|
||||||
pub gtfs_url: String,
|
|
||||||
|
|
||||||
#[serde(rename = "realtime-url")]
|
|
||||||
pub realtime_url: String,
|
|
||||||
|
|
||||||
#[serde(rename = "realtime-api-key")]
|
|
||||||
pub realtime_api_key: String,
|
|
||||||
|
|
||||||
#[serde(rename = "refresh-seconds")]
|
|
||||||
pub refresh_seconds: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Utility functions
|
|
||||||
impl Preferences {
|
|
||||||
pub fn gtfs_file_name(&self) -> Result<String, Error> {
|
|
||||||
let file_name_part = self.gtfs_url.split("/").last();
|
|
||||||
if file_name_part.is_none() {
|
|
||||||
error!("The config for gtfs-url is not a valid URL: {}", self.gtfs_url);
|
|
||||||
return Err(Error {_message: String::from("Failed to refresh GTFS data")});
|
|
||||||
}
|
|
||||||
return Ok(String::from(file_name_part.unwrap()));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn gtfs_file_path(&self) -> Result<String, Error> {
|
|
||||||
return Ok(format!("{}/{}", self.data_folder, self.gtfs_file_name()?));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -94,7 +32,7 @@ pub struct Gtfs {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Arrival<'a> {
|
pub struct Arrival<'a> {
|
||||||
pub departure_time: u32,
|
pub departure_time: NaiveTime,
|
||||||
pub route: &'a Route,
|
pub route: &'a Route,
|
||||||
pub stop: &'a Stop,
|
pub stop: &'a Stop,
|
||||||
pub stop_time: &'a RawStopTime,
|
pub stop_time: &'a RawStopTime,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use crate::gtfs::Gtfs;
|
use crate::gtfs::Gtfs;
|
||||||
|
|
||||||
pub(crate) fn stop_ids_from_codes(gtfs: &Gtfs, stop_codes: &HashSet<String>) -> HashSet<String> {
|
pub fn stop_ids_from_codes(gtfs: &Gtfs, stop_codes: &HashSet<String>) -> HashSet<String> {
|
||||||
let mut ids: HashSet<String> = HashSet::new();
|
let mut ids: HashSet<String> = HashSet::new();
|
||||||
|
|
||||||
for stop in >fs.stops {
|
for stop in >fs.stops {
|
||||||
|
|
@ -13,7 +13,7 @@ pub(crate) fn stop_ids_from_codes(gtfs: &Gtfs, stop_codes: &HashSet<String>) ->
|
||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn route_ids_from_numbers(gtfs: &Gtfs, route_numbers: &HashSet<String>) -> HashSet<String> {
|
pub fn route_ids_from_numbers(gtfs: &Gtfs, route_numbers: &HashSet<String>) -> HashSet<String> {
|
||||||
let mut ids: HashSet<String> = HashSet::new();
|
let mut ids: HashSet<String> = HashSet::new();
|
||||||
|
|
||||||
for route in >fs.routes {
|
for route in >fs.routes {
|
||||||
|
|
|
||||||
70
src/main.rs
70
src/main.rs
|
|
@ -1,12 +1,12 @@
|
||||||
mod gtfs;
|
mod gtfs;
|
||||||
mod renderer;
|
mod renderer;
|
||||||
mod config;
|
use std::{collections::{HashSet, btree_map::Entry}, ops::Add, process, rc::Rc, thread::Builder, time::SystemTime};
|
||||||
use std::{ops::Add, process, thread::Builder, time::SystemTime};
|
|
||||||
use chrono::{DateTime, Duration, Local, NaiveTime};
|
use chrono::{DateTime, Duration, Local, NaiveTime};
|
||||||
use log::{Level, Metadata, Record, error, info};
|
use log::{Metadata, Record, debug, error, info};
|
||||||
use sdl3::event::Event;
|
use sdl3::event::Event;
|
||||||
use crate::{config::load_config, gtfs::structs::{Arrival, Gtfs}, renderer::structs::{DisplayData, DisplayEntry, Screen}};
|
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 NUM_ARRIVALS: usize = 4;
|
||||||
|
|
||||||
// Custom Event to signal data refresh
|
// Custom Event to signal data refresh
|
||||||
|
|
@ -15,41 +15,41 @@ struct RefreshDataEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn refresh_schedule<'a>(gtfs: &'a Gtfs, screen : &mut Screen<'a>) -> Option<Vec<Arrival<'a>>> {
|
fn refresh_schedule<'a>(gtfs: &'a Gtfs, screen: &mut Screen<'a>) -> Vec<Arrival<'a>> {
|
||||||
let current_timestamp = SystemTime::now();
|
let current_timestamp = SystemTime::now();
|
||||||
let datetime: DateTime<Local> = current_timestamp.clone().into();
|
let datetime: DateTime<Local> = current_timestamp.clone().into();
|
||||||
let mut next_arrivals: Vec<Arrival<'_>> = gtfs.get_next_arrivals_for(&datetime)?;
|
let mut next_arrivals: Vec<Arrival<'_>> = gtfs.get_next_arrivals_for(&datetime);
|
||||||
|
|
||||||
if next_arrivals.len() < NUM_ARRIVALS {
|
if next_arrivals.len() < NUM_ARRIVALS {
|
||||||
// If we don't have enough entries today, look for arrivals tomorrow.
|
// If we don't have enough entries today, look for arrivals tomorrow.
|
||||||
let mut tomorrow: DateTime<Local> = datetime.clone();
|
let mut tomorrow: DateTime<Local> = datetime.clone();
|
||||||
tomorrow = tomorrow.with_time(NaiveTime::from_hms_opt(0, 0, 0).unwrap()).unwrap();
|
tomorrow = tomorrow.with_time(NaiveTime::from_hms_opt(0, 0, 0).unwrap()).unwrap();
|
||||||
tomorrow = tomorrow.add(Duration::days(1));
|
tomorrow = tomorrow.add(Duration::days(1));
|
||||||
next_arrivals.append(&mut gtfs.get_next_arrivals_for(&tomorrow)?);
|
next_arrivals.append(&mut gtfs.get_next_arrivals_for(&tomorrow));
|
||||||
}
|
}
|
||||||
|
|
||||||
next_arrivals.sort();
|
next_arrivals.sort();
|
||||||
|
|
||||||
// Create the DisplayData structure to render the information to screen
|
// Create the DisplayData structure to render the information to screen
|
||||||
|
let current_time = Local::now().time();
|
||||||
let mut display_data: DisplayData = DisplayData {
|
let mut display_data: DisplayData = DisplayData {
|
||||||
lines: Vec::<DisplayEntry>::new(),
|
lines: Vec::<DisplayEntry>::new(),
|
||||||
status: None
|
status: None
|
||||||
};
|
};
|
||||||
|
|
||||||
display_data.lines.extend(next_arrivals.iter().map(|arrival| -> DisplayEntry {
|
display_data.lines.extend(next_arrivals.iter().map(|arrival| -> DisplayEntry {
|
||||||
|
|
||||||
DisplayEntry {
|
DisplayEntry {
|
||||||
destination: arrival.stop_time.stop_headsign.clone()
|
destination: arrival.stop_time.stop_headsign.clone()
|
||||||
.or(arrival.trip.trip_headsign.clone()
|
.or(arrival.trip.trip_headsign.clone()
|
||||||
.or(Option::Some(String::from("Unknown")
|
.or(Option::Some(String::from("Unknown")
|
||||||
))).unwrap(),
|
))).unwrap(),
|
||||||
route: arrival.route.short_name.clone().or(arrival.route.long_name.clone()).unwrap(),
|
route: arrival.route.short_name.clone().or(arrival.route.long_name.clone()).unwrap(),
|
||||||
departure_time: arrival.departure_time,
|
due_in: (arrival.departure_time - current_time).num_minutes().try_into().unwrap()
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
screen.update_information(&display_data);
|
screen.update_information(&display_data);
|
||||||
return Some(next_arrivals);
|
return next_arrivals;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -58,8 +58,8 @@ fn main() {
|
||||||
static MY_LOGGER: MyLogger = MyLogger;
|
static MY_LOGGER: MyLogger = MyLogger;
|
||||||
struct MyLogger;
|
struct MyLogger;
|
||||||
impl log::Log for MyLogger {
|
impl log::Log for MyLogger {
|
||||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
fn enabled(&self, _metadata: &Metadata) -> bool {
|
||||||
return metadata.level() < Level::Trace;
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log(&self, record: &Record) {
|
fn log(&self, record: &Record) {
|
||||||
|
|
@ -71,42 +71,28 @@ fn main() {
|
||||||
fn flush(&self) {}
|
fn flush(&self) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
let logger = log::set_logger(&MY_LOGGER);
|
log::set_logger(&MY_LOGGER).unwrap();
|
||||||
if logger.is_err() {
|
|
||||||
print!("Error setting up the main logger:{:#?}", logger.err());
|
|
||||||
process::exit(-1);
|
|
||||||
}
|
|
||||||
log::set_max_level(log::LevelFilter::Trace);
|
log::set_max_level(log::LevelFilter::Trace);
|
||||||
|
|
||||||
|
|
||||||
let config_result = load_config(String::from("resources/dublinbus.yaml"));
|
|
||||||
if config_result.is_err() {
|
|
||||||
error!("Error loading the config file: {:#?}", config_result.err());
|
|
||||||
process::exit(-1);
|
|
||||||
}
|
|
||||||
// Create preferences structures from config
|
// Create preferences structures from config
|
||||||
let config = config_result.unwrap();
|
let gtfs_prefs = gtfs::structs::Preferences {
|
||||||
let gtfs_prefs = config.gtfs_prefs;
|
route_numbers: HashSet::from([String::from("15A"), String::from("F1"), String::from("F2"), String::from("F3")]),
|
||||||
let screen_prefs = config.screen_prefs;
|
stop_codes: HashSet::from([String::from("1117")])
|
||||||
|
};
|
||||||
|
|
||||||
|
let screen_prefs = renderer::structs::Prefs {
|
||||||
|
font_path: String::from("resources/jd_lcd_rounded/jd-lcd-rounded.ttf"),
|
||||||
|
screen_width: 1920,
|
||||||
|
screen_height: 720,
|
||||||
|
};
|
||||||
|
|
||||||
// Init GTFS static info
|
// Init GTFS static info
|
||||||
info!("Loading GTFS data...");
|
info!("Loading GTFS data...");
|
||||||
let res = Gtfs::load(>fs_prefs);
|
let gtfs = Gtfs::load(SRC_FILE, >fs_prefs);
|
||||||
|
|
||||||
if let Err(e) = res {
|
|
||||||
error!("Error loading GTFS data: {:#?}", e);
|
|
||||||
process::exit(-1);
|
|
||||||
}
|
|
||||||
let gtfs = res.unwrap();
|
|
||||||
|
|
||||||
// Init screen
|
// Init screen
|
||||||
info!("Initializing screen...");
|
info!("Initializing screen...");
|
||||||
let screen_result = Screen::init(&screen_prefs);
|
let mut screen = Screen::init(&screen_prefs);
|
||||||
if let Err(e) = screen_result {
|
|
||||||
error!("Error initializing video: {}", e.message);
|
|
||||||
process::exit(-1);
|
|
||||||
}
|
|
||||||
let mut screen = screen_result.unwrap();
|
|
||||||
info!("Startup done.");
|
info!("Startup done.");
|
||||||
|
|
||||||
// Register our custom event and obtain the event-related objects to interact with the event loop
|
// Register our custom event and obtain the event-related objects to interact with the event loop
|
||||||
|
|
@ -119,7 +105,7 @@ fn main() {
|
||||||
.name("updater".to_string())
|
.name("updater".to_string())
|
||||||
.spawn(move || {
|
.spawn(move || {
|
||||||
loop {
|
loop {
|
||||||
std::thread::sleep(std::time::Duration::new(gtfs_prefs.refresh_seconds,0));
|
std::thread::sleep(std::time::Duration::new(60,0));
|
||||||
let event = RefreshDataEvent {};
|
let event = RefreshDataEvent {};
|
||||||
let send_result = event_sender.push_custom_event(event);
|
let send_result = event_sender.push_custom_event(event);
|
||||||
if send_result.is_err() {
|
if send_result.is_err() {
|
||||||
|
|
@ -147,7 +133,9 @@ fn main() {
|
||||||
// Is the custom event a Refresh Data event?
|
// Is the custom event a Refresh Data event?
|
||||||
let refresh_data = event.as_user_event_type::<RefreshDataEvent>();
|
let refresh_data = event.as_user_event_type::<RefreshDataEvent>();
|
||||||
if refresh_data.is_some() {
|
if refresh_data.is_some() {
|
||||||
let _data: Option<Vec<Arrival<'_>>> = refresh_schedule(>fs, &mut screen);
|
debug!("Received user event: {:#?}", refresh_data.unwrap());
|
||||||
|
let _data: Vec<Arrival<'_>> = refresh_schedule(>fs, &mut screen);
|
||||||
|
debug!("-------------------------------- Refresh done.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,20 @@
|
||||||
|
|
||||||
pub mod structs;
|
pub mod structs;
|
||||||
use std::{cmp::min};
|
use std::cmp::min;
|
||||||
use chrono::{Datelike, Local, Timelike};
|
|
||||||
use log::{error, trace, warn};
|
|
||||||
use sdl3::{Sdl, pixels::Color, rect::Rect};
|
use sdl3::{Sdl, pixels::Color, rect::Rect};
|
||||||
use structs::{Prefs, DisplayData};
|
use structs::{Prefs, DisplayData};
|
||||||
|
|
||||||
use crate::{config::Error, renderer::structs::Screen};
|
use crate::renderer::structs::Screen;
|
||||||
|
|
||||||
const LINE_COUNT: i32 = 5;
|
const LINE_COUNT: i32 = 6;
|
||||||
const COLOR_LCD_AMBER : Color = Color::RGB(0xf4, 0xcb, 0x60);
|
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_TEXT_BG : Color = Color::RGBA(0x0, 0x0, 0x0, 0x0);
|
//const COLOR_BACKGROUND = pygame.Color(0, 0, 0)
|
||||||
const TEXT_SIZE: f32 = 160.0; // Size of the font in pixels
|
const UPDATE_INTERVAL_SECONDS: u32 = 62;
|
||||||
|
const TEXT_SIZE: u32 = 160; // Size of the font in pixels
|
||||||
|
|
||||||
// Offsets of each part within a line
|
// Offsets of each part within a line
|
||||||
const XOFFSET_ROUTE: u32 = 24;
|
const XOFFSET_ROUTE: u32 = 24;
|
||||||
|
|
@ -21,6 +22,7 @@ const XOFFSET_DESTINATION: u32 = 300;
|
||||||
const XOFFSEET_TIME_LEFT: u32 = 1606;
|
const XOFFSEET_TIME_LEFT: u32 = 1606;
|
||||||
const INTER_LINE_OVERLAP: u32 = 15;
|
const INTER_LINE_OVERLAP: u32 = 15;
|
||||||
|
|
||||||
|
|
||||||
impl Screen<'_> {
|
impl Screen<'_> {
|
||||||
|
|
||||||
pub fn get_context(&self) -> &Sdl {
|
pub fn get_context(&self) -> &Sdl {
|
||||||
|
|
@ -33,88 +35,45 @@ impl Screen<'_> {
|
||||||
return COLOR_LCD_RED;
|
return COLOR_LCD_RED;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_due_for(&self, due_in_mins: i32, departure_time: u32) -> String {
|
|
||||||
trace!("Due in mins: {:02}", due_in_mins);
|
|
||||||
if due_in_mins <= 1 {
|
|
||||||
return String::from("due");
|
|
||||||
}
|
|
||||||
if due_in_mins < 60 {
|
|
||||||
return due_in_mins.to_string() + "min";
|
|
||||||
}
|
|
||||||
return format!("{:02}:{:02}", (departure_time / 3600) as i32, ((departure_time / 60) % 60) as i32);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn update_information(&mut self, display_data: &DisplayData) {
|
pub fn update_information(&mut self, display_data: &DisplayData) {
|
||||||
let local_time = Local::now();
|
|
||||||
let seconds_since_midnight = local_time.num_seconds_from_midnight();
|
|
||||||
|
|
||||||
self.do_clear();
|
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());
|
||||||
// Print status first
|
for line in 0..num_arrivals {
|
||||||
self.color = COLOR_LCD_AMBER;
|
|
||||||
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!("Current time: {:02}/{:02}/{:4} {:02}:{:02}",
|
|
||||||
local_time.day(), local_time.month(), local_time.year(),
|
|
||||||
local_time.hour(), local_time.minute()
|
|
||||||
).to_string(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then data lines from the bottom
|
|
||||||
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
|
// Compose a line of text with all the information
|
||||||
let entry = display_data.lines.get(line as usize).unwrap();
|
let entry = display_data.lines.get(line as usize).unwrap();
|
||||||
let due_in_mins = ((entry.departure_time - seconds_since_midnight) / 60) as i32;
|
let line: u32 = line.try_into().unwrap();
|
||||||
let due_color: Color = self.color_for(due_in_mins);
|
let due_in_mins = (entry.due_in / 60) as i32;
|
||||||
let due_text = self.format_due_for(due_in_mins, entry.departure_time);
|
let arrival_color: Color = self.color_for(due_in_mins);
|
||||||
|
|
||||||
self.color = COLOR_LCD_AMBER;
|
self.color = COLOR_LCD_AMBER;
|
||||||
self.do_print_at(line, &entry.route, XOFFSET_ROUTE);
|
self.do_print_at(line, &entry.route, XOFFSET_ROUTE);
|
||||||
self.do_print_at(line, &entry.destination, XOFFSET_DESTINATION);
|
self.do_print_at(line, &entry.destination, XOFFSET_DESTINATION);
|
||||||
self.color = due_color;
|
self.color = arrival_color;
|
||||||
self.do_print_at(line, &due_text, XOFFSEET_TIME_LEFT);
|
self.do_print_at(line, &due_in_mins.to_string(), 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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_print_at(line, text, 0);
|
||||||
self.do_update();
|
self.do_update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn do_print_at(&mut self, line: u32, text: &str, left: u32) {
|
fn do_print_at(&mut self, line: u32, text: &str, left: u32) -> u32 {
|
||||||
if text.len() == 0 {
|
let rendered_text = self.font.render(text).solid(self.color).unwrap();
|
||||||
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);
|
let texture = rendered_text.as_texture(&texture_creator).unwrap();
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -130,16 +89,16 @@ impl Screen<'_> {
|
||||||
|
|
||||||
|
|
||||||
fn do_clear(&mut self) {
|
fn do_clear(&mut self) {
|
||||||
self.canvas.set_draw_color(COLOR_BACKGROUND);
|
self.canvas.set_draw_color(Color::BLACK);
|
||||||
self.canvas.clear();
|
self.canvas.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize video, allocate buffers and load fonts
|
/// Initialize video, allocate buffers and load fonts
|
||||||
/// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs
|
/// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs
|
||||||
pub fn init(prefs: &Prefs) -> Result<Screen<'_>, Error> {
|
pub fn init(prefs: &Prefs) -> Screen<'_> {
|
||||||
// Initialize the screen
|
// Initialize the screen
|
||||||
let sdl_context = sdl3::init().unwrap();
|
let sdl_context = sdl3::init().unwrap();
|
||||||
let video_subsys = sdl_context.video()?;
|
let video_subsys = sdl_context.video().unwrap();
|
||||||
|
|
||||||
let window = video_subsys
|
let window = video_subsys
|
||||||
.window("Dublin Bus", prefs.screen_width, prefs.screen_height)
|
.window("Dublin Bus", prefs.screen_width, prefs.screen_height)
|
||||||
|
|
@ -150,8 +109,8 @@ impl Screen<'_> {
|
||||||
|
|
||||||
// Load font
|
// Load font
|
||||||
let ttf_context = sdl3::ttf::init().unwrap();
|
let ttf_context = sdl3::ttf::init().unwrap();
|
||||||
let font = ttf_context.load_font(&prefs.font_path, TEXT_SIZE)?;
|
let font = ttf_context.load_font(&prefs.font_path, 128.0).unwrap();
|
||||||
|
|
||||||
let mut screen: Screen = Screen {
|
let mut screen: Screen = Screen {
|
||||||
canvas: Box::new(window.into_canvas()),
|
canvas: Box::new(window.into_canvas()),
|
||||||
font: Box::new(font),
|
font: Box::new(font),
|
||||||
|
|
@ -160,6 +119,6 @@ impl Screen<'_> {
|
||||||
};
|
};
|
||||||
|
|
||||||
screen.clear();
|
screen.clear();
|
||||||
return Ok(screen);
|
return screen;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use sdl3::{Sdl, pixels::Color, render::Canvas, ttf::Font, video::Window};
|
use sdl3::{Sdl, pixels::Color, render::Canvas, ttf::Font, video::Window};
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
|
|
||||||
pub struct Screen<'a> {
|
pub struct Screen<'a> {
|
||||||
pub(crate) canvas: Box<Canvas<Window>>,
|
pub(crate) canvas: Box<Canvas<Window>>,
|
||||||
|
|
@ -8,16 +8,9 @@ pub struct Screen<'a> {
|
||||||
pub(crate) context: Box<Sdl>
|
pub(crate) context: Box<Sdl>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
||||||
pub struct Prefs {
|
pub struct Prefs {
|
||||||
|
|
||||||
#[serde(rename = "font-path")]
|
|
||||||
pub font_path: String,
|
pub font_path: String,
|
||||||
|
|
||||||
#[serde(rename = "width")]
|
|
||||||
pub screen_width: u32,
|
pub screen_width: u32,
|
||||||
|
|
||||||
#[serde(rename = "height")]
|
|
||||||
pub screen_height: u32,
|
pub screen_height: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -25,7 +18,7 @@ pub struct Prefs {
|
||||||
pub struct DisplayEntry {
|
pub struct DisplayEntry {
|
||||||
pub route: String,
|
pub route: String,
|
||||||
pub destination: String,
|
pub destination: String,
|
||||||
pub departure_time: u32,
|
pub due_in: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DisplayData {
|
pub struct DisplayData {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue