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
impl<COLOR: ColorType> Sled<COLOR>
§Construction, output, and basic sled info
Sourcepub fn new(config_file_path: &str) -> Result<Self, SledError>
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) -->centeris 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.densityrepresents 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: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)--segments-- (-2, 0) --> (0.5, -1) --> (3.5, 0) | (2, 2) --> (-2, 2) --> (-2, 0)
Sourcepub fn new_from_str(string: &str) -> Result<Self, SledError>
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.
Sourcepub fn leds(&self) -> impl Iterator<Item = &Led<COLOR>>
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
);
}Sourcepub fn colors(&self) -> impl Iterator<Item = &COLOR> + '_
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 -*/
}Sourcepub fn positions(&self) -> impl Iterator<Item = Vec2> + '_
pub fn positions(&self) -> impl Iterator<Item = Vec2> + '_
Returns an Iterator over Vec2s, representing the position of each LED in the system.
O(LEDS)
Sourcepub fn colors_and_positions(&self) -> impl Iterator<Item = (COLOR, Vec2)> + '_
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)
Sourcepub fn center_point(&self) -> Vec2
pub fn center_point(&self) -> Vec2
Returns the static reference point declared in the config file.
O(1)
Sourcepub fn num_segments(&self) -> usize
pub fn num_segments(&self) -> usize
Returns the total number of line segments in the system.
O(1)
Sourcepub fn num_vertices(&self) -> usize
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)
Sourcepub fn domain(&self) -> Range<Vec2>
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.
impl<COLOR: ColorType> Sled<COLOR>
§Index-based read and write methods.
Sourcepub fn get(&self, index: usize) -> Option<&Led<COLOR>>
pub fn get(&self, index: usize) -> Option<&Led<COLOR>>
Returns Some(&Led<COLOR>) if an LED at index exists, None if not.
O(1)
Sourcepub fn modulate<F: Fn(&Led<COLOR>) -> COLOR>(
&mut self,
index: usize,
color_rule: F,
) -> Result<(), SledError>
pub fn modulate<F: Fn(&Led<COLOR>) -> COLOR>( &mut self, index: usize, color_rule: F, ) -> Result<(), SledError>
Source§impl<COLOR: ColorType> Sled<COLOR>
§Index and range-based read and write methods
impl<COLOR: ColorType> Sled<COLOR>
§Index and range-based read and write methods
Sourcepub fn modulate_range<F: Fn(&Led<COLOR>) -> COLOR>(
&mut self,
index_range: Range<usize>,
color_rule: F,
) -> Result<(), SledError>
pub fn modulate_range<F: Fn(&Led<COLOR>) -> COLOR>( &mut self, index_range: Range<usize>, color_rule: F, ) -> Result<(), SledError>
Sourcepub fn set_range(
&mut self,
index_range: Range<usize>,
color: COLOR,
) -> Result<(), SledError>
pub fn set_range( &mut self, index_range: Range<usize>, color: COLOR, ) -> Result<(), SledError>
Sourcepub fn for_each_in_range<F: FnMut(&mut Led<COLOR>)>(
&mut self,
index_range: Range<usize>,
func: F,
) -> Result<(), SledError>
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.
impl<Color: ColorType> Sled<Color>
§Segment-based read and write methods.
Sourcepub fn segment(&self, segment_index: usize) -> Option<Filter>
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)
Sourcepub fn modulate_segment<F: Fn(&Led<Color>) -> Color>(
&mut self,
segment_index: usize,
color_rule: F,
) -> Result<(), SledError>
pub fn modulate_segment<F: Fn(&Led<Color>) -> Color>( &mut self, segment_index: usize, color_rule: F, ) -> Result<(), SledError>
Sourcepub fn segments(&self, range: Range<usize>) -> Option<Filter>
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));Sourcepub fn modulate_segments<F: Fn(&Led<Color>) -> Color>(
&mut self,
range: Range<usize>,
color_rule: F,
) -> Result<(), SledError>
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))?;Sourcepub fn for_each_in_segment<F: FnMut(&mut Led<Color>, f32)>(
&mut self,
segment_index: usize,
func: F,
) -> Result<(), SledError>
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);
});
Source§impl<Color: ColorType> Sled<Color>
§Vertex-based read and write methods.
impl<Color: ColorType> Sled<Color>
§Vertex-based read and write methods.
Sourcepub fn vertex(&self, vertex_index: usize) -> Option<&Led<Color>>
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)
Sourcepub fn modulate_vertex<F: Fn(&Led<Color>) -> Color>(
&mut self,
vertex_index: usize,
color_rule: F,
) -> Result<(), SledError>
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)?;Sourcepub fn modulate_vertices<F: Fn(&Led<Color>) -> Color>(&mut self, color_rule: F)
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);Sourcepub fn set_vertices(&mut self, color: Color)
pub fn set_vertices(&mut self, color: Color)
Sets the color of each LED that represents a vertex in the system.
O(VERTICES)
Sourcepub fn for_each_vertex<F: FnMut(&mut Led<Color>)>(&mut self, f: F)
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
impl<Color: ColorType> Sled<Color>
§directional read and write methods
Sourcepub fn at_dir_from(&self, dir: Vec2, pos: Vec2) -> Filter
pub fn at_dir_from(&self, dir: Vec2, pos: Vec2) -> Filter
Sourcepub fn modulate_at_dir<F: Fn(&Led<Color>) -> Color>(
&mut self,
dir: Vec2,
color_rule: F,
) -> bool
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);Sourcepub fn modulate_at_dir_from<F: Fn(&Led<Color>) -> Color>(
&mut self,
dir: Vec2,
pos: Vec2,
color_rule: F,
) -> bool
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
});Sourcepub fn set_at_dir(&mut self, dir: Vec2, color: Color) -> bool
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)
Sourcepub fn set_at_dir_from(&mut self, dir: Vec2, pos: Vec2, color: Color) -> bool
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)
Sourcepub fn at_angle(&self, angle: f32) -> Filter
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)
Sourcepub fn at_angle_from(&self, angle: f32, pos: Vec2) -> Filter
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)
Sourcepub fn modulate_at_angle<F: Fn(&Led<Color>) -> Color>(
&mut self,
angle: f32,
color_rule: F,
) -> bool
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);Sourcepub fn modulate_at_angle_from<F: Fn(&Led<Color>) -> Color>(
&mut self,
angle: f32,
pos: Vec2,
color_rule: F,
) -> bool
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);Sourcepub fn set_at_angle(&mut self, angle: f32, color: Color) -> bool
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)
Sourcepub fn set_at_angle_from(&mut self, angle: f32, pos: Vec2, color: Color) -> bool
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
impl<Color: ColorType> Sled<Color>
§position-based read and write methods
Sourcepub fn index_of_closest_to(&self, pos: Vec2) -> usize
pub fn index_of_closest_to(&self, pos: Vec2) -> usize
Returns the index of the LED closest to a given point.
O(SEGMENTS)
Sourcepub fn closest_to(&self, pos: Vec2) -> &Led<Color>
pub fn closest_to(&self, pos: Vec2) -> &Led<Color>
Returns the LED closest to a given point.
O(SEGMENTS)
Sourcepub fn modulate_closest<F: Fn(&Led<Color>) -> Color>(&mut self, color_rule: F)
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));Sourcepub fn modulate_closest_to<F: Fn(&Led<Color>) -> Color>(
&mut self,
pos: Vec2,
color_rule: F,
)
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)
});Sourcepub fn set_closest(&mut self, color: Color)
pub fn set_closest(&mut self, color: Color)
Sets the color of the LED closest to the center point.
O(1)
Sourcepub fn set_closest_to(&mut self, pos: Vec2, color: Color)
pub fn set_closest_to(&mut self, pos: Vec2, color: Color)
Sets the color of the LED closest to a given point.
O(SEGMENTS)
Sourcepub fn index_of_furthest_from(&self, pos: Vec2) -> usize
pub fn index_of_furthest_from(&self, pos: Vec2) -> usize
Returns the index of the LED furthest from a given point.
O(VERTICES)
Sourcepub fn index_of_furthest(&self) -> usize
pub fn index_of_furthest(&self) -> usize
Returns the index of the LED furthest from the center point.
O(1)
Sourcepub fn furthest_from(&self, pos: Vec2) -> &Led<Color>
pub fn furthest_from(&self, pos: Vec2) -> &Led<Color>
Returns the LED furthest from a given point.
O(VERTICES)
Sourcepub fn modulate_furthest<F: Fn(&Led<Color>) -> Color>(&mut self, color_rule: F)
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());Sourcepub fn modulate_furthest_from<F: Fn(&Led<Color>) -> Color>(
&mut self,
pos: Vec2,
color_rule: F,
)
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)
});Sourcepub fn set_furthest(&mut self, color: Color)
pub fn set_furthest(&mut self, color: Color)
Sets the color of the LED furthest from the center point.
O(1)
Sourcepub fn set_furthest_from(&mut self, pos: Vec2, color: Color)
pub fn set_furthest_from(&mut self, pos: Vec2, color: Color)
Sets the color of the LED furthest from a given point.
O(VERTICES)
pub fn at_dist(&self, dist: f32) -> Filter
pub fn at_dist_from(&self, dist: f32, pos: Vec2) -> Filter
pub fn modulate_at_dist<F: Fn(&Led<Color>) -> Color>( &mut self, dist: f32, color_rule: F, ) -> bool
pub fn modulate_at_dist_from<F: Fn(&Led<Color>) -> Color>( &mut self, dist: f32, pos: Vec2, color_rule: F, ) -> bool
pub fn set_at_dist(&mut self, dist: f32, color: Color) -> bool
pub fn set_at_dist_from(&mut self, dist: f32, pos: Vec2, color: Color) -> bool
pub fn within_dist(&self, dist: f32) -> Filter
pub fn within_dist_from(&self, dist: f32, pos: Vec2) -> Filter
pub fn modulate_within_dist<F: Fn(&Led<Color>) -> Color>( &mut self, dist: f32, color_rule: F, ) -> bool
pub fn set_within_dist(&mut self, dist: f32, color: Color) -> bool
pub fn modulate_within_dist_from<F: Fn(&Led<Color>) -> Color>( &mut self, dist: f32, pos: Vec2, color_rule: F, ) -> bool
pub fn set_within_dist_from( &mut self, dist: f32, pos: Vec2, color: Color, ) -> bool
Source§impl<Color: ColorType> Sled<Color>
Maps
impl<Color: ColorType> Sled<Color>
Maps
Sourcepub fn map(&mut self, led_to_color_map: impl Fn(&Led<Color>) -> Color)
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)
}
});Sourcepub fn map_by_index(&mut self, index_to_color_map: impl Fn(usize) -> Color)
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)
}
});Sourcepub fn map_by_segment(
&mut self,
segment_index_to_color_map: impl Fn(usize) -> Color,
)
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)
}
});Sourcepub fn map_by_pos(&mut self, pos_to_color_map: impl Fn(Vec2) -> Color)
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
)
});Sourcepub fn map_by_dir(&mut self, dir_to_color_map: impl Fn(Vec2) -> Color)
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
)
});Sourcepub fn map_by_dir_from(
&mut self,
point: Vec2,
dir_to_color_map: impl Fn(Vec2) -> Color,
)
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
)
});Sourcepub fn map_by_angle(&mut self, angle_to_color_map: impl Fn(f32) -> Color)
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)
});Sourcepub fn map_by_angle_from(
&mut self,
point: Vec2,
angle_to_color_map: impl Fn(f32) -> Color,
)
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)
});Sourcepub fn map_by_dist(&mut self, dist_to_color_map: impl Fn(f32) -> Color)
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)
});Sourcepub fn map_by_dist_from(
&mut self,
pos: Vec2,
dist_to_color_map: impl Fn(f32) -> Color,
)
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
impl<Color: ColorType> Sled<Color>
Filters
Sourcepub fn filter(&self, filter: impl Fn(&Led<Color>) -> bool) -> Filter
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));Sourcepub fn filter_by_angle(&self, angle_filter: impl Fn(f32) -> bool) -> Filter
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(®ion, Rgb::new(1.0, 1.0, 1.0));Sourcepub fn filter_by_dir(&self, dir_filter: impl Fn(Vec2) -> bool) -> Filter
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));Sourcepub fn filter_by_pos(&self, pos_filter: impl Fn(Vec2) -> bool) -> Filter
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));Sourcepub fn filter_by_dist(&self, dist_filter: impl Fn(f32) -> bool) -> Filter
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));Sourcepub fn filter_by_dist_from(
&self,
pos: Vec2,
dist_filter: impl Fn(f32) -> bool,
) -> Filter
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>
impl<Color: ColorType> Sled<Color>
Sourcepub fn set_filter(&mut self, filter: &Filter, color: Color)
pub fn set_filter(&mut self, filter: &Filter, color: Color)
Sets all LEDs in the given filter to color.
O(LEDS_IN_FILTER)
Sourcepub fn modulate_filter<F: Fn(&Led<Color>) -> Color>(
&mut self,
filter: &Filter,
color_rule: F,
)
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);Sourcepub fn map_filter(
&mut self,
filter: &Filter,
color_map: impl Fn(&Led<Color>) -> Color,
)
pub fn map_filter( &mut self, filter: &Filter, color_map: impl Fn(&Led<Color>) -> Color, )
Functionally identical to modulate_filter().
Sourcepub fn for_each_in_filter<F: FnMut(&mut Led<Color>)>(
&mut self,
filter: &Filter,
func: F,
)
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);
}
});