From 407dec86a380bfdb0c734c258661840c028519b0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 21 Oct 2024 01:15:48 +0800 Subject: [PATCH] adopt platform & display -agnostic APIs for SH1107 frame buffer --- libs/cramium-hal/src/sh1107.rs | 127 ++++++++++++++++++++++----------- 1 file changed, 84 insertions(+), 43 deletions(-) diff --git a/libs/cramium-hal/src/sh1107.rs b/libs/cramium-hal/src/sh1107.rs index 98de994a2..d26a493f3 100644 --- a/libs/cramium-hal/src/sh1107.rs +++ b/libs/cramium-hal/src/sh1107.rs @@ -2,12 +2,37 @@ use crate::{ ifram::IframRange, iox::{IoGpio, IoSetup, IoxPort}, + minigfx::{ColorNative, FrameBuffer, Point}, udma::{PeriphId, Spim, SpimClkPha, SpimClkPol, SpimCs, UdmaGlobalConfig}, }; -pub const COLUMN: u8 = 128; -pub const ROW: u8 = 128; -pub const PAGE: u8 = ROW / 8; +pub const COLUMN: isize = 128; +pub const ROW: isize = 128; +pub const PAGE: u8 = ROW as u8 / 8; + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct MonoColor(ColorNative); +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Mono { + Black, + White, +} +impl From for Mono { + fn from(value: ColorNative) -> Self { + match value.0 { + 0 => Mono::Black, + _ => Mono::White, + } + } +} +impl Into for Mono { + fn into(self) -> ColorNative { + match self { + Mono::Black => ColorNative::from(0), + Mono::White => ColorNative::from(1), + } + } +} #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum DisplayState { @@ -235,40 +260,6 @@ impl<'a> Oled128x128<'a> { pub fn buffer(&self) -> &[u8] { self.buffers[self.active_buffer.as_index()] } - /// Transfers the back buffer - pub fn draw(&mut self) { - // this must be opposite of what `buffer` / `buffer_mut` returns - let buffer_start = self.active_buffer.swap() as usize; - let chunk_size = 128; - let chunks = self.buffer().len() / chunk_size; - // we don't do this with an iterator because it involves an immutable borrow of - // `buffer`, which prevents us from doing anything with the interface inside the loop. - for page in 0..chunks { - // The cs_active() waits are necessary because the UDMA block will eagerly report - // the transaction is done before the data is done transmitting, and we have to - // toggle set_data() only after the physical transaction is done, not after the - // the last UDMA action has been queued. - self.send_command(Command::SetPageAddress(page as u8).encode()); - self.send_command(Command::SetColumnAddress(0).encode()); - // wait for commands to finish before toggling set_data - // self.spim.tx_data_await(false); - // crate::println!("Send page {}, offset {:x}", page, buffer_start + page * chunk_size); - self.set_data(); - // safety: data is already copied into the DMA buffer. size & len are in bounds. - unsafe { - self.spim - .txrx_data_async_from_parts::( - buffer_start + page * chunk_size, - chunk_size, - true, - false, - ) - .expect("Couldn't initiate oled data transfer"); - } - self.spim.txrx_await(false).unwrap(); - } - } - pub fn send_command<'b, U>(&'b mut self, cmd: U) where U: IntoIterator + 'b, @@ -320,18 +311,68 @@ impl<'a> Oled128x128<'a> { self.send_command(bytes); } } +} - pub fn clear(&mut self) { self.buffer_mut().fill(0); } +impl<'a> FrameBuffer for Oled128x128<'a> { + /// Transfers the back buffer + fn draw(&mut self) { + // this must be opposite of what `buffer` / `buffer_mut` returns + let buffer_start = self.active_buffer.swap() as usize; + let chunk_size = 128; + let chunks = self.buffer().len() / chunk_size; + // we don't do this with an iterator because it involves an immutable borrow of + // `buffer`, which prevents us from doing anything with the interface inside the loop. + for page in 0..chunks { + // The cs_active() waits are necessary because the UDMA block will eagerly report + // the transaction is done before the data is done transmitting, and we have to + // toggle set_data() only after the physical transaction is done, not after the + // the last UDMA action has been queued. + self.send_command(Command::SetPageAddress(page as u8).encode()); + self.send_command(Command::SetColumnAddress(0).encode()); + // wait for commands to finish before toggling set_data + // self.spim.tx_data_await(false); + // crate::println!("Send page {}, offset {:x}", page, buffer_start + page * chunk_size); + self.set_data(); + // safety: data is already copied into the DMA buffer. size & len are in bounds. + unsafe { + self.spim + .txrx_data_async_from_parts::( + buffer_start + page * chunk_size, + chunk_size, + true, + false, + ) + .expect("Couldn't initiate oled data transfer"); + } + self.spim.txrx_await(false).unwrap(); + } + } - pub fn put_pixel(&mut self, x: u8, y: u8, on: bool) { - if x > COLUMN || y > ROW { + fn clear(&mut self) { self.buffer_mut().fill(0); } + + fn put_pixel(&mut self, p: Point, on: ColorNative) { + if p.x > COLUMN || p.y > ROW || p.x < 0 || p.y < 0 { return; } let buffer = self.buffer_mut(); - if on { - buffer[x as usize + (y as usize / 8) * COLUMN as usize] |= 1 << (y % 8); + if on.0 != 0 { + buffer[p.x as usize + (p.y as usize / 8) * COLUMN as usize] |= 1 << (p.y % 8); } else { - buffer[x as usize + (y as usize / 8) * COLUMN as usize] &= !(1 << (y % 8)); + buffer[p.x as usize + (p.y as usize / 8) * COLUMN as usize] &= !(1 << (p.y % 8)); + } + } + + fn resolution(&self) -> Point { Point::new(COLUMN, ROW) } + + fn get_pixel(&mut self, p: Point) -> Option { + if p.x > COLUMN || p.y > ROW || p.x < 0 || p.y < 0 { + return None; + } + let buffer = self.buffer_mut(); + if buffer[p.x as usize + (p.y as usize / 8) * COLUMN as usize] & (1 << (p.y % 8)) != 0 { + Some(Mono::White.into()) + } else { + Some(Mono::Black.into()) } } }