Skip to content

Commit 8f305c0

Browse files
Get rid of generic-array (#158)
* Get rid of generic-array * State MSRV bump and adjust Buffer types
1 parent 186f463 commit 8f305c0

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
## [Unreleased] - ReleaseDate
88

9+
### Changed
10+
11+
- **(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.
12+
913
## [0.6.0] - 2021-06-22
1014

1115
### Changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ embedded-hal = "0.2.5"
2323
display-interface = "0.4.1"
2424
display-interface-i2c = "0.4.0"
2525
display-interface-spi = "0.4.1"
26-
generic-array = "0.14.4"
2726
embedded-graphics-core = { version = "0.3.2", optional = true }
2827

2928
[dev-dependencies]

src/mode/buffered_graphics.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
//! Buffered graphics mode.
22
3-
use crate::{command::AddrMode, rotation::DisplayRotation, size::DisplaySize, Ssd1306};
3+
use crate::{
4+
command::AddrMode,
5+
rotation::DisplayRotation,
6+
size::{DisplaySize, NewZeroed},
7+
Ssd1306,
8+
};
49
use display_interface::{DisplayError, WriteOnlyDataCommand};
5-
use generic_array::GenericArray;
610

711
/// Buffered graphics mode.
812
///
@@ -15,7 +19,7 @@ pub struct BufferedGraphicsMode<SIZE>
1519
where
1620
SIZE: DisplaySize,
1721
{
18-
buffer: GenericArray<u8, SIZE::BufferSize>,
22+
buffer: SIZE::Buffer,
1923
min_x: u8,
2024
max_x: u8,
2125
min_y: u8,
@@ -29,7 +33,7 @@ where
2933
/// Create a new buffered graphics mode instance.
3034
pub(crate) fn new() -> Self {
3135
Self {
32-
buffer: GenericArray::default(),
36+
buffer: NewZeroed::new_zeroed(),
3337
min_x: 255,
3438
max_x: 0,
3539
min_y: 255,
@@ -66,7 +70,9 @@ where
6670
{
6771
/// Clear the display buffer. You need to call `disp.flush()` for any effect on the screen
6872
pub fn clear(&mut self) {
69-
self.mode.buffer = GenericArray::default();
73+
for b in self.mode.buffer.as_mut() {
74+
*b = 0;
75+
}
7076

7177
let (width, height) = self.dimensions();
7278
self.mode.min_x = 0;
@@ -125,7 +131,7 @@ where
125131

126132
Self::flush_buffer_chunks(
127133
&mut self.interface,
128-
&self.mode.buffer,
134+
self.mode.buffer.as_mut(),
129135
width as usize,
130136
(disp_min_x, disp_min_y),
131137
(disp_max_x, disp_max_y),
@@ -139,7 +145,7 @@ where
139145

140146
Self::flush_buffer_chunks(
141147
&mut self.interface,
142-
&self.mode.buffer,
148+
self.mode.buffer.as_mut(),
143149
height as usize,
144150
(disp_min_y, disp_min_x),
145151
(disp_max_y, disp_max_x),
@@ -169,7 +175,7 @@ where
169175
}
170176
};
171177

172-
if let Some(byte) = self.mode.buffer.get_mut(idx) {
178+
if let Some(byte) = self.mode.buffer.as_mut().get_mut(idx) {
173179
// Keep track of max and min values
174180
self.mode.min_x = self.mode.min_x.min(x as u8);
175181
self.mode.max_x = self.mode.max_x.max(x as u8);

src/size.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
33
use super::command::Command;
44
use display_interface::{DisplayError, WriteOnlyDataCommand};
5-
use generic_array::{
6-
typenum::{U1024, U192, U360, U384, U512},
7-
ArrayLength,
8-
};
5+
6+
/// Workaround trait, since `Default` is only implemented to arrays up to 32 of size
7+
pub trait NewZeroed {
8+
/// Creates a new value with its memory set to zero
9+
fn new_zeroed() -> Self;
10+
}
11+
12+
impl<const N: usize> NewZeroed for [u8; N] {
13+
fn new_zeroed() -> Self {
14+
[0u8; N]
15+
}
16+
}
917

1018
/// Display information.
1119
///
@@ -32,7 +40,7 @@ pub trait DisplaySize {
3240

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)