graphics: ssd1306: refactor

This commit is contained in:
2025-11-09 17:09:27 +01:00
parent 092885f163
commit 9c37cb58d1

View File

@@ -13,28 +13,24 @@ use embassy_time::Delay;
use crate::graphics::display::DisplayControls; use crate::graphics::display::DisplayControls;
pub struct SsdOutput { #[derive(Clone, Copy)]
pixbuf: [u8; 128 * 64 / 8], pub struct SsdPixel {
target: ssd1306::Ssd1306Async<ssd1306::prelude::I2CInterface<esp_hal::i2c::master::I2c<'static, esp_hal::Async>>, ssd1306::prelude::DisplaySize128x64, ssd1306::mode::BasicMode>, byte: *mut u8,
controls: DisplayControls, bit: u8,
is_on: bool, coords: Coordinates<Matrix2DSpace>
last_brightness: u8
} }
#[derive(Copy, Clone)]
pub struct SsdPixel(*mut u8, u8, Coordinates<Matrix2DSpace>);
impl SsdPixel { impl SsdPixel {
fn set_pixel(&self, value: BinaryColor) { fn set_pixel(&self, value: BinaryColor) {
if value.is_on() { if value.is_on() {
unsafe { *self.0 |= 1 << self.1 }; unsafe { *self.byte |= 1 << self.bit };
} else { } else {
unsafe { *self.0 &= !(1 << self.1) }; unsafe { *self.byte &= !(1 << self.bit) };
} }
} }
fn get_pixel(&self) -> BinaryColor { fn get_pixel(&self) -> BinaryColor {
if unsafe { *self.0 >> self.1 & 0x01 == 0x01 } { if unsafe { *self.byte >> self.bit & 0x01 == 0x01 } {
BinaryColor::On BinaryColor::On
} else { } else {
BinaryColor::Off BinaryColor::Off
@@ -51,8 +47,8 @@ impl PixelSink<BinaryColor> for SsdPixel {
impl PixelBlend<BinaryColor> for SsdPixel { impl PixelBlend<BinaryColor> for SsdPixel {
fn blend_pixel(self, overlay: BinaryColor, opacity: Fract8) -> Self { fn blend_pixel(self, overlay: BinaryColor, opacity: Fract8) -> Self {
let scale = 48; let scale = 48;
let x = self.2.x * scale; let x = self.coords.x * scale;
let y = self.2.y * scale; let y = self.coords.y * scale;
let stiple_idx = noise::inoise8(x as i16, y as i16); let stiple_idx = noise::inoise8(x as i16, y as i16);
if opacity >= stiple_idx { if opacity >= stiple_idx {
self.set_pixel(overlay); self.set_pixel(overlay);
@@ -98,6 +94,14 @@ impl<'a> Iterator for SsdSampler<'a> {
} }
} }
pub struct SsdOutput {
pixbuf: [u8; 128 * 64 / 8],
target: ssd1306::Ssd1306Async<ssd1306::prelude::I2CInterface<esp_hal::i2c::master::I2c<'static, esp_hal::Async>>, ssd1306::prelude::DisplaySize128x64, ssd1306::mode::BasicMode>,
controls: DisplayControls,
is_on: bool,
last_brightness: u8
}
impl SsdOutput { impl SsdOutput {
pub fn get_pixel(&mut self, coords: Coordinates<Matrix2DSpace>) -> Option<SsdPixel> { pub fn get_pixel(&mut self, coords: Coordinates<Matrix2DSpace>) -> Option<SsdPixel> {
if coords.x < 0 || coords.y < 0 || coords.x >= 128 || coords.y >= 64 { if coords.x < 0 || coords.y < 0 || coords.x >= 128 || coords.y >= 64 {
@@ -107,7 +111,7 @@ impl SsdOutput {
let bit = (coords.y % 8) as u8; let bit = (coords.y % 8) as u8;
//info!("sample {coords:?} {idx}"); //info!("sample {coords:?} {idx}");
let pixref = &mut self.pixbuf[idx] as *mut u8; let pixref = &mut self.pixbuf[idx] as *mut u8;
Some(SsdPixel(pixref, bit, coords)) Some(SsdPixel { byte: pixref, bit, coords })
} }
pub async fn new(i2c: I2c<'static, Async>, mut reset_pin: Output<'static>, controls: DisplayControls) -> Self { pub async fn new(i2c: I2c<'static, Async>, mut reset_pin: Output<'static>, controls: DisplayControls) -> Self {