Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove DisplayMode #119

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Changed

- [#119](https://github.com/jamwaffles/ssd1306/pull/119) Remove `DisplayMode` and `RawDisplayMode`
bugadani marked this conversation as resolved.
Show resolved Hide resolved
- [#120](https://github.com/jamwaffles/ssd1306/pull/120) Update to v0.4 [`display-interface`](https://crates.io/crates/display-interface)
- **(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)
- **(breaking)** [#113](https://github.com/jamwaffles/ssd1306/pull/113) Removed public `send_bounded_data` from DisplayInterface and implementations
Expand Down
29 changes: 13 additions & 16 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
//! To finish the builder and produce a connected display interface, call `.connect(interface)`
//! where `interface` is an instantiated `DisplayInterface` implementation. For I2C interfaces
//! there's also an [`I2CDIBuilder`] to simplify the construction of an I2C `DisplayInterface`. The
//! builder will be consumed into a [`mode::RawMode`] object which can be coerced into a richer
//! display mode like [`mode::Graphics`].
//!
//! [`I2CDIBuilder`]: ./struct.I2CDIBuilder.html
//! [`mode::RawMode`]: ../mode/raw/struct.RawMode.html
//! [`mode::Graphics`]: ../mode/graphics/struct.GraphicsMode.html
//! builder will be consumed into a [`DisplayProperties`] object which can be coerced into a richer
//! display mode like [`GraphicsMode`] or [`TerminalMode`].
//!
//! # Examples
//!
Expand Down Expand Up @@ -40,9 +36,9 @@
//! .connect(interface);
//! ```
//!
//! The above examples will produce a [RawMode](../mode/raw/struct.RawMode.html) instance
//! The above examples will produce a [`DisplayProperties`] instance
//! by default. You need to coerce them into a mode by specifying a type on assignment. For
//! example, to use [`TerminalMode` mode](../mode/terminal/struct.TerminalMode.html):
//! example, to use [`TerminalMode`] mode:
//!
//! ```rust
//! # use ssd1306::test_helpers::{PinStub, SpiStub};
Expand All @@ -53,14 +49,16 @@
//! let interface = display_interface_spi::SPIInterfaceNoCS::new(spi, dc);
//! let display: TerminalMode<_> = Builder::new().connect(interface).into();
//! ```
//!
//! [`I2CDIBuilder`]: crate::builder::I2CDIBuilder
//! [`DisplayProperties`]: crate::properties::DisplayProperties
//! [`GraphicsMode`]: crate::mode::graphics::GraphicsMode
//! [`TerminalMode`]: crate::mode::terminal::TerminalMode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much cleaner, thanks. As mentioned in the review comments, please change these links (and all others in the PR) back to relative paths. Linkchecker and manual docs surfing should help here.


use display_interface::WriteOnlyDataCommand;

use crate::{
displayrotation::DisplayRotation,
displaysize::DisplaySize,
mode::{displaymode::DisplayMode, raw::RawMode},
properties::DisplayProperties,
displayrotation::DisplayRotation, displaysize::DisplaySize, properties::DisplayProperties,
};

/// Builder struct. Driver options and interface are set using its methods.
Expand Down Expand Up @@ -95,20 +93,19 @@ impl Builder {

/// Set the rotation of the display to one of four values. Defaults to no rotation. Note that
/// 90º and 270º rotations are not supported by
/// [`TerminalMode`](../mode/terminal/struct.TerminalMode.html).
/// [`TerminalMode`](crate::mode::terminal::TerminalMode).
pub fn with_rotation(self, rotation: DisplayRotation) -> Self {
Self { rotation, ..self }
}

/// Finish the builder and use some interface communicate with the display
///
/// This method consumes the builder and must come last in the method call chain
pub fn connect<I>(self, interface: I) -> DisplayMode<RawMode<I>>
pub fn connect<I>(self, interface: I) -> DisplayProperties<I>
where
I: WriteOnlyDataCommand,
{
let properties = DisplayProperties::new(interface, self.display_size, self.rotation);
DisplayMode::<RawMode<I>>::new(properties)
DisplayProperties::new(interface, self.display_size, self.rotation)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/displayrotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// Display rotation.
///
/// Note that 90º and 270º rotations are not supported by
// [`TerminalMode`](../mode/terminal/struct.TerminalMode.html).
// [`TerminalMode`](crate::mode::terminal::TerminalMode).
#[derive(Clone, Copy)]
pub enum DisplayRotation {
/// No rotation, normal display
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//!
//! The driver must be initialised by passing an I2C or SPI interface peripheral to the [`Builder`],
//! which will in turn create a driver instance in a particular mode. By default, the builder
//! returns a [`RawMode`] instance which isn't very useful by itself. You can coerce the driver
//! into a more useful mode by calling `into()` and defining the type you want to coerce to. For
//! returns a [`DisplayProperties`] instance which is a low level interface to manipulate the display properties (e.g. rotation).
//! You can coerce the driver into a more useful mode by calling `into()` and defining the type you want to coerce to. For
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap this to 100 columns.

While we're here, I'd also suggest rewording the You can coerce the driver part to The driver can be coerced.

//! example, to initialise the display with an I2C interface and [`GraphicsMode`], you would do
//! something like this:
//!
Expand Down Expand Up @@ -113,10 +113,10 @@
//! disp.flush().unwrap();
//! ```
//!
//! [`Builder`]: ./builder/struct.Builder.html
//! [`GraphicsMode`]: ./mode/graphics/struct.GraphicsMode.html
//! [`TerminalMode`]: ./mode/terminal/struct.TerminalMode.html
//! [`RawMode`]: ./mode/raw/struct.RawMode.html
//! [`Builder`]: crate::builder::Builder
//! [`DisplayProperties`]: crate::properties::DisplayProperties
//! [`GraphicsMode`]: crate::mode::graphics::GraphicsMode
//! [`TerminalMode`]: crate::mode::terminal::TerminalMode

#![no_std]
// #![deny(missing_debug_implementations)]
Expand Down
27 changes: 0 additions & 27 deletions src/mode/displaymode.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
//! Abstraction of different operating modes for the SSD1306

use display_interface::WriteOnlyDataCommand;

use crate::properties::DisplayProperties;

/// Display mode abstraction
pub struct DisplayMode<MODE>(pub MODE);

/// Trait with core functionality for display mode switching
pub trait DisplayModeTrait<DI> {
/// Allocate all required data and initialise display for mode
Expand All @@ -15,25 +10,3 @@ pub trait DisplayModeTrait<DI> {
/// Release resources for reuse with different mode
fn release(self) -> DisplayProperties<DI>;
}

impl<MODE> DisplayMode<MODE> {
/// Setup display to run in requested mode
pub fn new<DI>(properties: DisplayProperties<DI>) -> Self
where
DI: WriteOnlyDataCommand,
MODE: DisplayModeTrait<DI>,
{
DisplayMode(MODE::new(properties))
}

/// Change into any mode implementing DisplayModeTrait
// TODO: Figure out how to stay as generic DisplayMode but act as particular mode
pub fn into<DI, NMODE: DisplayModeTrait<DI>>(self) -> NMODE
where
DI: WriteOnlyDataCommand,
MODE: DisplayModeTrait<DI>,
{
let properties = self.0.release();
NMODE::new(properties)
}
}
42 changes: 19 additions & 23 deletions src/mode/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ use crate::{

// TODO: Add to prelude
/// Graphics mode handler
pub struct GraphicsMode<DI>
where
DI: WriteOnlyDataCommand,
{
pub struct GraphicsMode<DI> {
properties: DisplayProperties<DI>,
buffer: [u8; 1024],
min_x: u8,
Expand All @@ -77,10 +74,7 @@ where
max_y: u8,
}

impl<DI> DisplayModeTrait<DI> for GraphicsMode<DI>
where
DI: WriteOnlyDataCommand,
{
impl<DI> DisplayModeTrait<DI> for GraphicsMode<DI> {
/// Create new GraphicsMode instance
fn new(properties: DisplayProperties<DI>) -> Self {
GraphicsMode {
Expand All @@ -99,21 +93,7 @@ where
}
}

impl<DI> GraphicsMode<DI>
where
DI: WriteOnlyDataCommand,
{
/// Clear the display buffer. You need to call `disp.flush()` for any effect on the screen
pub fn clear(&mut self) {
self.buffer = [0; 1024];

let (width, height) = self.get_dimensions();
self.min_x = 0;
self.max_x = width - 1;
self.min_y = 0;
self.max_y = height - 1;
}

impl<DI> GraphicsMode<DI> {
/// Reset display
// TODO: Move to a more appropriate place
pub fn reset<RST, DELAY, PinE>(
Expand All @@ -131,6 +111,22 @@ where
delay.delay_ms(10);
rst.set_high().map_err(Error::Pin)
}
}

impl<DI> GraphicsMode<DI>
where
DI: WriteOnlyDataCommand,
{
/// Clear the display buffer. You need to call `disp.flush()` for any effect on the screen
pub fn clear(&mut self) {
self.buffer = [0; 1024];

let (width, height) = self.get_dimensions();
self.min_x = 0;
self.max_x = width - 1;
self.min_y = 0;
self.max_y = height - 1;
}

/// Write out data to a display.
///
Expand Down
3 changes: 1 addition & 2 deletions src/mode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

pub mod displaymode;
pub mod graphics;
pub mod raw;
pub mod terminal;

pub use self::{graphics::GraphicsMode, raw::RawMode, terminal::TerminalMode};
pub use self::{graphics::GraphicsMode, terminal::TerminalMode};
38 changes: 0 additions & 38 deletions src/mode/raw.rs

This file was deleted.

36 changes: 19 additions & 17 deletions src/mode/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,25 @@ where
}
}

impl<DI> TerminalMode<DI> {
/// Reset display
pub fn reset<RST, DELAY, PinE>(
&mut self,
rst: &mut RST,
delay: &mut DELAY,
) -> Result<(), Error<(), PinE>>
where
RST: OutputPin<Error = PinE>,
DELAY: DelayMs<u8>,
{
rst.set_high().map_err(Error::Pin)?;
delay.delay_ms(1);
rst.set_low().map_err(Error::Pin)?;
delay.delay_ms(10);
rst.set_high().map_err(Error::Pin)
}
}

impl<DI> TerminalMode<DI>
where
DI: WriteOnlyDataCommand,
Expand Down Expand Up @@ -204,23 +223,6 @@ where
Ok(())
}

/// Reset display
pub fn reset<RST, DELAY, PinE>(
&mut self,
rst: &mut RST,
delay: &mut DELAY,
) -> Result<(), Error<(), PinE>>
where
RST: OutputPin<Error = PinE>,
DELAY: DelayMs<u8>,
{
rst.set_high().map_err(Error::Pin)?;
delay.delay_ms(1);
rst.set_low().map_err(Error::Pin)?;
delay.delay_ms(10);
rst.set_high().map_err(Error::Pin)
}

/// Write out data to display. This is a noop in terminal mode.
pub fn flush(&mut self) -> Result<(), TerminalModeError> {
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub use display_interface_spi::{SPIInterface, SPIInterfaceNoCS};
pub use super::{
displayrotation::DisplayRotation,
displaysize::DisplaySize,
mode::{GraphicsMode, TerminalMode},
mode::{displaymode::DisplayModeTrait, GraphicsMode, TerminalMode},
};
14 changes: 14 additions & 0 deletions src/properties.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Container to store and set display properties

use crate::mode::displaymode::DisplayModeTrait;
use crate::{
command::{AddrMode, Command, VcomhLevel},
displayrotation::DisplayRotation,
Expand Down Expand Up @@ -43,6 +44,11 @@ where
}
}

/// Releases the display interface
pub fn release(self) -> DI {
self.iface
}

/// Initialise the display in column mode (i.e. a byte walks down a column of 8 pixels) with
/// column 0 on the left and column _(display_width - 1)_ on the right.
pub fn init_column_mode(&mut self) -> Result<(), DisplayError> {
Expand Down Expand Up @@ -242,4 +248,12 @@ where
pub fn display_on(&mut self, on: bool) -> Result<(), DisplayError> {
Command::DisplayOn(on).send(&mut self.iface)
}

/// Change into any mode implementing DisplayModeTrait
pub fn into<NMODE: DisplayModeTrait<DI>>(self) -> NMODE
where
DI: WriteOnlyDataCommand,
{
NMODE::new(self)
}
}