1
+ //! Functions for accessing and writing text to the LCD.
2
+ //!
3
+ //! The display has two layers that are blended on top of each other, and a background layer
4
+ //! with an uniform color.
5
+
1
6
pub use self :: color:: Color ;
2
7
pub use self :: init:: init;
3
8
pub use self :: stdout:: init as init_stdout;
@@ -10,18 +15,28 @@ pub mod stdout;
10
15
mod color;
11
16
mod init;
12
17
18
+ /// The height of the display in pixels.
13
19
pub const HEIGHT : usize = 272 ;
20
+ /// The width of the display in pixels.
14
21
pub const WIDTH : usize = 480 ;
15
22
23
+ /// The number of bytes per pixel for layer 1.
16
24
pub const LAYER_1_OCTETS_PER_PIXEL : usize = 4 ;
25
+ /// The length of the layer 1 buffer in bytes.
17
26
pub const LAYER_1_LENGTH : usize = HEIGHT * WIDTH * LAYER_1_OCTETS_PER_PIXEL ;
27
+ /// The number of bytes per pixel for layer 2.
18
28
pub const LAYER_2_OCTETS_PER_PIXEL : usize = 2 ;
29
+ /// The length of the layer 1 buffer in bytes.
19
30
pub const LAYER_2_LENGTH : usize = HEIGHT * WIDTH * LAYER_2_OCTETS_PER_PIXEL ;
20
31
32
+ /// Start address of the SDRAM where the framebuffers live.
21
33
pub const SDRAM_START : usize = 0xC000_0000 ;
34
+ /// Start address of the layer 1 framebuffer.
22
35
pub const LAYER_1_START : usize = SDRAM_START ;
36
+ /// Start address of the layer 2 framebuffer.
23
37
pub const LAYER_2_START : usize = SDRAM_START + LAYER_1_LENGTH ;
24
38
39
+ /// Represents the LCD and provides methods to access both layers.
25
40
pub struct Lcd < ' a > {
26
41
controller : & ' a mut LTDC ,
27
42
layer_1_in_use : bool ,
@@ -37,12 +52,14 @@ impl<'a> Lcd<'a> {
37
52
}
38
53
}
39
54
55
+ /// Sets the color of the background layer.
40
56
pub fn set_background_color ( & mut self , color : Color ) {
41
57
self . controller
42
58
. bccr
43
59
. modify ( |_, w| unsafe { w. bc ( ) . bits ( color. to_rgb ( ) ) } ) ;
44
60
}
45
61
62
+ /// Returns a reference to layer 1.
46
63
pub fn layer_1 ( & mut self ) -> Option < Layer < FramebufferArgb8888 > > {
47
64
if self . layer_1_in_use {
48
65
None
@@ -53,6 +70,7 @@ impl<'a> Lcd<'a> {
53
70
}
54
71
}
55
72
73
+ /// Returns a reference to layer 2.
56
74
pub fn layer_2 ( & mut self ) -> Option < Layer < FramebufferAl88 > > {
57
75
if self . layer_2_in_use {
58
76
None
@@ -64,10 +82,15 @@ impl<'a> Lcd<'a> {
64
82
}
65
83
}
66
84
85
+ /// Represents a buffer of pixels.
67
86
pub trait Framebuffer {
87
+ /// Set the pixel at the specified coordinates to the specified color.
68
88
fn set_pixel ( & mut self , x : usize , y : usize , color : Color ) ;
69
89
}
70
90
91
+ /// A framebuffer in the ARGB8888 format.
92
+ ///
93
+ /// It uses 8bits for alpha, red, green, and black respectively, totaling in 32bits per pixel.
71
94
pub struct FramebufferArgb8888 {
72
95
base_addr : usize ,
73
96
}
@@ -86,6 +109,10 @@ impl Framebuffer for FramebufferArgb8888 {
86
109
}
87
110
}
88
111
112
+ /// A framebuffer in the AL88 format.
113
+ ///
114
+ /// There are 8bits for the alpha channel and 8 bits for specifying a color using a
115
+ /// lookup table. Thus, each pixel is represented by 16bits.
89
116
pub struct FramebufferAl88 {
90
117
base_addr : usize ,
91
118
}
@@ -104,11 +131,15 @@ impl Framebuffer for FramebufferAl88 {
104
131
}
105
132
}
106
133
134
+ /// Represents a layer of the LCD controller.
107
135
pub struct Layer < T > {
108
136
framebuffer : T ,
109
137
}
110
138
111
139
impl < T : Framebuffer > Layer < T > {
140
+ /// Fill the layer with horizontal stripes.
141
+ ///
142
+ /// Useful for testing.
112
143
pub fn horizontal_stripes ( & mut self ) {
113
144
let colors = [
114
145
0xffffff , 0xcccccc , 0x999999 , 0x666666 , 0x333333 , 0x0 , 0xff0000 , 0x0000ff ,
@@ -126,6 +157,9 @@ impl<T: Framebuffer> Layer<T> {
126
157
}
127
158
}
128
159
160
+ /// Fill the layer with vertical stripes.
161
+ ///
162
+ /// Useful for testing.
129
163
pub fn vertical_stripes ( & mut self ) {
130
164
let colors = [
131
165
0xcccccc , 0x999999 , 0x666666 , 0x333333 , 0x0 , 0xff0000 , 0x0000ff , 0xffffff ,
@@ -143,6 +177,9 @@ impl<T: Framebuffer> Layer<T> {
143
177
}
144
178
}
145
179
180
+ /// Clear all pixels.
181
+ ///
182
+ /// This method sets each pixel to transparent or black, depending on the framebuffer format.
146
183
pub fn clear ( & mut self ) {
147
184
for i in 0 ..HEIGHT {
148
185
for j in 0 ..WIDTH {
@@ -151,17 +188,20 @@ impl<T: Framebuffer> Layer<T> {
151
188
}
152
189
}
153
190
191
+ /// Sets the pixel at the specified coordinates to white.
154
192
pub fn print_point_at ( & mut self , x : usize , y : usize ) {
155
193
self . print_point_color_at ( x, y, Color :: from_hex ( 0xffffff ) ) ;
156
194
}
157
195
196
+ /// Sets the pixel at the specified coordinates to the specified color.
158
197
pub fn print_point_color_at ( & mut self , x : usize , y : usize , color : Color ) {
159
198
assert ! ( x < WIDTH ) ;
160
199
assert ! ( y < HEIGHT ) ;
161
200
162
201
self . framebuffer . set_pixel ( x, y, color) ;
163
202
}
164
203
204
+ /// Creates a text writer on this layer.
165
205
pub fn text_writer ( & mut self ) -> TextWriter < T > {
166
206
TextWriter {
167
207
layer : self ,
@@ -171,13 +211,15 @@ impl<T: Framebuffer> Layer<T> {
171
211
}
172
212
}
173
213
214
+ /// Allows to print audio data.
174
215
pub struct AudioWriter {
175
216
next_pixel : usize ,
176
217
next_col : usize ,
177
218
prev_value : ( usize , usize ) ,
178
219
}
179
220
180
221
impl AudioWriter {
222
+ /// Creates a new audio writer starting at the left edge of the screen.
181
223
pub fn new ( ) -> Self {
182
224
AudioWriter {
183
225
next_pixel : 0 ,
@@ -186,11 +228,15 @@ impl AudioWriter {
186
228
}
187
229
}
188
230
231
+ /// Sets the next pixel on the layer.
232
+ ///
233
+ /// Useful for testing.
189
234
pub fn set_next_pixel < F : Framebuffer > ( & mut self , layer : & mut Layer < F > , color : Color ) {
190
235
layer. print_point_color_at ( self . next_pixel % WIDTH , self . next_pixel / WIDTH , color) ;
191
236
self . next_pixel = ( self . next_pixel + 1 ) % ( HEIGHT * WIDTH ) ;
192
237
}
193
238
239
+ /// Sets the next column of the screen according to the passed audio data.
194
240
pub fn set_next_col < F : Framebuffer > ( & mut self , layer : & mut Layer < F > , value0 : u32 , value1 : u32 ) {
195
241
let value0 = value0 + 2u32 . pow ( 15 ) ;
196
242
let value0 = value0 as u16 as usize ;
@@ -232,6 +278,10 @@ impl AudioWriter {
232
278
}
233
279
}
234
280
281
+ /// Allows writing text to the wrapped layer.
282
+ ///
283
+ /// This struct implements the [fmt::Write](core::fmt::Write) trait, which makes it possible
284
+ /// to use the `writeln!` macro with this struct.
235
285
pub struct TextWriter < ' a , T : Framebuffer + ' a > {
236
286
layer : & ' a mut Layer < T > ,
237
287
x_pos : usize ,
0 commit comments