Init commit of ili dispalay test application
This commit is contained in:
110
src/ilidisplay.rs
Normal file
110
src/ilidisplay.rs
Normal file
@@ -0,0 +1,110 @@
|
||||
|
||||
use std::error::Error;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
use rppal::gpio::{Gpio, OutputPin};
|
||||
use rppal::spi::{Bus, Mode, Segment, SlaveSelect, Spi, Polarity};
|
||||
use rppal::system::DeviceInfo;
|
||||
|
||||
use image::RgbImage;
|
||||
|
||||
pub struct IliDisplay {
|
||||
spi: Spi,
|
||||
dc_pin: OutputPin,
|
||||
reset_pin: OutputPin,
|
||||
led_pin: OutputPin,
|
||||
}
|
||||
|
||||
// Gpio uses BCM pin numbering. BCM GPIO 23 is tied to physical pin 16.
|
||||
const GPIO_LED: u8 = 23;
|
||||
const GPIO_DC: u8 = 24;
|
||||
const GPIO_RESET: u8 = 25;
|
||||
|
||||
impl IliDisplay {
|
||||
|
||||
|
||||
// Initialize an IliDisplay struct with defaults
|
||||
|
||||
pub fn init() -> Result<IliDisplay, Box<dyn Error>> {
|
||||
|
||||
let mut gpio = Gpio::new()?;
|
||||
|
||||
let mut dc_pin = gpio.get(GPIO_DC)?.into_output();
|
||||
let mut led_pin = gpio.get(GPIO_LED)?.into_output();
|
||||
let mut reset_pin = gpio.get(GPIO_RESET)?.into_output();
|
||||
|
||||
let mut spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 1_000_000, Mode::Mode0)?;
|
||||
spi.set_ss_polarity(Polarity::ActiveHigh);
|
||||
|
||||
return Ok(IliDisplay {
|
||||
spi, dc_pin, reset_pin, led_pin
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pub fn send_command (&mut self, command: u8, data: &[u8]) {
|
||||
// set DC low for byte one
|
||||
self.dc_pin.set_low();
|
||||
self.spi.write(&[command]);
|
||||
self.dc_pin.set_high();
|
||||
if (data.len() > 0) {
|
||||
self.spi.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Send initialization sequence to Ili9488
|
||||
pub fn init_chip(&mut self) {
|
||||
|
||||
self.dc_pin.set_high();
|
||||
// drop the reset pin to low and set it high again to fully reset the display
|
||||
self.reset_pin.set_high();
|
||||
thread::sleep(Duration::from_millis(20));
|
||||
self.reset_pin.set_low();
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
self.reset_pin.set_high();
|
||||
thread::sleep(Duration::from_millis(20));
|
||||
|
||||
self.send_command(0x01, &[]);
|
||||
thread::sleep(Duration::from_millis(50));
|
||||
self.send_command(0x28, &[]);
|
||||
self.send_command(0xE0, &[0x00, 0x03, 0x09, 0x08, 0x16, 0x0A, 0x3F, 0x78, 0x4C, 0x09, 0x0A, 0x08, 0x16, 0x1A, 0x0F]);
|
||||
self.send_command(0xE1, &[0x00, 0x16, 0x19, 0x03, 0x0F, 0x05, 0x32, 0x45, 0x46, 0x04, 0x0E, 0x0D, 0x35, 0x37, 0x0F]);
|
||||
self.send_command(0xC0, &[0x17, 0x15]);
|
||||
self.send_command(0xC1, &[0x41]);
|
||||
self.send_command(0x36, &[0xE8]);
|
||||
self.send_command(0x3A, &[0x66]);
|
||||
self.send_command(0xB0, &[0x80]);
|
||||
self.send_command(0xB1, &[0xA0]);
|
||||
self.send_command(0xB4, &[0x02]);
|
||||
self.send_command(0xB6, &[0x02, 0x02, 0x3B]);
|
||||
self.send_command(0xE9, &[0x00]);
|
||||
self.send_command(0xF7, &[0xA9, 0x51, 0x2C, 0x82]);
|
||||
self.send_command(0x11, &[]);
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
self.send_command(0x29, &[]);
|
||||
thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
|
||||
pub fn turn_on (&mut self) {
|
||||
self.led_pin.set_high();
|
||||
}
|
||||
|
||||
pub fn turn_off (&mut self) {
|
||||
self.led_pin.set_low();
|
||||
}
|
||||
|
||||
// send one image to the display
|
||||
pub fn send_image(&mut self, image: RgbImage) -> () {
|
||||
println!("send image");
|
||||
// assume 480x320 for both image and screen
|
||||
self.send_command(0x2A, &[0, 0, (480 >> 8) as u8, (480 & 0xFF) as u8]);
|
||||
self.send_command(0x2B, &[0, 0, (320 >> 8) as u8, (320 & 0xFF) as u8]);
|
||||
let imagedata = image.into_raw();
|
||||
// let imagedata_ref = array_ref![imagedata, 0, 480*320*3];
|
||||
let imagedata_ref = array_ref![imagedata, 0, 4096];
|
||||
self.spi.write(imagedata_ref);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user