From 3f8971b921d2c919c73d16d858ad85a6c933ce07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Chodzikiewicz?= Date: Sun, 20 Dec 2020 12:11:06 +0100 Subject: [PATCH] WIP: Plot --- examples/basic-plot/main.rs | 4 ++-- src/curve.rs | 26 +++++++++++++++++++------- src/drawable_curve.rs | 10 ++++++---- src/lib.rs | 3 ++- src/plot.rs | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 src/plot.rs diff --git a/examples/basic-plot/main.rs b/examples/basic-plot/main.rs index 4a41585..aefa48b 100644 --- a/examples/basic-plot/main.rs +++ b/examples/basic-plot/main.rs @@ -9,7 +9,7 @@ use embedded_graphics_simulator::{ OutputSettingsBuilder }; -use embedded_plots::curve::{PlotPoint, CurvePoints}; +use embedded_plots::curve::{PlotPoint, Curve}; fn main() -> Result<(), core::convert::Infallible> { let mut display: SimulatorDisplay = SimulatorDisplay::new(Size::new(480, 272)); @@ -20,7 +20,7 @@ fn main() -> Result<(), core::convert::Infallible> { PlotPoint{x: 2,y: 1}, PlotPoint{x: 3,y: 0}, ]; - CurvePoints::new(data.as_slice()) + Curve::new(data.as_slice()) .into_drawable_curve(&(0..3),&(0..1),&Point{x: 20, y: 20}, &Point{x:450,y:250},RgbColor::WHITE) .draw(&mut display)?; diff --git a/src/curve.rs b/src/curve.rs index 93333d7..4915622 100644 --- a/src/curve.rs +++ b/src/curve.rs @@ -9,16 +9,22 @@ pub struct PlotPoint { pub y: i32, } -pub struct CurvePoints<'a>{ +pub struct Curve<'a>{ points: &'a [PlotPoint], } -impl<'a> CurvePoints<'a> { - pub fn new(points: &'a [PlotPoint]) -> CurvePoints { - CurvePoints{points} +impl<'a> Curve<'a> { + pub fn new(points: &'a [PlotPoint]) -> Curve { + Curve {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> + 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); @@ -28,8 +34,14 @@ impl<'a> CurvePoints<'a> { 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}), + 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) } diff --git a/src/drawable_curve.rs b/src/drawable_curve.rs index a45656a..2421ed9 100644 --- a/src/drawable_curve.rs +++ b/src/drawable_curve.rs @@ -1,4 +1,4 @@ -use embedded_graphics::drawable::{Drawable, Pixel}; +use embedded_graphics::drawable::{Drawable}; use embedded_graphics::DrawTarget; use embedded_graphics::geometry::Point; use embedded_graphics::pixelcolor::{PixelColor}; @@ -13,7 +13,7 @@ pub struct DrawableCurve color: C, } -impl<'a, C,I> DrawableCurve +impl DrawableCurve where C: PixelColor, I: Iterator, @@ -25,7 +25,7 @@ impl<'a, C,I> DrawableCurve } } } -impl<'a, C,I> Drawable for DrawableCurve +impl Drawable for DrawableCurve where C: PixelColor, I: Iterator, { @@ -34,7 +34,9 @@ impl<'a, C,I> Drawable for DrawableCurve 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)?; + Line::new(prev,point) + .into_styled(style) + .draw(display)?; prev = point; } Ok(()) diff --git a/src/lib.rs b/src/lib.rs index d9d35b6..62c1e0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![no_std] pub mod curve; -pub mod drawable_curve; +pub mod plot; +mod drawable_curve; mod range_conv; diff --git a/src/plot.rs b/src/plot.rs new file mode 100644 index 0000000..44d6f95 --- /dev/null +++ b/src/plot.rs @@ -0,0 +1,37 @@ +use crate::curve::Curve; +use embedded_graphics::drawable::Drawable; +use embedded_graphics::DrawTarget; +use embedded_graphics::prelude::Point; +use embedded_graphics::pixelcolor::PixelColor; + +struct Plot<'a, C> + where + C: PixelColor +{ + curves: &'a [Curve<'a>], + color: C, +} + +impl<'a, C> Plot<'a, C> { + fn new(curves: &'a [Curve<'a>], color : C) -> Plot { + Plot{curves, color} + } +} + +impl Drawable for Plot<'_, C> + where + C: PixelColor +{ + fn draw>(self, display: &mut D) -> Result<(), >::Error> { + for curve in self.curves { + curve.into_drawable_curve( + &(0..50), + &(-10..10), + &Point{x: 10,y: 10}, + &Point{x: 470, y: 270}, + self.color + ).draw(display)?; + } + Ok(()) + } +} \ No newline at end of file