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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::{
command::AddrMode,
rotation::DisplayRotation,
size::{DisplaySize, NewZeroed},
Ssd1306,
};
use display_interface::{DisplayError, WriteOnlyDataCommand};
#[derive(Clone, Debug)]
pub struct BufferedGraphicsMode<SIZE>
where
SIZE: DisplaySize,
{
buffer: SIZE::Buffer,
min_x: u8,
max_x: u8,
min_y: u8,
max_y: u8,
}
impl<SIZE> BufferedGraphicsMode<SIZE>
where
SIZE: DisplaySize,
{
pub(crate) fn new() -> Self {
Self {
buffer: NewZeroed::new_zeroed(),
min_x: 255,
max_x: 0,
min_y: 255,
max_y: 0,
}
}
}
impl<DI, SIZE> DisplayConfig for Ssd1306<DI, SIZE, BufferedGraphicsMode<SIZE>>
where
DI: WriteOnlyDataCommand,
SIZE: DisplaySize,
{
type Error = DisplayError;
fn set_rotation(&mut self, rot: DisplayRotation) -> Result<(), DisplayError> {
self.set_rotation(rot)
}
fn init(&mut self) -> Result<(), DisplayError> {
self.clear();
self.init_with_addr_mode(AddrMode::Horizontal)
}
}
impl<DI, SIZE> Ssd1306<DI, SIZE, BufferedGraphicsMode<SIZE>>
where
DI: WriteOnlyDataCommand,
SIZE: DisplaySize,
{
pub fn clear(&mut self) {
for b in self.mode.buffer.as_mut() {
*b = 0;
}
let (width, height) = self.dimensions();
self.mode.min_x = 0;
self.mode.max_x = width - 1;
self.mode.min_y = 0;
self.mode.max_y = height - 1;
}
pub fn flush(&mut self) -> Result<(), DisplayError> {
if self.mode.max_x < self.mode.min_x || self.mode.max_y < self.mode.min_y {
return Ok(());
}
let (width, height) = self.dimensions();
let disp_min_x = self.mode.min_x;
let disp_min_y = self.mode.min_y;
let (disp_max_x, disp_max_y) = match self.rotation {
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => (
(self.mode.max_x + 1).min(width),
(self.mode.max_y | 7).min(height),
),
DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => (
(self.mode.max_x | 7).min(width),
(self.mode.max_y + 1).min(height),
),
};
self.mode.min_x = 255;
self.mode.max_x = 0;
self.mode.min_y = 255;
self.mode.max_y = 0;
let offset_x = match self.rotation {
DisplayRotation::Rotate0 | DisplayRotation::Rotate270 => SIZE::OFFSETX,
DisplayRotation::Rotate180 | DisplayRotation::Rotate90 => {
SIZE::DRIVER_COLS - SIZE::WIDTH - SIZE::OFFSETX
}
};
match self.rotation {
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
self.set_draw_area(
(disp_min_x + offset_x, disp_min_y + SIZE::OFFSETY),
(disp_max_x + offset_x, disp_max_y + SIZE::OFFSETY),
)?;
Self::flush_buffer_chunks(
&mut self.interface,
self.mode.buffer.as_mut(),
width as usize,
(disp_min_x, disp_min_y),
(disp_max_x, disp_max_y),
)
}
DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => {
self.set_draw_area(
(disp_min_y + offset_x, disp_min_x + SIZE::OFFSETY),
(disp_max_y + offset_x, disp_max_x + SIZE::OFFSETY),
)?;
Self::flush_buffer_chunks(
&mut self.interface,
self.mode.buffer.as_mut(),
height as usize,
(disp_min_y, disp_min_x),
(disp_max_y, disp_max_x),
)
}
}
}
pub fn set_pixel(&mut self, x: u32, y: u32, value: bool) {
let value = value as u8;
let rotation = self.rotation;
let (idx, bit) = match rotation {
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
let idx = ((y as usize) / 8 * SIZE::WIDTH as usize) + (x as usize);
let bit = y % 8;
(idx, bit)
}
DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => {
let idx = ((x as usize) / 8 * SIZE::WIDTH as usize) + (y as usize);
let bit = x % 8;
(idx, bit)
}
};
if let Some(byte) = self.mode.buffer.as_mut().get_mut(idx) {
self.mode.min_x = self.mode.min_x.min(x as u8);
self.mode.max_x = self.mode.max_x.max(x as u8);
self.mode.min_y = self.mode.min_y.min(y as u8);
self.mode.max_y = self.mode.max_y.max(y as u8);
*byte = *byte & !(1 << bit) | (value << bit)
}
}
}
#[cfg(feature = "graphics")]
use embedded_graphics_core::{
draw_target::DrawTarget,
geometry::Size,
geometry::{Dimensions, OriginDimensions},
pixelcolor::BinaryColor,
Pixel,
};
use super::DisplayConfig;
#[cfg(feature = "graphics")]
impl<DI, SIZE> DrawTarget for Ssd1306<DI, SIZE, BufferedGraphicsMode<SIZE>>
where
DI: WriteOnlyDataCommand,
SIZE: DisplaySize,
{
type Color = BinaryColor;
type Error = DisplayError;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
let bb = self.bounding_box();
pixels
.into_iter()
.filter(|Pixel(pos, _color)| bb.contains(*pos))
.for_each(|Pixel(pos, color)| {
self.set_pixel(pos.x as u32, pos.y as u32, color.is_on())
});
Ok(())
}
}
#[cfg(feature = "graphics")]
impl<DI, SIZE> OriginDimensions for Ssd1306<DI, SIZE, BufferedGraphicsMode<SIZE>>
where
DI: WriteOnlyDataCommand,
SIZE: DisplaySize,
{
fn size(&self) -> Size {
let (w, h) = self.dimensions();
Size::new(w.into(), h.into())
}
}