diff --git a/src/axis.rs b/src/axis.rs new file mode 100644 index 0000000..a09c678 --- /dev/null +++ b/src/axis.rs @@ -0,0 +1,82 @@ +use embedded_graphics::primitives::Line; +use embedded_graphics::drawable::Drawable; +use embedded_graphics::DrawTarget; +use core::ops::Range; +use embedded_graphics::prelude::*; +use embedded_graphics::style::{PrimitiveStyle}; +use core::borrow::Borrow; + +enum Orientation { + X { + x1: i32, + x2: i32, + y: i32, + }, + Y { + y1: i32, + y2: i32, + x: i32, + }, +} + +enum Scale { + Fixed { + interval: usize, + }, + RangeFraction { + fraction: usize, + }, +} + +pub struct Axis { + orientation: Orientation, + range: Range, + scale: Scale, + color: C +} + +impl Axis + where + C: PixelColor, +{ + fn new(orientation: Orientation, range: Range, scale: Scale, color: C) -> Axis { + Axis { orientation, range, scale, color } + } +} + + +impl Drawable for Axis + where + C: PixelColor, +{ + fn draw>(self, display: &mut D) -> Result<(), D::Error> { + let lines = match self.scale { + Scale::Fixed { interval } => { + self.range.step_by(interval) + } + Scale::RangeFraction { fraction } => { + let len = self.range.len(); + self.range.step_by(len / fraction) + } + }; + match self.orientation { + Orientation::X{x1,x2,y} => { + Line{start: Point{x: x1,y}, end: Point{x: x2,y}} + .into_styled(PrimitiveStyle::with_stroke(self.color,1)) + .draw(display)?; + for line in lines { + line.abs(); + } + } + Orientation::Y{y1,y2,x} => { + Line{start: Point{x,y: y1}, end: Point{x,y: y2}} + .into_styled(PrimitiveStyle::with_stroke(self.color,1)) + .draw(display)?; + for line in lines { + line.abs(); + } + } + } + Ok(()) + } +} \ No newline at end of file diff --git a/src/drawable_curve.rs b/src/drawable_curve.rs index c082045..4699bb6 100644 --- a/src/drawable_curve.rs +++ b/src/drawable_curve.rs @@ -6,8 +6,6 @@ use embedded_graphics::primitives::{Line, Primitive}; use embedded_graphics::style::PrimitiveStyle; pub struct DrawableCurve - where - I: Iterator, { scaled_data: I, color: C, diff --git a/src/lib.rs b/src/lib.rs index b8033c5..b4e540a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,3 +6,4 @@ pub mod single_plot; mod drawable_curve; mod range_conv; +mod axis; diff --git a/src/plot.rs b/src/plot.rs deleted file mode 100644 index 44d6f95..0000000 --- a/src/plot.rs +++ /dev/null @@ -1,37 +0,0 @@ -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 diff --git a/src/polyplot.rs b/src/polyplot.rs index 9ce8f82..34ae499 100644 --- a/src/polyplot.rs +++ b/src/polyplot.rs @@ -5,8 +5,6 @@ use embedded_graphics::prelude::Point; use embedded_graphics::pixelcolor::PixelColor; pub struct PolyPlot<'a, C> - where - C: PixelColor { curves: &'a [(Curve<'a>, C)], top_left: Point,