Merge pull request #1 from Caemor/graphics_buffer
Added mutable reference to buffer directly in graphics.embedded-hal-1.0
commit
1568cc2890
|
|
@ -105,22 +105,22 @@ fn main() {
|
||||||
let mut buffer = [0u8; 15000];
|
let mut buffer = [0u8; 15000];
|
||||||
|
|
||||||
// draw something
|
// draw something
|
||||||
let graphics = Graphics::new(400, 300);
|
let mut graphics = Graphics::new(400, 300, &mut buffer);
|
||||||
graphics.clear(&mut buffer, &Color::White);
|
graphics.clear(&Color::White);
|
||||||
graphics.draw_line(&mut buffer, 0,0,400,300, &Color::Black);
|
graphics.draw_line(0,0,400,300, &Color::Black);
|
||||||
|
|
||||||
graphics.draw_filled_rectangle(&mut buffer, 200,200, 230, 230, &Color::Black);
|
graphics.draw_filled_rectangle(200,200, 230, 230, &Color::Black);
|
||||||
graphics.draw_line(&mut buffer, 202,202,218,228, &Color::White);
|
graphics.draw_line(202,202,218,228, &Color::White);
|
||||||
|
|
||||||
graphics.draw_circle(&mut buffer, 200, 150, 130, &Color::Black);
|
graphics.draw_circle(200, 150, 130, &Color::Black);
|
||||||
|
|
||||||
graphics.draw_pixel(&mut buffer, 390, 290, &Color::Black);
|
graphics.draw_pixel(390, 290, &Color::Black);
|
||||||
|
|
||||||
graphics.draw_horizontal_line(&mut buffer, 0, 150, 400, &Color::Black);
|
graphics.draw_horizontal_line(0, 150, 400, &Color::Black);
|
||||||
|
|
||||||
graphics.draw_vertical_line(&mut buffer, 200, 50, 200, &Color::Black);
|
graphics.draw_vertical_line(200, 50, 200, &Color::Black);
|
||||||
|
|
||||||
epd4in2.display_and_transfer_frame(&buffer, None).expect("display and transfer error");
|
epd4in2.display_and_transfer_frame(graphics.get_buffer(), None).expect("display and transfer error");
|
||||||
|
|
||||||
epd4in2.delay_ms(3000);
|
epd4in2.delay_ms(3000);
|
||||||
|
|
||||||
|
|
@ -128,19 +128,19 @@ fn main() {
|
||||||
|
|
||||||
//Test fast updating a bit more
|
//Test fast updating a bit more
|
||||||
let mut small_buffer = [0x00; 128];
|
let mut small_buffer = [0x00; 128];
|
||||||
let circle_graphics = Graphics::new(32,32);
|
let mut circle_graphics = Graphics::new(32,32, &mut small_buffer);
|
||||||
circle_graphics.draw_circle(&mut small_buffer, 16,16, 10, &Color::Black);
|
circle_graphics.draw_circle(16,16, 10, &Color::Black);
|
||||||
|
|
||||||
epd4in2.set_partial_window(&small_buffer, 16,16, 32, 32, false).expect("Partial Window Error");
|
epd4in2.set_partial_window(circle_graphics.get_buffer(), 16,16, 32, 32, false).expect("Partial Window Error");
|
||||||
epd4in2.display_frame().expect("Display Frame Error");
|
epd4in2.display_frame().expect("Display Frame Error");
|
||||||
|
|
||||||
epd4in2.set_partial_window(&small_buffer, 128,64, 32, 32, false).expect("Partial Window Error");
|
epd4in2.set_partial_window(circle_graphics.get_buffer(), 128,64, 32, 32, false).expect("Partial Window Error");
|
||||||
epd4in2.display_frame().expect("Display Frame Error");
|
epd4in2.display_frame().expect("Display Frame Error");
|
||||||
|
|
||||||
epd4in2.set_partial_window(&small_buffer, 320,24, 32, 32, false).expect("Partial Window Error");
|
epd4in2.set_partial_window(circle_graphics.get_buffer(), 320,24, 32, 32, false).expect("Partial Window Error");
|
||||||
epd4in2.display_frame().expect("Display Frame Error");
|
epd4in2.display_frame().expect("Display Frame Error");
|
||||||
|
|
||||||
epd4in2.set_partial_window(&small_buffer, 160,240, 32, 32, false).expect("Partial Window Error");
|
epd4in2.set_partial_window(circle_graphics.get_buffer(), 160,240, 32, 32, false).expect("Partial Window Error");
|
||||||
epd4in2.display_frame().expect("Display Frame Error");
|
epd4in2.display_frame().expect("Display Frame Error");
|
||||||
|
|
||||||
epd4in2.delay_ms(3000);
|
epd4in2.delay_ms(3000);
|
||||||
|
|
@ -149,10 +149,10 @@ fn main() {
|
||||||
|
|
||||||
|
|
||||||
//pub fn draw_string_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, color: &Color) {
|
//pub fn draw_string_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, color: &Color) {
|
||||||
graphics.draw_string_8x8(&mut buffer, 16, 16, "hello", &Color::Black);
|
graphics.draw_string_8x8(16, 16, "hello", &Color::Black);
|
||||||
graphics.draw_char_8x8(&mut buffer, 250, 250, '#', &Color::Black);
|
graphics.draw_char_8x8(250, 250, '#', &Color::Black);
|
||||||
graphics.draw_char_8x8(&mut buffer, 300, 16, '7', &Color::Black);
|
graphics.draw_char_8x8(300, 16, '7', &Color::Black);
|
||||||
epd4in2.display_and_transfer_frame(&buffer, None).expect("display and transfer error");
|
epd4in2.display_and_transfer_frame(graphics.get_buffer(), None).expect("display and transfer error");
|
||||||
|
|
||||||
epd4in2.delay_ms(3000);
|
epd4in2.delay_ms(3000);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,30 +50,37 @@ impl Display {
|
||||||
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct Graphics {
|
pub struct Graphics<'a> {
|
||||||
width: u16,
|
width: u16,
|
||||||
height: u16,
|
height: u16,
|
||||||
rotation: Displayorientation,
|
rotation: Displayorientation,
|
||||||
|
buffer: &'a mut [u8]
|
||||||
//buffer: Box<u8>//[u8; 15000],
|
//buffer: Box<u8>//[u8; 15000],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Graphics {
|
impl<'a> Graphics<'a> {
|
||||||
/// width needs to be a multiple of 8!
|
/// width needs to be a multiple of 8!
|
||||||
pub fn new(width: u16, height: u16) -> Graphics{
|
pub fn new(width: u16, height: u16, buffer: &'a mut [u8]) -> Graphics<'a>{
|
||||||
Graphics {width, height, rotation: Displayorientation::Rotate0}
|
let len = buffer.len();
|
||||||
|
assert!(width / 8 * height >= len as u16);
|
||||||
|
Graphics {width, height, rotation: Displayorientation::Rotate0, buffer}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clears/Fills the full buffer with `color`
|
/// Clears/Fills the full buffer with `color`
|
||||||
pub fn clear(&self, buffer: &mut[u8], color: &Color) {
|
pub fn clear(&mut self, color: &Color) {
|
||||||
for elem in buffer.iter_mut() {
|
for elem in self.buffer.iter_mut() {
|
||||||
*elem = color.get_byte_value();
|
*elem = color.get_byte_value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_buffer(&'a self) -> &'a [u8] {
|
||||||
|
self.buffer
|
||||||
|
}
|
||||||
|
|
||||||
/// Draw a single Pixel with `color`
|
/// Draw a single Pixel with `color`
|
||||||
///
|
///
|
||||||
/// limited to i16::max images (buffer_size) at the moment
|
/// limited to i16::max images (buffer_size) at the moment
|
||||||
pub fn draw_pixel(&self, buffer: &mut[u8], x: u16, y: u16, color: &Color) {
|
pub fn draw_pixel(&mut self, x: u16, y: u16, color: &Color) {
|
||||||
let (idx, bit) = match self.rotation {
|
let (idx, bit) = match self.rotation {
|
||||||
Displayorientation::Rotate0 | Displayorientation::Rotate180
|
Displayorientation::Rotate0 | Displayorientation::Rotate180
|
||||||
=> ((x as usize / 8 + (self.width as usize / 8) * y as usize) ,
|
=> ((x as usize / 8 + (self.width as usize / 8) * y as usize) ,
|
||||||
|
|
@ -83,16 +90,16 @@ impl Graphics {
|
||||||
0x80 >> (y % 8)),
|
0x80 >> (y % 8)),
|
||||||
};
|
};
|
||||||
|
|
||||||
if idx >= buffer.len() {
|
if idx >= self.buffer.len() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
match color {
|
match color {
|
||||||
Color::Black => {
|
Color::Black => {
|
||||||
buffer[idx] &= !bit;
|
self.buffer[idx] &= !bit;
|
||||||
},
|
},
|
||||||
Color::White => {
|
Color::White => {
|
||||||
buffer[idx] |= bit;
|
self.buffer[idx] |= bit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -101,7 +108,7 @@ impl Graphics {
|
||||||
///
|
///
|
||||||
/// limited to i16::max images (buffer_size) at the moment
|
/// limited to i16::max images (buffer_size) at the moment
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn draw_byte(&self, buffer: &mut[u8], x: u16, y: u16, filling: u8, color: &Color) {
|
fn draw_byte(&mut self, x: u16, y: u16, filling: u8, color: &Color) {
|
||||||
let idx = match self.rotation {
|
let idx = match self.rotation {
|
||||||
Displayorientation::Rotate0 | Displayorientation::Rotate180
|
Displayorientation::Rotate0 | Displayorientation::Rotate180
|
||||||
=> x as usize / 8 + (self.width as usize / 8) * y as usize,
|
=> x as usize / 8 + (self.width as usize / 8) * y as usize,
|
||||||
|
|
@ -109,31 +116,31 @@ impl Graphics {
|
||||||
=> y as usize / 8 + (self.width as usize / 8) * x as usize,
|
=> y as usize / 8 + (self.width as usize / 8) * x as usize,
|
||||||
};
|
};
|
||||||
|
|
||||||
if idx >= buffer.len() {
|
if idx >= self.buffer.len() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
match color {
|
match color {
|
||||||
Color::Black => {
|
Color::Black => {
|
||||||
buffer[idx] = !filling;
|
self.buffer[idx] = !filling;
|
||||||
},
|
},
|
||||||
Color::White => {
|
Color::White => {
|
||||||
buffer[idx] = filling;
|
self.buffer[idx] = filling;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///TODO: test!
|
///TODO: test!
|
||||||
pub fn draw_char(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, font: &Font, color: &Color) {
|
pub fn draw_char(&mut self, x0: u16, y0: u16, input: char, font: &Font, color: &Color) {
|
||||||
self.draw_char_helper(buffer, x0, y0, input, font, color);
|
self.draw_char_helper(x0, y0, input, font, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
///TODO: test!
|
///TODO: test!
|
||||||
/// no autobreak line yet
|
/// no autobreak line yet
|
||||||
pub fn draw_string(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, font: &Font, color: &Color) {
|
pub fn draw_string(&mut self, x0: u16, y0: u16, input: &str, font: &Font, color: &Color) {
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
for input_char in input.chars() {
|
for input_char in input.chars() {
|
||||||
self.draw_char(buffer, x0 + counter, y0, input_char, font, color);
|
self.draw_char(x0 + counter, y0, input_char, font, color);
|
||||||
counter += font.get_char_width(input_char) as u16;
|
counter += font.get_char_width(input_char) as u16;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -141,7 +148,7 @@ impl Graphics {
|
||||||
|
|
||||||
//TODO: add support for font_height = 0
|
//TODO: add support for font_height = 0
|
||||||
//TODO: add support for char offset in y direction to reduce font file size
|
//TODO: add support for char offset in y direction to reduce font file size
|
||||||
fn draw_char_helper(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, font: &Font, color: &Color) {
|
fn draw_char_helper(&mut self, x0: u16, y0: u16, input: char, font: &Font, color: &Color) {
|
||||||
//width: u8, height: u8, charbuffer: &[u8]
|
//width: u8, height: u8, charbuffer: &[u8]
|
||||||
//TODO: font.get_char(input) -> FontChar {width, height, [u8]}
|
//TODO: font.get_char(input) -> FontChar {width, height, [u8]}
|
||||||
//TODO: font.get_char_offset(input) -> u16
|
//TODO: font.get_char_offset(input) -> u16
|
||||||
|
|
@ -156,7 +163,6 @@ impl Graphics {
|
||||||
for _ in 0..8 {
|
for _ in 0..8 {
|
||||||
|
|
||||||
self.draw_pixel(
|
self.draw_pixel(
|
||||||
buffer,
|
|
||||||
x0 + width_counter as u16,
|
x0 + width_counter as u16,
|
||||||
y0 + row_counter,
|
y0 + row_counter,
|
||||||
&Color::get_color(elem, width_counter % 8, color));
|
&Color::get_color(elem, width_counter % 8, color));
|
||||||
|
|
@ -174,12 +180,12 @@ impl Graphics {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a single 8x8 Char somewhere (1 pixel padding included)
|
/// Draws a single 8x8 Char somewhere (1 pixel padding included)
|
||||||
pub fn draw_char_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: char, color: &Color) {
|
pub fn draw_char_8x8(&mut self, x0: u16, y0: u16, input: char, color: &Color) {
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
// includes special draw_char instructions as this one is ordered columnwise and not rowwise (first byte == first 8 pixel columnwise)
|
// includes special draw_char instructions as this one is ordered columnwise and not rowwise (first byte == first 8 pixel columnwise)
|
||||||
for &elem in font::bitmap_8x8(input).iter() {
|
for &elem in font::bitmap_8x8(input).iter() {
|
||||||
for i in 0..8u8 {
|
for i in 0..8u8 {
|
||||||
self.draw_pixel(buffer, x0 + counter, y0 + 7 - i as u16, &Color::convert_color(elem, i, color))
|
self.draw_pixel(x0 + counter, y0 + 7 - i as u16, &Color::convert_color(elem, i, color))
|
||||||
}
|
}
|
||||||
counter += 1;
|
counter += 1;
|
||||||
}
|
}
|
||||||
|
|
@ -190,10 +196,10 @@ impl Graphics {
|
||||||
/// Is quite small for the 400x300 E-Ink
|
/// Is quite small for the 400x300 E-Ink
|
||||||
///
|
///
|
||||||
/// no autobreak line yet
|
/// no autobreak line yet
|
||||||
pub fn draw_string_8x8(&self, buffer: &mut[u8], x0: u16, y0: u16, input: &str, color: &Color) {
|
pub fn draw_string_8x8(&mut self, x0: u16, y0: u16, input: &str, color: &Color) {
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
for input_char in input.chars() {
|
for input_char in input.chars() {
|
||||||
self.draw_char_8x8(buffer, x0 + counter*8, y0, input_char, color);
|
self.draw_char_8x8(x0 + counter*8, y0, input_char, color);
|
||||||
counter += 1;
|
counter += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -214,7 +220,7 @@ impl Graphics {
|
||||||
// }
|
// }
|
||||||
//bresenham algorithm for lines
|
//bresenham algorithm for lines
|
||||||
/// draw line
|
/// draw line
|
||||||
pub fn draw_line(&self, buffer: &mut[u8], x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) {
|
pub fn draw_line(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) {
|
||||||
let mut x0 = x0 as i16;
|
let mut x0 = x0 as i16;
|
||||||
let x1 = x1 as i16;
|
let x1 = x1 as i16;
|
||||||
let mut y0 = y0 as i16;
|
let mut y0 = y0 as i16;
|
||||||
|
|
@ -229,7 +235,7 @@ impl Graphics {
|
||||||
let mut err = dx + dy;
|
let mut err = dx + dy;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
self.draw_pixel(buffer, x0 as u16, y0 as u16, color);
|
self.draw_pixel(x0 as u16, y0 as u16, color);
|
||||||
|
|
||||||
if x0 == x1 && y0 == y1 {
|
if x0 == x1 && y0 == y1 {
|
||||||
break;
|
break;
|
||||||
|
|
@ -251,50 +257,50 @@ impl Graphics {
|
||||||
|
|
||||||
/// Draw a horizontal line
|
/// Draw a horizontal line
|
||||||
/// TODO: maybe optimize by grouping up the bytes? But is it worth the longer and more complicated function? is it even faster?
|
/// TODO: maybe optimize by grouping up the bytes? But is it worth the longer and more complicated function? is it even faster?
|
||||||
pub fn draw_horizontal_line(&self, buffer: &mut[u8], x: u16, y: u16, length: u16, color: &Color) {
|
pub fn draw_horizontal_line(&mut self, x: u16, y: u16, length: u16, color: &Color) {
|
||||||
for i in 0..length {
|
for i in 0..length {
|
||||||
self.draw_pixel(buffer, x + i, y, color);
|
self.draw_pixel(x + i, y, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a vertical line
|
/// Draws a vertical line
|
||||||
pub fn draw_vertical_line(&self, buffer: &mut[u8], x: u16, y: u16, length: u16, color: &Color) {
|
pub fn draw_vertical_line(&mut self, x: u16, y: u16, length: u16, color: &Color) {
|
||||||
for i in 0..length {
|
for i in 0..length {
|
||||||
self.draw_pixel(buffer, x, y + i, color);
|
self.draw_pixel(x, y + i, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a rectangle. (x0,y0) is top left corner, (x1,y1) bottom right
|
/// Draws a rectangle. (x0,y0) is top left corner, (x1,y1) bottom right
|
||||||
pub fn draw_rectangle(&self, buffer: &mut[u8], x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) {
|
pub fn draw_rectangle(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) {
|
||||||
let (min_x, max_x) = if x0 <= x1 { (x0, x1) } else { (x1, x0) };
|
let (min_x, max_x) = if x0 <= x1 { (x0, x1) } else { (x1, x0) };
|
||||||
let (min_y, max_y) = if y0 <= y1 { (y0, y1) } else { (y1, y0) };
|
let (min_y, max_y) = if y0 <= y1 { (y0, y1) } else { (y1, y0) };
|
||||||
let x_len = max_x - min_x;
|
let x_len = max_x - min_x;
|
||||||
let y_len = max_y - min_y;
|
let y_len = max_y - min_y;
|
||||||
self.draw_horizontal_line(buffer, min_x, min_y, x_len, color);
|
self.draw_horizontal_line(min_x, min_y, x_len, color);
|
||||||
self.draw_horizontal_line(buffer, min_x, max_y, x_len, color);
|
self.draw_horizontal_line(min_x, max_y, x_len, color);
|
||||||
self.draw_vertical_line(buffer, min_x, min_y, y_len, color);
|
self.draw_vertical_line(min_x, min_y, y_len, color);
|
||||||
self.draw_vertical_line(buffer, max_x, min_y, y_len, color);
|
self.draw_vertical_line(max_x, min_y, y_len, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a filled rectangle. For more info see draw_rectangle
|
/// Draws a filled rectangle. For more info see draw_rectangle
|
||||||
pub fn draw_filled_rectangle(&self, buffer: &mut[u8], x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) {
|
pub fn draw_filled_rectangle(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, color: &Color) {
|
||||||
let (min_x, max_x) = if x0 <= x1 { (x0, x1) } else { (x1, x0) };
|
let (min_x, max_x) = if x0 <= x1 { (x0, x1) } else { (x1, x0) };
|
||||||
let (min_y, max_y) = if y0 <= y1 { (y0, y1) } else { (y1, y0) };
|
let (min_y, max_y) = if y0 <= y1 { (y0, y1) } else { (y1, y0) };
|
||||||
let x_len = max_x - min_x;
|
let x_len = max_x - min_x;
|
||||||
let y_len = max_y - min_y;
|
let y_len = max_y - min_y;
|
||||||
for i in 0..y_len {
|
for i in 0..y_len {
|
||||||
self.draw_horizontal_line(buffer, min_x, min_y + i, x_len, color);
|
self.draw_horizontal_line(min_x, min_y + i, x_len, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_pixel_helper(&self, buffer: &mut[u8], x: i16, y: i16, color: &Color) {
|
fn draw_pixel_helper(&mut self, x: i16, y: i16, color: &Color) {
|
||||||
if x >= 0 && y >= 0 {
|
if x >= 0 && y >= 0 {
|
||||||
self.draw_pixel(buffer, x as u16, y as u16, color);
|
self.draw_pixel(x as u16, y as u16, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn draw_circle_helper(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, filled: bool, color: &Color) {
|
fn draw_circle_helper(&mut self, x0: u16, y0: u16, radius: u16, filled: bool, color: &Color) {
|
||||||
let mut x = radius - 1;
|
let mut x = radius - 1;
|
||||||
let mut y = 0;
|
let mut y = 0;
|
||||||
let mut dx = 1;
|
let mut dx = 1;
|
||||||
|
|
@ -303,9 +309,9 @@ fn draw_circle_helper(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, fi
|
||||||
|
|
||||||
while x >= y {
|
while x >= y {
|
||||||
if filled {
|
if filled {
|
||||||
self.circle_helper_filled_putpixel(buffer, x0, y0, x, y, color);
|
self.circle_helper_filled_putpixel(x0, y0, x, y, color);
|
||||||
} else {
|
} else {
|
||||||
self.circle_helper_putpixel(buffer, x0, y0, x, y, color);
|
self.circle_helper_putpixel(x0, y0, x, y, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
if err <= 0 {
|
if err <= 0 {
|
||||||
|
|
@ -323,35 +329,35 @@ fn draw_circle_helper(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn circle_helper_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x: u16, y: u16, color: &Color) {
|
fn circle_helper_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, color: &Color) {
|
||||||
self.draw_horizontal_line(buffer, x0 - x, y0 + y, 2*x, color);
|
self.draw_horizontal_line(x0 - x, y0 + y, 2*x, color);
|
||||||
// self.draw_pixel(buffer, x0 + x, y0 + y, color);
|
// self.draw_pixel(buffer, x0 + x, y0 + y, color);
|
||||||
// self.draw_pixel(buffer, x0 - x, y0 + y, color);
|
// self.draw_pixel(buffer, x0 - x, y0 + y, color);
|
||||||
|
|
||||||
self.draw_horizontal_line(buffer, x0 - y, y0 + x, 2*y, color);
|
self.draw_horizontal_line(x0 - y, y0 + x, 2*y, color);
|
||||||
// self.draw_pixel(buffer, x0 + y, y0 + x, color);
|
// self.draw_pixel(buffer, x0 + y, y0 + x, color);
|
||||||
// self.draw_pixel(buffer, x0 - y, y0 + x, color);
|
// self.draw_pixel(buffer, x0 - y, y0 + x, color);
|
||||||
|
|
||||||
self.draw_horizontal_line(buffer, x0 - x, y0 - y, 2*x, color);
|
self.draw_horizontal_line(x0 - x, y0 - y, 2*x, color);
|
||||||
// self.draw_pixel(buffer, x0 - x, y0 - y, color);
|
// self.draw_pixel(buffer, x0 - x, y0 - y, color);
|
||||||
// self.draw_pixel(buffer, x0 + x, y0 - y, color);
|
// self.draw_pixel(buffer, x0 + x, y0 - y, color);
|
||||||
|
|
||||||
self.draw_horizontal_line(buffer, x0 - y, y0 - y, 2*y, color);
|
self.draw_horizontal_line(x0 - y, y0 - y, 2*y, color);
|
||||||
// self.draw_pixel(buffer, x0 - y, y0 - x, color);
|
// self.draw_pixel(buffer, x0 - y, y0 - x, color);
|
||||||
// self.draw_pixel(buffer, x0 + y, y0 - x, color);
|
// self.draw_pixel(buffer, x0 + y, y0 - x, color);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Test
|
//TODO: Test
|
||||||
fn circle_helper_filled_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x: u16, y: u16, color: &Color) {
|
fn circle_helper_filled_putpixel(&mut self, x0: u16, y0: u16, x: u16, y: u16, color: &Color) {
|
||||||
self.draw_pixel(buffer, x0 + x, y0 + y, color);
|
self.draw_pixel(x0 + x, y0 + y, color);
|
||||||
self.draw_pixel(buffer, x0 + y, y0 + x, color);
|
self.draw_pixel(x0 + y, y0 + x, color);
|
||||||
self.draw_pixel(buffer, x0 - y, y0 + x, color);
|
self.draw_pixel(x0 - y, y0 + x, color);
|
||||||
self.draw_pixel(buffer, x0 - x, y0 + y, color);
|
self.draw_pixel(x0 - x, y0 + y, color);
|
||||||
self.draw_pixel(buffer, x0 - x, y0 - y, color);
|
self.draw_pixel(x0 - x, y0 - y, color);
|
||||||
self.draw_pixel(buffer, x0 - y, y0 - x, color);
|
self.draw_pixel(x0 - y, y0 - x, color);
|
||||||
self.draw_pixel(buffer, x0 + y, y0 - x, color);
|
self.draw_pixel(x0 + y, y0 - x, color);
|
||||||
self.draw_pixel(buffer, x0 + x, y0 - y, color);
|
self.draw_pixel(x0 + x, y0 - y, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -359,13 +365,13 @@ fn circle_helper_filled_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x:
|
||||||
|
|
||||||
///TODO: test if circle looks good
|
///TODO: test if circle looks good
|
||||||
/// Draws a circle
|
/// Draws a circle
|
||||||
pub fn draw_circle(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, color: &Color) {
|
pub fn draw_circle(&mut self, x0: u16, y0: u16, radius: u16, color: &Color) {
|
||||||
self.draw_circle_helper(buffer, x0, y0, radius, false, color);
|
self.draw_circle_helper(x0, y0, radius, false, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
///TODO: test if circle looks good
|
///TODO: test if circle looks good
|
||||||
/// Draws a circle
|
/// Draws a circle
|
||||||
pub fn draw_circle2(&self, buffer: &mut[u8], x: u16, y: u16, radius: u16, color: &Color) {
|
pub fn draw_circle2(&mut self, x: u16, y: u16, radius: u16, color: &Color) {
|
||||||
let radius = radius as i16;
|
let radius = radius as i16;
|
||||||
let x_mid = x as i16;
|
let x_mid = x as i16;
|
||||||
let y_mid = y as i16;
|
let y_mid = y as i16;
|
||||||
|
|
@ -374,10 +380,10 @@ fn circle_helper_filled_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x:
|
||||||
let mut err: i16 = 2 - 2 * radius;
|
let mut err: i16 = 2 - 2 * radius;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
self.draw_pixel_helper(buffer, x_mid - x_pos, y_mid + y_pos, color);
|
self.draw_pixel_helper(x_mid - x_pos, y_mid + y_pos, color);
|
||||||
self.draw_pixel_helper(buffer, x_mid - y_pos, y_mid - x_pos, color);
|
self.draw_pixel_helper(x_mid - y_pos, y_mid - x_pos, color);
|
||||||
self.draw_pixel_helper(buffer, x_mid + x_pos, y_mid - y_pos, color);
|
self.draw_pixel_helper(x_mid + x_pos, y_mid - y_pos, color);
|
||||||
self.draw_pixel_helper(buffer, x_mid + y_pos, y_mid + x_pos, color);
|
self.draw_pixel_helper(x_mid + y_pos, y_mid + x_pos, color);
|
||||||
|
|
||||||
let radius = err;
|
let radius = err;
|
||||||
|
|
||||||
|
|
@ -399,8 +405,8 @@ fn circle_helper_filled_putpixel(&self, buffer: &mut[u8], x0: u16, y0: u16, x:
|
||||||
|
|
||||||
|
|
||||||
///TODO: test!
|
///TODO: test!
|
||||||
pub fn draw_filled_circle(&self, buffer: &mut[u8], x0: u16, y0: u16, radius: u16, color: &Color) {
|
pub fn draw_filled_circle(&mut self, x0: u16, y0: u16, radius: u16, color: &Color) {
|
||||||
self.draw_circle_helper(buffer, x0, y0, radius, true, color);
|
self.draw_circle_helper(x0, y0, radius, true, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -429,12 +435,12 @@ mod graphics {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_filled_rectangle() {
|
fn test_filled_rectangle() {
|
||||||
let mut buffer = [Color::White.get_byte_value(); 150];
|
let mut buffer = [Color::White.get_byte_value(); 150];
|
||||||
let graphics = Graphics::new(40, 30);
|
let mut graphics = Graphics::new(40, 30, &mut buffer);
|
||||||
graphics.draw_filled_rectangle(&mut buffer, 0, 0, 40, 30, &Color::Black);
|
graphics.draw_filled_rectangle(0, 0, 40, 30, &Color::Black);
|
||||||
|
|
||||||
assert_eq!(buffer[0], Color::Black.get_byte_value());
|
assert_eq!(graphics.buffer[0], Color::Black.get_byte_value());
|
||||||
|
|
||||||
for &elem in buffer.iter() {
|
for &elem in graphics.buffer.iter() {
|
||||||
|
|
||||||
assert_eq!(elem, Color::Black.get_byte_value());
|
assert_eq!(elem, Color::Black.get_byte_value());
|
||||||
}
|
}
|
||||||
|
|
@ -446,13 +452,13 @@ mod graphics {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_filled_rectangle2() {
|
fn test_filled_rectangle2() {
|
||||||
let mut buffer = [Color::White.get_byte_value(); 8];
|
let mut buffer = [Color::White.get_byte_value(); 8];
|
||||||
let graphics = Graphics::new(8, 8);
|
let mut graphics = Graphics::new(8, 8, &mut buffer);
|
||||||
graphics.draw_filled_rectangle(&mut buffer, 0, 0, 4, 4, &Color::Black);
|
graphics.draw_filled_rectangle(0, 0, 4, 4, &Color::Black);
|
||||||
|
|
||||||
assert_eq!(buffer[0], 0x0f);
|
assert_eq!(graphics.buffer[0], 0x0f);
|
||||||
|
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
for &elem in buffer.iter() {
|
for &elem in graphics.buffer.iter() {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
|
|
||||||
if counter <= 4 {
|
if counter <= 4 {
|
||||||
|
|
@ -468,27 +474,27 @@ mod graphics {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_horizontal_line() {
|
fn test_horizontal_line() {
|
||||||
let mut buffer = [Color::White.get_byte_value(); 4];
|
let mut buffer = [Color::White.get_byte_value(); 4];
|
||||||
let graphics = Graphics::new(16, 2);
|
let mut graphics = Graphics::new(16, 2, &mut buffer);
|
||||||
graphics.draw_horizontal_line(&mut buffer, 1, 0, 14, &Color::Black);
|
graphics.draw_horizontal_line(1, 0, 14, &Color::Black);
|
||||||
|
|
||||||
assert_eq!(buffer[0], 0x80);
|
assert_eq!(graphics.buffer[0], 0x80);
|
||||||
assert_eq!(buffer[1], 0x01);
|
assert_eq!(graphics.buffer[1], 0x01);
|
||||||
assert_eq!(buffer[2], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[2], Color::White.get_byte_value());
|
||||||
assert_eq!(buffer[3], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[3], Color::White.get_byte_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vertical_line() {
|
fn test_vertical_line() {
|
||||||
let mut buffer = [Color::White.get_byte_value(); 8];
|
let mut buffer = [Color::White.get_byte_value(); 8];
|
||||||
let graphics = Graphics::new(8, 8);
|
let mut graphics = Graphics::new(8, 8, &mut buffer);
|
||||||
graphics.draw_vertical_line(&mut buffer, 0, 0, 8, &Color::Black);
|
graphics.draw_vertical_line(0, 0, 8, &Color::Black);
|
||||||
|
|
||||||
graphics.draw_vertical_line(&mut buffer, 5, 0, 8, &Color::Black);
|
graphics.draw_vertical_line(5, 0, 8, &Color::Black);
|
||||||
|
|
||||||
|
|
||||||
assert_eq!(buffer[0], 0x7b);
|
assert_eq!(graphics.buffer[0], 0x7b);
|
||||||
|
|
||||||
for &elem in buffer.iter() {
|
for &elem in graphics.buffer.iter() {
|
||||||
|
|
||||||
assert_eq!(elem, 0x7bu8);
|
assert_eq!(elem, 0x7bu8);
|
||||||
}
|
}
|
||||||
|
|
@ -498,17 +504,17 @@ mod graphics {
|
||||||
#[test]
|
#[test]
|
||||||
fn draw_line_1() {
|
fn draw_line_1() {
|
||||||
let mut buffer = [Color::White.get_byte_value(); 8];
|
let mut buffer = [Color::White.get_byte_value(); 8];
|
||||||
let graphics = Graphics::new(8, 8);
|
let mut graphics = Graphics::new(8, 8, &mut buffer);
|
||||||
|
|
||||||
graphics.draw_vertical_line(&mut buffer, 5, 0, 8, &Color::Black);
|
graphics.draw_vertical_line(5, 0, 8, &Color::Black);
|
||||||
|
|
||||||
let mut buffer2 = [Color::White.get_byte_value(); 8];
|
let mut buffer2 = [Color::White.get_byte_value(); 8];
|
||||||
let graphics2 = Graphics::new(8, 8);
|
let mut graphics2 = Graphics::new(8, 8, &mut buffer2);
|
||||||
|
|
||||||
graphics2.draw_line(&mut buffer2, 5, 0, 5, 8, &Color::Black);
|
graphics2.draw_line(5, 0, 5, 8, &Color::Black);
|
||||||
|
|
||||||
for i in 0..buffer.len() {
|
for i in 0..graphics.buffer.len() {
|
||||||
assert_eq!(buffer[i], buffer2[i]);
|
assert_eq!(graphics.buffer[i], graphics2.buffer[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -516,15 +522,15 @@ mod graphics {
|
||||||
#[test]
|
#[test]
|
||||||
fn draw_line_2() {
|
fn draw_line_2() {
|
||||||
let mut buffer = [Color::White.get_byte_value(); 4];
|
let mut buffer = [Color::White.get_byte_value(); 4];
|
||||||
let graphics = Graphics::new(16, 2);
|
let mut graphics = Graphics::new(16, 2, &mut buffer);
|
||||||
graphics.draw_horizontal_line(&mut buffer, 1, 0, 14, &Color::Black);
|
graphics.draw_horizontal_line(1, 0, 14, &Color::Black);
|
||||||
|
|
||||||
let mut buffer2 = [Color::White.get_byte_value(); 4];
|
let mut buffer2 = [Color::White.get_byte_value(); 4];
|
||||||
let graphics2 = Graphics::new(16, 2);
|
let mut graphics2 = Graphics::new(16, 2, &mut buffer2);
|
||||||
graphics2.draw_line(&mut buffer2, 1, 0, 14, 0, &Color::Black);
|
graphics2.draw_line(1, 0, 14, 0, &Color::Black);
|
||||||
|
|
||||||
for i in 0..buffer.len() {
|
for i in 0..graphics.buffer.len() {
|
||||||
assert_eq!(buffer[i], buffer2[i]);
|
assert_eq!(graphics.buffer[i], graphics2.buffer[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -532,12 +538,12 @@ mod graphics {
|
||||||
#[test]
|
#[test]
|
||||||
fn draw_line_3() {
|
fn draw_line_3() {
|
||||||
let mut buffer = [Color::White.get_byte_value(); 8];
|
let mut buffer = [Color::White.get_byte_value(); 8];
|
||||||
let graphics = Graphics::new(8, 8);
|
let mut graphics = Graphics::new(8, 8, &mut buffer);
|
||||||
|
|
||||||
graphics.draw_line(&mut buffer, 0, 0, 16, 16, &Color::Black);
|
graphics.draw_line(0, 0, 16, 16, &Color::Black);
|
||||||
|
|
||||||
for i in 0..buffer.len() {
|
for i in 0..graphics.buffer.len() {
|
||||||
assert_eq!(buffer[i], !(0x80 >> i % 8));
|
assert_eq!(graphics.buffer[i], !(0x80 >> i % 8));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -546,33 +552,33 @@ mod graphics {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pixel() {
|
fn test_pixel() {
|
||||||
let mut buffer = [Color::White.get_byte_value(); 8];
|
let mut buffer = [Color::White.get_byte_value(); 8];
|
||||||
let graphics = Graphics::new(8, 8);
|
let mut graphics = Graphics::new(8, 8, &mut buffer);
|
||||||
graphics.draw_pixel(&mut buffer, 1, 0, &Color::Black);
|
graphics.draw_pixel(1, 0, &Color::Black);
|
||||||
|
|
||||||
assert_eq!(buffer[0], !0x40);
|
assert_eq!(graphics.buffer[0], !0x40);
|
||||||
|
|
||||||
|
|
||||||
let mut buffer = [Color::White.get_byte_value(); 16];
|
let mut buffer = [Color::White.get_byte_value(); 16];
|
||||||
let graphics = Graphics::new(16, 8);
|
let mut graphics = Graphics::new(16, 8, &mut buffer);
|
||||||
graphics.draw_pixel(&mut buffer, 9, 0, &Color::Black);
|
graphics.draw_pixel(9, 0, &Color::Black);
|
||||||
assert_eq!(buffer[0], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[0], Color::White.get_byte_value());
|
||||||
assert_eq!(buffer[1], !0x40);
|
assert_eq!(graphics.buffer[1], !0x40);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_byte() {
|
fn test_byte() {
|
||||||
let mut buffer = [Color::White.get_byte_value(); 8];
|
let mut buffer = [Color::White.get_byte_value(); 8];
|
||||||
let graphics = Graphics::new(8, 8);
|
let mut graphics = Graphics::new(8, 8, &mut buffer);
|
||||||
graphics.draw_byte(&mut buffer, 0, 0, 0xff, &Color::Black);
|
graphics.draw_byte(0, 0, 0xff, &Color::Black);
|
||||||
|
|
||||||
assert_eq!(buffer[0], Color::Black.get_byte_value());
|
assert_eq!(graphics.buffer[0], Color::Black.get_byte_value());
|
||||||
|
|
||||||
for i in 1..buffer.len() {
|
for i in 1..graphics.buffer.len() {
|
||||||
assert_eq!(buffer[i], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[i], Color::White.get_byte_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
graphics.draw_byte(&mut buffer, 0, 0, 0x5A, &Color::Black) ;
|
graphics.draw_byte(0, 0, 0x5A, &Color::Black) ;
|
||||||
assert_eq!(buffer[0], !0x5A);
|
assert_eq!(graphics.buffer[0], !0x5A);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -580,30 +586,30 @@ mod graphics {
|
||||||
|
|
||||||
// Test !
|
// Test !
|
||||||
let mut buffer = [Color::White.get_byte_value(); 8];
|
let mut buffer = [Color::White.get_byte_value(); 8];
|
||||||
let graphics = Graphics::new(8, 8);
|
let mut graphics = Graphics::new(8, 8, &mut buffer);
|
||||||
graphics.draw_char_8x8(&mut buffer, 0, 0, '!', &Color::Black);
|
graphics.draw_char_8x8(0, 0, '!', &Color::Black);
|
||||||
|
|
||||||
for i in 0..5 {
|
for i in 0..5 {
|
||||||
assert_eq!(buffer[i], !0x20);
|
assert_eq!(graphics.buffer[i], !0x20);
|
||||||
}
|
}
|
||||||
assert_eq!(buffer[5], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[5], Color::White.get_byte_value());
|
||||||
assert_eq!(buffer[6], !0x20);
|
assert_eq!(graphics.buffer[6], !0x20);
|
||||||
assert_eq!(buffer[7], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[7], Color::White.get_byte_value());
|
||||||
|
|
||||||
|
|
||||||
// Test H
|
// Test H
|
||||||
let mut buffer = [Color::White.get_byte_value(); 8];
|
let mut buffer = [Color::White.get_byte_value(); 8];
|
||||||
let graphics = Graphics::new(8, 8);
|
let mut graphics = Graphics::new(8, 8, &mut buffer);
|
||||||
graphics.draw_char_8x8(&mut buffer, 0, 0, 'H', &Color::Black);
|
graphics.draw_char_8x8(0, 0, 'H', &Color::Black);
|
||||||
|
|
||||||
for i in 0..3 {
|
for i in 0..3 {
|
||||||
assert_eq!(buffer[i], !0x88);
|
assert_eq!(graphics.buffer[i], !0x88);
|
||||||
}
|
}
|
||||||
assert_eq!(buffer[3], !0xF8);
|
assert_eq!(graphics.buffer[3], !0xF8);
|
||||||
for i in 4..7 {
|
for i in 4..7 {
|
||||||
assert_eq!(buffer[i], !0x88);
|
assert_eq!(graphics.buffer[i], !0x88);
|
||||||
}
|
}
|
||||||
assert_eq!(buffer[7], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[7], Color::White.get_byte_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -611,24 +617,24 @@ mod graphics {
|
||||||
|
|
||||||
// Test !H
|
// Test !H
|
||||||
let mut buffer = [Color::White.get_byte_value(); 16];
|
let mut buffer = [Color::White.get_byte_value(); 16];
|
||||||
let graphics = Graphics::new(16, 8);
|
let mut graphics = Graphics::new(16, 8, &mut buffer);
|
||||||
graphics.draw_string_8x8(&mut buffer, 0, 0, "!H", &Color::Black);
|
graphics.draw_string_8x8(0, 0, "!H", &Color::Black);
|
||||||
|
|
||||||
for i in 0..5 {
|
for i in 0..5 {
|
||||||
assert_eq!(buffer[i*2], !0x20);
|
assert_eq!(graphics.buffer[i*2], !0x20);
|
||||||
}
|
}
|
||||||
assert_eq!(buffer[5*2], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[5*2], Color::White.get_byte_value());
|
||||||
assert_eq!(buffer[6*2], !0x20);
|
assert_eq!(graphics.buffer[6*2], !0x20);
|
||||||
assert_eq!(buffer[7*2], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[7*2], Color::White.get_byte_value());
|
||||||
|
|
||||||
|
|
||||||
for i in 0..3 {
|
for i in 0..3 {
|
||||||
assert_eq!(buffer[i*2 + 1], !0x88);
|
assert_eq!(graphics.buffer[i*2 + 1], !0x88);
|
||||||
}
|
}
|
||||||
assert_eq!(buffer[3*2 + 1], !0xF8);
|
assert_eq!(graphics.buffer[3*2 + 1], !0xF8);
|
||||||
for i in 4..7 {
|
for i in 4..7 {
|
||||||
assert_eq!(buffer[i*2 + 1], !0x88);
|
assert_eq!(graphics.buffer[i*2 + 1], !0x88);
|
||||||
}
|
}
|
||||||
assert_eq!(buffer[7*2 + 1], Color::White.get_byte_value());
|
assert_eq!(graphics.buffer[7*2 + 1], Color::White.get_byte_value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue