lcd_ili9341/
lib.rs

1#![no_std]
2
3/// Trait representing the interface to the hardware.
4/// Intended to abstract the various buses (SPI, MPU 8/9/16/18-bit) from the
5/// Controller code.
6pub trait Interface {
7	fn write_parameters(&self, command: u8, data: &[u8]);
8	fn write_memory<I>(&self, iterable: I) where I: IntoIterator<Item=u32>;
9	fn read_parameters(&self, command: u8, data: &mut [u8]);
10	fn read_memory(&self, data: &mut [u32]);
11}
12
13pub enum TearingEffect {
14	Off,
15	VBlankOnly,
16	HAndVBlank,
17}
18
19// TODO: Implement access "methods" on these types.
20
21#[derive(Copy, Clone, Default)]
22pub struct DisplayIdentification {
23	raw: [u8; 3],
24}
25
26#[derive(Copy, Clone, Default)]
27pub struct DisplayStatus {
28	raw: [u8; 4],
29}
30
31#[derive(Copy, Clone, Default)]
32pub struct DisplayPowerMode {
33	raw: [u8; 1],
34}
35
36#[derive(Copy, Clone, Default)]
37pub struct MADCtl {
38	raw: [u8; 1],
39}
40
41#[derive(Copy, Clone, Default)]
42pub struct PixelFormat {
43	raw: [u8; 1],
44}
45
46#[derive(Copy, Clone, Default)]
47pub struct ImageFormat {
48	raw: [u8; 1],
49}
50
51#[derive(Copy, Clone, Default)]
52pub struct SignalMode {
53	raw: [u8; 1],
54}
55
56#[derive(Copy, Clone, Default)]
57pub struct SelfDiagnosticResult {
58	raw: [u8; 1],
59}
60
61#[derive(Copy, Clone, Default)]
62pub struct MemoryAccessControl {
63	raw: [u8; 1],
64}
65
66#[derive(Copy, Clone, Default)]
67pub struct CtrlDisplay {
68	raw: [u8; 1],
69}
70
71/// Controller implements the LCD command set and calls on the Interface trait
72/// to communicate with the LCD panel.
73#[derive(Copy, Clone)]
74pub struct Controller<T>
75	where T: Interface
76{
77	iface: T,
78}
79
80impl<T: Interface> Controller<T> 
81	where T: Interface
82{
83	pub fn new(iface: T) -> Controller<T> {
84		Controller {
85			iface: iface,
86		}
87	}
88
89	fn write_command(&self, command: u8) {
90		self.iface.write_parameters(command, &[]);
91	}
92
93	fn write_parameters(&self, command: u8, parameters: &[u8]) {
94		self.iface.write_parameters(command, parameters);
95	}
96
97	fn read_parameters(&self, command: u8, parameters: &mut [u8]) {
98		self.iface.read_parameters(command, parameters);
99	}
100
101	pub fn nop(&self) {
102		self.write_command(0x00);
103	}
104
105	pub fn software_reset(&self) {
106		self.write_command(0x01);
107	}
108
109	pub fn read_display_identification(&self) -> DisplayIdentification {
110		let mut result = DisplayIdentification::default();
111		self.read_parameters(0x04, &mut result.raw);
112		result
113	}
114
115	pub fn read_display_status(&self) -> DisplayStatus {
116		let mut result = DisplayStatus::default();
117		self.read_parameters(0x09, &mut result.raw);
118		result
119	}
120
121	pub fn read_display_power_mode(&self) -> DisplayPowerMode {
122		let mut result = DisplayPowerMode::default();
123		self.read_parameters(0x0a, &mut result.raw);
124		result
125	}
126
127	pub fn read_display_madctl(&self) -> MADCtl {
128		let mut result = MADCtl::default();
129		self.read_parameters(0x0b, &mut result.raw);
130		result
131	}
132
133	pub fn read_pixel_format(&self) -> PixelFormat {
134		let mut result = PixelFormat::default();
135		self.read_parameters(0x0c, &mut result.raw);
136		result
137	}
138
139	pub fn read_image_format(&self) -> ImageFormat {
140		let mut result = ImageFormat::default();
141		self.read_parameters(0x0d, &mut result.raw);
142		result
143	}
144
145	pub fn read_signal_mode(&self) -> SignalMode {
146		let mut result = SignalMode::default();
147		self.read_parameters(0x0e, &mut result.raw);
148		result
149	}
150
151	pub fn read_self_diagnostic_result(&self) -> SelfDiagnosticResult {
152		let mut result = SelfDiagnosticResult::default();
153		self.read_parameters(0x0f, &mut result.raw);
154		result
155	}
156
157	pub fn enter_sleep_mode(&self) {
158		self.write_command(0x10);
159	}
160
161	pub fn sleep_out(&self) {
162		self.write_command(0x11);
163	}
164
165	pub fn partial_mode_on(&self) {
166		self.write_command(0x12);
167	}
168
169	pub fn normal_display_mode_on(&self) {
170		self.write_command(0x13);
171	}
172
173	pub fn display_inversion(&self, on: bool) {
174		let command = match on {
175			false => 0x20,
176			true  => 0x21,
177		};
178		self.write_command(command);
179	}
180
181	pub fn gamma_set(&self, gc: u8) {
182		self.write_parameters(0x26, &[gc]);
183	}
184
185	pub fn display(&self, on: bool) {
186		let command = match on {
187			false => 0x28,
188			true  => 0x29,
189		};
190		self.write_command(command);
191	}
192
193	pub fn column_address_set(&self, sc: u16, ec: u16) {
194		self.write_parameters(0x2a, &[
195			(sc >> 8) as u8, (sc & 0xff) as u8,
196			(ec >> 8) as u8, (ec & 0xff) as u8,
197		]);
198	}
199
200	pub fn page_address_set(&self, sp: u16, ep: u16) {
201		self.write_parameters(0x2b, &[
202			(sp >> 8) as u8, (sp & 0xff) as u8,
203			(ep >> 8) as u8, (ep & 0xff) as u8,
204		]);
205	}
206
207	pub fn memory_write_start(&self) {
208		self.write_command(0x2c);
209	}
210
211	pub fn color_set(&self, data: &[u8; 128]) {
212		self.write_parameters(0x2d, data);
213	}
214
215	pub fn memory_read_start(&self) {
216		self.write_command(0x2e);
217	}
218
219	pub fn partial_area(&self, sr: u16, er: u16) {
220		self.write_parameters(0x30, &[
221			(sr >> 8) as u8, (sr & 0xff) as u8,
222			(er >> 8) as u8, (er & 0xff) as u8,
223		]);
224	}
225
226	pub fn vertical_scrolling_definition(&self, tfa: u16, vsa: u16, bfa: u16) {
227		self.write_parameters(0x33, &[
228			(tfa >> 8) as u8, (tfa & 0xff) as u8,
229			(vsa >> 8) as u8, (vsa & 0xff) as u8,
230			(bfa >> 8) as u8, (bfa & 0xff) as u8,
231		]);
232	}
233
234	pub fn tearing_effect(&self, mode: TearingEffect) {
235		match mode {
236			TearingEffect::VBlankOnly => self.write_parameters(0x35, &[0u8]),
237			TearingEffect::HAndVBlank => self.write_parameters(0x35, &[1u8]),
238			_                         => self.write_command(0x34),
239		};
240	}
241
242	pub fn memory_access_control(&self, value: MemoryAccessControl) {
243		self.write_parameters(0x36, &value.raw);
244	}
245
246	pub fn vertical_scrolling_start_address(&self, vsp: u16) {
247		self.write_parameters(0x37, &[
248			(vsp >> 8) as u8, (vsp & 0xff) as u8,
249		]);
250	}
251
252	pub fn idle_mode(&self, on: bool) {
253		let command = match on {
254			false => 0x38,
255			true  => 0x39,
256		};
257		self.write_command(command);
258	}
259
260	pub fn pixel_format_set(&self, value: PixelFormat) {
261		self.write_parameters(0x3a, &value.raw);
262	}
263
264	pub fn write_memory_continue(&self) {
265		self.write_command(0x3c);
266	}
267
268	pub fn write_memory<I>(&self, iterable: I)
269		where I: IntoIterator<Item=u32>
270	{
271		self.iface.write_memory(iterable);
272	}
273
274	pub fn read_memory_continue(&self) {
275		self.write_command(0x3e);
276	}
277
278	pub fn read_memory(&self, data: &mut [u32]) {
279		self.iface.read_memory(data);
280	}
281	
282	pub fn set_tear_scanline(&self, sts: u16) {
283		self.write_parameters(0x44, &[
284			(sts >> 8) as u8, (sts & 0xff) as u8,
285		]);
286	}
287
288	pub fn get_scanline(&self) -> u16 {
289		let mut result = [0u8; 2];
290		self.read_parameters(0x45, &mut result);
291		((result[0] as u16) << 8) | result[1] as u16
292	}
293
294	pub fn write_display_brightness(&self, dbv: u8) {
295		self.write_parameters(0x51, &[dbv]);
296	}
297
298	pub fn read_display_brightness(&self) -> u8 {
299		let mut result = [0u8; 1];
300		self.read_parameters(0x52, &mut result);
301		result[0]
302	}
303
304	pub fn write_ctrl_display(&self, value: CtrlDisplay) {
305		self.write_parameters(0x53, &value.raw);
306	}
307
308	pub fn read_ctrl_display(&self) -> CtrlDisplay {
309		let mut result = CtrlDisplay::default();
310		self.read_parameters(0x54, &mut result.raw);
311		result
312	}
313
314	pub fn write_cabc(&self, c: u8) {
315		self.write_parameters(0x55, &[c]);
316	}
317
318	pub fn read_cabc(&self) -> u8 {
319		let mut result = [0u8; 1];
320		self.read_parameters(0x56, &mut result);
321		result[0]
322	}
323
324	pub fn write_cabc_minimum_brightness(&self, cmb: u8) {
325		self.write_parameters(0x5e, &[cmb]);
326	}
327
328	pub fn read_cabc_minimum_brightness(&self) -> u8 {
329		let mut result = [0u8; 1];
330		self.read_parameters(0x5f, &mut result);
331		result[0]
332	}
333
334	pub fn read_id1(&self) -> u8 {
335		let mut result = [0u8; 1];
336		self.read_parameters(0xda, &mut result);
337		result[0]
338	}
339
340	pub fn read_id2(&self) -> u8 {
341		let mut result = [0u8; 1];
342		self.read_parameters(0xdb, &mut result);
343		result[0]
344	}
345
346	pub fn read_id3(&self) -> u8 {
347		let mut result = [0u8; 1];
348		self.read_parameters(0xdc, &mut result);
349		result[0]
350	}
351
352	// TODO: Implement extended command set
353}