Skip to content

Commit 9119b39

Browse files
committed
Fix/update doc examples, test them as part of build.sh
Hot dayum some of these were outdated
1 parent 9f2ac6b commit 9119b39

File tree

7 files changed

+181
-117
lines changed

7 files changed

+181
-117
lines changed

build.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ cargo build --target $TARGET --all-features --release
66

77
if [ -z $DISABLE_EXAMPLES ]; then
88
cargo build --target $TARGET --all-features --examples
9-
fi
9+
fi
10+
11+
cargo test --lib --target x86_64-unknown-linux-gnu
12+
cargo test --doc --target x86_64-unknown-linux-gnu

src/builder.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,26 @@
1010
//!
1111
//! Connect over SPI with default rotation (0 deg) and size (128x64):
1212
//!
13-
//! ```rust,ignore
14-
//! let spi = /* SPI interface from your HAL of choice */;
15-
//! let dc = /* GPIO data/command select pin */;
13+
//! ```rust,no_run
14+
//! use sh1106::{mode::GraphicsMode, Builder};
15+
//! let spi = /* SPI interface from your HAL of choice */
16+
//! # sh1106::test_helpers::SpiStub;
17+
//! let dc = /* GPIO data/command select pin */
18+
//! # sh1106::test_helpers::PinStub;
1619
//!
17-
//! Builder::new().connect_spi(spi, dc);
20+
//! // This example does not use a Chip Select pin
21+
//! let cs = sh1106::builder::NoOutputPin::new();
22+
//!
23+
//! Builder::new().connect_spi(spi, dc, cs);
1824
//! ```
1925
//!
2026
//! Connect over I2C, changing lots of options
2127
//!
22-
//! ```rust,ignore
23-
//! let i2c = /* I2C interface from your HAL of choice */;
28+
//! ```rust,no_run
29+
//! use sh1106::{displayrotation::DisplayRotation, displaysize::DisplaySize, Builder};
30+
//!
31+
//! let i2c = /* I2C interface from your HAL of choice */
32+
//! # sh1106::test_helpers::I2cStub;
2433
//!
2534
//! Builder::new()
2635
//! .with_rotation(DisplayRotation::Rotate180)
@@ -33,11 +42,17 @@
3342
//! by default. You need to coerce them into a mode by specifying a type on assignment. For
3443
//! example, to use [`GraphicsMode` mode](../mode/graphics/struct.GraphicsMode.html):
3544
//!
36-
//! ```rust,ignore
37-
//! let spi = /* SPI interface from your HAL of choice */;
38-
//! let dc = /* GPIO data/command select pin */;
45+
//! ```rust,no_run
46+
//! use sh1106::{mode::GraphicsMode, Builder};
47+
//! let spi = /* SPI interface from your HAL of choice */
48+
//! # sh1106::test_helpers::SpiStub;
49+
//! let dc = /* GPIO data/command select pin */
50+
//! # sh1106::test_helpers::PinStub;
51+
//!
52+
//! // This example does not use a Chip Select pin
53+
//! let cs = sh1106::builder::NoOutputPin::new();
3954
//!
40-
//! let display: GraphicsMode<_> = Builder::new().connect_spi(spi, dc).into();
55+
//! let display: GraphicsMode<_> = Builder::new().connect_spi(spi, dc, cs).into();
4156
//! ```
4257
4358
use core::marker::PhantomData;
@@ -52,6 +67,8 @@ use crate::{
5267
};
5368

5469
/// Builder struct. Driver options and interface are set using its methods.
70+
///
71+
/// See the [module level documentation](crate::builder) for more details.
5572
#[derive(Clone, Copy)]
5673
pub struct Builder {
5774
display_size: DisplaySize,

src/interface/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
//! a type alias. Here's an example for the I2C1 on an STM32F103xx:
1010
//!
1111
//! ```rust
12-
//! # extern crate sh1106;
13-
//! # extern crate stm32f103xx_hal as hal;
14-
//! # use hal::gpio::gpiob::{PB8, PB9};
15-
//! # use hal::gpio::{Alternate, OpenDrain};
16-
//! # use hal::i2c::I2c;
17-
//! # use hal::prelude::*;
18-
//! # use hal::stm32f103xx::I2C1;
19-
//! # use sh1106::interface::I2cInterface;
12+
//! # use stm32f1xx_hal::gpio::gpiob::{PB8, PB9};
13+
//! # use stm32f1xx_hal::gpio::{Alternate, OpenDrain};
14+
//! # use stm32f1xx_hal::i2c::I2c;
15+
//! # use stm32f1xx_hal::prelude::*;
16+
//! # use stm32f1xx_hal::pac::I2C1;
17+
//! # use sh1106::{interface::I2cInterface, mode::GraphicsMode, Builder};
2018
//! type OledDisplay = GraphicsMode<
2119
//! I2cInterface<I2c<I2C1, (PB8<Alternate<OpenDrain>>, PB9<Alternate<OpenDrain>>)>>,
2220
//! >;
@@ -25,25 +23,27 @@
2523
//! Here's one for SPI1 on an STM32F103xx:
2624
//!
2725
//! ```rust
28-
//! # extern crate sh1106;
29-
//! # extern crate stm32f103xx_hal as hal;
30-
//! # use hal::gpio::gpioa::{PA5, PA6, PA7};
31-
//! # use hal::gpio::gpiob::PB1;
32-
//! # use hal::gpio::{Alternate, Floating, Input, Output, PushPull};
33-
//! # use hal::spi::Spi;
34-
//! # use hal::stm32f103xx::SPI1;
35-
//! # use sh1106::interface::SpiInterface;
26+
//! # use stm32f1xx_hal::gpio::gpioa::{PA5, PA6, PA7};
27+
//! # use stm32f1xx_hal::gpio::gpiob::PB1;
28+
//! # use stm32f1xx_hal::gpio::{Alternate, Floating, Input, Output, PushPull};
29+
//! # use stm32f1xx_hal::spi;
30+
//! # use stm32f1xx_hal::spi::Spi;
31+
//! # use stm32f1xx_hal::pac::SPI1;
32+
//! # use sh1106::{interface::SpiInterface, mode::GraphicsMode};
3633
//! pub type OledDisplay = GraphicsMode<
3734
//! SpiInterface<
3835
//! Spi<
3936
//! SPI1,
37+
//! spi::Spi1NoRemap,
4038
//! (
4139
//! PA5<Alternate<PushPull>>,
4240
//! PA6<Input<Floating>>,
4341
//! PA7<Alternate<PushPull>>,
4442
//! ),
43+
//! u8
4544
//! >,
4645
//! PB1<Output<PushPull>>,
46+
//! sh1106::builder::NoOutputPin,
4747
//! >,
4848
//! >;
4949
//! ```

src/lib.rs

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
//! [`mode::GraphicsMode`](mode/graphics/struct.GraphicsMode.html), you would do something like
1010
//! this:
1111
//!
12-
//! ```rust,ignore
13-
//! let i2c = I2c::i2c1(/* snip */);
12+
//! ```rust,no_run
13+
//! use sh1106::{prelude::*, Builder};
14+
//! # let i2c = sh1106::test_helpers::I2cStub;
1415
//!
1516
//! let mut display: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();
16-
//! display.init();
17+
//!
18+
//! display.init().unwrap();
19+
//! display.flush().unwrap();
1720
//!
1821
//! display.set_pixel(10, 20, 1);
22+
//!
23+
//! display.flush().unwrap();
1924
//! ```
2025
//!
2126
//! See the [example](https://github.com/jamwaffles/sh1106/blob/master/examples/graphics_i2c.rs)
@@ -34,56 +39,35 @@
3439
//!
3540
//! Uses [mode::GraphicsMode] and [embedded_graphics](../embedded_graphics/index.html).
3641
//!
37-
//! ```rust,no-run
38-
//! #![no_std]
39-
//!
40-
//! extern crate cortex_m;
41-
//! extern crate embedded_graphics;
42-
//! extern crate embedded_hal as hal;
43-
//! extern crate panic_abort;
44-
//! extern crate sh1106;
45-
//! extern crate stm32f103xx_hal as blue_pill;
46-
//!
47-
//! use blue_pill::i2c::{DutyCycle, I2c, Mode};
48-
//! use blue_pill::prelude::*;
49-
//! use embedded_graphics::fonts::Font6x8;
50-
//! use embedded_graphics::prelude::*;
51-
//! use sh1106::{mode::GraphicsMode, Builder};
52-
//!
53-
//! fn main() {
54-
//! let dp = blue_pill::stm32f103xx::Peripherals::take().unwrap();
55-
//! let mut flash = dp.FLASH.constrain();
56-
//! let mut rcc = dp.RCC.constrain();
57-
//! let clocks = rcc.cfgr.freeze(&mut flash.acr);
58-
//! let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
59-
//! let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
60-
//! let scl = gpiob.pb8.into_alternate_open_drain(&mut gpiob.crh);
61-
//! let sda = gpiob.pb9.into_alternate_open_drain(&mut gpiob.crh);
62-
//!
63-
//! let i2c = I2c::i2c1(
64-
//! dp.I2C1,
65-
//! (scl, sda),
66-
//! &mut afio.mapr,
67-
//! Mode::Fast {
68-
//! frequency: 400_000,
69-
//! duty_cycle: DutyCycle::Ratio1to1,
70-
//! },
71-
//! clocks,
72-
//! &mut rcc.apb1,
73-
//! );
74-
//!
75-
//! let mut display: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();
76-
//!
77-
//! display.init().unwrap();
78-
//! display.flush().unwrap();
79-
//! display.draw(Font6x8::render_str("Hello world!", 1u8.into()).into_iter());
80-
//! display.draw(
81-
//! Font6x8::render_str("Hello Rust!")
82-
//! .translate(Coord::new(0, 16))
83-
//! .into_iter(),
84-
//! );
85-
//! display.flush().unwrap();
86-
//! }
42+
//! ```rust,no_run
43+
//! use embedded_graphics::{
44+
//! mono_font::{ascii::FONT_6X10, MonoTextStyleBuilder},
45+
//! pixelcolor::BinaryColor,
46+
//! prelude::*,
47+
//! text::{Baseline, Text},
48+
//! };
49+
//! use sh1106::{prelude::*, Builder};
50+
//! # let i2c = sh1106::test_helpers::I2cStub;
51+
//!
52+
//! let mut display: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();
53+
//!
54+
//! display.init().unwrap();
55+
//! display.flush().unwrap();
56+
//!
57+
//! let text_style = MonoTextStyleBuilder::new()
58+
//! .font(&FONT_6X10)
59+
//! .text_color(BinaryColor::On)
60+
//! .build();
61+
//!
62+
//! Text::with_baseline("Hello world!", Point::zero(), text_style, Baseline::Top)
63+
//! .draw(&mut display)
64+
//! .unwrap();
65+
//!
66+
//! Text::with_baseline("Hello Rust!", Point::new(0, 16), text_style, Baseline::Top)
67+
//! .draw(&mut display)
68+
//! .unwrap();
69+
//!
70+
//! display.flush().unwrap();
8771
//! ```
8872
8973
#![no_std]
@@ -110,10 +94,12 @@ extern crate embedded_hal as hal;
11094
pub mod builder;
11195
mod command;
11296
pub mod displayrotation;
113-
mod displaysize;
97+
pub mod displaysize;
11498
pub mod interface;
11599
pub mod mode;
116100
pub mod prelude;
117101
pub mod properties;
102+
#[doc(hidden)]
103+
pub mod test_helpers;
118104

119105
pub use crate::builder::{Builder, NoOutputPin};

src/mode/graphics.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
1-
//! Buffered display module for use with the [embedded_graphics] crate
1+
//! Buffered display module for use with the [embedded-graphics] crate
22
//!
3-
//! ```rust,ignore
4-
//! let i2c = /* I2C interface from your HAL of choice */;
5-
//! let display: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();
6-
//! let image = include_bytes!("image_16x16.raw");
3+
//! ```rust,no_run
4+
//! use embedded_graphics::{
5+
//! pixelcolor::BinaryColor,
6+
//! prelude::*,
7+
//! primitives::{Circle, Line, PrimitiveStyle, Rectangle},
8+
//! };
9+
//! use sh1106::{prelude::*, Builder};
10+
//! # let i2c = sh1106::test_helpers::I2cStub;
11+
//!
12+
//! let mut display: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();
713
//!
814
//! display.init().unwrap();
915
//! display.flush().unwrap();
10-
//! display.draw(Line::new(Coord::new(0, 0), (16, 16), 1.into()).into_iter());
11-
//! display.draw(Rect::new(Coord::new(24, 0), (40, 16), 1u8.into()).into_iter());
12-
//! display.draw(Circle::new(Coord::new(64, 8), 8, 1u8.into()).into_iter());
13-
//! display.draw(Image1BPP::new(image, 0, 24));
14-
//! display.draw(Font6x8::render_str("Hello Rust!", 1u8.into()).translate(Coord::new(24, 24)).into_iter());
16+
//!
17+
//! Line::new(Point::new(8, 16 + 16), Point::new(8 + 16, 16 + 16))
18+
//! .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
19+
//! .draw(&mut display)
20+
//! .unwrap();
21+
//!
22+
//! Line::new(Point::new(8, 16 + 16), Point::new(8 + 8, 16))
23+
//! .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
24+
//! .draw(&mut display)
25+
//! .unwrap();
26+
//!
27+
//! Line::new(Point::new(8 + 16, 16 + 16), Point::new(8 + 8, 16))
28+
//! .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
29+
//! .draw(&mut display)
30+
//! .unwrap();
31+
//!
32+
//! Rectangle::with_corners(Point::new(48, 16), Point::new(48 + 16, 16 + 16))
33+
//! .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
34+
//! .draw(&mut display)
35+
//! .unwrap();
36+
//!
37+
//! Circle::new(Point::new(88, 16), 16)
38+
//! .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
39+
//! .draw(&mut display)
40+
//! .unwrap();
41+
//!
1542
//! display.flush().unwrap();
1643
//! ```
1744

src/properties.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,32 +125,6 @@ where
125125
}
126126

127127
/// Get display dimensions, taking into account the current rotation of the display
128-
///
129-
/// ```rust
130-
/// # struct FakeInterface;
131-
/// #
132-
/// # impl DisplayInterface for FakeInterface {
133-
/// # fn send_command(&mut self, cmd: u8) -> Result<(), ()> { Ok(()) }
134-
/// # fn send_data(&mut self, buf: &[u8]) -> Result<(), ()> { Ok(()) }
135-
/// # }
136-
/// #
137-
/// # let interface = FakeInterface {};
138-
/// #
139-
/// let disp = DisplayProperties::new(
140-
/// interface,
141-
/// DisplaySize::Display128x64,
142-
/// DisplayRotation::Rotate0,
143-
/// );
144-
/// assert_eq!(display.get_dimensions(), (128, 64));
145-
///
146-
/// # let interface = FakeInterface {};
147-
/// let rotated_disp = DisplayProperties::new(
148-
/// interface,
149-
/// DisplaySize::Display128x64,
150-
/// DisplayRotation::Rotate90,
151-
/// );
152-
/// assert_eq!(rotated_display.get_dimensions(), (64, 128));
153-
/// ```
154128
pub fn get_dimensions(&self) -> (u8, u8) {
155129
let (w, h) = self.display_size.dimensions();
156130

src/test_helpers.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//! Helpers for use in examples and tests
2+
3+
use embedded_hal::{
4+
blocking::{
5+
i2c,
6+
spi::{self, Transfer},
7+
},
8+
digital::v2::OutputPin,
9+
};
10+
11+
#[allow(dead_code)]
12+
#[derive(Debug, Clone, Copy)]
13+
pub struct SpiStub;
14+
15+
impl spi::Write<u8> for SpiStub {
16+
type Error = ();
17+
18+
fn write(&mut self, _buf: &[u8]) -> Result<(), ()> {
19+
Ok(())
20+
}
21+
}
22+
23+
impl Transfer<u8> for SpiStub {
24+
type Error = ();
25+
26+
fn transfer<'a>(&mut self, buf: &'a mut [u8]) -> Result<&'a [u8], ()> {
27+
Ok(buf)
28+
}
29+
}
30+
31+
#[allow(dead_code)]
32+
#[derive(Debug, Clone, Copy)]
33+
pub struct PinStub;
34+
35+
impl OutputPin for PinStub {
36+
type Error = ();
37+
38+
fn set_high(&mut self) -> Result<(), ()> {
39+
Ok(())
40+
}
41+
42+
fn set_low(&mut self) -> Result<(), ()> {
43+
Ok(())
44+
}
45+
}
46+
47+
#[allow(dead_code)]
48+
#[derive(Debug, Clone, Copy)]
49+
pub struct I2cStub;
50+
51+
impl i2c::Write for I2cStub {
52+
type Error = ();
53+
54+
fn write(&mut self, _addr: u8, _buf: &[u8]) -> Result<(), ()> {
55+
Ok(())
56+
}
57+
}

0 commit comments

Comments
 (0)