Moved graphics primitives into own trait

This commit is contained in:
Sebastian 2018-09-07 02:04:07 +02:00
parent 307b3d6e72
commit eb74151b3b
3 changed files with 65 additions and 11 deletions

View File

@ -25,6 +25,7 @@ mod max6675;
mod st7735;
use printer::UsartPrinter;
use st7735::gfx::PrimitveGFX;
fn configure_clocks(rcc: &mut rcc::RCC) {

30
src/st7735/gfx.rs Normal file
View File

@ -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);
}

View File

@ -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<SPIAddr, GPIOAddr> St7735IO<SPIAddr, GPIOAddr>
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<spi::SPI, SPIAddr>, PeripheralRef<gpio::GPIO, GPIOAddr>) {
self.set_cs();
self.spi.set_enabled(false);
(self.st7735, self.spi, self.gpio)
}
}
impl<SPIAddr, GPIOAddr> gfx::PrimitveGFX for St7735IO<SPIAddr, GPIOAddr>
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<SPIAddr, GPIOAddr> St7735IO<SPIAddr, GPIOAddr>
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<SPIAddr, GPIOAddr> St7735IO<SPIAddr, GPIOAddr>
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<spi::SPI, SPIAddr>, PeripheralRef<gpio::GPIO, GPIOAddr>) {
self.set_cs();
self.spi.set_enabled(false);
(self.st7735, self.spi, self.gpio)
}
}