From eb74151b3b165cc6a6482ea4620fbf270d288d56 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Fri, 7 Sep 2018 02:04:07 +0200 Subject: [PATCH] Moved graphics primitives into own trait --- src/main.rs | 1 + src/st7735/gfx.rs | 30 ++++++++++++++++++++++++++++++ src/st7735/mod.rs | 45 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 src/st7735/gfx.rs diff --git a/src/main.rs b/src/main.rs index d0f9753..e569a21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ mod max6675; mod st7735; use printer::UsartPrinter; +use st7735::gfx::PrimitveGFX; fn configure_clocks(rcc: &mut rcc::RCC) { diff --git a/src/st7735/gfx.rs b/src/st7735/gfx.rs new file mode 100644 index 0000000..43c2c5a --- /dev/null +++ b/src/st7735/gfx.rs @@ -0,0 +1,30 @@ +pub trait PrimitveGFX { + type ColorType; + type CoordinateType; + + + fn draw_pixel(&mut self, + x: Self::CoordinateType, + y: Self::CoordinateType, + color: Self::ColorType); + + fn fill_rect(&mut self, + x: Self::CoordinateType, + y: Self::CoordinateType, + w: Self::CoordinateType, + h: Self::CoordinateType, + color: Self::ColorType); + + + fn draw_fast_vline(&mut self, + x: Self::CoordinateType, + y: Self::CoordinateType, + h: Self::CoordinateType, + color: Self::ColorType); + + fn draw_fast_hline(&mut self, + x: Self::CoordinateType, + y: Self::CoordinateType, + w: Self::CoordinateType, + color: Self::ColorType); +} diff --git a/src/st7735/mod.rs b/src/st7735/mod.rs index dfa5c19..4da2a26 100644 --- a/src/st7735/mod.rs +++ b/src/st7735/mod.rs @@ -7,9 +7,11 @@ use systick::delay_ms; mod commands; mod init; +pub mod gfx; use st7735::commands::*; + const DEFAULT_WIDTH : u8 = 128; // for 1.44" display const DEFAULT_HEIGHT_144 : u8 = 128; @@ -415,7 +417,23 @@ impl St7735IO self.write_command(&Command::MADCTL(param)); } - pub fn draw_pixel(&mut self, x : u8, y : u8, color : u16) { + + pub fn done(mut self) -> (St7735, PeripheralRef, PeripheralRef) { + self.set_cs(); + self.spi.set_enabled(false); + (self.st7735, self.spi, self.gpio) + } +} + + +impl gfx::PrimitveGFX for St7735IO + where SPIAddr: Location, + GPIOAddr: Location { + type ColorType = u16; + type CoordinateType = u8; + + fn draw_pixel(&mut self, x: Self::CoordinateType, y: Self::CoordinateType, + color: Self::ColorType) { if x >= self.st7735.width || y >= self.st7735.height { return; } @@ -430,7 +448,9 @@ impl St7735IO self.set_cs(); } - pub fn fill_rect(&mut self, x : u8, y : u8, w : u8, h : u8, color: u16) { + fn fill_rect(&mut self, x: Self::CoordinateType, y: Self::CoordinateType, + w: Self::CoordinateType, h: Self::CoordinateType, + color: Self::ColorType) { if x >= self.st7735.width || y >= self.st7735.height { return; @@ -464,17 +484,20 @@ impl St7735IO self.set_cs(); } - pub fn draw_fast_vline(&mut self, x : u8, y : u8, h : u8, color : u16) { - self.fill_rect(x, y, 1, h, color); + fn draw_fast_vline(&mut self, + x: Self::CoordinateType, + y: Self::CoordinateType, + h: Self::CoordinateType, + color: Self::ColorType) { + self.fill_rect(x, y, 1, h, color); } - pub fn draw_fast_hline(&mut self, x : u8, y : u8, w : u8, color : u16) { - self.fill_rect(x, y, w, 1, color); + fn draw_fast_hline(&mut self, + x: Self::CoordinateType, + y: Self::CoordinateType, + w: Self::CoordinateType, + color: Self::ColorType) { + self.fill_rect(x, y, w, 1, color); } - pub fn done(mut self) -> (St7735, PeripheralRef, PeripheralRef) { - self.set_cs(); - self.spi.set_enabled(false); - (self.st7735, self.spi, self.gpio) - } }