ws_oled_driver/
gfx.rs

1use crate::display::Display;
2use anyhow::Result;
3/// Point type is used to address a pixel on the display buffer. The Point type is a `(isize, isize)` which
4/// can be used to address a pixel (x, y).
5type Point = (isize, isize);
6
7/// Fill the display buffer with a value which maybe either `0x00` or `0xFF`.
8pub fn fill(display: &mut Display, value: u8) {
9    display.memory.iter_mut().for_each(|mem| *mem = value);
10}
11
12/// Draws a single point onto the display buffer.
13pub fn draw_point(display: &mut Display, point: Point, color: u8) {
14    let index = point.0 + (point.1 / 8) * display.width as isize;
15    display.memory[index as usize] = color;
16}
17
18pub fn draw_line(display: &mut Display, (x1, y1): Point, (x2, y2): Point) {
19    let dx = (x2 - x1).abs();
20    let dy = (y2 - y1).abs();
21
22    let mut x = x1;
23    let mut y = y1;
24
25    let x_inc = if x2 > x1 { 1 } else { -1 };
26    let y_inc = if y2 > y1 { 1 } else { -1 };
27
28    let mut error = dx - dy;
29
30    while x != x2 || y != y2 {
31        draw_point(display, (x, y), 0xFF);
32
33        let double_error = error * 2;
34
35        if double_error > -dy {
36            error -= dy;
37            x += x_inc;
38        }
39
40        if double_error < dx {
41            error += dx;
42            y += y_inc;
43        }
44    }
45
46    draw_point(display, (x2, y2), 0xFF);
47}
48
49
50/// Clears the display buffer. Fills it with 0x00.
51pub fn clear(display: &mut Display) -> Result<()> {
52    fill(display, 0x00);
53
54    Ok(())
55}