Skip to content

Commit

Permalink
Move bounded drawing calculation from interfaces into the only call s…
Browse files Browse the repository at this point in the history
…ite (#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 <[email protected]>
  • Loading branch information
therealprof authored Mar 23, 2020
1 parent e10b527 commit 6dfba59
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 97 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 0 additions & 48 deletions src/interface/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
}
}
11 changes: 0 additions & 11 deletions src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
27 changes: 0 additions & 27 deletions src/interface/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
19 changes: 17 additions & 2 deletions src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

0 comments on commit 6dfba59

Please sign in to comment.