src: implement simulation data sources

This commit is contained in:
2025-09-24 08:32:34 +02:00
parent 19875f6ae5
commit 0cd2cc94b9
32 changed files with 389785 additions and 284 deletions

View File

@@ -1,26 +1,26 @@
use figments::{hardware::{Brightness, Gamma}, mappings::linear::LinearSpace, power::AsMilliwatts, prelude::*, smart_leds::BrightnessWriter};
use figments::{hardware::{Brightness, Gamma, OutputAsync}, mappings::linear::LinearSpace, power::AsMilliwatts, prelude::*, smart_leds::{BrightnessWriter, BrightnessWriterAsync}};
use core::{fmt::Debug, ops::IndexMut};
//use std::io::Write;
//use super::{Output};
use figments::hardware::Output;
use smart_leds::SmartLedsWriteAsync;
use smart_leds::{SmartLedsWrite, SmartLedsWriteAsync};
pub trait LinearPixbuf: Pixbuf + Sample<'static, LinearSpace, Output = Self::Format> + IndexMut<usize, Output = Self::Format> + AsRef<[Self::Format]> {}
impl<T> LinearPixbuf for T where T: Pixbuf + Sample<'static, LinearSpace, Output = Self::Format> + IndexMut<usize, Output = Self::Format> + AsRef<[Self::Format]> {}
pub struct BikeOutput<T: SmartLedsWriteAsync> {
pub struct BikeOutput<T: SmartLedsWrite> {
pixbuf: [T::Color; 178],
writer: BrightnessWriter<T>
}
impl<T: SmartLedsWriteAsync> Debug for BikeOutput<T> {
impl<T: SmartLedsWrite> Debug for BikeOutput<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BikeOutput").field("pixbuf", &core::any::type_name_of_val(&self.pixbuf)).field("writer", &core::any::type_name_of_val(&self.writer)).finish()
}
}
impl<T: SmartLedsWriteAsync> BikeOutput<T> where T::Color: HardwarePixel + Gamma + Fract8Ops, T::Error: core::fmt::Debug {
impl<T: SmartLedsWrite> BikeOutput<T> where T::Color: HardwarePixel + Gamma + Fract8Ops, T::Error: core::fmt::Debug {
pub fn new(target: T, max_mw: u32) -> Self {
Self {
pixbuf: [Default::default(); 178],
@@ -29,7 +29,7 @@ impl<T: SmartLedsWriteAsync> BikeOutput<T> where T::Color: HardwarePixel + Gamma
}
}
impl<T: SmartLedsWriteAsync> Brightness for BikeOutput<T> where T::Color: HardwarePixel + Gamma + Fract8Ops, T::Error: core::fmt::Debug {
impl<T: SmartLedsWrite> Brightness for BikeOutput<T> where T::Color: HardwarePixel + Gamma + Fract8Ops, T::Error: core::fmt::Debug {
fn set_brightness(&mut self, brightness: u8) {
self.writer.set_brightness(brightness);
}
@@ -43,13 +43,78 @@ impl<T: SmartLedsWriteAsync> Brightness for BikeOutput<T> where T::Color: Hardwa
}
}
impl<'a, T: SmartLedsWriteAsync + 'a> Output<'a, BikeSpace> for BikeOutput<T> where T::Color: 'static + HardwarePixel + Gamma + Fract8Ops, [T::Color; 178]: AsMilliwatts + Gamma + Copy, T::Error: core::fmt::Debug {
async fn blank(&mut self) -> Result<(), Self::Error> {
impl<'a, T: SmartLedsWrite + 'a> Output<'a, BikeSpace> for BikeOutput<T> where T::Color: 'static + HardwarePixel + Gamma + Fract8Ops, [T::Color; 178]: AsMilliwatts + Gamma + Copy, T::Error: core::fmt::Debug {
fn blank(&mut self) -> Result<(), Self::Error> {
self.pixbuf.blank();
Ok(())
}
async fn commit(&mut self) -> Result<(), Self::Error> {
fn commit(&mut self) -> Result<(), Self::Error> {
self.writer.write(&self.pixbuf)
}
type HardwarePixel = T::Color;
type Error = T::Error;
}
impl<'a, T: SmartLedsWrite> Sample<'a, BikeSpace> for BikeOutput<T> where T::Color: HardwarePixel + Gamma + Fract8Ops, [T::Color; 178]: AsMilliwatts + 'static {
type Output = T::Color;
fn sample(&mut self, rect: &Rectangle<BikeSpace>) -> impl Iterator<Item = (Coordinates<BikeSpace>, &'a mut Self::Output)> {
let bufref = unsafe {
&mut *(&mut self.pixbuf as *mut [T::Color; 178])
};
BikeIter {
pixbuf: bufref,
cur: rect.top_left,
end: rect.bottom_right
}
}
}
// ASYNC
pub struct BikeOutputAsync<T: SmartLedsWriteAsync> {
pixbuf: [T::Color; 178],
writer: BrightnessWriterAsync<T>
}
impl<T: SmartLedsWriteAsync> Debug for BikeOutputAsync<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BikeOutput").field("pixbuf", &core::any::type_name_of_val(&self.pixbuf)).field("writer", &core::any::type_name_of_val(&self.writer)).finish()
}
}
impl<T: SmartLedsWriteAsync> BikeOutputAsync<T> where T::Color: HardwarePixel + Gamma + Fract8Ops, T::Error: core::fmt::Debug {
pub fn new(target: T, max_mw: u32) -> Self {
Self {
pixbuf: [Default::default(); 178],
writer: BrightnessWriterAsync::new(target, max_mw)
}
}
}
impl<T: SmartLedsWriteAsync> Brightness for BikeOutputAsync<T> where T::Color: HardwarePixel + Gamma + Fract8Ops, T::Error: core::fmt::Debug {
fn set_brightness(&mut self, brightness: u8) {
self.writer.set_brightness(brightness);
}
fn set_on(&mut self, is_on: bool) {
self.writer.set_on(is_on);
}
fn set_gamma(&mut self, gamma: f32) {
self.writer.set_gamma(gamma);
}
}
impl<'a, T: SmartLedsWriteAsync + 'a> OutputAsync<'a, BikeSpace> for BikeOutputAsync<T> where T::Color: 'static + HardwarePixel + Gamma + Fract8Ops, [T::Color; 178]: AsMilliwatts + Gamma + Copy, T::Error: core::fmt::Debug {
async fn blank_async(&mut self) -> Result<(), Self::Error> {
self.pixbuf.blank();
Ok(())
}
async fn commit_async(&mut self) -> Result<(), Self::Error> {
self.writer.write(&self.pixbuf).await
}
@@ -57,7 +122,7 @@ impl<'a, T: SmartLedsWriteAsync + 'a> Output<'a, BikeSpace> for BikeOutput<T> wh
type Error = T::Error;
}
impl<'a, T: SmartLedsWriteAsync> Sample<'a, BikeSpace> for BikeOutput<T> where T::Color: HardwarePixel + Gamma + Fract8Ops, [T::Color; 178]: AsMilliwatts + 'static {
impl<'a, T: SmartLedsWriteAsync> Sample<'a, BikeSpace> for BikeOutputAsync<T> where T::Color: HardwarePixel + Gamma + Fract8Ops, [T::Color; 178]: AsMilliwatts + 'static {
type Output = T::Color;
fn sample(&mut self, rect: &Rectangle<BikeSpace>) -> impl Iterator<Item = (Coordinates<BikeSpace>, &'a mut Self::Output)> {