From 9ff1f377a73e2da0c1c83a2b9e88176b156225a8 Mon Sep 17 00:00:00 2001 From: thomas725 <68635351+thomas725@users.noreply.github.com> Date: Thu, 31 Aug 2023 16:21:35 +0200 Subject: [PATCH 1/4] add get_pixel to buffered_graphics see also: https://github.com/jamwaffles/ssd1306/issues/192 --- src/mode/buffered_graphics.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/mode/buffered_graphics.rs b/src/mode/buffered_graphics.rs index 6a9cfb66..069cd468 100644 --- a/src/mode/buffered_graphics.rs +++ b/src/mode/buffered_graphics.rs @@ -156,13 +156,8 @@ where } } - /// Turn a pixel on or off. A non-zero `value` is treated as on, `0` as off. If the X and Y - /// coordinates are out of the bounds of the display, this method call is a noop. - pub fn set_pixel(&mut self, x: u32, y: u32, value: bool) { - let value = value as u8; - let rotation = self.rotation; - - let (idx, bit) = match rotation { + fn pixel_location(&self, x: u32, y: u32) -> (usize, u32) { + match rotation { DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => { let idx = ((y as usize) / 8 * SIZE::WIDTH as usize) + (x as usize); let bit = y % 8; @@ -175,7 +170,21 @@ where (idx, bit) } - }; + } + } + + pub fn get_pixel(&self, x: u32, y: u32) -> Option { + let (idx, bit) = self.pixel_location(x,y); + self.mode.buffer.as_mut().get(idx).map(|byte| byte & !(1 << bit) != 0) + } + + /// Turn a pixel on or off. A non-zero `value` is treated as on, `0` as off. If the X and Y + /// coordinates are out of the bounds of the display, this method call is a noop. + pub fn set_pixel(&mut self, x: u32, y: u32, value: bool) { + let value = value as u8; + let rotation = self.rotation; + + let (idx, bit) = self.pixel_location(x,y); if let Some(byte) = self.mode.buffer.as_mut().get_mut(idx) { // Keep track of max and min values From 73d76542804562742a013c0b16e2d4cbb86a149e Mon Sep 17 00:00:00 2001 From: thomas725 <68635351+thomas725@users.noreply.github.com> Date: Thu, 31 Aug 2023 16:27:43 +0200 Subject: [PATCH 2/4] bugfix --- src/mode/buffered_graphics.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/mode/buffered_graphics.rs b/src/mode/buffered_graphics.rs index 069cd468..24262d72 100644 --- a/src/mode/buffered_graphics.rs +++ b/src/mode/buffered_graphics.rs @@ -157,7 +157,7 @@ where } fn pixel_location(&self, x: u32, y: u32) -> (usize, u32) { - match rotation { + match self.rotation { DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => { let idx = ((y as usize) / 8 * SIZE::WIDTH as usize) + (x as usize); let bit = y % 8; @@ -182,8 +182,6 @@ where /// coordinates are out of the bounds of the display, this method call is a noop. pub fn set_pixel(&mut self, x: u32, y: u32, value: bool) { let value = value as u8; - let rotation = self.rotation; - let (idx, bit) = self.pixel_location(x,y); if let Some(byte) = self.mode.buffer.as_mut().get_mut(idx) { From 7632ab7c478f92e38a5b80a1db0adf9a6d6b5138 Mon Sep 17 00:00:00 2001 From: thomas725 <68635351+thomas725@users.noreply.github.com> Date: Thu, 31 Aug 2023 16:33:26 +0200 Subject: [PATCH 3/4] bugfix --- src/mode/buffered_graphics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mode/buffered_graphics.rs b/src/mode/buffered_graphics.rs index 24262d72..c40d1c71 100644 --- a/src/mode/buffered_graphics.rs +++ b/src/mode/buffered_graphics.rs @@ -175,7 +175,7 @@ where pub fn get_pixel(&self, x: u32, y: u32) -> Option { let (idx, bit) = self.pixel_location(x,y); - self.mode.buffer.as_mut().get(idx).map(|byte| byte & !(1 << bit) != 0) + self.mode.buffer.get(idx).map(|byte| byte & !(1 << bit) != 0) } /// Turn a pixel on or off. A non-zero `value` is treated as on, `0` as off. If the X and Y From 00fc9e65162efd1338edbf7802e68207ed1ae8fa Mon Sep 17 00:00:00 2001 From: thomas725 <68635351+thomas725@users.noreply.github.com> Date: Thu, 31 Aug 2023 16:41:56 +0200 Subject: [PATCH 4/4] bad bugfix, should be possible with read-only reference but I don't know how.. --- src/mode/buffered_graphics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mode/buffered_graphics.rs b/src/mode/buffered_graphics.rs index c40d1c71..874ff2a4 100644 --- a/src/mode/buffered_graphics.rs +++ b/src/mode/buffered_graphics.rs @@ -173,9 +173,9 @@ where } } - pub fn get_pixel(&self, x: u32, y: u32) -> Option { + pub fn get_pixel(&mut self, x: u32, y: u32) -> Option { let (idx, bit) = self.pixel_location(x,y); - self.mode.buffer.get(idx).map(|byte| byte & !(1 << bit) != 0) + self.mode.buffer.as_mut().get(idx).map(|byte| byte & !(1 << bit) != 0) } /// Turn a pixel on or off. A non-zero `value` is treated as on, `0` as off. If the X and Y