Init commit of ili dispalay test application

This commit is contained in:
2020-05-20 07:49:23 +02:00
commit f04b969afb
4 changed files with 740 additions and 0 deletions

110
src/ilidisplay.rs Normal file
View 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);
}
}

43
src/main.rs Normal file
View File

@@ -0,0 +1,43 @@
#[macro_use]
extern crate arrayref;
use std::error::Error;
use std::thread;
use std::time::Duration;
use rppal::gpio::Gpio;
use rppal::spi::{Bus, Mode, Segment, SlaveSelect, Spi};
use rppal::system::DeviceInfo;
use image::{
Rgb,
RgbImage
};
use imageproc::rect::Rect;
use imageproc::drawing::draw_filled_rect_mut;
mod ilidisplay;
fn main() -> Result<(), Box<dyn Error>> {
let mut img = RgbImage::new(480, 320);
let thickness = 10;
draw_filled_rect_mut(&mut img, Rect::at(0,0).of_size(240, 160), Rgb([255,0,0]));
draw_filled_rect_mut(&mut img, Rect::at(0,160).of_size(240, 160), Rgb([255,0,0]));
draw_filled_rect_mut(&mut img, Rect::at(240,0).of_size(240, 160), Rgb([0, 0, 255]));
let mut e = ilidisplay::IliDisplay::init()?;
println!("Before init, sleeping for 5s");
thread::sleep(Duration::from_millis(5000));
e.init_chip();
println!("After init, sleeping for 5s");
thread::sleep(Duration::from_millis(5000));
e.turn_on();
println!("Before image, screen has now been reset and turned on, sleeping for 5s");
thread::sleep(Duration::from_millis(5000));
e.send_image(img);
println!("You should see image now, sleeping for 5s");
thread::sleep(Duration::from_millis(5000));
e.turn_off();
Ok(())
}