Struct CustomDriver

Source
pub struct CustomDriver<INSTANT, COLOR>
where INSTANT: Instant, COLOR: ColorType,
{ /* 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>
where INSTANT: Instant, COLOR: ColorType,

Source

pub fn new() -> Self

Source

pub fn sled(&self) -> Option<&Sled<COLOR>>

Returns Some(&Sled) if the Driver has been mounted, None if it hasn’t.

Source

pub fn elapsed(&self) -> Duration

Returns a duration representing how long it has been since the Driver was initially mounted.

Source

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);
}
Source

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);
}
Source

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);
}
Source

pub fn mount(&mut self, sled: Sled<COLOR>)

Takes ownership of the given Sled and runs the Driver’s startup commands.

Source

pub fn step(&mut self)

Runs the Driver’s compute commands first, and then runs its draw commands.

Source

pub fn step_by(&mut self, delta: Duration)

Source

pub fn dismount(&mut self) -> Sled<COLOR>

Returns full ownership over the Driver’s assigned Sled. Panics if Driver::mount() was never called.

Source

pub fn leds(&self) -> impl Iterator<Item = &Led<COLOR>>

Source

pub fn colors(&self) -> impl Iterator<Item = &COLOR> + '_

Source

pub fn positions(&self) -> impl Iterator<Item = Vec2> + '_

Source

pub fn colors_and_positions(&self) -> impl Iterator<Item = (COLOR, Vec2)> + '_

Source

pub fn data(&self) -> &Data

Returns a reference to the Driver’s Data. Helpful for displaying data values to the program user.

Source

pub fn data_mut(&mut self) -> &mut Data

Returns a mutable reference to the Driver’s Data. Helpful for changing data values as the user provides input to the program.

Trait Implementations§

Source§

impl<INSTANT, COLOR> Default for CustomDriver<INSTANT, COLOR>
where INSTANT: Instant, COLOR: ColorType,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<INSTANT, COLOR> Freeze for CustomDriver<INSTANT, COLOR>
where INSTANT: Freeze,

§

impl<INSTANT, COLOR> !RefUnwindSafe for CustomDriver<INSTANT, COLOR>

§

impl<INSTANT, COLOR> !Send for CustomDriver<INSTANT, COLOR>

§

impl<INSTANT, COLOR> !Sync for CustomDriver<INSTANT, COLOR>

§

impl<INSTANT, COLOR> Unpin for CustomDriver<INSTANT, COLOR>
where INSTANT: Unpin, COLOR: Unpin,

§

impl<INSTANT, COLOR> !UnwindSafe for CustomDriver<INSTANT, COLOR>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.