diff --git a/src/area_serializer.rs b/src/area_serializer.rs index 2f39927..74c7358 100644 --- a/src/area_serializer.rs +++ b/src/area_serializer.rs @@ -9,18 +9,18 @@ use embedded_graphics_core::{ pub struct AreaSerializer { area: Rectangle, rows_per_step: usize, - buffer: Vec, + buffer: Vec, } impl AreaSerializer { pub fn new(area: Rectangle, color: Gray4) -> Self { - let max_entries: usize = 512; // 1 KByte + let max_entries: usize = 1024; // 1 KByte Self::with_buffer_size(area, color, max_entries) } pub fn with_buffer_size(area: Rectangle, color: Gray4, buffer_size: usize) -> Self { - let raw_color: u16 = color.luma() as u16; - let data_entry = raw_color << 12 | raw_color << 8 | raw_color << 4 | raw_color; + let raw_color = color.luma() as u8; + let data_entry = raw_color << 4 | raw_color; // calculate the buffer size let entries_per_row = get_entires_per_row(area) as usize; @@ -50,7 +50,7 @@ impl<'a> AreaSerializerIterator<'a> { } impl<'a> Iterator for AreaSerializerIterator<'a> { - type Item = (AreaImgInfo, &'a [u16]); + type Item = (AreaImgInfo, &'a [u8]); fn next(&mut self) -> Option { let area_height = self.area_serializer.area.size.height; diff --git a/src/interface.rs b/src/interface.rs index 25cc0cd..934c34d 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -1,10 +1,23 @@ //! Contains the controller interface use embedded_hal::{ + delay::*, digital::{InputPin, OutputPin}, - {delay::*, spi::SpiDevice}, + spi::{Operation, SpiDevice}, }; +fn convert_u16_to_u8_slice(from: &[u16]) -> &[u8] { + if cfg!(target_endian = "little") { + + //for byte in from.iter_mut() { + // *byte = byte.to_be(); + //} + } + + let len = from.len().checked_mul(2).unwrap(); + let ptr: *const u8 = from.as_ptr().cast(); + unsafe { core::slice::from_raw_parts(ptr, len) } +} /// Interface Error #[derive(Debug, PartialEq, Eq)] pub enum Error { @@ -26,7 +39,7 @@ pub trait IT8951Interface { fn write_data(&mut self, data: u16) -> Result<(), Error>; /// write mutliple 16bit values to the controller - fn write_multi_data(&mut self, data: &[u16]) -> Result<(), Error>; + fn write_multi_data(&mut self, data: &[u8]) -> Result<(), Error>; /// issue a command on the controller fn write_command(&mut self, cmd: u16) -> Result<(), Error>; @@ -119,21 +132,32 @@ where Ok(()) } - fn write_multi_data(&mut self, data: &[u16]) -> Result<(), Error> { + fn write_multi_data(&mut self, data: &[u8]) -> Result<(), Error> { self.wait_while_busy()?; // Write Data: // 0x0000 -> Prefix for a Data Write - let mut buf = vec![0u8; data.len()*2 + 2 /*write data prefix */]; - - for index in 0..data.len() { - buf[index * 2 + 2] = (data[index] >> 8) as u8; - buf[index * 2 + 2 + 1] = data[index] as u8; - } - - if self.spi.write(&buf).is_err() { + // if self.spi.write(&[0x00, 0x00]).is_err() { + // return Err(Error::SpiError); + // } + + // let mut buf = vec![0u8; data.len()*2 /*write data prefix */]; + + // for index in 0..data.len() { + // buf[index * 2 + 1] = (data[index] >> 8) as u8; + // buf[index * 2] = data[index] as u8; + // } + + if self + .spi + .transaction(&mut [Operation::Write(&[0x00, 0x00]), Operation::Write(&data)]) + .is_err() + { return Err(Error::SpiError); - } + }; + // if self.spi.write(&buf).is_err() { + // return Err(Error::SpiError); + // } Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 95159a2..76b9d72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,13 +183,13 @@ impl IT8951 { /// set all pixel of the frame buffer to the value of raw_color /// raw color must be in range 0..16 - fn clear_frame_buffer(&mut self, raw_color: u16) -> Result<(), Error> { + fn clear_frame_buffer(&mut self, raw_color: u8) -> Result<(), Error> { let dev_info = self.get_dev_info(); let width = dev_info.panel_width; let height = dev_info.panel_height; let mem_addr = dev_info.memory_address; - let data_entry = raw_color << 12 | raw_color << 8 | raw_color << 4 | raw_color; + let data_entry = raw_color << 4 | raw_color; // we need to split the data in multiple transfers to keep the buffer size small for w in 0..height { @@ -207,7 +207,7 @@ impl IT8951 { area_w: width, area_h: 1, }, - &vec![data_entry; width as usize / 4], + &vec![data_entry; width as usize / 2], )?; } Ok(()) @@ -222,7 +222,7 @@ impl IT8951 { &mut self, target_mem_addr: u32, image_settings: MemoryConverterSetting, - data: &[u16], + data: &[u8], ) -> Result<(), Error> { self.set_target_memory_addr(target_mem_addr)?; @@ -245,7 +245,7 @@ impl IT8951 { target_mem_addr: u32, image_settings: MemoryConverterSetting, area_info: &AreaImgInfo, - data: &[u16], + data: &[u8], ) -> Result<(), Error> { self.set_target_memory_addr(target_mem_addr)?; @@ -302,7 +302,7 @@ impl IT8951 { } /// Writes a buffer of u16 values to the given memory address in the controller ram - pub fn memory_burst_write(&mut self, memory_address: u32, data: &[u16]) -> Result<(), Error> { + pub fn memory_burst_write(&mut self, memory_address: u32, data: &[u8]) -> Result<(), Error> { let args = [ memory_address as u16, (memory_address >> 16) as u16, @@ -508,7 +508,7 @@ impl DrawTarget for IT8951 Result<(), Self::Error> { - let raw_color = color.luma() as u16; + let raw_color = color.luma() as u8; self.clear_frame_buffer(raw_color) } @@ -557,7 +557,7 @@ impl DrawTarget for IT8951= 0 && coord.x < width) || (coord.y >= 0 || coord.y < height) { - let data: u16 = (color.luma() as u16) << ((coord.x % 4) * 4); + let data: u8 = (color.luma() as u8) << ((coord.x % 4) * 4); self.load_image_area( dev_info.memory_address, diff --git a/src/pixel_serializer.rs b/src/pixel_serializer.rs index aa2cc3a..ec55f2b 100644 --- a/src/pixel_serializer.rs +++ b/src/pixel_serializer.rs @@ -37,7 +37,7 @@ impl>> PixelSerializer { } impl>> Iterator for PixelSerializer { - type Item = (AreaImgInfo, Vec); + type Item = (AreaImgInfo, Vec); fn next(&mut self) -> Option { if self.row >= self.area.size.height as usize { @@ -51,7 +51,7 @@ impl>> Iterator for PixelSerializer { let max_rows = (self.max_entries / entries_per_row).min(self.area.size.height as usize); assert!(max_rows > 0, "Buffer size to small for one row"); //let mut bytes = Vec::with_capacity(entries_per_row * max_rows); - let mut bytes = vec![0x0000; entries_per_row * max_rows]; + let mut bytes = vec![0x00; entries_per_row * max_rows]; // add all pixels to buffer for Pixel(point, color) in self.pixels.by_ref() { @@ -59,7 +59,7 @@ impl>> Iterator for PixelSerializer { + entries_per_row * (self.row - start_row); let bit_pos = (point.x % 4) * 4; - bytes[byte_pos] |= (color.luma() as u16) << bit_pos; + bytes[byte_pos] |= (color.luma() as u8) << bit_pos; // end of row if point.x >= self.area.top_left.x + self.area.size.width as i32 - 1 { diff --git a/src/serialization_helper.rs b/src/serialization_helper.rs index 9283c6d..def363b 100644 --- a/src/serialization_helper.rs +++ b/src/serialization_helper.rs @@ -7,7 +7,7 @@ pub fn get_entires_per_row(area: Rectangle) -> u32 { let alignment_pixels = area.top_left.x as u32 % 4; - (area.size.width + alignment_pixels).div_ceil(PIXEL_PER_WORD) + (area.size.width + alignment_pixels).div_ceil(PIXEL_PER_WORD) * 2 } #[cfg(test)]