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

127 lines
3.1 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);
}
}
fn draw_circle(&mut self, x0 : u8, y0 : u8, r : u8, color : u16) {
let mut f : i16 = 1 - (r as i16);
let mut ddF_x : i16 = 1;
let mut ddF_y : i16 = -2 * (r as i16);
let mut x : i16 = 0;
let mut y : i16 = r as i16;
self.draw_pixel(x0, y0 + r, color);
self.draw_pixel(x0, y0 - r, color);
self.draw_pixel(x0 + r, y0, color);
self.draw_pixel(x0 - r, y0, color);
let x0 = x0 as i16;
let y0 = y0 as i16;
let r = r as i16;
while x < y {
if f >= 0 {
y -= 1;
ddF_y += 2;
f += ddF_y;
}
x += 1;
ddF_x += 2;
f += ddF_x;
self.draw_pixel((x0 + x) as u8, (y0 + y) as u8, color);
self.draw_pixel((x0 - x) as u8, (y0 + y) as u8, color);
self.draw_pixel((x0 + x) as u8, (y0 - y) as u8, color);
self.draw_pixel((x0 - x) as u8, (y0 - y) as u8, color);
self.draw_pixel((x0 + y) as u8, (y0 + x) as u8, color);
self.draw_pixel((x0 - y) as u8, (y0 + x) as u8, color);
self.draw_pixel((x0 + y) as u8, (y0 - x) as u8, color);
self.draw_pixel((x0 - y) as u8, (y0 - x) as u8, color);
}
}
}