Skip to content

Commit 83c6cf6

Browse files
bugadaniDániel Bugajamwaffles
authored
Remove DisplayMode (#119)
* Add release method This enabled reclaiming gpios which may be necessary for some hardware configurations to enter low power mode * Remove DisplayMode * Remove some unnecessary trait bounds * Move reset method as it doesn't need WriteOnlyDataCommand * Add DisplayModeTrait to prelude * Doc link fixes * Run cargo fmt * Fix links * Add CHANGELOG entry * More link fixes * Word wrapping fix * Change back to standard relative links * Fix missed link * Update CHANGELOG.md Co-authored-by: James Waples <[email protected]> Co-authored-by: Dániel Buga <[email protected]> Co-authored-by: James Waples <[email protected]>
1 parent 96170fd commit 83c6cf6

File tree

10 files changed

+78
-131
lines changed

10 files changed

+78
-131
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Changed
1010

11+
-**(breaking)** [#119](https://github.com/jamwaffles/ssd1306/pull/119) Remove `DisplayMode` and `RawMode`
1112
- [#120](https://github.com/jamwaffles/ssd1306/pull/120) Update to v0.4 [`display-interface`](https://crates.io/crates/display-interface)
1213
- **(breaking)** [#116](https://github.com/jamwaffles/ssd1306/pull/116) Replace custom I2C and SPI interfaces by generic [`display-interface`](https://crates.io/crates/display-interface)
1314
- **(breaking)** [#113](https://github.com/jamwaffles/ssd1306/pull/113) Removed public `send_bounded_data` from DisplayInterface and implementations

src/builder.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
//! Interface factory
22
//!
3-
//! This is the easiest way to create a driver instance, with the ability to set various parameters of the driver.
3+
//! This is the easiest way to create a driver instance, with the ability to set various parameters
4+
//! of the driver.
45
//!
56
//! To finish the builder and produce a connected display interface, call `.connect(interface)`
67
//! where `interface` is an instantiated `DisplayInterface` implementation. For I2C interfaces
78
//! there's also an [`I2CDIBuilder`] to simplify the construction of an I2C `DisplayInterface`. The
8-
//! builder will be consumed into a [`mode::RawMode`] object which can be coerced into a richer
9-
//! display mode like [`mode::Graphics`].
10-
//!
11-
//! [`I2CDIBuilder`]: ./struct.I2CDIBuilder.html
12-
//! [`mode::RawMode`]: ../mode/raw/struct.RawMode.html
13-
//! [`mode::Graphics`]: ../mode/graphics/struct.GraphicsMode.html
9+
//! builder will be consumed into a [`DisplayProperties`] object which can be coerced into a richer
10+
//! display mode like [`GraphicsMode`] or [`TerminalMode`].
1411
//!
1512
//! # Examples
1613
//!
@@ -40,9 +37,9 @@
4037
//! .connect(interface);
4138
//! ```
4239
//!
43-
//! The above examples will produce a [RawMode](../mode/raw/struct.RawMode.html) instance
40+
//! The above examples will produce a [`DisplayProperties`] instance
4441
//! by default. You need to coerce them into a mode by specifying a type on assignment. For
45-
//! example, to use [`TerminalMode` mode](../mode/terminal/struct.TerminalMode.html):
42+
//! example, to use [`TerminalMode`] mode:
4643
//!
4744
//! ```rust
4845
//! # use ssd1306::test_helpers::{PinStub, SpiStub};
@@ -53,14 +50,16 @@
5350
//! let interface = display_interface_spi::SPIInterfaceNoCS::new(spi, dc);
5451
//! let display: TerminalMode<_> = Builder::new().connect(interface).into();
5552
//! ```
53+
//!
54+
//! [`I2CDIBuilder`]: ./struct.I2CDIBuilder.html
55+
//! [`DisplayProperties`]: ../properties/struct.DisplayProperties.html
56+
//! [`GraphicsMode`]: ../mode/graphics/struct.GraphicsMode.html
57+
//! [`TerminalMode`]: ../mode/terminal/struct.TerminalMode.html
5658
5759
use display_interface::WriteOnlyDataCommand;
5860

5961
use crate::{
60-
displayrotation::DisplayRotation,
61-
displaysize::DisplaySize,
62-
mode::{displaymode::DisplayMode, raw::RawMode},
63-
properties::DisplayProperties,
62+
displayrotation::DisplayRotation, displaysize::DisplaySize, properties::DisplayProperties,
6463
};
6564

6665
/// Builder struct. Driver options and interface are set using its methods.
@@ -103,12 +102,11 @@ impl Builder {
103102
/// Finish the builder and use some interface communicate with the display
104103
///
105104
/// This method consumes the builder and must come last in the method call chain
106-
pub fn connect<I>(self, interface: I) -> DisplayMode<RawMode<I>>
105+
pub fn connect<I>(self, interface: I) -> DisplayProperties<I>
107106
where
108107
I: WriteOnlyDataCommand,
109108
{
110-
let properties = DisplayProperties::new(interface, self.display_size, self.rotation);
111-
DisplayMode::<RawMode<I>>::new(properties)
109+
DisplayProperties::new(interface, self.display_size, self.rotation)
112110
}
113111
}
114112

src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
//!
33
//! The driver must be initialised by passing an I2C or SPI interface peripheral to the [`Builder`],
44
//! which will in turn create a driver instance in a particular mode. By default, the builder
5-
//! returns a [`RawMode`] instance which isn't very useful by itself. You can coerce the driver
6-
//! into a more useful mode by calling `into()` and defining the type you want to coerce to. For
7-
//! example, to initialise the display with an I2C interface and [`GraphicsMode`], you would do
8-
//! something like this:
5+
//! returns a [`DisplayProperties`] instance which is a low level interface to manipulate the
6+
//! display properties (e.g. rotation). The driver can be coerced into a more useful mode by calling
7+
//! `into()` and defining the type you want to coerce to. For example, to initialise the display
8+
//! with an I2C interface and [`GraphicsMode`], you would do something like this:
99
//!
1010
//! ```rust
1111
//! # use ssd1306::test_helpers::I2cStub as I2cInterface;
@@ -22,7 +22,7 @@
2222
//! ```
2323
//!
2424
//! See the [example](https://github.com/jamwaffles/ssd1306/blob/master/examples/graphics_i2c.rs)
25-
//! for more usage. The [entire `embedded_graphics` featureset](https://github.com/jamwaffles/embedded-graphics#features)
25+
//! for more usage. The entire `embedded_graphics` [featureset]
2626
//! is supported by this driver.
2727
//!
2828
//! There is also [`TerminalMode`] which allows drawing of characters to the display without
@@ -113,10 +113,11 @@
113113
//! disp.flush().unwrap();
114114
//! ```
115115
//!
116+
//! [featureset]: https://github.com/jamwaffles/embedded-graphics#features
116117
//! [`Builder`]: ./builder/struct.Builder.html
118+
//! [`DisplayProperties`]: ./properties/struct.DisplayProperties.html
117119
//! [`GraphicsMode`]: ./mode/graphics/struct.GraphicsMode.html
118120
//! [`TerminalMode`]: ./mode/terminal/struct.TerminalMode.html
119-
//! [`RawMode`]: ./mode/raw/struct.RawMode.html
120121
121122
#![no_std]
122123
// #![deny(missing_debug_implementations)]

src/mode/displaymode.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
//! Abstraction of different operating modes for the SSD1306
22
3-
use display_interface::WriteOnlyDataCommand;
4-
53
use crate::properties::DisplayProperties;
64

7-
/// Display mode abstraction
8-
pub struct DisplayMode<MODE>(pub MODE);
9-
105
/// Trait with core functionality for display mode switching
116
pub trait DisplayModeTrait<DI> {
127
/// Allocate all required data and initialise display for mode
@@ -15,25 +10,3 @@ pub trait DisplayModeTrait<DI> {
1510
/// Release resources for reuse with different mode
1611
fn release(self) -> DisplayProperties<DI>;
1712
}
18-
19-
impl<MODE> DisplayMode<MODE> {
20-
/// Setup display to run in requested mode
21-
pub fn new<DI>(properties: DisplayProperties<DI>) -> Self
22-
where
23-
DI: WriteOnlyDataCommand,
24-
MODE: DisplayModeTrait<DI>,
25-
{
26-
DisplayMode(MODE::new(properties))
27-
}
28-
29-
/// Change into any mode implementing DisplayModeTrait
30-
// TODO: Figure out how to stay as generic DisplayMode but act as particular mode
31-
pub fn into<DI, NMODE: DisplayModeTrait<DI>>(self) -> NMODE
32-
where
33-
DI: WriteOnlyDataCommand,
34-
MODE: DisplayModeTrait<DI>,
35-
{
36-
let properties = self.0.release();
37-
NMODE::new(properties)
38-
}
39-
}

src/mode/graphics.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ use crate::{
6565

6666
// TODO: Add to prelude
6767
/// Graphics mode handler
68-
pub struct GraphicsMode<DI>
69-
where
70-
DI: WriteOnlyDataCommand,
71-
{
68+
pub struct GraphicsMode<DI> {
7269
properties: DisplayProperties<DI>,
7370
buffer: [u8; 1024],
7471
min_x: u8,
@@ -77,10 +74,7 @@ where
7774
max_y: u8,
7875
}
7976

80-
impl<DI> DisplayModeTrait<DI> for GraphicsMode<DI>
81-
where
82-
DI: WriteOnlyDataCommand,
83-
{
77+
impl<DI> DisplayModeTrait<DI> for GraphicsMode<DI> {
8478
/// Create new GraphicsMode instance
8579
fn new(properties: DisplayProperties<DI>) -> Self {
8680
GraphicsMode {
@@ -99,21 +93,7 @@ where
9993
}
10094
}
10195

102-
impl<DI> GraphicsMode<DI>
103-
where
104-
DI: WriteOnlyDataCommand,
105-
{
106-
/// Clear the display buffer. You need to call `disp.flush()` for any effect on the screen
107-
pub fn clear(&mut self) {
108-
self.buffer = [0; 1024];
109-
110-
let (width, height) = self.get_dimensions();
111-
self.min_x = 0;
112-
self.max_x = width - 1;
113-
self.min_y = 0;
114-
self.max_y = height - 1;
115-
}
116-
96+
impl<DI> GraphicsMode<DI> {
11797
/// Reset display
11898
// TODO: Move to a more appropriate place
11999
pub fn reset<RST, DELAY, PinE>(
@@ -131,6 +111,22 @@ where
131111
delay.delay_ms(10);
132112
rst.set_high().map_err(Error::Pin)
133113
}
114+
}
115+
116+
impl<DI> GraphicsMode<DI>
117+
where
118+
DI: WriteOnlyDataCommand,
119+
{
120+
/// Clear the display buffer. You need to call `disp.flush()` for any effect on the screen
121+
pub fn clear(&mut self) {
122+
self.buffer = [0; 1024];
123+
124+
let (width, height) = self.get_dimensions();
125+
self.min_x = 0;
126+
self.max_x = width - 1;
127+
self.min_y = 0;
128+
self.max_y = height - 1;
129+
}
134130

135131
/// Write out data to a display.
136132
///

src/mode/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
pub mod displaymode;
77
pub mod graphics;
8-
pub mod raw;
98
pub mod terminal;
109

11-
pub use self::{graphics::GraphicsMode, raw::RawMode, terminal::TerminalMode};
10+
pub use self::{graphics::GraphicsMode, terminal::TerminalMode};

src/mode/raw.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/mode/terminal.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ impl Cursor {
6464
}
6565

6666
/// Advances the logical cursor by one character.
67-
/// Returns a value indicating if this caused the cursor to wrap to the next line or the next screen.
67+
/// Returns a value indicating if this caused the cursor to wrap to the next line or the next
68+
/// screen.
6869
pub fn advance(&mut self) -> Option<CursorWrapEvent> {
6970
self.col = (self.col + 1) % self.width;
7071
if self.col == 0 {
@@ -158,6 +159,25 @@ where
158159
}
159160
}
160161

162+
impl<DI> TerminalMode<DI> {
163+
/// Reset display
164+
pub fn reset<RST, DELAY, PinE>(
165+
&mut self,
166+
rst: &mut RST,
167+
delay: &mut DELAY,
168+
) -> Result<(), Error<(), PinE>>
169+
where
170+
RST: OutputPin<Error = PinE>,
171+
DELAY: DelayMs<u8>,
172+
{
173+
rst.set_high().map_err(Error::Pin)?;
174+
delay.delay_ms(1);
175+
rst.set_low().map_err(Error::Pin)?;
176+
delay.delay_ms(10);
177+
rst.set_high().map_err(Error::Pin)
178+
}
179+
}
180+
161181
impl<DI> TerminalMode<DI>
162182
where
163183
DI: WriteOnlyDataCommand,
@@ -204,23 +224,6 @@ where
204224
Ok(())
205225
}
206226

207-
/// Reset display
208-
pub fn reset<RST, DELAY, PinE>(
209-
&mut self,
210-
rst: &mut RST,
211-
delay: &mut DELAY,
212-
) -> Result<(), Error<(), PinE>>
213-
where
214-
RST: OutputPin<Error = PinE>,
215-
DELAY: DelayMs<u8>,
216-
{
217-
rst.set_high().map_err(Error::Pin)?;
218-
delay.delay_ms(1);
219-
rst.set_low().map_err(Error::Pin)?;
220-
delay.delay_ms(10);
221-
rst.set_high().map_err(Error::Pin)
222-
}
223-
224227
/// Write out data to display. This is a noop in terminal mode.
225228
pub fn flush(&mut self) -> Result<(), TerminalModeError> {
226229
Ok(())

src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ pub use display_interface_spi::{SPIInterface, SPIInterfaceNoCS};
77
pub use super::{
88
displayrotation::DisplayRotation,
99
displaysize::DisplaySize,
10-
mode::{GraphicsMode, TerminalMode},
10+
mode::{displaymode::DisplayModeTrait, GraphicsMode, TerminalMode},
1111
};

src/properties.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Container to store and set display properties
22
3+
use crate::mode::displaymode::DisplayModeTrait;
34
use crate::{
45
command::{AddrMode, Command, VcomhLevel},
56
displayrotation::DisplayRotation,
@@ -43,6 +44,11 @@ where
4344
}
4445
}
4546

47+
/// Releases the display interface
48+
pub fn release(self) -> DI {
49+
self.iface
50+
}
51+
4652
/// Initialise the display in column mode (i.e. a byte walks down a column of 8 pixels) with
4753
/// column 0 on the left and column _(display_width - 1)_ on the right.
4854
pub fn init_column_mode(&mut self) -> Result<(), DisplayError> {
@@ -242,4 +248,12 @@ where
242248
pub fn display_on(&mut self, on: bool) -> Result<(), DisplayError> {
243249
Command::DisplayOn(on).send(&mut self.iface)
244250
}
251+
252+
/// Change into any mode implementing DisplayModeTrait
253+
pub fn into<NMODE: DisplayModeTrait<DI>>(self) -> NMODE
254+
where
255+
DI: WriteOnlyDataCommand,
256+
{
257+
NMODE::new(self)
258+
}
245259
}

0 commit comments

Comments
 (0)