Skip to content

Commit

Permalink
use u8 buffer internally
Browse files Browse the repository at this point in the history
  • Loading branch information
pbert519 committed Jun 6, 2024
1 parent 15d615f commit e00f5ad
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/area_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ use embedded_graphics_core::{
pub struct AreaSerializer {
area: Rectangle,
rows_per_step: usize,
buffer: Vec<u16>,
buffer: Vec<u8>,
}

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;
Expand Down Expand Up @@ -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<Self::Item> {
let area_height = self.area_serializer.area.size.height;
Expand Down
48 changes: 36 additions & 12 deletions src/interface.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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>;
Expand Down Expand Up @@ -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(())
}
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ impl<IT8951Interface: interface::IT8951Interface> IT8951<IT8951Interface, Run> {

/// 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 {
Expand All @@ -207,7 +207,7 @@ impl<IT8951Interface: interface::IT8951Interface> IT8951<IT8951Interface, Run> {
area_w: width,
area_h: 1,
},
&vec![data_entry; width as usize / 4],
&vec![data_entry; width as usize / 2],
)?;
}
Ok(())
Expand All @@ -222,7 +222,7 @@ impl<IT8951Interface: interface::IT8951Interface> IT8951<IT8951Interface, Run> {
&mut self,
target_mem_addr: u32,
image_settings: MemoryConverterSetting,
data: &[u16],
data: &[u8],
) -> Result<(), Error> {
self.set_target_memory_addr(target_mem_addr)?;

Expand All @@ -245,7 +245,7 @@ impl<IT8951Interface: interface::IT8951Interface> IT8951<IT8951Interface, Run> {
target_mem_addr: u32,
image_settings: MemoryConverterSetting,
area_info: &AreaImgInfo,
data: &[u16],
data: &[u8],
) -> Result<(), Error> {
self.set_target_memory_addr(target_mem_addr)?;

Expand Down Expand Up @@ -302,7 +302,7 @@ impl<IT8951Interface: interface::IT8951Interface> IT8951<IT8951Interface, Run> {
}

/// 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,
Expand Down Expand Up @@ -508,7 +508,7 @@ impl<IT8951Interface: interface::IT8951Interface> DrawTarget for IT8951<IT8951In
type Error = Error;

fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
let raw_color = color.luma() as u16;
let raw_color = color.luma() as u8;
self.clear_frame_buffer(raw_color)
}

Expand Down Expand Up @@ -557,7 +557,7 @@ impl<IT8951Interface: interface::IT8951Interface> DrawTarget for IT8951<IT8951In
let height = dev_info.panel_height as i32;
for Pixel(coord, color) in pixels.into_iter() {
if (coord.x >= 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,
Expand Down
6 changes: 3 additions & 3 deletions src/pixel_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<I: Iterator<Item = Pixel<Gray4>>> PixelSerializer<I> {
}

impl<I: Iterator<Item = Pixel<Gray4>>> Iterator for PixelSerializer<I> {
type Item = (AreaImgInfo, Vec<u16>);
type Item = (AreaImgInfo, Vec<u8>);

fn next(&mut self) -> Option<Self::Item> {
if self.row >= self.area.size.height as usize {
Expand All @@ -51,15 +51,15 @@ impl<I: Iterator<Item = Pixel<Gray4>>> Iterator for PixelSerializer<I> {
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() {
let byte_pos = ((point.x - (self.area.top_left.x / 4 * 4)) / 4) as usize
+ 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/serialization_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit e00f5ad

Please sign in to comment.