pub struct CustomDriver<INSTANT, COLOR>{ /* private fields */ }
Expand description
Drivers are useful for encapsulating everything you need to drive a complicated lighting effect all in one place.
Implementations§
Source§impl<INSTANT, COLOR> CustomDriver<INSTANT, COLOR>
impl<INSTANT, COLOR> CustomDriver<INSTANT, COLOR>
pub fn new() -> Self
Sourcepub fn sled(&self) -> Option<&Sled<COLOR>>
pub fn sled(&self) -> Option<&Sled<COLOR>>
Returns Some(&Sled)
if the Driver has been mounted, None
if it hasn’t.
Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns a duration representing how long it has been since the Driver was initially mounted.
Sourcepub fn set_startup_commands<F: Fn(&mut Sled<COLOR>, &mut Data) -> Result<(), SledError> + 'static>(
&mut self,
startup_commands: F,
)
pub fn set_startup_commands<F: Fn(&mut Sled<COLOR>, &mut Data) -> Result<(), SledError> + 'static>( &mut self, startup_commands: F, )
Define commands to be called as soon as a Sled is mounted to the driver. This is a good place to initialize important data.
fn startup(_: &mut Sled<Rgb>, data: &mut Data) -> SledResult {
let streak_positions = vec![
Vec2::new(-1.2, 0.3),
Vec2::new(0.9, 1.6),
Vec2::new(0.4, -2.3),
];
data.set("positions", streak_positions);
Ok(())
}
pub fn main() {
let mut driver = Driver::<Rgb>::new();
driver.set_startup_commands(startup);
}
Sourcepub fn set_compute_commands<F: Fn(&Sled<COLOR>, &mut Data, &Time) -> Result<(), SledError> + 'static>(
&mut self,
compute_commands: F,
)
pub fn set_compute_commands<F: Fn(&Sled<COLOR>, &mut Data, &Time) -> Result<(), SledError> + 'static>( &mut self, compute_commands: F, )
Define commands to be called each time CustomDriver::step() is called, right before we run draw commands.
const WIND: Vec2 = Vec2::new(0.25, 1.5);
fn compute(_: &Sled<Rgb>, data: &mut Data, time: &Time) -> SledResult {
let streak_positions: &mut Vec<Vec2> = data.get_mut("positions")?;
let elapsed = time.elapsed.as_secs_f32();
for p in streak_positions {
*p += WIND * elapsed
}
Ok(())
}
pub fn main() {
let mut driver = Driver::<Rgb>::new();
driver.set_compute_commands(compute);
}
Sourcepub fn set_draw_commands<F: Fn(&mut Sled<COLOR>, &Data, &Time) -> Result<(), SledError> + 'static>(
&mut self,
draw_commands: F,
)
pub fn set_draw_commands<F: Fn(&mut Sled<COLOR>, &Data, &Time) -> Result<(), SledError> + 'static>( &mut self, draw_commands: F, )
Define commands to be called each time CustomDriver::step() is called, right after we run compute commands.
fn draw(sled: &mut Sled<Rgb>, data: &Data, _:&Time) -> SledResult {
// gradually fade all LEDs to black
sled.map(|led| led.color * 0.95);
// For each position in our vector, draw white in the direction to it.
let streak_positions = data.get::<Vec<Vec2>>("positions")?;
let center = sled.center_point();
for pos in streak_positions {
let dir = (pos - center).normalize();
sled.set_at_dir(dir, Rgb::new(1.0, 1.0, 1.0));
}
Ok(())
}
pub fn main() {
let mut driver = Driver::new();
driver.set_draw_commands(draw);
}
Sourcepub fn mount(&mut self, sled: Sled<COLOR>)
pub fn mount(&mut self, sled: Sled<COLOR>)
Takes ownership of the given Sled and runs the Driver’s startup commands.
Sourcepub fn step(&mut self)
pub fn step(&mut self)
Runs the Driver’s compute commands first, and then runs its draw commands.
pub fn step_by(&mut self, delta: Duration)
Sourcepub fn dismount(&mut self) -> Sled<COLOR>
pub fn dismount(&mut self) -> Sled<COLOR>
Returns full ownership over the Driver’s assigned Sled. Panics if Driver::mount() was never called.
Sourcepub fn leds(&self) -> impl Iterator<Item = &Led<COLOR>>
pub fn leds(&self) -> impl Iterator<Item = &Led<COLOR>>
See Sled::leds().
Sourcepub fn colors(&self) -> impl Iterator<Item = &COLOR> + '_
pub fn colors(&self) -> impl Iterator<Item = &COLOR> + '_
See Sled::colors().
Sourcepub fn positions(&self) -> impl Iterator<Item = Vec2> + '_
pub fn positions(&self) -> impl Iterator<Item = Vec2> + '_
See Sled::positions().