/
device.rs
153 lines (131 loc) · 4.45 KB
/
device.rs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use anyhow::Result;
use bitvec::prelude::*;
use embedded_graphics::{pixelcolor::BinaryColor, prelude::*};
#[cfg(feature = "async")]
use std::future::Future;
const FB_SIZE: usize = 40 * 128 / 8 + 2;
#[derive(Copy, Clone, Debug)]
pub struct FrameBuffer {
/// The framebuffer with one bit value per pixel.
/// Two extra bytes are added, one for the header byte `0x61` and one for a
/// trailing null byte. This is done to prevent superfluous copies when
/// sending the image to a display device. The implementations of
/// `Drawable` and `DrawTarget` take this quirk into account.
pub framebuffer: BitArray<[u8; FB_SIZE], Msb0>,
}
impl Default for FrameBuffer {
fn default() -> Self {
let mut framebuffer = BitArray::<[u8; FB_SIZE], Msb0>::ZERO;
framebuffer.as_raw_mut_slice()[0] = 0x61;
FrameBuffer { framebuffer }
}
}
impl FrameBuffer {
/// Initializes a new `FrameBuffer` with all pixels set to
/// `BinaryColor::Off `
pub fn new() -> Self {
Self::default()
}
}
/// This trait represents a device that can receive new images to be displayed.
pub trait Device {
/// Sends a `FrameBuffer` to the device.
fn draw(&mut self, display: &FrameBuffer) -> Result<()>;
/// Convenience method for clearing the whole screen.
/// Most implementations will send an empty `FrameBuffer` to `Device::draw`
/// but there may be more efficient ways for some devices to implement here.
fn clear(&mut self) -> Result<()>;
fn shutdown(&mut self) -> Result<()>;
}
impl Drawable for FrameBuffer {
type Color = BinaryColor;
type Output = ();
fn draw<D>(&self, target: &mut D) -> Result<Self::Output, <D as DrawTarget>::Error>
where
D: DrawTarget<Color = Self::Color>,
{
let iter = (0..5120).map(|i| {
let pos = Point::new(i % 128, i / 128);
Pixel(
pos,
if *self.framebuffer.get(i as usize + 8_usize).unwrap() {
BinaryColor::On
} else {
BinaryColor::Off
},
)
});
target.draw_iter(iter)?;
Ok::<Self::Output, <D as DrawTarget>::Error>(())
}
}
impl OriginDimensions for FrameBuffer {
fn size(&self) -> Size {
Size::new(128, 40)
}
}
impl DrawTarget for FrameBuffer {
type Color = BinaryColor;
type Error = anyhow::Error;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
for Pixel(coord, color) in pixels {
if let (x @ 0..=127, y @ 0..=39) = (coord.x, coord.y) {
// Calculate the index in the framebuffer.
let index: i32 = x + y * 128 + 8;
self.framebuffer.set(index as u32 as usize, color.is_on());
}
}
Ok(())
}
}
#[cfg(feature = "async")]
pub trait AsyncDevice {
type DrawResult<'a>: Future<Output = Result<()>> + 'a
where
Self: 'a;
type ClearResult<'a>: Future<Output = Result<()>> + 'a
where
Self: 'a;
type ShutdownResult<'a>: Future<Output = Result<()>> + 'a
where
Self: 'a;
#[allow(clippy::needless_lifetimes)]
fn draw<'this>(&'this mut self, display: &'this FrameBuffer) -> Self::DrawResult<'this>;
#[allow(clippy::needless_lifetimes)]
fn clear<'this>(&'this mut self) -> Self::ClearResult<'this>;
#[allow(clippy::needless_lifetimes)]
fn shutdown<'this>(&'this mut self) -> Self::ShutdownResult<'this>;
}
#[cfg(feature = "async")]
impl<T: Device> AsyncDevice for T
where
T: 'static,
{
type ClearResult<'a> = impl Future<Output = Result<()>> + 'a
where
Self: 'a;
type DrawResult<'a> = impl Future<Output = Result<()>> + 'a
where
Self: 'a;
type ShutdownResult<'a> = impl Future<Output = Result<()>> + 'a
where
Self: 'a;
#[allow(clippy::needless_lifetimes)]
fn draw<'this>(&'this mut self, display: &'this FrameBuffer) -> Self::DrawResult<'this> {
let x = <Self as Device>::draw(self, display);
async { x }
}
#[allow(clippy::needless_lifetimes)]
fn clear<'this>(&'this mut self) -> Self::ClearResult<'this> {
let x = <Self as Device>::clear(self);
async { x }
}
#[allow(clippy::needless_lifetimes)]
fn shutdown<'this>(&'this mut self) -> Self::ShutdownResult<'this> {
let x = <Self as Device>::shutdown(self);
async { x }
}
}