Skip to content

Commit

Permalink
Make crate compatible with no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiomattera committed Nov 12, 2024
1 parent 2743f2a commit 8ab642f
Show file tree
Hide file tree
Showing 28 changed files with 444 additions and 162 deletions.
14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ documentation = "https://docs.rs/spatial_led"
exclude = ["*.gif", "*.cast"]

[features]
default = ["drivers", "scheduler"]
default = ["drivers", "scheduler", "std"]
drivers = ["compact_str", "sled_driver_macros"]
scheduler = ["spin_sleep"]
scheduler = []
named_colors = []
std = ["glam/std"]
async = []
spin_sleep = ["std", "dep:spin_sleep"]

[dependencies]
glam = { version = "0.29" }
glam = { version = "0.29", default-features = false, features = ["libm"] }
palette = { version = "0.7", default-features = false, features = [
"std",
"libm",
"approx",
] }
smallvec = "1.13"
compact_str = { version = "0.8", optional = true }
compact_str = { version = "0.8", default-features = false, optional = true }
sled_driver_macros = { version = "0.1.2", optional = true }
num-traits = { version = "0.2", default-features = false }
spin_sleep = { version = "1.2", optional = true }

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions examples/comet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use drivers::comet;
mod resources;
use resources::tui::SledTerminalDisplay;

use spatial_led::{scheduler::Scheduler, Sled};
use spatial_led::{scheduler::StdScheduler, Sled};

fn main() {
let sled = Sled::new("./examples/resources/complex_room.yap").unwrap();
let mut display = SledTerminalDisplay::start("Comet", sled.domain());
let mut driver = comet::build_driver();
driver.mount(sled);

let mut scheduler = Scheduler::new(500.0);
let mut scheduler = StdScheduler::new(500.0);
scheduler.loop_until_err(|| {
driver.step();
display.set_leds(driver.colors_and_positions_coerced());
Expand Down
6 changes: 3 additions & 3 deletions examples/drivers/comet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use spatial_led::driver::{StdDriver, TimeInfo};
use spatial_led::driver_macros::*;
use spatial_led::driver::{Driver, TimeInfo};
use spatial_led::SledResult;
use spatial_led::{color::Rgb, Sled};

Expand All @@ -17,8 +17,8 @@ const BLUE: Rgb = Rgb::new(0.4, 0.51, 0.93);
const TRAIL_RADIUS: f32 = 1.2;

#[allow(dead_code)]
pub fn build_driver() -> Driver {
let mut driver = Driver::new();
pub fn build_driver() -> StdDriver {
let mut driver = StdDriver::new();
driver.set_draw_commands(draw);
driver
}
Expand Down
2 changes: 1 addition & 1 deletion examples/drivers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod comet;
pub mod ripples;
pub mod scan;
pub mod warpspeed;
pub mod scan;
8 changes: 4 additions & 4 deletions examples/drivers/ripples.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use spatial_led::driver_macros::*;
use rand::Rng;
use spatial_led::driver::{BufferContainer, Driver, TimeInfo};
use spatial_led::driver::{BufferContainer, StdDriver, TimeInfo};
use spatial_led::driver_macros::*;
use spatial_led::SledResult;
use spatial_led::{color::Rgb, Sled, Vec2};
use std::ops::Range;
Expand All @@ -11,8 +11,8 @@ const FEATHERING: f32 = 0.15;
const INV_F: f32 = 1.0 / FEATHERING;

#[allow(dead_code)]
pub fn build_driver() -> Driver {
let mut driver = Driver::new();
pub fn build_driver() -> StdDriver {
let mut driver = StdDriver::new();

driver.set_startup_commands(startup);
driver.set_compute_commands(compute);
Expand Down
6 changes: 3 additions & 3 deletions examples/drivers/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use std::f32::consts::{PI, TAU};
use std::time::Duration;

use glam::Vec2;
use spatial_led::driver::{Driver, TimeInfo};
use spatial_led::driver::{StdDriver, TimeInfo};
use spatial_led::driver_macros::*;
use spatial_led::BufferContainer;
use spatial_led::{color::Rgb, Sled, SledResult};

const SCAN_DURATION: f32 = 4.0;

#[allow(dead_code)]
pub fn build_driver() -> Driver {
let mut driver = Driver::new();
pub fn build_driver() -> StdDriver {
let mut driver = StdDriver::new();
driver.set_startup_commands(startup);
driver.set_compute_commands(compute);
driver.set_draw_commands(draw);
Expand Down
8 changes: 4 additions & 4 deletions examples/drivers/warpspeed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use spatial_led::driver_macros::*;
use rand::Rng;
use spatial_led::driver::{BufferContainer, Driver, TimeInfo};
use spatial_led::driver::{BufferContainer, StdDriver, TimeInfo};
use spatial_led::driver_macros::*;
use spatial_led::SledResult;
use spatial_led::{color::Rgb, Sled, Vec2};

Expand All @@ -9,8 +9,8 @@ const VELOCITY: f32 = 6.0;
const DIRECTION: Vec2 = Vec2::new(0.7071, -0.7071);

#[allow(dead_code)]
pub fn build_driver() -> Driver {
let mut driver = Driver::new();
pub fn build_driver() -> StdDriver {
let mut driver = StdDriver::new();

driver.set_startup_commands(startup);
driver.set_compute_commands(compute);
Expand Down
4 changes: 2 additions & 2 deletions examples/ripples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use drivers::ripples;
mod resources;
use resources::tui::SledTerminalDisplay;

use spatial_led::{scheduler::Scheduler, Sled};
use spatial_led::{scheduler::StdScheduler, Sled};

fn main() {
let sled = Sled::new("./examples/resources/complex_room.yap").unwrap();
let mut display = SledTerminalDisplay::start("Ripples", sled.domain());
let mut driver = ripples::build_driver();
driver.mount(sled);

let mut scheduler = Scheduler::new(500.0);
let mut scheduler = StdScheduler::new(500.0);
scheduler.loop_until_err(|| {
driver.step();
display.set_leds(driver.colors_and_positions_coerced());
Expand Down
4 changes: 2 additions & 2 deletions examples/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use drivers::scan;
mod resources;
use resources::tui::SledTerminalDisplay;

use spatial_led::{scheduler::Scheduler, Sled};
use spatial_led::{scheduler::StdScheduler, Sled};

fn main() {
let sled = Sled::new("./examples/resources/complex_room.yap").unwrap();
let mut display = SledTerminalDisplay::start("Scan", sled.domain());
let mut driver = scan::build_driver();
driver.mount(sled);

let mut scheduler = Scheduler::new(500.0);
let mut scheduler = StdScheduler::new(500.0);
scheduler.loop_until_err(|| {
driver.step();
display.set_leds(driver.colors_and_positions_coerced());
Expand Down
4 changes: 2 additions & 2 deletions examples/warpspeed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use drivers::warpspeed;
mod resources;
use resources::tui::SledTerminalDisplay;

use spatial_led::{color::Rgb, scheduler::Scheduler, Sled};
use spatial_led::{color::Rgb, scheduler::StdScheduler, Sled};

fn main() {
let sled = Sled::new("./examples/resources/complex_room.yap").unwrap();
Expand All @@ -19,7 +19,7 @@ fn main() {
Rgb::new(0.0, 0.0, 1.0),
]);

let mut scheduler = Scheduler::new(500.0);
let mut scheduler = StdScheduler::new(500.0);
scheduler.loop_until_err(|| {
driver.step();
display.set_leds(driver.colors_and_positions_coerced());
Expand Down
2 changes: 1 addition & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod consts {
//!
//! Adapted from palette's [named](https://docs.rs/palette/0.7.3/palette/named/index.html) module
//! but expressed as 32-bit rgb instead of 8-bit for better compatability with sled.
//!
//!
//! Colors are taken from the [SVG keyword
//! colors](https://www.w3.org/TR/SVG11/types.html#ColorKeywords) (same as in
//! CSS3) and they can be used as if they were pixel values:
Expand Down
17 changes: 13 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use alloc::string::String;
use alloc::string::ToString as _;
use alloc::vec;
use alloc::vec::Vec;

use crate::error::SledError;
use core::str::Lines;
use glam::Vec2;
use smallvec::SmallVec;
use std::{fs, str::Lines};

#[cfg(not(feature = "std"))]
use num_traits::float::Float as _;

pub(crate) struct Config {
pub center_point: Vec2,
Expand Down Expand Up @@ -95,7 +103,7 @@ fn extract_segments_from_string(s: &str) -> Vec<LineSegment> {
}

impl Config {
pub fn from_string(string: String) -> Result<Self, SledError> {
pub fn from_string(string: &str) -> Result<Self, SledError> {
let mut lines = string.lines();

let (center, density) = extract_center_and_density_from_lines(&mut lines);
Expand All @@ -121,9 +129,10 @@ impl Config {
})
}

#[cfg(feature = "std")]
pub fn from_toml_file(path: &str) -> Result<Self, SledError> {
let as_string = fs::read_to_string(path).map_err(SledError::from_error)?;
Config::from_string(as_string)
let as_string = std::fs::read_to_string(path).map_err(SledError::from_error)?;
Config::from_string(&as_string)
}
}

Expand Down
26 changes: 15 additions & 11 deletions src/driver/buffers.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
use std::{
use core::{
any::{type_name, Any},
fmt::Debug,
};

use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::format;
use alloc::vec::Vec;

use compact_str::{CompactString, ToCompactString};
use std::collections::HashMap;

use crate::SledError;

#[derive(Debug)]
pub struct BufferContainer {
buffers: HashMap<CompactString, Box<dyn Buffer>>,
buffers: BTreeMap<CompactString, Box<dyn Buffer>>,
}

impl BufferContainer {
pub fn new() -> Self {
BufferContainer {
buffers: HashMap::new(),
buffers: BTreeMap::new(),
}
}

Expand Down Expand Up @@ -235,18 +239,18 @@ impl BufferContainer {
Ok(())
}

pub fn iter(&self) -> std::collections::hash_map::Iter<CompactString, Box<dyn Buffer>> {
pub fn iter(&self) -> alloc::collections::btree_map::Iter<CompactString, Box<dyn Buffer>> {
self.buffers.iter()
}

pub fn iter_mut(
&mut self,
) -> std::collections::hash_map::IterMut<CompactString, Box<dyn Buffer>> {
) -> alloc::collections::btree_map::IterMut<CompactString, Box<dyn Buffer>> {
self.buffers.iter_mut()
}
}

pub trait Buffer: std::fmt::Debug {
pub trait Buffer: core::fmt::Debug {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
Expand All @@ -264,16 +268,16 @@ impl<T: BufferableData + Debug> Buffer for Vec<T> {
}
}

impl std::iter::IntoIterator for BufferContainer {
impl core::iter::IntoIterator for BufferContainer {
type Item = (CompactString, Box<dyn Buffer>);
type IntoIter = std::collections::hash_map::IntoIter<CompactString, Box<dyn Buffer>>;
type IntoIter = alloc::collections::btree_map::IntoIter<CompactString, Box<dyn Buffer>>;

fn into_iter(self) -> Self::IntoIter {
self.buffers.into_iter()
}
}

impl std::iter::FromIterator<(CompactString, Box<dyn Buffer>)> for BufferContainer {
impl core::iter::FromIterator<(CompactString, Box<dyn Buffer>)> for BufferContainer {
fn from_iter<T: IntoIterator<Item = (CompactString, Box<dyn Buffer>)>>(iter: T) -> Self {
let mut bc = BufferContainer::new();

Expand All @@ -285,7 +289,7 @@ impl std::iter::FromIterator<(CompactString, Box<dyn Buffer>)> for BufferContain
}
}

impl std::iter::Extend<(CompactString, Box<dyn Buffer>)> for BufferContainer {
impl core::iter::Extend<(CompactString, Box<dyn Buffer>)> for BufferContainer {
fn extend<T: IntoIterator<Item = (CompactString, Box<dyn Buffer>)>>(&mut self, iter: T) {
for (key, value) in iter {
self.buffers.insert(key, value);
Expand Down
15 changes: 9 additions & 6 deletions src/driver/filters.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::{Filter, SledError};
use alloc::collections::BTreeMap;
use alloc::format;

use compact_str::{CompactString, ToCompactString};
use std::collections::HashMap;

use crate::{Filter, SledError};

#[derive(Clone, Debug)]
pub struct Filters {
map: HashMap<CompactString, Filter>,
map: BTreeMap<CompactString, Filter>,
}

impl Default for Filters {
Expand All @@ -16,7 +19,7 @@ impl Default for Filters {
impl Filters {
pub fn new() -> Self {
Filters {
map: HashMap::new(),
map: BTreeMap::new(),
}
}

Expand All @@ -30,14 +33,14 @@ impl Filters {
.ok_or_else(|| SledError::new(format!("No filter found with key '{}'", key)))
}

pub fn iter(&self) -> std::collections::hash_map::Iter<CompactString, Filter> {
pub fn iter(&self) -> alloc::collections::btree_map::Iter<CompactString, Filter> {
self.map.iter()
}
}

impl IntoIterator for Filters {
type Item = (CompactString, Filter);
type IntoIter = std::collections::hash_map::IntoIter<CompactString, Filter>;
type IntoIter = alloc::collections::btree_map::IntoIter<CompactString, Filter>;

fn into_iter(self) -> Self::IntoIter {
self.map.into_iter()
Expand Down
Loading

0 comments on commit 8ab642f

Please sign in to comment.