From 2bde443b253745accf2e5f437a58518e6126851b Mon Sep 17 00:00:00 2001 From: David Cosby Date: Thu, 26 Sep 2024 16:55:35 -0600 Subject: [PATCH 1/3] swapped out toml configs for custom format --- Cargo.toml | 3 +- examples/calibration.rs | 2 +- examples/resources/config.yap | 5 + .../{config.toml => oldconfigformat.toml} | 0 examples/resources/tui.rs | 2 +- examples/ripples.rs | 2 +- examples/warpspeed.rs | 2 +- src/config.rs | 164 ++++++++++++++---- src/lib.rs | 22 +-- src/sled/indexical.rs | 8 +- src/sled/meta.rs | 15 +- src/sled/mod.rs | 1 + src/sled/segmental.rs | 12 +- 13 files changed, 167 insertions(+), 71 deletions(-) create mode 100644 examples/resources/config.yap rename examples/resources/{config.toml => oldconfigformat.toml} (100%) diff --git a/Cargo.toml b/Cargo.toml index 50b6dc0..2e33de2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,12 +11,11 @@ scheduler = ["spin_sleep"] named_colors = [] [dependencies] -glam = { version = "0.29", features = ["serde"] } +glam = { version = "0.29" } palette = { version = "0.7", default-features = false, features = [ "std", "approx", ] } -toml = "0.8" smallvec = "1.13" compact_str = { version = "0.8", optional = true } spin_sleep = { version = "1.2", optional = true } diff --git a/examples/calibration.rs b/examples/calibration.rs index f114860..fb215d9 100644 --- a/examples/calibration.rs +++ b/examples/calibration.rs @@ -6,7 +6,7 @@ use glam::Vec2; use sled::{color::Rgb, Sled, SledError}; fn main() -> Result<(), SledError> { - let mut sled = Sled::new("./examples/resources/config.toml")?; + let mut sled = Sled::new("./examples/resources/config.yap")?; let mut display = SledTerminalDisplay::start("Calibration", sled.domain()); diff --git a/examples/resources/config.yap b/examples/resources/config.yap new file mode 100644 index 0000000..ad3b512 --- /dev/null +++ b/examples/resources/config.yap @@ -0,0 +1,5 @@ +center: (0, 0.5) +density: 30 +--segments-- +(-2, 0) --> (0.5, -1) --> (3.5, 0) | +(2, 2) --> (-2, 2) --> (-2, 0) \ No newline at end of file diff --git a/examples/resources/config.toml b/examples/resources/oldconfigformat.toml similarity index 100% rename from examples/resources/config.toml rename to examples/resources/oldconfigformat.toml diff --git a/examples/resources/tui.rs b/examples/resources/tui.rs index 0c71535..227d9ac 100644 --- a/examples/resources/tui.rs +++ b/examples/resources/tui.rs @@ -96,7 +96,7 @@ impl SledTerminalDisplay { }); self.terminal.draw(|frame| { - frame.render_widget(canvas, frame.size()); + frame.render_widget(canvas, frame.area()); })?; Ok(()) diff --git a/examples/ripples.rs b/examples/ripples.rs index f136fd6..c5aa90b 100644 --- a/examples/ripples.rs +++ b/examples/ripples.rs @@ -7,7 +7,7 @@ use resources::tui::SledTerminalDisplay; use sled::{scheduler::Scheduler, Sled}; fn main() { - let sled = Sled::new("./examples/resources/config.toml").unwrap(); + let sled = Sled::new("./examples/resources/config.yap").unwrap(); let mut display = SledTerminalDisplay::start("Ripples", sled.domain()); let mut driver = ripples::build_driver(); driver.mount(sled); diff --git a/examples/warpspeed.rs b/examples/warpspeed.rs index d4dbfa4..f7b3b76 100644 --- a/examples/warpspeed.rs +++ b/examples/warpspeed.rs @@ -7,7 +7,7 @@ use resources::tui::SledTerminalDisplay; use sled::{color::Rgb, scheduler::Scheduler, Sled}; fn main() { - let sled = Sled::new("./examples/resources/config.toml").unwrap(); + let sled = Sled::new("./examples/resources/config.yap").unwrap(); let mut display = SledTerminalDisplay::start("Warpspeed", sled.domain()); let mut driver = warpspeed::build_driver(); driver.mount(sled); diff --git a/src/config.rs b/src/config.rs index 504e2ee..4e31c8e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,34 +1,145 @@ use crate::error::SledError; use glam::Vec2; -use serde::{Deserialize, Deserializer, Serialize}; use smallvec::SmallVec; -use std::fs; +use std::{fs, str::Lines}; -use std::sync::OnceLock; - -static DEFAULT_DENSITY: OnceLock = OnceLock::new(); - -#[derive(Serialize, Deserialize, Debug)] pub(crate) struct Config { pub center_point: Vec2, - #[serde(rename = "density")] - #[serde(deserialize_with = "Config::set_default_density")] - pub default_density: f32, - #[serde(rename = "line_segment")] + pub density: f32, pub line_segments: Vec, } -#[derive(Serialize, Deserialize, Debug, Clone)] +fn extract_center_and_density_from_lines(lines: &mut Lines) -> (Option, Option) { + let mut center: Option = None; + let mut density: Option = None; + let mut segment_marker_found = false; + loop { + if let Some(top_line) = lines.next() { + let trimmed = top_line.trim(); + if trimmed.starts_with("--segments--") { + segment_marker_found = true; + break; + } else if trimmed.starts_with("center:") { + center = Some(get_center_from_line(top_line)); + } else if trimmed.starts_with("density:") { + density = Some(get_density_from_line(top_line)); + } + } else { + break; + } + } + + if !segment_marker_found { + panic!("Error parsing config file: no segment marker of form `--segments--` found.") + } + + return (center, density); +} + +fn get_center_from_line(line: &str) -> Vec2 { + let colon_pos = line.find(':').unwrap(); + parse_string_to_vec2(&line[(colon_pos + 1)..line.len()].trim()) +} + +fn get_density_from_line(line: &str) -> f32 { + let colon_pos = line.find(':').unwrap(); + line[(colon_pos + 1)..line.len()].trim().parse().unwrap() +} + +fn parse_string_to_vec2(s: &str) -> Vec2 { + if s.starts_with('(') & s.ends_with(')') { + let sub: &str = &s[1..(s.len() - 1)]; + let nums: Vec = sub + .split(',') + .map(|s| { + s.trim().parse().expect(&format!( + "Error parsing config file: malformed Vec2: `{}`", + s + )) + }) + .collect(); + if !nums.len() == 2 { + panic!("Error parsing config file: malformed Vec2: {}", s); + } + return Vec2::new(nums[0], nums[1]); + } else { + panic!("Error parsing config file: malformed Vec2: `{}`", s); + } +} + +fn lines_to_string(lines: &mut Lines) -> String { + let mut composite = String::from(""); + + loop { + if let Some(top_line) = lines.next() { + composite += top_line.trim(); + } else { + break; + } + } + + composite +} + +fn extract_segments_from_string(s: &String) -> Vec { + let connected: Vec<&str> = s.split("|").collect(); + let mut segments: Vec = vec![]; + for sequence in connected { + let vertex_strings: Vec<&str> = sequence.split("-->").map(|s| s.trim()).collect(); + let mut last_vertex: Option = None; + for vertex_string in vertex_strings { + let vertex = parse_string_to_vec2(vertex_string); + if let Some(lv) = last_vertex { + segments.push(LineSegment { + start: lv, + end: vertex, + }); + } + last_vertex = Some(vertex); + } + } + + return segments; +} + +impl Config { + pub fn from_toml_file(path: &str) -> Result { + let as_string = fs::read_to_string(path).map_err(SledError::from_error)?; + let mut lines = as_string.lines(); + + let (center, density) = extract_center_and_density_from_lines(&mut lines); + + if center.is_none() { + return Err(SledError::new( + "Error parsing config file: no center point descriptor found.".to_string(), + )); + } + if density.is_none() { + return Err(SledError::new( + "Error parsing config file: no density descriptor found.".to_string(), + )); + } + + let back_to_str = lines_to_string(&mut lines); + let line_segments = extract_segments_from_string(&back_to_str); + + Ok(Config { + density: density.unwrap(), + center_point: center.unwrap(), + line_segments, + }) + } +} + +#[derive(Debug, Clone, Copy)] pub(crate) struct LineSegment { pub start: Vec2, pub end: Vec2, - #[serde(default = "Config::default_density")] - pub density: f32, } impl LineSegment { - pub fn num_leds(&self) -> usize { - (self.length() * self.density).round() as usize + pub fn num_leds(&self, density: f32) -> usize { + (self.length() * density).round() as usize } pub fn length(&self) -> f32 { @@ -97,24 +208,3 @@ impl LineSegment { (self.start + atob * t, t) } } - -impl Config { - pub fn from_toml_file(path: &str) -> Result { - let file_contents = fs::read_to_string(path).map_err(SledError::from_error)?; - let config = toml::from_str(&file_contents).map_err(SledError::from_error)?; - Ok(config) - } - - fn set_default_density<'de, D>(des: D) -> Result - where - D: Deserializer<'de>, - { - let deserialized = f32::deserialize(des).unwrap_or(0.0); - let density = DEFAULT_DENSITY.get_or_init(|| deserialized); - Ok(*density) - } - - fn default_density() -> f32 { - *DEFAULT_DENSITY.get().unwrap_or(&0.0) - } -} diff --git a/src/lib.rs b/src/lib.rs index 27e5868..9b19716 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ //! To create a Sled struct, you need to create a configuration file and provide its path to the constructor: //! ```rust, ignore //! use sled::Sled; -//! let mut sled = Sled::new("/path/to/config.toml")?; +//! let mut sled = Sled::new("/path/to/config.yap")?; //! ``` //! //! A configuration file explains the layout of your LED strips in 2D space. This is used to pre-calculate some important information, speeding up complex draw calls. @@ -58,7 +58,7 @@ //! **Set all vertices to white:** //! ```rust //! # use sled::{Sled, color::Rgb}; -//! # let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); +//! # let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); //! sled.set_vertices(Rgb::new(1.0, 1.0, 1.0)); //! ``` //! ![Set all Vertices](https://github.com/DavJCosby/sled/blob/master/resources/vertices.png?raw=true) @@ -68,7 +68,7 @@ //! ```rust //! # use sled::{Sled, color::Rgb}; //! # fn main() -> Result<(), sled::SledError> { -//! # let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); +//! # let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); //! sled.set_at_dist(2.0, Rgb::new(1.0, 0.0, 0.0)); //! # Ok(()) //! # } @@ -78,7 +78,7 @@ //! **Set each LED using a function of its direction from point `(2, 1)`:** //! ```rust //! # use sled::{Sled, Vec2, color::Rgb}; -//! # let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); +//! # let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); //! sled.map_by_dir_from(Vec2::new(2.0, 1.0), |dir| { //! let red = (dir.x + 1.0) * 0.5; //! let green = (dir.y + 1.0) * 0.5; @@ -91,7 +91,7 @@ //! ```rust //! # use sled::{Sled}; //! # fn main() -> Result<(), sled::SledError> { -//! # let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); +//! # let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); //! sled.modulate_segment(3, |led| led.color * 0.25)?; //! # Ok(()) //! # } @@ -101,7 +101,7 @@ //! **Set all LEDs within the overlapping areas of two different circles to blue:** //! ```rust //! # use sled::{Sled, Filter, Vec2, color::Rgb}; -//! # let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); +//! # let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); //! let circle_1: Filter = sled.within_dist_from( //! 2.0, //! Vec2::new(1.0, 0.5) @@ -124,7 +124,7 @@ //! //! ```rust //! # use sled::{Sled, Vec2, color::Rgb}; -//! # let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); +//! # let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); //! // An Iterator of Rgbs, 32-bits/channel //! let colors_f32 = sled.colors(); //! @@ -138,7 +138,7 @@ //! //! ```rust //! # use sled::{Sled, Vec2, color::Rgb}; -//! # let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); +//! # let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); //! // An Iterator of Rgbs, 8-bits/channel (overhead for conversion) //! let colors_u8 = sled.colors_coerced::(); //! // An Iterator of Vec2s, representing the position of each leds @@ -190,7 +190,7 @@ //! ```rust, no_run //! # use sled::{Sled, driver::Driver}; //! # fn main() -> Result<(), sled::SledError> { -//! let sled = Sled::new("path/to/config.toml")?; +//! let sled = Sled::new("path/to/config.yap")?; //! # let mut driver = Driver::new(); //! driver.mount(sled); // sled gets moved into driver here. //! @@ -208,7 +208,7 @@ //! If you need to retrieve ownership of your sled later, you can do: //! ```rust //! # use sled::{Sled, driver::Driver}; -//! # let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); +//! # let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); //! # let mut driver = Driver::new(); //! # driver.mount(sled); //! let sled = driver.dismount(); @@ -326,7 +326,7 @@ //! Most getter methods on Sled will return a Filter, but if you need more precise control you can do something like this: //! ```rust //! # use sled::{Sled}; -//! # let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); +//! # let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); //! let even_filter = sled.filter(|led| led.index() % 2 == 0); //! ``` //! diff --git a/src/sled/indexical.rs b/src/sled/indexical.rs index febcb6c..005da5b 100644 --- a/src/sled/indexical.rs +++ b/src/sled/indexical.rs @@ -24,7 +24,7 @@ impl Sled { /// ```rust ///# use sled::{Sled, SledError, color::Rgb}; ///# fn demo() -> Result<(), SledError> { - ///# let mut sled = Sled::new("./examples/resources/config.toml")?; + ///# let mut sled = Sled::new("./examples/resources/config.yap")?; /// sled.modulate(0, /// |led| led.color + Rgb::new(0.5, 0.0, 0.0) /// )?; @@ -75,7 +75,7 @@ impl Sled { /// /// ```rust ///# use sled::{Sled, color::Rgb}; - ///# let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); + ///# let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); /// sled.for_each(|led| { /// if led.index() % 2 == 1 { /// led.color = Rgb::new(1.0, 0.0, 0.0); @@ -115,7 +115,7 @@ impl Sled { /// ```rust ///# use sled::{Sled, SledError}; ///# fn main() -> Result<(), SledError> { - ///# let mut sled = Sled::new("./examples/resources/config.toml")?; + ///# let mut sled = Sled::new("./examples/resources/config.yap")?; /// sled.modulate_range(0..50, |led| led.color * 0.5)?; ///# Ok(()) ///# } @@ -162,7 +162,7 @@ impl Sled { /// /// ```rust ///# use sled::{Sled, color::Rgb}; - ///# let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); + ///# let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); /// sled.for_each_in_range(50..100, |led| { /// if led.index() % 2 == 1 { /// led.color = Rgb::new(1.0, 0.0, 0.0); diff --git a/src/sled/meta.rs b/src/sled/meta.rs index bcfe2eb..6fd3881 100644 --- a/src/sled/meta.rs +++ b/src/sled/meta.rs @@ -77,6 +77,7 @@ impl Sled { center_point: config.center_point, leds, num_leds, + density: config.density, line_segments: config.line_segments, index_of_closest, index_of_furthest, @@ -95,7 +96,7 @@ impl Sled { /// /// ```rust ///# use sled::{Sled}; - ///# let sled = Sled::new("./examples/resources/config.toml").unwrap(); + ///# let sled = Sled::new("./examples/resources/config.yap").unwrap(); /// for led in sled.leds() { /// println!("Segment {}, Index {}: {:?}", /// led.segment(), led.index(), led.color @@ -112,7 +113,7 @@ impl Sled { /// /// ```rust ///# use sled::{Sled, color::Rgb}; - ///# let sled = Sled::new("./examples/resources/config.toml").unwrap(); + ///# let sled = Sled::new("./examples/resources/config.yap").unwrap(); /// let colors = sled.colors(); /// /// for color in colors { @@ -131,7 +132,7 @@ impl Sled { /// /// ```rust ///# use sled::{Sled, color::Rgb}; - ///# let sled = Sled::new("./examples/resources/config.toml").unwrap(); + ///# let sled = Sled::new("./examples/resources/config.yap").unwrap(); /// let colors = sled.colors_coerced::(); /// /// for color in colors { @@ -167,7 +168,7 @@ impl Sled { /// /// ```rust /// # use sled::{Sled, color::Rgb}; - ///# let sled = Sled::new("./examples/resources/config.toml").unwrap(); + ///# let sled = Sled::new("./examples/resources/config.yap").unwrap(); /// let col_and_pos = sled.colors_and_positions_coerced::(); /// /// for (color, position) in col_and_pos { @@ -228,7 +229,7 @@ impl Sled { config .line_segments .iter() - .map(|line| line.num_leds()) + .map(|line| line.num_leds(config.density)) .collect() } @@ -287,7 +288,7 @@ impl Sled { vertex_indices.push(last_index); } - let num_leds = line.num_leds(); + let num_leds = line.num_leds(config.density); vertex_indices.push(last_index + num_leds - 1); last_index += num_leds; @@ -320,7 +321,7 @@ impl Sled { pub(crate) fn alpha_to_index(&self, segment_alpha: f32, segment_index: usize) -> usize { let segment = &self.line_segments[segment_index]; let startpoint_index = self.line_segment_endpoint_indices[segment_index].0; - let leds_in_segment = segment.num_leds() as f32; + let leds_in_segment = segment.num_leds(self.density) as f32; (startpoint_index + (segment_alpha * leds_in_segment).floor() as usize) % self.num_leds } diff --git a/src/sled/mod.rs b/src/sled/mod.rs index 0f00b4b..11d197b 100644 --- a/src/sled/mod.rs +++ b/src/sled/mod.rs @@ -13,6 +13,7 @@ pub struct Sled { center_point: Vec2, leds: Vec, num_leds: usize, + density: f32, line_segments: Vec, // utility lookup tables line_segment_endpoint_indices: Vec<(usize, usize)>, diff --git a/src/sled/segmental.rs b/src/sled/segmental.rs index 3aac91e..049263f 100644 --- a/src/sled/segmental.rs +++ b/src/sled/segmental.rs @@ -22,7 +22,7 @@ impl Sled { ///```rust ///# use sled::{Sled, SledError}; ///# fn demo() -> Result<(), SledError> { - ///# let mut sled = Sled::new("./examples/resources/config.toml")?; + ///# let mut sled = Sled::new("./examples/resources/config.yap")?; /// sled.modulate_segment(1, |led| led.color * 2.0)?; ///# Ok(()) ///# } @@ -74,7 +74,7 @@ impl Sled { /// ```rust ///# use sled::{Sled, SledError, Filter, color::Rgb}; ///# fn main() -> Result<(), SledError> { - ///# let mut sled = Sled::new("./examples/resources/config.toml")?; + ///# let mut sled = Sled::new("./examples/resources/config.yap")?; /// let first_three_walls: Filter = sled.segments(0..3).unwrap(); /// sled.set_filter(&first_three_walls, Rgb::new(1.0, 1.0, 1.0)); ///# Ok(()) @@ -99,7 +99,7 @@ impl Sled { /// ```rust ///# use sled::{Sled, SledError, color::Rgb}; ///# fn demo() -> Result<(), SledError> { - ///# let mut sled = Sled::new("./examples/resources/config.toml")?; + ///# let mut sled = Sled::new("./examples/resources/config.yap")?; /// sled.modulate_segments(2..4, |led| led.color * Rgb::new(1.0, 0.0, 0.0))?; ///# Ok(()) ///# } @@ -154,7 +154,7 @@ impl Sled { /// /// ```rust ///# use sled::{Sled, color::Rgb}; - ///# let mut sled = Sled::new("./examples/resources/config.toml").unwrap(); + ///# let mut sled = Sled::new("./examples/resources/config.yap").unwrap(); /// sled.for_each_in_segment(2, |led, alpha| { /// led.color = Rgb::new(alpha, alpha, alpha); /// }); @@ -207,7 +207,7 @@ impl Sled { /// ```rust ///# use sled::{Sled, SledError, color::Rgb}; ///# fn demo() -> Result<(), SledError> { - ///# let mut sled = Sled::new("./examples/resources/config.toml")?; + ///# let mut sled = Sled::new("./examples/resources/config.yap")?; /// // make the given vertex 25% brighter /// sled.modulate_vertex(3, |led| led.color * 1.25)?; ///# Ok(()) @@ -262,7 +262,7 @@ impl Sled { /// ```rust ///# use sled::{Sled, SledError}; ///# fn demo() -> Result<(), SledError> { - ///# let mut sled = Sled::new("./examples/resources/config.toml")?; + ///# let mut sled = Sled::new("./examples/resources/config.yap")?; /// // make each vertex 25% brighter /// sled.modulate_vertices(|led| led.color * 1.25); ///# Ok(()) From 15c6a982c48f1e9afa0a4b56e42a91cf77a188ef Mon Sep 17 00:00:00 2001 From: David Cosby Date: Thu, 26 Sep 2024 16:59:27 -0600 Subject: [PATCH 2/3] Missed one --- examples/comet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/comet.rs b/examples/comet.rs index 4b38914..77b7847 100644 --- a/examples/comet.rs +++ b/examples/comet.rs @@ -7,7 +7,7 @@ use resources::tui::SledTerminalDisplay; use sled::{scheduler::Scheduler, Sled}; fn main() { - let sled = Sled::new("./examples/resources/config.toml").unwrap(); + let sled = Sled::new("./examples/resources/config.yap").unwrap(); let mut display = SledTerminalDisplay::start("Comet", sled.domain()); let mut driver = comet::build_driver(); driver.mount(sled); From 6710ab411443d6e019002481bf2d3b911f93d82b Mon Sep 17 00:00:00 2001 From: David Cosby Date: Thu, 26 Sep 2024 17:13:41 -0600 Subject: [PATCH 3/3] Updated documentation --- README.md | 50 +++++++++++++++++++++++--------------------------- src/lib.rs | 49 ++++++++++++++++++++++--------------------------- 2 files changed, 45 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 572d069..f255b5b 100644 --- a/README.md +++ b/README.md @@ -26,40 +26,36 @@ To create a Sled struct, you need to create a configuration file and provide its ```rust use sled::Sled; fn main() -> Result<(), sled::SledError> { - let mut sled = Sled::new("/path/to/config.toml")?; + let mut sled = Sled::new("/path/to/config.yap")?; Ok(()) } ``` A configuration file explains the layout of your LED strips in 2D space. This is used to pre-calculate some important information, speeding up complex draw calls. - Example .toml file: - ```toml - center_point = [0, 0.5] - density = 30.0 - - [[line_segment]] - start = [-2, 0] - end = [0.5, -1] - - [[line_segment]] - start = [0.5, -1] - end = [3.5, 0] - - [[line_segment]] - start = [3, 0] - end = [2, 2] - - [[line_segment]] - start = [2, 2] - end = [-2, 2] - [[line_segment]] - start = [-2, 2] - end = [-2, 0] + Example config file: ``` - * `center_point` is a static reference point that's used to make some calculations faster. 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. If one or more LED strip has a different density for whatever reason, you can override this default for each `[[line_segment]]`. - * Add as many `[[line_segment]]` tags as you need to represent your scene. +center: (0.0, 0.5) +density: 30.0 +--segments-- +(-2, 0) --> (0.5, -1) --> (3.5, 0) --> +(2, 2) --> (-2, 2) --> (-2, 0) + ``` + * `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) + ``` ### Drawing Once you have your Sled struct, you can start drawing to it right away! Here’s a taste of some of the things Sled lets you do: diff --git a/src/lib.rs b/src/lib.rs index 9b19716..1cba869 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,34 +24,29 @@ //! //! A configuration file explains the layout of your LED strips in 2D space. This is used to pre-calculate some important information, speeding up complex draw calls. //! -//! Example .toml file: -//! ```toml, no_run -//! center_point = [0.0, 0.5] -//! density = 30.0 -//! -//! [[line_segment]] -//! start = [-2.0, 0.0] -//! end = [0.5, -1.0] -//! -//! [[line_segment]] -//! start = [0.5, -1.0] -//! end = [3.5, 0.0] -//! -//! [[line_segment]] -//! start = [3.5, 0.0] -//! end = [2, 2] -//! -//! [[line_segment]] -//! start = [2.0, 2] -//! end = [-2.0, 2] -//! -//! [[line_segment]] -//! start = [-2.0, 2] -//! end = [-2.0, 0.0] +//! Example file: +//! ```yaml, no_run +//! center: (0.0, 0.5) +//! density: 30.0 +//! --segments-- +//! (-2, 0) --> (0.5, -1) --> (3.5, 0) --> +//! (2, 2) --> (-2, 2) --> (-2, 0) //! ``` -//! * `center_point` is a static 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 LED's per unit we can expect for the line segments below. If one or more LED strip has a different density for whatever reason, you can override this default for each `[[line_segment]]`. -//! * Add as many `[[line_segment]]` tags as you need to represent your scene. +//! * `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: +//! ```yaml, no_run +//! --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: +//! ```yaml, no_run +//! --segments-- +//! (-2, 0) --> (0.5, -1) +//! --> (3.5, 0) | (2, 2) +//! --> (-2, 2) --> (-2, 0) +//! ``` //! ### Drawing //! Once you have your Sled struct, you can start drawing to it right away! Here’s a taste of some of the things SLED lets you do: //!