epd_datafuri/graphics/
display290_mono.rs1use display_interface::DisplayError;
3use embedded_graphics::{pixelcolor::BinaryColor, prelude::*};
4
5use crate::color::Color;
6use crate::prelude::Display;
7
8use crate::graphics::buffer_len;
9use crate::graphics::DisplayRotation;
10
11pub const WIDTH: u16 = 128;
13pub const HEIGHT: u16 = 296;
15
16pub struct Display2in9Mono {
18 buffer: [u8; buffer_len(WIDTH as usize, HEIGHT as usize)],
19 rotation: DisplayRotation,
20 is_inverted: bool,
21}
22
23impl Default for Display2in9Mono {
24 fn default() -> Self {
25 Self::new()
26 }
27}
28
29impl Display2in9Mono {
30 pub fn new() -> Self {
32 Self {
33 buffer: [Color::White.get_byte_value(); buffer_len(WIDTH as usize, HEIGHT as usize)],
34 rotation: DisplayRotation::Rotate270,
35 is_inverted: false,
36 }
37 }
38}
39
40impl DrawTarget for Display2in9Mono {
41 type Error = DisplayError;
42 type Color = BinaryColor;
43
44 fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
45 where
46 I: IntoIterator<Item = Pixel<Self::Color>>,
47 {
48 for p in pixels.into_iter() {
49 self.draw_helper(WIDTH.into(), HEIGHT.into(), p)?;
50 }
51 Ok(())
52 }
53}
54
55impl OriginDimensions for Display2in9Mono {
56 fn size(&self) -> Size {
57 match self.rotation() {
59 DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
60 Size::new(WIDTH.into(), HEIGHT.into())
61 }
62 DisplayRotation::Rotate90 | DisplayRotation::Rotate270 => {
63 Size::new(HEIGHT.into(), WIDTH.into())
64 }
65 }
66 }
67}
68
69impl Display for Display2in9Mono {
70 fn buffer(&self) -> &[u8] {
71 &self.buffer
72 }
73
74 fn get_mut_buffer(&mut self) -> &mut [u8] {
75 &mut self.buffer
76 }
77
78 fn set_rotation(&mut self, rotation: DisplayRotation) {
79 self.rotation = rotation;
80 }
81
82 fn rotation(&self) -> DisplayRotation {
83 self.rotation
84 }
85
86 fn is_inverted(&self) -> bool {
87 self.is_inverted
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::{Display, Display2in9Mono, DisplayRotation, HEIGHT, WIDTH};
94 use crate::color::{Black, Color};
95 use crate::graphics::{find_position, outside_display};
96 use embedded_graphics::{prelude::*, primitives::Line, primitives::PrimitiveStyle};
97
98 #[test]
99 fn buffer_clear() {
100 let mut display = Display2in9Mono::new();
101
102 for &byte in display.buffer().iter() {
103 assert_eq!(byte, Color::White.get_byte_value());
104 }
105
106 display.clear_buffer(Color::Black);
107
108 for &byte in display.buffer().iter() {
109 assert_eq!(byte, Color::Black.get_byte_value());
110 }
111 }
112
113 #[test]
114 fn rotation_overflow() {
115 let width = WIDTH as u32;
116 let height = HEIGHT as u32;
117 test_rotation_overflow(width, height, DisplayRotation::Rotate0);
118 test_rotation_overflow(width, height, DisplayRotation::Rotate90);
119 test_rotation_overflow(width, height, DisplayRotation::Rotate180);
120 test_rotation_overflow(width, height, DisplayRotation::Rotate270);
121 }
122
123 fn test_rotation_overflow(width: u32, height: u32, rotation: DisplayRotation) {
124 let max_value = width.div_ceil(8) * height;
125 for x in 0..(width + height) {
126 for y in 0..u32::MAX {
127 if outside_display(Point::new(x as i32, y as i32), width, height, rotation) {
128 break;
129 } else {
130 let (idx, _) = find_position(x, y, width, height, rotation);
131 assert!(idx < max_value, "{idx} !< {max_value}",);
132 }
133 }
134 }
135 }
136
137 #[test]
138 fn graphics_rotation_0() {
139 let mut display = Display2in9Mono::new();
140 display.set_rotation(DisplayRotation::Rotate0);
141
142 let _ = Line::new(Point::new(0, 0), Point::new(7, 0))
143 .into_styled(PrimitiveStyle::with_stroke(Black, 1))
144 .draw(&mut display);
145
146 let buffer = display.buffer();
147 assert_eq!(buffer[0], Color::Black.get_byte_value());
148
149 for &byte in buffer.iter().skip(1) {
150 assert_eq!(byte, Color::White.get_byte_value());
151 }
152 }
153}