Skip to content

Commit

Permalink
Get rid of generic-array (#158)
Browse files Browse the repository at this point in the history
* Get rid of generic-array

* State MSRV bump and adjust Buffer types
  • Loading branch information
thalesfragoso authored Jul 8, 2021
1 parent 186f463 commit 8f305c0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 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)** [#158](https://github.com/jamwaffles/ssd1306/pull/158) Migrate away from `generic-array` to a solution using const generics. This raises the crate MSRV to 1.51.

## [0.6.0] - 2021-06-22

### Changed
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ embedded-hal = "0.2.5"
display-interface = "0.4.1"
display-interface-i2c = "0.4.0"
display-interface-spi = "0.4.1"
generic-array = "0.14.4"
embedded-graphics-core = { version = "0.3.2", optional = true }

[dev-dependencies]
Expand Down
22 changes: 14 additions & 8 deletions src/mode/buffered_graphics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! Buffered graphics mode.
use crate::{command::AddrMode, rotation::DisplayRotation, size::DisplaySize, Ssd1306};
use crate::{
command::AddrMode,
rotation::DisplayRotation,
size::{DisplaySize, NewZeroed},
Ssd1306,
};
use display_interface::{DisplayError, WriteOnlyDataCommand};
use generic_array::GenericArray;

/// Buffered graphics mode.
///
Expand All @@ -15,7 +19,7 @@ pub struct BufferedGraphicsMode<SIZE>
where
SIZE: DisplaySize,
{
buffer: GenericArray<u8, SIZE::BufferSize>,
buffer: SIZE::Buffer,
min_x: u8,
max_x: u8,
min_y: u8,
Expand All @@ -29,7 +33,7 @@ where
/// Create a new buffered graphics mode instance.
pub(crate) fn new() -> Self {
Self {
buffer: GenericArray::default(),
buffer: NewZeroed::new_zeroed(),
min_x: 255,
max_x: 0,
min_y: 255,
Expand Down Expand Up @@ -66,7 +70,9 @@ where
{
/// Clear the display buffer. You need to call `disp.flush()` for any effect on the screen
pub fn clear(&mut self) {
self.mode.buffer = GenericArray::default();
for b in self.mode.buffer.as_mut() {
*b = 0;
}

let (width, height) = self.dimensions();
self.mode.min_x = 0;
Expand Down Expand Up @@ -125,7 +131,7 @@ where

Self::flush_buffer_chunks(
&mut self.interface,
&self.mode.buffer,
self.mode.buffer.as_mut(),
width as usize,
(disp_min_x, disp_min_y),
(disp_max_x, disp_max_y),
Expand All @@ -139,7 +145,7 @@ where

Self::flush_buffer_chunks(
&mut self.interface,
&self.mode.buffer,
self.mode.buffer.as_mut(),
height as usize,
(disp_min_y, disp_min_x),
(disp_max_y, disp_max_x),
Expand Down Expand Up @@ -169,7 +175,7 @@ where
}
};

if let Some(byte) = self.mode.buffer.get_mut(idx) {
if let Some(byte) = self.mode.buffer.as_mut().get_mut(idx) {
// Keep track of max and min values
self.mode.min_x = self.mode.min_x.min(x as u8);
self.mode.max_x = self.mode.max_x.max(x as u8);
Expand Down
28 changes: 18 additions & 10 deletions src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
use super::command::Command;
use display_interface::{DisplayError, WriteOnlyDataCommand};
use generic_array::{
typenum::{U1024, U192, U360, U384, U512},
ArrayLength,
};

/// Workaround trait, since `Default` is only implemented to arrays up to 32 of size
pub trait NewZeroed {
/// Creates a new value with its memory set to zero
fn new_zeroed() -> Self;
}

impl<const N: usize> NewZeroed for [u8; N] {
fn new_zeroed() -> Self {
[0u8; N]
}
}

/// Display information.
///
Expand All @@ -32,7 +40,7 @@ pub trait DisplaySize {

/// Size of framebuffer. Because the display is monocrome, this is
/// width * height / 8
type BufferSize: ArrayLength<u8>;
type Buffer: AsMut<[u8]> + NewZeroed;

/// Send resolution and model-dependent configuration to the display
///
Expand All @@ -48,7 +56,7 @@ pub struct DisplaySize128x64;
impl DisplaySize for DisplaySize128x64 {
const WIDTH: u8 = 128;
const HEIGHT: u8 = 64;
type BufferSize = U1024;
type Buffer = [u8; Self::WIDTH as usize * Self::HEIGHT as usize / 8];

fn configure(&self, iface: &mut impl WriteOnlyDataCommand) -> Result<(), DisplayError> {
Command::ComPinConfig(true, false).send(iface)
Expand All @@ -61,7 +69,7 @@ pub struct DisplaySize128x32;
impl DisplaySize for DisplaySize128x32 {
const WIDTH: u8 = 128;
const HEIGHT: u8 = 32;
type BufferSize = U512;
type Buffer = [u8; Self::WIDTH as usize * Self::HEIGHT as usize / 8];

fn configure(&self, iface: &mut impl WriteOnlyDataCommand) -> Result<(), DisplayError> {
Command::ComPinConfig(false, false).send(iface)
Expand All @@ -74,7 +82,7 @@ pub struct DisplaySize96x16;
impl DisplaySize for DisplaySize96x16 {
const WIDTH: u8 = 96;
const HEIGHT: u8 = 16;
type BufferSize = U192;
type Buffer = [u8; Self::WIDTH as usize * Self::HEIGHT as usize / 8];

fn configure(&self, iface: &mut impl WriteOnlyDataCommand) -> Result<(), DisplayError> {
Command::ComPinConfig(false, false).send(iface)
Expand All @@ -89,7 +97,7 @@ impl DisplaySize for DisplaySize72x40 {
const HEIGHT: u8 = 40;
const OFFSETX: u8 = 28;
const OFFSETY: u8 = 0;
type BufferSize = U360;
type Buffer = [u8; Self::WIDTH as usize * Self::HEIGHT as usize / 8];

fn configure(&self, iface: &mut impl WriteOnlyDataCommand) -> Result<(), DisplayError> {
Command::ComPinConfig(true, false).send(iface)?;
Expand All @@ -105,7 +113,7 @@ impl DisplaySize for DisplaySize64x48 {
const HEIGHT: u8 = 48;
const OFFSETX: u8 = 32;
const OFFSETY: u8 = 0;
type BufferSize = U384;
type Buffer = [u8; Self::WIDTH as usize * Self::HEIGHT as usize / 8];

fn configure(&self, iface: &mut impl WriteOnlyDataCommand) -> Result<(), DisplayError> {
Command::ComPinConfig(true, false).send(iface)
Expand Down

0 comments on commit 8f305c0

Please sign in to comment.