Skip to content

Commit 7f4fcab

Browse files
eldruinjamwaffles
authored andcommitted
Use fallible OutputPin traits
1 parent 10e2759 commit 7f4fcab

File tree

9 files changed

+41
-32
lines changed

9 files changed

+41
-32
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ branch = "master"
1616
repository = "jamwaffles/ssd1306"
1717

1818
[dependencies]
19-
embedded-hal = "0.2.2"
19+
embedded-hal = "0.2.3"
2020

2121
[dev-dependencies.stm32f1xx-hal]
2222
version = "0.2.0"

examples/graphics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn main() -> ! {
7676

7777
let mut disp: GraphicsMode<_> = Builder::new().connect_spi(spi, dc).into();
7878

79-
disp.reset(&mut rst, &mut delay);
79+
disp.reset(&mut rst, &mut delay).unwrap();
8080
disp.init().unwrap();
8181
disp.flush().unwrap();
8282

examples/pixelsquare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn main() -> ! {
7575

7676
let mut disp: GraphicsMode<_> = Builder::new().connect_spi(spi, dc).into();
7777

78-
disp.reset(&mut rst, &mut delay);
78+
disp.reset(&mut rst, &mut delay).unwrap();
7979
disp.init().unwrap();
8080
disp.flush().unwrap();
8181

src/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! ```
4242
4343
use hal;
44-
use hal::digital::OutputPin;
44+
use hal::digital::v2::OutputPin;
4545

4646
use crate::displayrotation::DisplayRotation;
4747
use crate::displaysize::DisplaySize;
@@ -109,15 +109,15 @@ impl Builder {
109109
}
110110

111111
/// Finish the builder and use SPI to communicate with the display
112-
pub fn connect_spi<SPI, DC, CommE>(
112+
pub fn connect_spi<SPI, DC, CommE, PinE>(
113113
&self,
114114
spi: SPI,
115115
dc: DC,
116116
) -> DisplayMode<RawMode<SpiInterface<SPI, DC>>>
117117
where
118118
SPI: hal::blocking::spi::Transfer<u8, Error = CommE>
119119
+ hal::blocking::spi::Write<u8, Error = CommE>,
120-
DC: OutputPin,
120+
DC: OutputPin<Error = PinE>,
121121
{
122122
let properties =
123123
DisplayProperties::new(SpiInterface::new(spi, dc), self.display_size, self.rotation);

src/interface/i2c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<I2C, CommE> DisplayInterface for I2cInterface<I2C>
2626
where
2727
I2C: hal::blocking::i2c::Write<Error = CommE>,
2828
{
29-
type Error = Error<CommE>;
29+
type Error = Error<CommE, ()>;
3030

3131
fn send_commands(&mut self, cmds: &[u8]) -> Result<(), Self::Error> {
3232
// Copy over given commands to new aray to prefix with command identifier

src/interface/spi.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! SSD1306 SPI interface
22
33
use hal;
4-
use hal::digital::OutputPin;
4+
use hal::digital::v2::OutputPin;
55

66
use super::DisplayInterface;
77
use crate::Error;
@@ -15,38 +15,35 @@ pub struct SpiInterface<SPI, DC> {
1515
dc: DC,
1616
}
1717

18-
impl<SPI, DC, CommE> SpiInterface<SPI, DC>
18+
impl<SPI, DC, CommE, PinE> SpiInterface<SPI, DC>
1919
where
2020
SPI: hal::blocking::spi::Write<u8, Error = CommE>,
21-
DC: OutputPin,
21+
DC: OutputPin<Error = PinE>,
2222
{
2323
/// Create new SPI interface for communciation with SSD1306
2424
pub fn new(spi: SPI, dc: DC) -> Self {
2525
Self { spi, dc }
2626
}
2727
}
2828

29-
impl<SPI, DC, CommE> DisplayInterface for SpiInterface<SPI, DC>
29+
impl<SPI, DC, CommE, PinE> DisplayInterface for SpiInterface<SPI, DC>
3030
where
3131
SPI: hal::blocking::spi::Write<u8, Error = CommE>,
32-
DC: OutputPin,
32+
DC: OutputPin<Error = PinE>,
3333
{
34-
type Error = Error<CommE>;
34+
type Error = Error<CommE, PinE>;
3535

3636
fn send_commands(&mut self, cmds: &[u8]) -> Result<(), Self::Error> {
37-
self.dc.set_low();
37+
self.dc.set_low().map_err(Error::Pin)?;
3838

3939
self.spi.write(&cmds).map_err(Error::Comm)?;
4040

41-
self.dc.set_high();
42-
43-
Ok(())
41+
self.dc.set_high().map_err(Error::Pin)
4442
}
4543

4644
fn send_data(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
4745
// 1 = data, 0 = command
48-
self.dc.set_high();
49-
46+
self.dc.set_high().map_err(Error::Pin)?;
5047

5148
self.spi.write(&buf).map_err(Error::Comm)
5249
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@
168168

169169
/// Errors in this crate
170170
#[derive(Debug)]
171-
pub enum Error<CommE> {
171+
pub enum Error<CommE, PinE> {
172172
/// Communication error
173173
Comm(CommE),
174+
/// Pin setting error
175+
Pin(PinE),
174176
}
175177

176178
extern crate embedded_hal as hal;

src/mode/graphics.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
//! ```
1717
1818
use hal::blocking::delay::DelayMs;
19-
use hal::digital::OutputPin;
19+
use hal::digital::v2::OutputPin;
2020

2121
use crate::displayrotation::DisplayRotation;
2222
use crate::displaysize::DisplaySize;
2323
use crate::interface::DisplayInterface;
2424
use crate::mode::displaymode::DisplayModeTrait;
2525
use crate::properties::DisplayProperties;
26+
use crate::Error;
2627

2728
// TODO: Add to prelude
2829
/// Graphics mode handler
@@ -63,16 +64,20 @@ where
6364

6465
/// Reset display
6566
// TODO: Move to a more appropriate place
66-
pub fn reset<RST, DELAY>(&mut self, rst: &mut RST, delay: &mut DELAY)
67+
pub fn reset<RST, DELAY, PinE>(
68+
&mut self,
69+
rst: &mut RST,
70+
delay: &mut DELAY,
71+
) -> Result<(), Error<(), PinE>>
6772
where
68-
RST: OutputPin,
73+
RST: OutputPin<Error = PinE>,
6974
DELAY: DelayMs<u8>,
7075
{
71-
rst.set_high();
76+
rst.set_high().map_err(Error::Pin)?;
7277
delay.delay_ms(1);
73-
rst.set_low();
78+
rst.set_low().map_err(Error::Pin)?;
7479
delay.delay_ms(10);
75-
rst.set_high();
80+
rst.set_high().map_err(Error::Pin)
7681
}
7782

7883
/// Write out data to display

src/mode/terminal.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ use crate::displaysize::DisplaySize;
2323
use crate::interface::DisplayInterface;
2424
use crate::mode::displaymode::DisplayModeTrait;
2525
use crate::properties::DisplayProperties;
26+
use crate::Error;
2627
use core::fmt;
2728
use hal::blocking::delay::DelayMs;
28-
use hal::digital::OutputPin;
29+
use hal::digital::v2::OutputPin;
2930

3031
/// A trait to convert from a character to 8x8 bitmap
3132
pub trait CharacterBitmap<T> {
@@ -187,16 +188,20 @@ where
187188
}
188189

189190
/// Reset display
190-
pub fn reset<RST, DELAY>(&mut self, rst: &mut RST, delay: &mut DELAY)
191+
pub fn reset<RST, DELAY, PinE>(
192+
&mut self,
193+
rst: &mut RST,
194+
delay: &mut DELAY,
195+
) -> Result<(), Error<(), PinE>>
191196
where
192-
RST: OutputPin,
197+
RST: OutputPin<Error = PinE>,
193198
DELAY: DelayMs<u8>,
194199
{
195-
rst.set_high();
200+
rst.set_high().map_err(Error::Pin)?;
196201
delay.delay_ms(1);
197-
rst.set_low();
202+
rst.set_low().map_err(Error::Pin)?;
198203
delay.delay_ms(10);
199-
rst.set_high();
204+
rst.set_high().map_err(Error::Pin)
200205
}
201206

202207
/// Write out data to display. This is a noop in terminal mode.

0 commit comments

Comments
 (0)