Drawing lines works
This commit is contained in:
parent
eb74151b3b
commit
f367eefb1e
2
hcl
2
hcl
|
@ -1 +1 @@
|
||||||
Subproject commit 001d44d6b9693a9ad3fd6fc8bc44e8dc22ad7149
|
Subproject commit 9dc33a9abadbde8e0c2ffc853a35feaf6990c143
|
|
@ -25,7 +25,7 @@ mod max6675;
|
||||||
mod st7735;
|
mod st7735;
|
||||||
|
|
||||||
use printer::UsartPrinter;
|
use printer::UsartPrinter;
|
||||||
use st7735::gfx::PrimitveGFX;
|
use st7735::gfx::{PrimitveGFX, GFX};
|
||||||
|
|
||||||
|
|
||||||
fn configure_clocks(rcc: &mut rcc::RCC) {
|
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(4, 64, 76, 60, st7735::COLOR_GREEN);
|
||||||
st7735io.fill_rect(80, 4, 76, 60, st7735::COLOR_BLUE);
|
st7735io.fill_rect(80, 4, 76, 60, st7735::COLOR_BLUE);
|
||||||
st7735io.fill_rect(80, 64, 76, 60, st7735::COLOR_YELLOW);
|
st7735io.fill_rect(80, 64, 76, 60, st7735::COLOR_YELLOW);
|
||||||
|
|
||||||
|
|
||||||
|
st7735io.draw_line(4, 4, 80, 64, st7735::COLOR_GREEN);
|
||||||
systick::delay_ms(2000);
|
systick::delay_ms(2000);
|
||||||
}
|
}
|
||||||
let (st7735, mut spi, mut gpio) = st7735io.done();
|
let (st7735, mut spi, mut gpio) = st7735io.done();
|
||||||
|
|
|
@ -1,30 +1,88 @@
|
||||||
pub trait PrimitveGFX {
|
pub trait PrimitveGFX {
|
||||||
type ColorType;
|
|
||||||
type CoordinateType;
|
|
||||||
|
|
||||||
|
|
||||||
fn draw_pixel(&mut self,
|
fn draw_pixel(&mut self,
|
||||||
x: Self::CoordinateType,
|
x: u8,
|
||||||
y: Self::CoordinateType,
|
y: u8,
|
||||||
color: Self::ColorType);
|
color: u16);
|
||||||
|
|
||||||
fn fill_rect(&mut self,
|
fn fill_rect(&mut self,
|
||||||
x: Self::CoordinateType,
|
x: u8,
|
||||||
y: Self::CoordinateType,
|
y: u8,
|
||||||
w: Self::CoordinateType,
|
w: u8,
|
||||||
h: Self::CoordinateType,
|
h: u8,
|
||||||
color: Self::ColorType);
|
color: u16);
|
||||||
|
|
||||||
|
|
||||||
fn draw_fast_vline(&mut self,
|
fn draw_fast_vline(&mut self,
|
||||||
x: Self::CoordinateType,
|
x: u8,
|
||||||
y: Self::CoordinateType,
|
y: u8,
|
||||||
h: Self::CoordinateType,
|
h: u8,
|
||||||
color: Self::ColorType);
|
color: u16);
|
||||||
|
|
||||||
fn draw_fast_hline(&mut self,
|
fn draw_fast_hline(&mut self,
|
||||||
x: Self::CoordinateType,
|
x: u8,
|
||||||
y: Self::CoordinateType,
|
y: u8,
|
||||||
w: Self::CoordinateType,
|
w: u8,
|
||||||
color: Self::ColorType);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -429,11 +429,9 @@ impl<SPIAddr, GPIOAddr> St7735IO<SPIAddr, GPIOAddr>
|
||||||
impl<SPIAddr, GPIOAddr> gfx::PrimitveGFX for St7735IO<SPIAddr, GPIOAddr>
|
impl<SPIAddr, GPIOAddr> gfx::PrimitveGFX for St7735IO<SPIAddr, GPIOAddr>
|
||||||
where SPIAddr: Location,
|
where SPIAddr: Location,
|
||||||
GPIOAddr: Location {
|
GPIOAddr: Location {
|
||||||
type ColorType = u16;
|
|
||||||
type CoordinateType = u8;
|
|
||||||
|
|
||||||
fn draw_pixel(&mut self, x: Self::CoordinateType, y: Self::CoordinateType,
|
fn draw_pixel(&mut self, x: u8, y: u8,
|
||||||
color: Self::ColorType) {
|
color: u16) {
|
||||||
if x >= self.st7735.width || y >= self.st7735.height {
|
if x >= self.st7735.width || y >= self.st7735.height {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -448,9 +446,9 @@ impl<SPIAddr, GPIOAddr> gfx::PrimitveGFX for St7735IO<SPIAddr, GPIOAddr>
|
||||||
self.set_cs();
|
self.set_cs();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_rect(&mut self, x: Self::CoordinateType, y: Self::CoordinateType,
|
fn fill_rect(&mut self, x: u8, y: u8,
|
||||||
w: Self::CoordinateType, h: Self::CoordinateType,
|
w: u8, h: u8,
|
||||||
color: Self::ColorType) {
|
color: u16) {
|
||||||
|
|
||||||
if x >= self.st7735.width || y >= self.st7735.height {
|
if x >= self.st7735.width || y >= self.st7735.height {
|
||||||
return;
|
return;
|
||||||
|
@ -485,19 +483,22 @@ impl<SPIAddr, GPIOAddr> gfx::PrimitveGFX for St7735IO<SPIAddr, GPIOAddr>
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_fast_vline(&mut self,
|
fn draw_fast_vline(&mut self,
|
||||||
x: Self::CoordinateType,
|
x: u8,
|
||||||
y: Self::CoordinateType,
|
y: u8,
|
||||||
h: Self::CoordinateType,
|
h: u8,
|
||||||
color: Self::ColorType) {
|
color: u16) {
|
||||||
self.fill_rect(x, y, 1, h, color);
|
self.fill_rect(x, y, 1, h, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_fast_hline(&mut self,
|
fn draw_fast_hline(&mut self,
|
||||||
x: Self::CoordinateType,
|
x: u8,
|
||||||
y: Self::CoordinateType,
|
y: u8,
|
||||||
w: Self::CoordinateType,
|
w: u8,
|
||||||
color: Self::ColorType) {
|
color: u16) {
|
||||||
self.fill_rect(x, y, w, 1, color);
|
self.fill_rect(x, y, w, 1, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<SPIAddr, GPIOAddr> gfx::GFX for St7735IO<SPIAddr, GPIOAddr>
|
||||||
|
where SPIAddr: Location,
|
||||||
|
GPIOAddr: Location {}
|
||||||
|
|
Loading…
Reference in New Issue