From 6dfba59e320412c2fddf9bac95c67c0149864bbd Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Mon, 23 Mar 2020 12:45:26 +0100 Subject: [PATCH] Move bounded drawing calculation from interfaces into the only call site (#113) * Move bounded drawing calculation from interfaces into the only call site * Add CHANGELOG.md entry for public interface change Signed-off-by: Daniel Egger --- CHANGELOG.md | 4 ++++ src/interface/i2c.rs | 48 -------------------------------------------- src/interface/mod.rs | 11 ---------- src/interface/spi.rs | 27 ------------------------- src/properties.rs | 19 ++++++++++++++++-- src/test_helpers.rs | 9 --------- 6 files changed, 21 insertions(+), 97 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7202b388..b8e5ab7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ ## [Unreleased] - ReleaseDate +### Changed + +- **(breaking)** [#113](https://github.com/jamwaffles/ssd1306/pull/113) Removed public `send_bounded_data` from DisplayInterface and implementations + ## [0.3.1] - 2020-03-21 ### Fixed diff --git a/src/interface/i2c.rs b/src/interface/i2c.rs index 9a2f8117..a3d9462b 100644 --- a/src/interface/i2c.rs +++ b/src/interface/i2c.rs @@ -61,52 +61,4 @@ where .map_err(Error::Comm) }) } - - fn send_bounded_data( - &mut self, - buf: &[u8], - disp_width: usize, - upper_left: (u8, u8), - lower_right: (u8, u8), - ) -> Result<(), Self::Error> { - // Noop if the data buffer is empty - if buf.is_empty() { - return Ok(()); - } - - // Write buffer. Writes are sent in chunks of 16 bytes plus DC byte - let mut writebuf: [u8; 17] = [0x0; 17]; - - // Data mode - // 8.1.5.2 5) b) in the datasheet - writebuf[0] = 0x40; - - // Divide by 8 since each row is actually 8 pixels tall - let num_pages = ((lower_right.1 - upper_left.1) / 8) as usize + 1; - - // Each page is 8 bits tall, so calculate which page number to start at (rounded down) from - // the top of the display - let starting_page = (upper_left.1 / 8) as usize; - - // Calculate start and end X coordinates for each page - let page_lower = upper_left.0 as usize; - let page_upper = lower_right.0 as usize; - - buf.chunks(disp_width) - .skip(starting_page) - .take(num_pages) - .map(|s| &s[page_lower..page_upper]) - .try_for_each(|c| { - c.chunks(16).try_for_each(|c| { - let chunk_len = c.len(); - - // Copy over all data from buffer, leaving the data command byte intact - writebuf[1..=chunk_len].copy_from_slice(c); - - self.i2c - .write(self.addr, &writebuf[0..=chunk_len]) - .map_err(Error::Comm) - }) - }) - } } diff --git a/src/interface/mod.rs b/src/interface/mod.rs index a606713d..be8967f3 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -61,17 +61,6 @@ pub trait DisplayInterface { fn send_commands(&mut self, cmd: &[u8]) -> Result<(), Self::Error>; /// Send data to display. fn send_data(&mut self, buf: &[u8]) -> Result<(), Self::Error>; - /// Send data to display, taking advantage of bounded data. - /// - /// upper_left and lower_right should contain the x and y coordinates of the - /// minimum bounding rectangle of the modified pixels. - fn send_bounded_data( - &mut self, - buf: &[u8], - disp_width: usize, - upper_left: (u8, u8), - lower_right: (u8, u8), - ) -> Result<(), Self::Error>; } pub use self::{i2c::I2cInterface, spi::SpiInterface}; diff --git a/src/interface/spi.rs b/src/interface/spi.rs index 60e9c3a7..317b7a7f 100644 --- a/src/interface/spi.rs +++ b/src/interface/spi.rs @@ -46,31 +46,4 @@ where self.spi.write(&buf).map_err(Error::Comm) } - - fn send_bounded_data( - &mut self, - buf: &[u8], - disp_width: usize, - upper_left: (u8, u8), - lower_right: (u8, u8), - ) -> Result<(), Self::Error> { - self.dc.set_high().map_err(Error::Pin)?; - - // Divide by 8 since each row is actually 8 pixels tall - let num_pages = ((lower_right.1 - upper_left.1) / 8) as usize + 1; - - // Each page is 8 bits tall, so calculate which page number to start at (rounded down) from - // the top of the display - let starting_page = (upper_left.1 / 8) as usize; - - // Calculate start and end X coordinates for each page - let page_lower = upper_left.0 as usize; - let page_upper = lower_right.0 as usize; - - buf.chunks(disp_width) - .skip(starting_page) - .take(num_pages) - .map(|s| &s[page_lower..page_upper]) - .try_for_each(|c| self.spi.write(&c).map_err(Error::Comm)) - } } diff --git a/src/properties.rs b/src/properties.rs index 1f41cf03..491065e8 100644 --- a/src/properties.rs +++ b/src/properties.rs @@ -151,8 +151,23 @@ where upper_left: (u8, u8), lower_right: (u8, u8), ) -> Result<(), DI::Error> { - self.iface - .send_bounded_data(&buffer, disp_width, upper_left, lower_right) + // Divide by 8 since each row is actually 8 pixels tall + let num_pages = ((lower_right.1 - upper_left.1) / 8) as usize + 1; + + // Each page is 8 bits tall, so calculate which page number to start at (rounded down) from + // the top of the display + let starting_page = (upper_left.1 / 8) as usize; + + // Calculate start and end X coordinates for each page + let page_lower = upper_left.0 as usize; + let page_upper = lower_right.0 as usize; + + buffer + .chunks(disp_width) + .skip(starting_page) + .take(num_pages) + .map(|s| &s[page_lower..page_upper]) + .try_for_each(|c| self.iface.send_data(&c)) } /// Get the configured display size diff --git a/src/test_helpers.rs b/src/test_helpers.rs index 8f0339a2..2a641f27 100644 --- a/src/test_helpers.rs +++ b/src/test_helpers.rs @@ -70,13 +70,4 @@ impl DisplayInterface for StubInterface { fn send_data(&mut self, _buf: &[u8]) -> Result<(), ()> { Ok(()) } - fn send_bounded_data( - &mut self, - _buf: &[u8], - _disp_width: usize, - _upper_left: (u8, u8), - _lower_right: (u8, u8), - ) -> Result<(), ()> { - Ok(()) - } }