1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
106
107
108
109
110
111
use core::slice;
use core::fmt;
use table::table;
pub const RESX: isize = 96;
pub const RESY: isize = 68;
pub const RESY_B: usize = 9;
pub fn buffer() -> &'static mut [u8] {
unsafe { slice::from_raw_parts_mut(table().lcdBuffer, RESX as usize * RESY_B) }
}
pub fn set_pixel(x: i8, y: i8, f: bool) {
(table().lcdSetPixel)(x, y, f);
}
pub fn get_pixel(x: i8, y: i8) -> bool {
(table().lcdGetPixel)(x, y)
}
pub fn do_int(sx: isize, sy: isize, num: isize) -> isize {
(table().DoInt)(sx, sy, num)
}
pub fn do_string(sx: isize, sy: isize, s: &str) {
let mut sx = sx;
with_nul_terminated_bufs(s, |ptr, _is_last| {
sx = (table().DoString)(sx, sy, ptr);
});
}
fn with_nul_terminated_bufs<F>(s: &str, mut f: F)
where F: FnMut(*const u8, bool)
{
let mut bytes = s.as_bytes();
while bytes.len() > 0 {
let mut buf = [0u8; 16];
let len = bytes.len().min(buf.len() - 1);
buf[..len].copy_from_slice(&bytes[..len]);
f(buf.as_ptr(), len == bytes.len());
drop(buf);
bytes = &bytes[len..];
}
}
pub fn print(s: &str) {
with_nul_terminated_bufs(s, |ptr, _is_last| {
(table().lcdPrint)(ptr);
});
}
pub fn println(s: &str) {
with_nul_terminated_bufs(s, |ptr, is_last| {
if is_last {
(table().lcdPrintln)(ptr);
} else {
(table().lcdPrint)(ptr);
}
});
}
pub struct Stdout;
pub const STDOUT: Stdout = Stdout;
impl fmt::Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
let mut bytes = &s.as_bytes().as_ref()[..];
while bytes.len() > 0 {
let mut buf = [0u8; 16];
let len = s.len().min(buf.len() - 1);
buf[0..len].copy_from_slice(&bytes[..len]);
(table().lcdPrint)(buf.as_ptr());
bytes = &bytes[len..];
}
Ok(())
}
}
pub fn clear() {
(table().lcdClear)();
}
pub fn display() {
(table().lcdDisplay)();
}