reflow-firmware2.0/src/st7735/gfx.rs

89 lines
2.0 KiB
Rust

pub trait PrimitveGFX {
fn draw_pixel(&mut self,
x: u8,
y: u8,
color: u16);
fn fill_rect(&mut self,
x: u8,
y: u8,
w: u8,
h: u8,
color: u16);
fn draw_fast_vline(&mut self,
x: u8,
y: u8,
h: u8,
color: u16);
fn draw_fast_hline(&mut self,
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);
}
}
}