diff --git a/hcl b/hcl index 001d44d..9dc33a9 160000 --- a/hcl +++ b/hcl @@ -1 +1 @@ -Subproject commit 001d44d6b9693a9ad3fd6fc8bc44e8dc22ad7149 +Subproject commit 9dc33a9abadbde8e0c2ffc853a35feaf6990c143 diff --git a/src/main.rs b/src/main.rs index e569a21..2f8e437 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ mod max6675; mod st7735; use printer::UsartPrinter; -use st7735::gfx::PrimitveGFX; +use st7735::gfx::{PrimitveGFX, GFX}; fn configure_clocks(rcc: &mut rcc::RCC) { @@ -128,6 +128,9 @@ fn run(mut scs: scs::Instance, mut p: hcl::platform::Instance) { st7735io.fill_rect(4, 64, 76, 60, st7735::COLOR_GREEN); st7735io.fill_rect(80, 4, 76, 60, st7735::COLOR_BLUE); st7735io.fill_rect(80, 64, 76, 60, st7735::COLOR_YELLOW); + + + st7735io.draw_line(4, 4, 80, 64, st7735::COLOR_GREEN); systick::delay_ms(2000); } let (st7735, mut spi, mut gpio) = st7735io.done(); diff --git a/src/st7735/gfx.rs b/src/st7735/gfx.rs index 43c2c5a..c458627 100644 --- a/src/st7735/gfx.rs +++ b/src/st7735/gfx.rs @@ -1,30 +1,88 @@ pub trait PrimitveGFX { - type ColorType; - type CoordinateType; - - fn draw_pixel(&mut self, - x: Self::CoordinateType, - y: Self::CoordinateType, - color: Self::ColorType); + x: u8, + y: u8, + color: u16); fn fill_rect(&mut self, - x: Self::CoordinateType, - y: Self::CoordinateType, - w: Self::CoordinateType, - h: Self::CoordinateType, - color: Self::ColorType); + x: u8, + y: u8, + w: u8, + h: u8, + color: u16); fn draw_fast_vline(&mut self, - x: Self::CoordinateType, - y: Self::CoordinateType, - h: Self::CoordinateType, - color: Self::ColorType); + x: u8, + y: u8, + h: u8, + color: u16); fn draw_fast_hline(&mut self, - x: Self::CoordinateType, - y: Self::CoordinateType, - w: Self::CoordinateType, - color: Self::ColorType); + x: u8, + y: u8, + w: u8, + color: u16); +} + + +pub trait GFX : PrimitveGFX { + fn draw_line(&mut self, x0 : u8, y0 : u8, x1 : u8, y1 : u8, color : u16) { + let steep_dir_vertical = ((y1 as i16) - (y0 as i16)).abs() > ((x1 as i16) - (x0 as i16)).abs(); + + + let (x0, y0, x1, y1) = if (steep_dir_vertical) { + (y0, x0, y1, x1) + } + else { + (x0, y0, x1, y1) + }; + + let (x0, y0, x1, y1) = if (x0 > x1) { + (x1, y1, x0, y0) + } + else { + (x0, y0, x1, y1) + }; + + + let dx = (x1 as i16) - (x0 as i16); + let dy = ((y1 as i16) - (y0 as i16)).abs(); + + let mut err = dx / 2; + + let y_step : i16 = if (y0 < y1) { + 1 + } else { + -1 + }; + + + let mut seg = x0; + let mut cur_y = y0 as i16; + for cur_x in x0..(x1 + 1) { + err -= dy; + if (err < 0) { + if (steep_dir_vertical) { + self.draw_fast_vline(cur_y as u8, seg, cur_x - seg + 1, color); + } else { + self.draw_fast_hline(seg, cur_y as u8, cur_x - seg + 1, color); + } + cur_y += y_step; + err += dx; + seg = cur_x + 1; + } + } + + // Finish line case there is a piece missing + let cur_y = cur_y as u8; + let last_len = ((x1 as i16) - (seg as i16) + 1) as u8; + + if steep_dir_vertical { + self.draw_fast_vline(cur_y, seg, last_len, color); + } else { + self.draw_fast_hline(seg, cur_y, last_len, color); + } + + } } diff --git a/src/st7735/mod.rs b/src/st7735/mod.rs index 4da2a26..23e987b 100644 --- a/src/st7735/mod.rs +++ b/src/st7735/mod.rs @@ -429,11 +429,9 @@ impl St7735IO 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) { + fn draw_pixel(&mut self, x: u8, y: u8, + color: u16) { if x >= self.st7735.width || y >= self.st7735.height { return; } @@ -448,9 +446,9 @@ impl gfx::PrimitveGFX for St7735IO self.set_cs(); } - fn fill_rect(&mut self, x: Self::CoordinateType, y: Self::CoordinateType, - w: Self::CoordinateType, h: Self::CoordinateType, - color: Self::ColorType) { + fn fill_rect(&mut self, x: u8, y: u8, + w: u8, h: u8, + color: u16) { if x >= self.st7735.width || y >= self.st7735.height { return; @@ -485,19 +483,22 @@ impl gfx::PrimitveGFX for St7735IO } fn draw_fast_vline(&mut self, - x: Self::CoordinateType, - y: Self::CoordinateType, - h: Self::CoordinateType, - color: Self::ColorType) { + x: u8, + y: u8, + h: u8, + color: u16) { self.fill_rect(x, y, 1, h, color); } fn draw_fast_hline(&mut self, - x: Self::CoordinateType, - y: Self::CoordinateType, - w: Self::CoordinateType, - color: Self::ColorType) { + x: u8, + y: u8, + w: u8, + color: u16) { self.fill_rect(x, y, w, 1, color); } - } + +impl gfx::GFX for St7735IO + where SPIAddr: Location, + GPIOAddr: Location {}