Sled

Struct Sled 

Source
pub struct Sled<COLOR: ColorType> { /* private fields */ }
Expand description

A struct representing the layout of some LED configuration in 2D space, composed of line segments.

Sled structs are constructed from a .toml file that describe this layout. Upon construction, key information like the indices of vertices or the angle from each led from the center_point is precalculated and cached for faster access later. Sled takes a generic type parameter COLOR to define how you want to store color information. Whatever you set this to, all draw methods will expect to see. Documentation examples uses palette’s palette’s Rgb struct struct, but you can use any data type that implements Debug, Default, and Copy.

#[derive(Debug)]
struct RGBW {
    r: f32,
    g: f32,
    b: f32,
    w: f32
}
let mut u8_sled = Sled::<(u8, u8, u8)>::new("/path/to/config.yap")?;
let mut rgbw_sled = Sled::<RGBW>::new("/path/to/config.yap")?;

u8_sled.set(4, (255, 0, 0))?; // set 5th led to red
rgbw_sled.set_all(RGBW {
    r: 0.0,
    g: 1.0,
    b: 0.0,
    w: 0.0
});

Implementations§

Source§

impl<COLOR: ColorType> Sled<COLOR>

§Construction, output, and basic sled info

Source

pub fn new(config_file_path: &str) -> Result<Self, SledError>

Constructs a new Sled struct given the path to a config file. This is an expensive operation as many values are pre-calculated on construction (i.e, distances/angles from each LED to the center).

Example file:

center: (0.0, 0.5)
density: 30.0
--segments--
(-2, 0) --> (0.5, -1) --> (3.5, 0) -->
(2, 2) -->
  • center is a 2D reference point you can use to speed up draw calls. At initialization, directions, distances, etc relative to this point are pre-calculated for each Led.
  • density represents how many LEDs per unit we can expect for the line segments below.
  • (x, y) --> (x, y) Indicates a line segment spanning between those two connected vertices. If you wish to introduce a break between vertices, you must replace one of the --> separators with a |. Like this:
    --segments--
    (-2, 0) --> (0.5, -1) --> (3.5, 0) |
    (2, 2) --> (-2, 2) --> (-2, 0)
    Whitespace and linebreaks are generally irrelevant in formatting segments, meaning the above is functionally equivalent to:
    --segments--
        (-2, 0) --> (0.5, -1)
    --> (3.5, 0) | (2, 2)
    --> (-2, 2) --> (-2, 0)
Source

pub fn new_from_str(string: &str) -> Result<Self, SledError>

Works like Sled::new() but rather than reading the contents of a config file from disk, allows you to pass in the same information as a &str.

Source

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

Returns a read-only iterator over the system’s LEDs.

If you need owned copies of these values, .collect() this iterator into a Vector.

O(LEDS)

for led in sled.leds() {
   println!("Segment {}, Index {}: {:?}",
       led.segment(), led.index(), led.color
   );
}
Source

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

Returns an Iterator over the 32-bit RGB colors for each LED in the system

O(LEDS)

let colors = sled.colors();

for color in colors {
    let red: f32 = color.red;
    /*- snip -*/
}
Source

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

Returns an Iterator over Vec2s, representing the position of each LED in the system.

O(LEDS)

Source

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

Returns an Iterator over tuple pairs of the color and position of each LED in the system.

O(LEDS)

Source

pub fn center_point(&self) -> Vec2

Returns the static reference point declared in the config file.

O(1)

Source

pub fn num_leds(&self) -> usize

Returns the total number of LEDs in the system.

O(1)

Source

pub fn num_segments(&self) -> usize

Returns the total number of line segments in the system.

O(1)

Source

pub fn num_vertices(&self) -> usize

Returns the total number of vertices in the system.

Touching endpoints are merged into one vertex, meaning that a configuration of two line segments that meet at one point to form a corner would have three vertices, rather than four.

O(1)

Source

pub fn domain(&self) -> Range<Vec2>

Returns a bounding box around the LEDs where the minimum x and y position is Range::start, maximum x and y is Range::end.

O(1)

Source§

impl<COLOR: ColorType> Sled<COLOR>

§Index-based read and write methods.

Source

pub fn get(&self, index: usize) -> Option<&Led<COLOR>>

Returns Some(&Led<COLOR>) if an LED at index exists, None if not.

O(1)

Source

pub fn modulate<F: Fn(&Led<COLOR>) -> COLOR>( &mut self, index: usize, color_rule: F, ) -> Result<(), SledError>

Modulates the color of the LED at index given a color rule function. Returns an error if no LED exists at that index.

O(1)

sled.modulate(0,
    |led| led.color + Rgb::new(0.5, 0.0, 0.0)
)?;
Source

pub fn set(&mut self, index: usize, color: COLOR) -> Result<(), SledError>

Set the color of the LED at index to color. Returns an error if no LED exists at that index.

O(1)

Source

pub fn set_all(&mut self, color: COLOR)

Sets the color of all LEDs in the system to color.

O(LEDS)

Source

pub fn for_each<F: FnMut(&mut Led<COLOR>)>(&mut self, func: F)

For each method that grants mutable access to each LED in the system.

O(LEDS)

sled.for_each(|led| {
    if led.index() % 2 == 1 {
        led.color = Rgb::new(1.0, 0.0, 0.0);
    } else {
        led.color = Rgb::new(0.0, 1.0, 0.0);
    }
});
Source§

impl<COLOR: ColorType> Sled<COLOR>

§Index and range-based read and write methods

Source

pub fn range(&self, index_range: Range<usize>) -> Option<Filter>

Returns a Some(Filter) containing all LEDs with indices within index_range. Returns None if the range extends beyond the size of the system.

O(RANGE_SIZE)

Source

pub fn modulate_range<F: Fn(&Led<COLOR>) -> COLOR>( &mut self, index_range: Range<usize>, color_rule: F, ) -> Result<(), SledError>

Modulates the color of each LED with indices in index_range given a color rule function. Returns an error if the range extends beyond the size of the system.

O(RANGE_SIZE)

sled.modulate_range(0..50, |led| led.color * 0.5)?;
Source

pub fn set_range( &mut self, index_range: Range<usize>, color: COLOR, ) -> Result<(), SledError>

Sets the color of the each LED with indices in index_range to color. Returns an error if the range extends beyond the size of the system.

O(RANGE_SIZE)

Source

pub fn for_each_in_range<F: FnMut(&mut Led<COLOR>)>( &mut self, index_range: Range<usize>, func: F, ) -> Result<(), SledError>

For-each method granting mutable access to each LED with an index in index_range

Returns an error if the range extends beyond the size of the system.

O(RANGE_SIZE)

sled.for_each_in_range(50..100, |led| {
    if led.index() % 2 == 1 {
        led.color = Rgb::new(1.0, 0.0, 0.0);
    } else {
        led.color = Rgb::new(0.0, 1.0, 0.0);
    }
});
Source§

impl<Color: ColorType> Sled<Color>

§Segment-based read and write methods.

Source

pub fn segment(&self, segment_index: usize) -> Option<Filter>

Returns the set of all LEDs assigned to the line segment with index segment_index.

O(LEDS_IN_SEGMENT)

Source

pub fn modulate_segment<F: Fn(&Led<Color>) -> Color>( &mut self, segment_index: usize, color_rule: F, ) -> Result<(), SledError>

Modulates the color of each LED assigned to the line segment with index segment_index given a color rule function. Returns an error if there is no line segment with the given index.

O(LEDS_IN_SEGMENT)

 sled.modulate_segment(1, |led| led.color * 2.0)?;
Source

pub fn set_segment( &mut self, segment_index: usize, color: Color, ) -> Result<(), SledError>

Sets the color of each LED assigned to the line segment with index segment_index. Returns an error if there is no line segment with the given index.

O(LEDS_IN_SEGMENT)

Source

pub fn segments(&self, range: Range<usize>) -> Option<Filter>

Returns the set of all LEDs assigned to the line segments whose indices are within the given range.

If the range exceeds the number of segments in the system, returns None.

O(LEDS_IN_SEGMENTS)

use palette::rgb::Rgb;
let first_three_walls: Filter = sled.segments(0..3).unwrap();
sled.set_filter(&first_three_walls, Rgb::new(1.0, 1.0, 1.0));
Source

pub fn modulate_segments<F: Fn(&Led<Color>) -> Color>( &mut self, range: Range<usize>, color_rule: F, ) -> Result<(), SledError>

Modulates the color of each LED assigned to the line segments whose indices are within the given range. Returns an error if the range exceeds the number of line segments in the system.

O(LEDS_IN_SEGMENTS)

use palette::rgb::Rgb;
sled.modulate_segments(2..4, |led| led.color * Rgb::new(1.0, 0.0, 0.0))?;
Source

pub fn set_segments( &mut self, range: Range<usize>, color: Color, ) -> Result<(), SledError>

Sets the color of each LED assigned to the line segments whose indices are within the given range. Returns an error if the range exceeds the number of line segments in the system.

O(LEDS_IN_SEGMENTS)

Source

pub fn for_each_in_segment<F: FnMut(&mut Led<Color>, f32)>( &mut self, segment_index: usize, func: F, ) -> Result<(), SledError>

For-each method granting mutable access to each LED assigned to the line segment with index segment_index. Also passes an “alpha” value into the closure, representing how far along the line segment you are. 0 = first LED in segement, 1 = last.

Returns an error if the no segment of given index exists.

O(LEDS_IN_SEGMENT)

use palette::rgb::Rgb;
sled.for_each_in_segment(2, |led, alpha| {
    led.color = Rgb::new(alpha, alpha, alpha);
});

segment alpha example

Source§

impl<Color: ColorType> Sled<Color>

§Vertex-based read and write methods.

Source

pub fn vertex(&self, vertex_index: usize) -> Option<&Led<Color>>

Returns the LED that represents the vertex the given index, if it exists. Vertices are distinct from line segement endpoints in that line segments with touching endpoints will share a vertex.

O(1)

Source

pub fn modulate_vertex<F: Fn(&Led<Color>) -> Color>( &mut self, vertex_index: usize, color_rule: F, ) -> Result<(), SledError>

Modulates the color of the LED that represents the vertex the given index, if it exists. Returns an error if not. Vertices are distinct from line segement endpoints in that line segments with touching endpoints will share a vertex.

Returns an error if no vertex of given index exists.

O(1)

use palette::rgb::Rgb;
// make the given vertex 25% brighter
sled.modulate_vertex(3, |led| led.color * 1.25)?;
Source

pub fn set_vertex( &mut self, vertex_index: usize, color: Color, ) -> Result<(), SledError>

Sets the color of the LED that represents the vertex the given index, if it exists. Returns an error if not. Vertices are distinct from line segement endpoints in that line segments with touching endpoints will share a vertex.

Returns an error if no vertex of given index exists.

O(1)

Source

pub fn vertices(&self) -> Filter

Returns a Filter containing all vertices in the system.

Source

pub fn modulate_vertices<F: Fn(&Led<Color>) -> Color>(&mut self, color_rule: F)

Modulates the color of each LED that represents a vertex in the system. Vertices are distinct from line segement endpoints in that line segments with touching endpoints will share a vertex.

O(VERTICES)

use palette::rgb::Rgb;
// make each vertex 25% brighter
sled.modulate_vertices(|led| led.color * 1.25);
Source

pub fn set_vertices(&mut self, color: Color)

Sets the color of each LED that represents a vertex in the system.

O(VERTICES)

Source

pub fn for_each_vertex<F: FnMut(&mut Led<Color>)>(&mut self, f: F)

For each method that grants mutable access to each LED that represents a vertex in the system.

O(VERTICES)

Source§

impl<Color: ColorType> Sled<Color>

§directional read and write methods

Source

pub fn at_dir(&self, dir: Vec2) -> Filter

Returns A Filter containing each LED in the given direction from the center point. Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

If no LEDs exist at the given direction, the Filter will be empty.

O(SEGMENTS)

Source

pub fn at_dir_from(&self, dir: Vec2, pos: Vec2) -> Filter

Returns A Filter containing each LED in the given direction from a given point. Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

Currently returns no more than 4 LEDs, may change in the future.

Source

pub fn modulate_at_dir<F: Fn(&Led<Color>) -> Color>( &mut self, dir: Vec2, color_rule: F, ) -> bool

Modulates the color of each LED in the given direction from the center point. Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

Returns false if there is no LED in that direction, true otherwise.

O(SEGMENTS)

 sled.modulate_at_dir(Vec2::new(0.0, 1.0), |led| led.color * 2.0);
Source

pub fn modulate_at_dir_from<F: Fn(&Led<Color>) -> Color>( &mut self, dir: Vec2, pos: Vec2, color_rule: F, ) -> bool

Modulates the color of each LED in the given direction from a given point. Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

Returns false if there is no LED in that direction, true otherwise.

O(SEGMENTS)

 let dir = Vec2::new(-1.0, 0.0);
 let from = Vec2::new(0.25, -0.6);
 sled.modulate_at_dir_from(dir, from, |led| {
     led.color * 2.0
 });
Source

pub fn set_at_dir(&mut self, dir: Vec2, color: Color) -> bool

Sets the color of each LED in the given direction from the center. Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

Returns false if there is no LED in that direction, true otherwise.

O(SEGMENTS)

Source

pub fn set_at_dir_from(&mut self, dir: Vec2, pos: Vec2, color: Color) -> bool

Sets the color of each LED in the given direction from a given point. Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

Returns false if there is no LED in that direction, true otherwise.

O(SEGMENTS)

Source

pub fn at_angle(&self, angle: f32) -> Filter

Returns A Filter containing each LED whose direction relative to the center point forms a given radian angle.

Equivalent to at_dir(Vec2::new(angle.cos(), angle.sin()));

Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

If no LEDs exist at the given direction, the Filter will be empty.

Currently returns no more than 4 LEDs, may change in the future.

O(SEGMENTS)

Source

pub fn at_angle_from(&self, angle: f32, pos: Vec2) -> Filter

Returns A Filter containing each LED whose direction relative to a point forms a given radian angle.

Equivalent to at_dir_from(Vec2::new(angle.cos(), angle.sin()), pos);

Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

If no LEDs exist at the given direction, the Filter will be empty.

Currently returns no more than 4 LEDs, may change in the future.

O(SEGMENTS)

Source

pub fn modulate_at_angle<F: Fn(&Led<Color>) -> Color>( &mut self, angle: f32, color_rule: F, ) -> bool

Modulates the color of each LED whose direction relative to the center point forms a given radian angle.

Equivalent to modulate_at_dir(Vec2::new(angle.cos(), angle.sin()), /*-snip-*/);

Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

Returns false if there is no LED at that angle, true otherwise.

O(SEGMENTS)

 use core::f32::consts::PI;
 sled.modulate_at_angle(PI / 4.0, |led| led.color * 2.0);
Source

pub fn modulate_at_angle_from<F: Fn(&Led<Color>) -> Color>( &mut self, angle: f32, pos: Vec2, color_rule: F, ) -> bool

Modulates the color of each LED whose direction relative to a point forms a given radian angle.

Equivalent to modulate_at_dir_from(Vec2::new(angle.cos(), angle.sin()), pos, /*-snip-*/);

Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

Returns false if there is no LED at that angle, true otherwise.

O(SEGMENTS)

 use core::f32::consts::PI;
 let angle = PI * 1.25;
 let from = Vec2::new(0.3, 0.2);
 sled.modulate_at_angle_from(angle, from, |led| led.color * 2.0);
Source

pub fn set_at_angle(&mut self, angle: f32, color: Color) -> bool

Sets the color of each LED whose direction relative to the center point forms a given radian angle. Equivalent to set_at_dir(Vec2::new(angle.cos(), angle.sin()), color);

Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

Returns false if there is no LED at that angle, true otherwise.

O(SEGMENTS)

Source

pub fn set_at_angle_from(&mut self, angle: f32, pos: Vec2, color: Color) -> bool

Sets the color of each LED whose direction relative to a point forms a given radian angle. Equivalent to set_at_dir(Vec2::new(angle.cos(), angle.sin()), pos, color);

Calculated by performing a raycast against each line segment and finding the closest LED to the point of contact.

Returns false if there is no LED at that angle, true otherwise.

O(SEGMENTS)

Source§

impl<Color: ColorType> Sled<Color>

§position-based read and write methods

Source

pub fn index_of_closest_to(&self, pos: Vec2) -> usize

Returns the index of the LED closest to a given point.

O(SEGMENTS)

Source

pub fn closest(&self) -> &Led<Color>

Returns the LED closest to the center point.

O(1)

Source

pub fn closest_to(&self, pos: Vec2) -> &Led<Color>

Returns the LED closest to a given point.

O(SEGMENTS)

Source

pub fn modulate_closest<F: Fn(&Led<Color>) -> Color>(&mut self, color_rule: F)

Modulates the color of the LED closest to the center point.

O(1)

 sled.modulate_closest(|led| led.color + Rgb::new(0.2, 0.2, 0.2));
Source

pub fn modulate_closest_to<F: Fn(&Led<Color>) -> Color>( &mut self, pos: Vec2, color_rule: F, )

Modulates the color of the LED closest to a given point.

O(SEGMENTS)

 sled.modulate_closest_to(Vec2::new(0.5, 0.0), |led| {
     led.color + Rgb::new(0.2, 0.2, 0.2)
 });
Source

pub fn set_closest(&mut self, color: Color)

Sets the color of the LED closest to the center point.

O(1)

Source

pub fn set_closest_to(&mut self, pos: Vec2, color: Color)

Sets the color of the LED closest to a given point.

O(SEGMENTS)

Source

pub fn index_of_furthest_from(&self, pos: Vec2) -> usize

Returns the index of the LED furthest from a given point.

O(VERTICES)

Source

pub fn index_of_furthest(&self) -> usize

Returns the index of the LED furthest from the center point.

O(1)

Source

pub fn furthest(&self) -> &Led<Color>

Returns the LED furthest from the center point.

O(1)

Source

pub fn furthest_from(&self, pos: Vec2) -> &Led<Color>

Returns the LED furthest from a given point.

O(VERTICES)

Source

pub fn modulate_furthest<F: Fn(&Led<Color>) -> Color>(&mut self, color_rule: F)

Modulates the color of the LED furthest from the center point.

O(1)

 sled.modulate_furthest(|led| led.color / led.distance());
Source

pub fn modulate_furthest_from<F: Fn(&Led<Color>) -> Color>( &mut self, pos: Vec2, color_rule: F, )

Modulates the color of the LED furthest from a given point

O(SEGMENTS)

 sled.modulate_furthest_from(Vec2::new(0.0, -1.0), |led| {
     led.color - Rgb::new(0.2, 0.2, 0.2)
 });
Source

pub fn set_furthest(&mut self, color: Color)

Sets the color of the LED furthest from the center point.

O(1)

Source

pub fn set_furthest_from(&mut self, pos: Vec2, color: Color)

Sets the color of the LED furthest from a given point.

O(VERTICES)

Source

pub fn at_dist(&self, dist: f32) -> Filter

Source

pub fn at_dist_from(&self, dist: f32, pos: Vec2) -> Filter

Source

pub fn modulate_at_dist<F: Fn(&Led<Color>) -> Color>( &mut self, dist: f32, color_rule: F, ) -> bool

Source

pub fn modulate_at_dist_from<F: Fn(&Led<Color>) -> Color>( &mut self, dist: f32, pos: Vec2, color_rule: F, ) -> bool

Source

pub fn set_at_dist(&mut self, dist: f32, color: Color) -> bool

Source

pub fn set_at_dist_from(&mut self, dist: f32, pos: Vec2, color: Color) -> bool

Source

pub fn within_dist(&self, dist: f32) -> Filter

Source

pub fn within_dist_from(&self, dist: f32, pos: Vec2) -> Filter

Source

pub fn modulate_within_dist<F: Fn(&Led<Color>) -> Color>( &mut self, dist: f32, color_rule: F, ) -> bool

Source

pub fn set_within_dist(&mut self, dist: f32, color: Color) -> bool

Source

pub fn modulate_within_dist_from<F: Fn(&Led<Color>) -> Color>( &mut self, dist: f32, pos: Vec2, color_rule: F, ) -> bool

Source

pub fn set_within_dist_from( &mut self, dist: f32, pos: Vec2, color: Color, ) -> bool

Source§

impl<Color: ColorType> Sled<Color>

Maps

Source

pub fn map(&mut self, led_to_color_map: impl Fn(&Led<Color>) -> Color)

Maps LEDs to a color.

sled.map(|led| {
    if led.direction().x > 0.0 {
        Rgb::new(1.0, 0.0, 0.0)
    } else {
        Rgb::new(0.0, 0.0, 1.0)
    }
});
Source

pub fn map_by_index(&mut self, index_to_color_map: impl Fn(usize) -> Color)

Maps LED indices to a color.

sled.map_by_index(|index| {
    if index % 2 == 0 {
        // even indices red
        Rgb::new(1.0, 0.0, 0.0)
    } else {
        // odd indices blue
        Rgb::new(0.0, 0.0, 1.0)
    }
});
Source

pub fn map_by_segment( &mut self, segment_index_to_color_map: impl Fn(usize) -> Color, )

Maps LEDs to a color depending on which line segment they belong to.

sled.map_by_segment(|segment| {
    match segment {
        0..2 => Rgb::new(1.0, 0.0, 0.0),
        2..4 => Rgb::new(0.0, 1.0, 0.0),
        4..6 => Rgb::new(0.0, 0.0, 1.0),
        _ => Rgb::new(0.0, 0.0, 0.0)
    }
});
Source

pub fn map_by_pos(&mut self, pos_to_color_map: impl Fn(Vec2) -> Color)

Maps LEDs positions to a color.

sled.map_by_pos(|pos| {
    Rgb::new(
        (pos.x.abs() / 10.0).min(1.0),
        (pos.y.abs() / 10.0).min(1.0),
        0.5
    )
});
Source

pub fn map_by_dir(&mut self, dir_to_color_map: impl Fn(Vec2) -> Color)

Maps LEDs directions (relative to the center_point defined in the config) to a color.

sled.map_by_dir(|dir| {
    Rgb::new(
        (dir.x / 2.0) + 0.5,
        (dir.y / 2.0) + 0.5,
        0.5
    )
});
Source

pub fn map_by_dir_from( &mut self, point: Vec2, dir_to_color_map: impl Fn(Vec2) -> Color, )

Maps LEDs to a color depending on their direction from a given point.

sled.map_by_dir_from(Vec2::new(0.5, 1.5), |dir| {
    Rgb::new(
        (dir.x / 2.0) + 0.5,
        (dir.y / 2.0) + 0.5,
        0.5
    )
});
Source

pub fn map_by_angle(&mut self, angle_to_color_map: impl Fn(f32) -> Color)

Maps LEDs angles (relative to the center_point defined in the config) to a color.

sled.map_by_angle(|angle| {
    let brightness = angle / (PI * 2.0);
    Rgb::new(brightness, brightness, brightness)
});
Source

pub fn map_by_angle_from( &mut self, point: Vec2, angle_to_color_map: impl Fn(f32) -> Color, )

Maps LEDs to a color depending on their angle from a given point.

sled.map_by_angle_from(Vec2::new(-1.0, -1.0), |angle| {
    let brightness = angle / (PI * 2.0);
    Rgb::new(brightness, brightness, brightness)
});
Source

pub fn map_by_dist(&mut self, dist_to_color_map: impl Fn(f32) -> Color)

Maps LEDs to a color depending on their distance from the center_point.

sled.map_by_dist(|dist| {
    let transformed = (1.0 - (dist / 10.0)).max(0.0);
    Rgb::new(transformed, 0.0, 0.0)
});
Source

pub fn map_by_dist_from( &mut self, pos: Vec2, dist_to_color_map: impl Fn(f32) -> Color, )

Maps LEDs to a color depending on their distance from the given point.

sled.map_by_dist_from(Vec2::new(2.0, -1.0), |dist| {
    let transformed = (1.0 - (dist / 10.0)).max(0.0);
    Rgb::new(transformed, 0.0, 0.0)
});
Source§

impl<Color: ColorType> Sled<Color>

Filters

Source

pub fn filter(&self, filter: impl Fn(&Led<Color>) -> bool) -> Filter

Returns a Filter containing all LEDs that meet a certain criteria.

let odd = sled.filter(|led| led.index() % 2 == 0);
sled.set_filter(&odd, Rgb::new(1.0, 1.0, 1.0));
Source

pub fn filter_by_angle(&self, angle_filter: impl Fn(f32) -> bool) -> Filter

Returns a Filter containing all LEDs whose angle meets a certain criteria.

let region = sled.filter_by_angle(|angle| angle > 0.0 && angle < 3.14);
sled.set_filter(&region, Rgb::new(1.0, 1.0, 1.0));
Source

pub fn filter_by_dir(&self, dir_filter: impl Fn(Vec2) -> bool) -> Filter

Returns a Filter containing all LEDs whose direction from the center_point meets a certain criteria.

let left = sled.filter_by_dir(|dir| dir.x < 0.0);
sled.set_filter(&left, Rgb::new(1.0, 1.0, 1.0));
Source

pub fn filter_by_pos(&self, pos_filter: impl Fn(Vec2) -> bool) -> Filter

Returns a Filter containing all LEDs whose position meets a certain criteria.

let quadrants = sled.filter_by_pos(|pos| (pos.x * pos.y) > 0.0);
sled.set_filter(&quadrants, Rgb::new(1.0, 1.0, 1.0));
Source

pub fn filter_by_dist(&self, dist_filter: impl Fn(f32) -> bool) -> Filter

Returns a Filter containing all LEDs whose distance from the center_point meets a certain criteria.

let ring = sled.filter_by_dist(|dist| dist > 0.5 && dist < 0.75);
sled.set_filter(&ring, Rgb::new(1.0, 1.0, 1.0));
Source

pub fn filter_by_dist_from( &self, pos: Vec2, dist_filter: impl Fn(f32) -> bool, ) -> Filter

Returns a Filter containing all LEDs whose distance from the given point meets a certain criteria.

let ring = sled.filter_by_dist_from(
    Vec2::new(3.5, -0.25),
    |dist| dist > 0.5 && dist < 0.75
);
sled.set_filter(&ring, Rgb::new(1.0, 1.0, 1.0));
Source§

impl<Color: ColorType> Sled<Color>

Source

pub fn set_filter(&mut self, filter: &Filter, color: Color)

Sets all LEDs in the given filter to color.

O(LEDS_IN_FILTER)

Source

pub fn modulate_filter<F: Fn(&Led<Color>) -> Color>( &mut self, filter: &Filter, color_rule: F, )

Modulates the color of each LED contained in the filter.

O(LEDS_IN_FILTER)

let first_wall = sled.segment(0).unwrap();
let third_wall = sled.segment(2).unwrap();
let first_and_third = first_wall.or(&third_wall);
// dim first and third walls by 50%
sled.modulate_filter(&first_and_third, |led| led.color * 0.5);
Source

pub fn map_filter( &mut self, filter: &Filter, color_map: impl Fn(&Led<Color>) -> Color, )

Functionally identical to modulate_filter().

Source

pub fn for_each_in_filter<F: FnMut(&mut Led<Color>)>( &mut self, filter: &Filter, func: F, )

For-each method granting mutable access to each LED contained in the given filter.

O(LEDS_IN_FILTER)

let circle: Filter = sled.within_dist(2.5);
sled.for_each_in_filter(&circle, |led| {
    if led.position().x > 0.0 {
        led.color = Rgb::new(1.0, 0.0, 0.0);
    } else {
        led.color = Rgb::new(0.0, 0.0, 1.0);
    }
});

Trait Implementations§

Source§

impl<COLOR: Clone + ColorType> Clone for Sled<COLOR>

Source§

fn clone(&self) -> Sled<COLOR>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<COLOR: Debug + ColorType> Debug for Sled<COLOR>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<COLOR> Freeze for Sled<COLOR>

§

impl<COLOR> RefUnwindSafe for Sled<COLOR>
where COLOR: RefUnwindSafe,

§

impl<COLOR> Send for Sled<COLOR>
where COLOR: Send,

§

impl<COLOR> Sync for Sled<COLOR>
where COLOR: Sync,

§

impl<COLOR> Unpin for Sled<COLOR>
where COLOR: Unpin,

§

impl<COLOR> UnwindSafe for Sled<COLOR>
where COLOR: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.