mod.rs 2.41 KB
Newer Older
Astro's avatar
Astro committed
1
use core::mem::{transmute, uninitialized};
2
use core::ops::{Index, IndexMut};
Astro's avatar
Astro committed
3
use crate::bindings::*;
4
use crate::Display;
Astro's avatar
Astro committed
5

6
7
8
9
10
mod font;
pub use font::*;
mod text;
pub use text::TextRenderer;

Astro's avatar
Astro committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pub struct FrameBuffer<'d> {
    _display: &'d Display,
    buffer: disp_framebuffer,
}


impl<'d> FrameBuffer<'d> {
    pub fn uninitialized(display: &'d Display) -> Self {
        FrameBuffer {
            _display: display,
            buffer: unsafe {
                uninitialized()
            },
        }
    }

    pub fn send(&mut self) {
        unsafe {
            epic_disp_framebuffer(&mut self.buffer);
        }
    }
32

Astro's avatar
Astro committed
33
34
35
36
37
38
39
40
41
42
43
    pub fn clear(&mut self, color: RawColor) {
        for y in 0..Display::H {
            for x in 0..Display::W {
                let bytes: &mut RawColor = unsafe {
                    transmute(&mut self.buffer.fb[y as usize][x as usize])
                };
                *bytes = color;
            }
        }
    }

44
    pub fn text<'a, 'f>(&'a mut self, x: isize, y: isize, font: &'f Font, color: RawColor) -> TextRenderer<'a, 'd, 'f> {
45
46
47
48
49
        TextRenderer {
            framebuffer: self,
            x, y, font, color,
        }
    }
Astro's avatar
Astro committed
50
51
}

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
impl<'d> Index<(u16, u16)> for FrameBuffer<'d> {
    type Output = RawColor;
    fn index(&self, (x, y): (u16, u16)) -> &Self::Output {
        let x = usize::from(Display::W - 1 - x);
        let y = usize::from(Display::H - 1 - y);
        unsafe {
            transmute(&self.buffer.fb[y][x])
        }
    }
}

impl<'d> IndexMut<(u16, u16)> for FrameBuffer<'d> {
    fn index_mut(&mut self, (x, y): (u16, u16)) -> &mut Self::Output {
        let x = usize::from(Display::W - 1 - x);
        let y = usize::from(Display::H - 1 - y);
        unsafe {
            transmute(&mut self.buffer.fb[y][x])
        }
Astro's avatar
Astro committed
70
71
72
    }
}

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct RawColor([u8; 2]);

impl RawColor {
    pub fn red() -> Self {
        Self::rgb8(0xff, 0, 0)
    }

    pub fn blue() -> Self {
        Self::rgb8(0, 0, 0xff)
    }

    pub fn green() -> Self {
        Self::rgb8(0, 0xff, 0)
    }

    pub fn black() -> Self {
        Self::rgb8(0, 0, 0)
    }

    pub fn white() -> Self {
        Self::rgb8(0xff, 0xff, 0xff)
    }

    pub fn yellow() -> Self {
        Self::rgb8(0xff, 0xff, 0)
    }

    pub fn rgb8(r8: u8, g8: u8, b8: u8) -> Self {
        let b1 = (r8 & 0xF8) | (g8 >> 5);
        let b2 = ((g8 & 0xFA) << 3) | (b8 >> 3);
        RawColor([b1, b2])
Astro's avatar
Astro committed
106
107
    }
}