diff --git a/src/curve.rs b/src/curve.rs index e077825..0036141 100644 --- a/src/curve.rs +++ b/src/curve.rs @@ -1,10 +1,16 @@ use core::ops::{Range}; use crate::range_conv::Scalable; -use crate::drawable_curve::DrawableCurve; -use embedded_graphics::prelude::*; use itertools::{Itertools, MinMaxResult::MinMax, MinMaxResult}; +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; + + pub struct PlotPoint { pub x: i32, pub y: i32, @@ -43,8 +49,6 @@ impl<'a> Curve<'a> { pub fn into_drawable_curve(&self, top_left: &'a Point, bottom_right: &'a Point, - color: C, - thickness: usize, ) -> DrawableCurve + '_> where C: PixelColor { @@ -64,10 +68,61 @@ impl<'a> Curve<'a> { &Range { start: bottom_right.y, end: top_left.y }, ), }); - DrawableCurve::new(it, color,thickness) + DrawableCurve { + scaled_data: it, + color: None, + thickness: None, + } } } +pub struct DrawableCurve +{ + scaled_data: I, + color: Option, + thickness: Option, +} + +impl DrawableCurve + where + C: PixelColor, + I: Iterator, +{ + pub fn set_color(mut self, color: C) -> DrawableCurve { + self.color = Some(color); + self + } + pub fn set_thickness(mut self, thickness: usize) -> DrawableCurve { + self.thickness = Some(thickness); + self + } +} + +impl Drawable for DrawableCurve + where C: PixelColor + Default, + I: Iterator, +{ + fn draw>(self, display: &mut D) -> Result<(), D::Error> { + let color = match self.color { + None => C::default(), + Some(c) => c, + }; + let thickness = match self.thickness { + None => 2, + Some(t) => t, + }; + let style = PrimitiveStyle::with_stroke(color, thickness as u32); + 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(()) + } +} #[cfg(test)] mod tests { diff --git a/src/drawable_curve.rs b/src/drawable_curve.rs deleted file mode 100644 index fcc513f..0000000 --- a/src/drawable_curve.rs +++ /dev/null @@ -1,45 +0,0 @@ -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; - -pub struct DrawableCurve -{ - scaled_data: I, - color: C, - thickness: usize, -} - -impl DrawableCurve - where - C: PixelColor, - I: Iterator, -{ - pub(in crate) fn new(data: I, color: C, thickness: usize) -> DrawableCurve { - DrawableCurve { - scaled_data: data, - color, - thickness, - } - } -} - -impl Drawable for DrawableCurve - where C: PixelColor, - I: Iterator, -{ - fn draw>(self, display: &mut D) -> Result<(), D::Error> { - let style = PrimitiveStyle::with_stroke(self.color, self.thickness as u32); - 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 a41e3cb..fafaba5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,5 @@ pub mod axis; pub mod polyplot; pub mod single_plot; -mod drawable_curve; mod range_conv; mod drawable_axis; diff --git a/src/polyplot.rs b/src/polyplot.rs index 6c3984f..279d1c7 100644 --- a/src/polyplot.rs +++ b/src/polyplot.rs @@ -22,16 +22,16 @@ impl<'a, C> PolyPlot<'a, C> impl<'a, C> Drawable for PolyPlot<'a, C> where - C: PixelColor + C: PixelColor + Default, { fn draw>(self, display: &mut D) -> Result<(), D::Error> { for (curve, color) in self.curves { curve.into_drawable_curve( &self.top_left, &self.bottom_right, - *color, - 2 - ).draw(display)?; + ).set_color(*color) + .set_thickness(2) + .draw(display)?; } Ok(()) } diff --git a/src/single_plot.rs b/src/single_plot.rs index 230d962..59b233b 100644 --- a/src/single_plot.rs +++ b/src/single_plot.rs @@ -111,9 +111,9 @@ impl<'a, C> Drawable for DrawableSinglePlot<'a, C> self.plot.curve.into_drawable_curve( &self.top_left, &self.bottom_right, - color, - thickness, - ).draw(display)?; + ).set_color(color) + .set_thickness(thickness) + .draw(display)?; Ok(()) } } \ No newline at end of file