Skip to content

Commit

Permalink
Merge pull request #68 from DavJCosby/smaller-drivers
Browse files Browse the repository at this point in the history
shrink down drivers struct
  • Loading branch information
DavJCosby authored Feb 8, 2024
2 parents 2fed25a + 5936dca commit de0635f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 62 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[features]
default = ["drivers", "scheduler"]
drivers = ["micromap", "compact_str"]
drivers = ["compact_str"]
scheduler = ["spin_sleep"]
named_colors = []

Expand All @@ -18,7 +18,6 @@ palette = { version = "0.7.3", default-features = false, features = [
] }
toml = "0.8.0"
smallvec = "1.11.2"
micromap = { version = "0.0.15", optional = true }
compact_str = { version = "0.7", optional = true }
spin_sleep = { version = "1.1.1", optional = true }

Expand Down
3 changes: 0 additions & 3 deletions examples/calibration.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
mod resources;
use std::mem::size_of;

use resources::tui::SledTerminalDisplay;

use glam::Vec2;
use sled::{color::Rgb, Sled, SledError};

fn main() -> Result<(), SledError> {
print!("{}", size_of::<Sled>());
let mut sled = Sled::new("./examples/resources/config.toml")?;
let mut display = SledTerminalDisplay::start("Calibration", sled.domain());

Expand All @@ -22,4 +20,3 @@ fn main() -> Result<(), SledError> {
display.refresh().unwrap();
Ok(())
}

8 changes: 4 additions & 4 deletions examples/quirky_trail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ mod drivers;
use drivers::quirky_trail;

mod resources;
use resources::tui::SledTerminalDisplay;
// use resources::tui::SledTerminalDisplay;

use sled::{scheduler::Scheduler, Sled};

fn main() {
let sled = Sled::new("./examples/resources/config.toml").unwrap();
let mut display = SledTerminalDisplay::start("Quirky Trail", sled.domain());
// let mut display = SledTerminalDisplay::start("Quirky Trail", sled.domain());
let mut driver = quirky_trail::build_driver();
driver.mount(sled);

let mut scheduler = Scheduler::fixed_hz(500.0);
scheduler.loop_until_err(|| {
driver.step();
display.leds = driver.read_colors_and_positions();
display.refresh()?;
// display.leds = driver.read_colors_and_positions();
// display.refresh()?;
Ok(())
});
}
78 changes: 33 additions & 45 deletions src/driver/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,72 @@ use std::any::type_name;

use compact_str::{CompactString, ToCompactString};
use glam::Vec2;
use micromap::Map;
use palette::rgb::Rgb;
use smallvec::SmallVec;

const MAX_BUFFERS: usize = 4;
const DEFAULT_BUFFER_SIZE: usize = 12;

pub trait BufferDataStructure {}
impl<A: smallvec::Array> BufferDataStructure for SmallVec<A> {}

pub type Buffer<T, const N: usize = DEFAULT_BUFFER_SIZE> = SmallVec<[T; N]>;
use std::collections::HashMap;

pub struct BufferContainer {
f32s: Map<CompactString, Buffer<f32>, MAX_BUFFERS>,
rgbs: Map<CompactString, Buffer<Rgb>, MAX_BUFFERS>,
bools: Map<CompactString, Buffer<bool>, MAX_BUFFERS>,
vec2s: Map<CompactString, Buffer<Vec2>, MAX_BUFFERS>,
usizes: Map<CompactString, Buffer<usize>, MAX_BUFFERS>,
f32s: HashMap<CompactString, Vec<f32>>,
rgbs: HashMap<CompactString, Vec<Rgb>>,
bools: HashMap<CompactString, Vec<bool>>,
vec2s: HashMap<CompactString, Vec<Vec2>>,
usizes: HashMap<CompactString, Vec<usize>>,
}

mod internal_traits {
use super::{Buffer, BufferContainer};
use super::{CompactString, Map, MAX_BUFFERS};
use super::BufferContainer;
use super::{CompactString, HashMap};
use super::{Rgb, Vec2};
pub trait MapForType<T> {
fn map_for_type_mut(&mut self) -> &mut Map<CompactString, Buffer<T>, MAX_BUFFERS>;
fn map_for_type(&self) -> &Map<CompactString, Buffer<T>, MAX_BUFFERS>;
fn map_for_type_mut(&mut self) -> &mut HashMap<CompactString, Vec<T>>;
fn map_for_type(&self) -> &HashMap<CompactString, Vec<T>>;
}

impl MapForType<usize> for BufferContainer {
fn map_for_type(&self) -> &Map<CompactString, Buffer<usize>, MAX_BUFFERS> {
fn map_for_type(&self) -> &HashMap<CompactString, Vec<usize>> {
&self.usizes
}

fn map_for_type_mut(&mut self) -> &mut Map<CompactString, Buffer<usize>, MAX_BUFFERS> {
fn map_for_type_mut(&mut self) -> &mut HashMap<CompactString, Vec<usize>> {
&mut self.usizes
}
}

impl MapForType<bool> for BufferContainer {
fn map_for_type_mut(&mut self) -> &mut Map<CompactString, Buffer<bool>, MAX_BUFFERS> {
fn map_for_type_mut(&mut self) -> &mut HashMap<CompactString, Vec<bool>> {
&mut self.bools
}

fn map_for_type(&self) -> &Map<CompactString, Buffer<bool>, MAX_BUFFERS> {
fn map_for_type(&self) -> &HashMap<CompactString, Vec<bool>> {
&self.bools
}
}

impl MapForType<f32> for BufferContainer {
fn map_for_type_mut(&mut self) -> &mut Map<CompactString, Buffer<f32>, MAX_BUFFERS> {
fn map_for_type_mut(&mut self) -> &mut HashMap<CompactString, Vec<f32>> {
&mut self.f32s
}

fn map_for_type(&self) -> &Map<CompactString, Buffer<f32>, MAX_BUFFERS> {
fn map_for_type(&self) -> &HashMap<CompactString, Vec<f32>> {
&self.f32s
}
}

impl MapForType<Rgb> for BufferContainer {
fn map_for_type_mut(&mut self) -> &mut Map<CompactString, Buffer<Rgb>, MAX_BUFFERS> {
fn map_for_type_mut(&mut self) -> &mut HashMap<CompactString, Vec<Rgb>> {
&mut self.rgbs
}

fn map_for_type(&self) -> &Map<CompactString, Buffer<Rgb>, MAX_BUFFERS> {
fn map_for_type(&self) -> &HashMap<CompactString, Vec<Rgb>> {
&self.rgbs
}
}

impl MapForType<Vec2> for BufferContainer {
fn map_for_type_mut(&mut self) -> &mut Map<CompactString, Buffer<Vec2>, MAX_BUFFERS> {
fn map_for_type_mut(&mut self) -> &mut HashMap<CompactString, Vec<Vec2>> {
&mut self.vec2s
}

fn map_for_type(&self) -> &Map<CompactString, Buffer<Vec2>, MAX_BUFFERS> {
fn map_for_type(&self) -> &HashMap<CompactString, Vec<Vec2>> {
&self.vec2s
}
}
Expand All @@ -86,36 +77,35 @@ use crate::SledError;

pub use self::internal_traits::MapForType;

#[allow(dead_code)]
impl BufferContainer {
pub fn new() -> Self {
BufferContainer {
f32s: Map::new(),
rgbs: Map::new(),
bools: Map::new(),
vec2s: Map::new(),
usizes: Map::new(),
f32s: HashMap::new(),
rgbs: HashMap::new(),
bools: HashMap::new(),
vec2s: HashMap::new(),
usizes: HashMap::new(),
}
}

pub fn create<T>(&mut self, buffer_name: &str) -> &mut Buffer<T>
pub fn create<T>(&mut self, buffer_name: &str) -> &mut Vec<T>
where
BufferContainer: MapForType<T>,
{
let map = self.map_for_type_mut();
map.insert(buffer_name.to_compact_string(), Buffer::new());
&mut map[buffer_name]
map.insert(buffer_name.to_compact_string(), Vec::new());
map.get_mut(buffer_name).unwrap()
}

pub fn get_buffer<T>(&self, buffer_name: &str) -> Option<&Buffer<T>>
pub fn get_buffer<T>(&self, buffer_name: &str) -> Option<&Vec<T>>
where
BufferContainer: MapForType<T>,
{
let map = self.map_for_type();
map.get(buffer_name)
}

pub fn get_buffer_mut<T>(&mut self, buffer_name: &str) -> Option<&mut Buffer<T>>
pub fn get_buffer_mut<T>(&mut self, buffer_name: &str) -> Option<&mut Vec<T>>
where
BufferContainer: MapForType<T>,
{
Expand All @@ -138,7 +128,7 @@ impl BufferContainer {
{
let buffer = self.get_buffer_mut(buffer_name).ok_or_else(|| {
SledError::new(format!(
"There is no Buffer<{}> with the name `{}`.",
"There is no Vec<{}> with the name `{}`.",
type_name::<T>(),
buffer_name
))
Expand All @@ -154,7 +144,7 @@ impl BufferContainer {
{
let buffer = self.get_buffer_mut(buffer_name).ok_or_else(|| {
SledError::new(format!(
"There is no Buffer<{}> with the name `{}`.",
"There is no Vec<{}> with the name `{}`.",
type_name::<T>(),
buffer_name
))
Expand All @@ -172,10 +162,8 @@ fn deref_option<T: Copy>(option: Option<&T>) -> Option<T> {
}
}

trait TypedIndex<T> {}

impl std::ops::Index<&str> for BufferContainer {
type Output = Buffer<f32>;
type Output = Vec<f32>;

fn index(&self, index: &str) -> &Self::Output {
&self.f32s[index]
Expand Down
11 changes: 6 additions & 5 deletions src/driver/filters.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use compact_str::{CompactString, ToCompactString};
use micromap::Map;

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

pub struct Filters {
map: Map<CompactString, Filter, 12>,
map: HashMap<CompactString, Filter>,
}

impl Default for Filters {
Expand All @@ -15,7 +14,9 @@ impl Default for Filters {

impl Filters {
pub fn new() -> Self {
Filters { map: Map::new() }
Filters {
map: HashMap::new(),
}
}

pub fn set(&mut self, key: &str, value: Filter) {
Expand Down
4 changes: 1 addition & 3 deletions src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::{Duration, Instant};
mod filters;
// mod sliders;
mod buffers;
pub use buffers::{Buffer, BufferContainer};
pub use buffers::BufferContainer;
pub use filters::Filters;

pub enum RefreshTiming {
Expand All @@ -23,7 +23,6 @@ type ComputeCommands =
Box<dyn Fn(&Sled, &mut BufferContainer, &mut Filters, &TimeInfo) -> SledResult>;
type DrawCommands = Box<dyn Fn(&mut Sled, &BufferContainer, &Filters, &TimeInfo) -> SledResult>;
pub struct Driver {
_timing_strategy: RefreshTiming,
sled: Option<Sled>,
startup_commands: StartupCommands,
compute_commands: ComputeCommands,
Expand All @@ -43,7 +42,6 @@ impl Default for Driver {
impl Driver {
pub fn new() -> Self {
Driver {
_timing_strategy: RefreshTiming::None,
sled: None,
startup_commands: Box::new(|_, _, _| Ok(())),
compute_commands: Box::new(|_, _, _, _| Ok(())),
Expand Down

0 comments on commit de0635f

Please sign in to comment.