Remove the legacy glib channel

This commit is contained in:
Savanni D'Gerinel 2024-11-08 09:51:53 -05:00
parent 288cecc92f
commit f4d990546b
5 changed files with 537 additions and 501 deletions

896
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -32,5 +32,5 @@ members = [
"sgf", "sgf",
"timezone-testing", "timezone-testing",
"tree", "tree",
"visions/server", "gm-dash/server", "halloween-leds" "visions/server", "gm-dash/server"
] ]

View File

@ -0,0 +1,95 @@
use fixed::types::{I16F0, I8F0, I8F8, U16F0, U8F0};
use az::*;
use crate::{
calculate_frames, calculate_slope, linear_ease, sin, Animation, BodyPattern, DashboardPattern, Instant, OFF_BODY, OFF_DASHBOARD, RGB, WATER_1, WATER_BODY
};
const DROPLET_DIMMING: u8 = 30;
pub struct Ripple {
dashboard: DashboardPattern,
base: BodyPattern,
focii: [usize; 2],
dimming: I8F8,
slope: [RGB<I8F8>; 2],
pub start_time: Instant,
}
impl Ripple {
pub fn new(dashboard: DashboardPattern, base: BodyPattern, time: Instant) -> Self {
let mut slope = [Default::default(); 2];
let dimming = I8F8::lit("0.5");
let focii = [15, 45];
for (idx, focus) in focii.iter().enumerate() {
slope[idx] = RGB {
r: calculate_slope(
base[*focus].r,
base[*focus].r * dimming,
U16F0::from(DROPLET_DIMMING),
),
g: calculate_slope(
base[*focus].g,
base[*focus].g * dimming,
U16F0::from(DROPLET_DIMMING),
),
b: calculate_slope(
base[*focus].b,
base[*focus].b * dimming,
U16F0::from(DROPLET_DIMMING),
),
};
}
Self {
dashboard,
base,
focii,
dimming,
slope,
start_time: time,
}
}
fn pixel(&self, pos: U8F0, frames: U16F0) -> RGB<I8F8> {
let focus: I16F0 = if pos <= 30 { I16F0::from(15 as i16) } else { I16F0::from(45 as i16) };
let distance_from_focus: I8F0 = (focus - I16F0::from(pos)).saturating_as();
let pos: usize = pos.into();
/*
let target = RGB {
r: WATER_BODY[pos].r * self.dimming,
g: WATER_BODY[pos].g * self.dimming,
b: WATER_BODY[pos].b * self.dimming,
};
let slope = RGB{
r: calculate_slope(WATER_BODY[pos].r, target.r, U16F0::from(DROPLET_DIMMING)),
g: calculate_slope(WATER_BODY[pos].g, target.g, U16F0::from(DROPLET_DIMMING)),
b: calculate_slope(WATER_BODY[pos].b, target.b, U16F0::from(DROPLET_DIMMING)),
};
*/
RGB {
r: WATER_BODY[pos].r + sin(frames * 5) * I8F8::from_num(0.5),
g: WATER_BODY[pos].g + sin(frames * 6) * I8F8::from_num(0.5),
b: WATER_BODY[pos].b + sin(frames * 7) * I8F8::from_num(0.5),
}
}
}
impl Animation for Ripple {
fn tick(&mut self, time: Instant) -> (DashboardPattern, BodyPattern) {
let frames = calculate_frames(self.start_time.0, time.0);
let mut body_pattern: BodyPattern = OFF_BODY;
for i in 0..60 {
body_pattern[i] = self.pixel(U8F0::from(i as u8), frames);
}
(self.dashboard.clone(), body_pattern)
}
}

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
adw = { version = "0.5", package = "libadwaita", features = [ "v1_2" ] } adw = { version = "0.5", package = "libadwaita", features = [ "v1_2" ] }
async-channel = "2.3.1"
cairo-rs = { version = "0.18" } cairo-rs = { version = "0.18" }
fixed = { version = "1" } fixed = { version = "1" }
gio = { version = "0.18" } gio = { version = "0.18" }

View File

@ -1,16 +1,13 @@
use std::{cell::RefCell, env, rc::Rc};
use adw::prelude::*; use adw::prelude::*;
use async_channel::{unbounded, Receiver, Sender, TryRecvError};
use fixed::types::{I8F8, U128F0}; use fixed::types::{I8F8, U128F0};
use glib::{Object, Sender}; use glib::Object;
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
use lights_core::{ use lights_core::{
App, BodyPattern, DashboardPattern, Event, Instant, FPS, OFF_BODY, OFF_DASHBOARD, RGB, UI, App, BodyPattern, DashboardPattern, Event, Instant, FPS, OFF_BODY, OFF_DASHBOARD, RGB, UI,
}; };
use std::{
cell::RefCell,
env,
rc::Rc,
sync::mpsc::{Receiver, TryRecvError},
};
const WIDTH: i32 = 640; const WIDTH: i32 = 640;
const HEIGHT: i32 = 480; const HEIGHT: i32 = 480;
@ -156,13 +153,13 @@ impl UI for GTKUI {
match self.rx.try_recv() { match self.rx.try_recv() {
Ok(event) => Some(event), Ok(event) => Some(event),
Err(TryRecvError::Empty) => None, Err(TryRecvError::Empty) => None,
Err(TryRecvError::Disconnected) => None, Err(TryRecvError::Closed) => None,
} }
} }
fn update_lights(&self, dashboard_lights: DashboardPattern, lights: BodyPattern) { fn update_lights(&self, dashboard_lights: DashboardPattern, lights: BodyPattern) {
self.tx self.tx
.send(Update { .send_blocking(Update {
dashboard: dashboard_lights, dashboard: dashboard_lights,
lights, lights,
}) })
@ -176,9 +173,8 @@ fn main() {
.build(); .build();
adw_app.connect_activate(move |adw_app| { adw_app.connect_activate(move |adw_app| {
let (update_tx, update_rx) = let (update_tx, update_rx) = unbounded();
gtk::glib::MainContext::channel::<Update>(gtk::glib::Priority::DEFAULT); let (event_tx, event_rx) = unbounded();
let (event_tx, event_rx) = std::sync::mpsc::channel();
std::thread::spawn(move || { std::thread::spawn(move || {
let mut bike_app = App::new(Box::new(GTKUI { let mut bike_app = App::new(Box::new(GTKUI {
@ -218,21 +214,21 @@ fn main() {
left_button.connect_clicked({ left_button.connect_clicked({
let event_tx = event_tx.clone(); let event_tx = event_tx.clone();
move |_| { move |_| {
let _ = event_tx.send(Event::LeftBlinker); let _ = event_tx.send_blocking(Event::LeftBlinker);
} }
}); });
brake_button.connect_clicked({ brake_button.connect_clicked({
let event_tx = event_tx.clone(); let event_tx = event_tx.clone();
move |_| { move |_| {
let _ = event_tx.send(Event::Brake); let _ = event_tx.send_blocking(Event::Brake);
} }
}); });
right_button.connect_clicked({ right_button.connect_clicked({
let event_tx = event_tx.clone(); let event_tx = event_tx.clone();
move |_| { move |_| {
let _ = event_tx.send(Event::RightBlinker); let _ = event_tx.send_blocking(Event::RightBlinker);
} }
}); });
@ -251,14 +247,14 @@ fn main() {
previous_pattern.connect_clicked({ previous_pattern.connect_clicked({
let event_tx = event_tx.clone(); let event_tx = event_tx.clone();
move |_| { move |_| {
let _ = event_tx.send(Event::PreviousPattern); let _ = event_tx.send_blocking(Event::PreviousPattern);
} }
}); });
next_pattern.connect_clicked({ next_pattern.connect_clicked({
let event_tx = event_tx.clone(); let event_tx = event_tx.clone();
move |_| { move |_| {
let _ = event_tx.send(Event::NextPattern); let _ = event_tx.send_blocking(Event::NextPattern);
} }
}); });
@ -269,6 +265,7 @@ fn main() {
layout.append(&dashboard_lights); layout.append(&dashboard_lights);
layout.append(&bike_lights); layout.append(&bike_lights);
/*
update_rx.attach(None, { update_rx.attach(None, {
let dashboard_lights = dashboard_lights.clone(); let dashboard_lights = dashboard_lights.clone();
let bike_lights = bike_lights.clone(); let bike_lights = bike_lights.clone();
@ -278,6 +275,19 @@ fn main() {
glib::ControlFlow::Continue glib::ControlFlow::Continue
} }
}); });
*/
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);
}
}
});
window.set_content(Some(&layout)); window.set_content(Some(&layout));
window.present(); window.present();