From c23edcb93cbf88e2521a10b6c13614c93ac74e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Chodzikiewicz?= Date: Sat, 19 Dec 2020 15:09:32 +0100 Subject: [PATCH] Remove allocation --- examples/basic-plot/main.rs | 15 ++++--- src/curve.rs | 46 ++++++++++++++++++++ src/drawable_curve.rs | 42 +++++++++++++++++++ src/lib.rs | 84 ++----------------------------------- 4 files changed, 98 insertions(+), 89 deletions(-) create mode 100644 src/curve.rs create mode 100644 src/drawable_curve.rs diff --git a/examples/basic-plot/main.rs b/examples/basic-plot/main.rs index 5726c0c..4a41585 100644 --- a/examples/basic-plot/main.rs +++ b/examples/basic-plot/main.rs @@ -9,20 +9,19 @@ use embedded_graphics_simulator::{ OutputSettingsBuilder }; -use embedded_plots::{ - CurvePoints, - PlotPoint -}; +use embedded_plots::curve::{PlotPoint, CurvePoints}; fn main() -> Result<(), core::convert::Infallible> { let mut display: SimulatorDisplay = SimulatorDisplay::new(Size::new(480, 272)); let data = vec![ - PlotPoint{x: 100,y: 100}, - PlotPoint{x: 150,y: 100}, - PlotPoint{x: 200,y: 200}]; + PlotPoint{x: 0,y: 0}, + PlotPoint{x: 1,y: 1}, + PlotPoint{x: 2,y: 1}, + PlotPoint{x: 3,y: 0}, + ]; CurvePoints::new(data.as_slice()) - .into_drawable_curve(&(100..200),&(100..200),Point{x: 00, y: 0}, Point{x:480,y:272},RgbColor::WHITE) + .into_drawable_curve(&(0..3),&(0..1),&Point{x: 20, y: 20}, &Point{x:450,y:250},RgbColor::WHITE) .draw(&mut display)?; let output_settings = OutputSettingsBuilder::new() diff --git a/src/curve.rs b/src/curve.rs new file mode 100644 index 0000000..93333d7 --- /dev/null +++ b/src/curve.rs @@ -0,0 +1,46 @@ +use core::ops::{Range}; + +use crate::range_conv::Scalable; +use crate::drawable_curve::DrawableCurve; +use embedded_graphics::prelude::*; + +pub struct PlotPoint { + pub x: i32, + pub y: i32, +} + +pub struct CurvePoints<'a>{ + points: &'a [PlotPoint], +} + +impl<'a> CurvePoints<'a> { + pub fn new(points: &'a [PlotPoint]) -> CurvePoints { + CurvePoints{points} + } + + pub fn into_drawable_curve(self, x_range: &'a Range, y_range: &'a Range, top_left : &'a Point, bottom_right: &'a Point, color: C) -> DrawableCurve + 'a> + where C: PixelColor + { + assert!(top_left.x < bottom_right.x); + assert!(top_left.y < bottom_right.y); + assert!(!x_range.is_empty()); + assert!(!y_range.is_empty()); + + let it = self.points.iter() + .map(move |p| Point{ + x: p.x.scale_between_ranges(x_range,&Range{start: top_left.x, end: bottom_right.x}), + y: p.y.scale_between_ranges(y_range,&Range{start: bottom_right.y, end: top_left.y}), + }); + DrawableCurve::new(it,color) + } +} + + + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} diff --git a/src/drawable_curve.rs b/src/drawable_curve.rs new file mode 100644 index 0000000..a45656a --- /dev/null +++ b/src/drawable_curve.rs @@ -0,0 +1,42 @@ +use embedded_graphics::drawable::{Drawable, Pixel}; +use embedded_graphics::DrawTarget; +use embedded_graphics::geometry::Point; +use embedded_graphics::pixelcolor::{PixelColor}; +use embedded_graphics::primitives::{Line, Primitive}; +use embedded_graphics::style::PrimitiveStyle; + +pub struct DrawableCurve + where + I: Iterator, +{ + scaled_data: I, + color: C, +} + +impl<'a, C,I> DrawableCurve + where + C: PixelColor, + I: Iterator, +{ + pub fn new(data: I,color : C) -> DrawableCurve { + DrawableCurve { + scaled_data: data, + color, + } + } +} +impl<'a, C,I> Drawable for DrawableCurve + where C: PixelColor, + I: Iterator, +{ + fn draw>(self, display: &mut D) -> Result<(), >::Error> { + let style = PrimitiveStyle::with_stroke(self.color,2); + let mut iter = self.scaled_data.into_iter(); + let mut prev = iter.next().unwrap(); + for point in iter { + Line::new(prev,point).into_styled(style).draw(display)?; + prev = point; + } + Ok(()) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 61ddc8e..d9d35b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,83 +1,5 @@ -// #![no_std] +#![no_std] +pub mod curve; +pub mod drawable_curve; mod range_conv; - -use embedded_graphics::drawable::{Drawable}; -use embedded_graphics::DrawTarget; -use embedded_graphics::geometry::Point; -use embedded_graphics::pixelcolor::{PixelColor}; -use embedded_graphics::primitives::{Line, Primitive}; -use embedded_graphics::style::PrimitiveStyle; - -use crate::range_conv::Scalable; -use core::ops::{Range}; - -pub struct PlotPoint { - pub x: i32, - pub y: i32, -} - -pub struct CurvePoints<'a>{ - points: &'a [PlotPoint], -} - -impl<'a> CurvePoints<'a> { - pub fn new(points: &'a [PlotPoint]) -> CurvePoints { - CurvePoints{points} - } - - pub fn into_drawable_curve(self, x_range: &Range, y_range: &Range, top_left : Point, bottom_right: Point, color: C) -> DrawableCurve - where C: PixelColor - { - assert!(top_left.x < bottom_right.x); - assert!(top_left.y < bottom_right.y); - assert!(!x_range.is_empty()); - assert!(!y_range.is_empty()); - - let vec = self.points.iter() - .map(|p| Point{ - x: p.x.scale_between_ranges(x_range,&Range{start: top_left.x.into(), end: bottom_right.x.into()}).into(), - y: p.y.scale_between_ranges(y_range,&Range{start: top_left.y.into(), end: bottom_right.y.into()}).into(), - }) - .collect(); - DrawableCurve::new(vec,color) - } -} - -pub struct DrawableCurve -{ - scaled_and_pos_data: Vec, - color: C, -} - -impl<'a, C> DrawableCurve - where C: PixelColor -{ - pub fn new(data: Vec,color : C) -> DrawableCurve { - println!("data: {:?}",data); - DrawableCurve { - scaled_and_pos_data: data, - color, - } - } -} - -impl<'a, C> Drawable for DrawableCurve - where C: PixelColor -{ - fn draw>(self, display: &mut D) -> Result<(), >::Error> { - let style = PrimitiveStyle::with_stroke(self.color,2); - for i in 1..self.scaled_and_pos_data.len() { - Line::new(self.scaled_and_pos_data[i-1],self.scaled_and_pos_data[i]).into_styled(style).draw(display)?; - } - Ok(()) - } -} - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -}