diff --git a/Cargo.lock b/Cargo.lock index 8d0fc71..417830e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3743,6 +3743,7 @@ checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" name = "simulator" version = "0.1.0" dependencies = [ + "async-std", "cairo-rs", "fixed", "gio", diff --git a/bike-lights/simulator/Cargo.toml b/bike-lights/simulator/Cargo.toml index 42787ae..8a5c8e2 100644 --- a/bike-lights/simulator/Cargo.toml +++ b/bike-lights/simulator/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] adw = { version = "0.5", package = "libadwaita", features = [ "v1_2" ] } +async-std = "1.13.0" cairo-rs = { version = "0.18" } fixed = { version = "1" } gio = { version = "0.18" } diff --git a/bike-lights/simulator/src/main.rs b/bike-lights/simulator/src/main.rs index a1da0ac..bc66530 100644 --- a/bike-lights/simulator/src/main.rs +++ b/bike-lights/simulator/src/main.rs @@ -1,6 +1,7 @@ use adw::prelude::*; +use async_std::channel::Sender; use fixed::types::{I8F8, U128F0}; -use glib::{Object, Sender}; +use glib::Object; use gtk::subclass::prelude::*; use lights_core::{ App, BodyPattern, DashboardPattern, Event, Instant, FPS, OFF_BODY, OFF_DASHBOARD, RGB, UI, @@ -173,12 +174,15 @@ impl UI for GTKUI { } fn update_lights(&self, dashboard_lights: DashboardPattern, lights: BodyPattern) { - self.tx - .send(Update { - dashboard: dashboard_lights, - lights, - }) - .unwrap(); + let tx = self.tx.clone(); + glib::spawn_future(async move { + let _ = tx + .send(Update { + dashboard: dashboard_lights, + lights, + }) + .await; + }); } } @@ -188,8 +192,7 @@ fn main() { .build(); adw_app.connect_activate(move |adw_app| { - let (update_tx, update_rx) = - gtk::glib::MainContext::channel::(gtk::glib::Priority::DEFAULT); + let (update_tx, update_rx) = async_std::channel::unbounded(); let (event_tx, event_rx) = std::sync::mpsc::channel(); std::thread::spawn(move || { @@ -281,6 +284,17 @@ fn main() { layout.append(&dashboard_lights); layout.append(&bike_lights); + glib::spawn_future_local({ + let dashboard_lights = dashboard_lights.clone(); + let bike_lights = bike_lights.clone(); + async move { + while let Ok(Update { dashboard, lights }) = update_rx.recv().await { + dashboard_lights.set_lights(dashboard); + bike_lights.set_lights(lights); + } + } + }); + /* update_rx.attach(None, { let dashboard_lights = dashboard_lights.clone(); let bike_lights = bike_lights.clone(); @@ -290,6 +304,7 @@ fn main() { glib::ControlFlow::Continue } }); + */ window.set_content(Some(&layout)); window.present();