Skip to content

Commit 6507266

Browse files
committed
Upgrade to embedded-graphics 0.7
1 parent 5bd6c86 commit 6507266

File tree

4 files changed

+84
-73
lines changed

4 files changed

+84
-73
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ projects using an SPI interface.
88

99
## [Unreleased] - ReleaseDate
1010

11+
### Changed
12+
13+
- **(breaking)** [#12](https://github.com/jamwaffles/ssd1331/pull/12) Upgrade to `embedded-graphics` 0.7.
14+
1115
## [0.2.3] - 2021-04-06
1216

1317
### Added
@@ -129,9 +133,9 @@ disp.rotation();
129133
- **(breaking)** Removed `Builder` struct.
130134

131135
<!-- next-url -->
136+
132137
[unreleased]: https://github.com/jamwaffles/ssd1331/compare/v0.2.3...HEAD
133138
[0.2.3]: https://github.com/jamwaffles/ssd1331/compare/v0.2.2...v0.2.3
134-
135139
[0.2.2]: https://github.com/jamwaffles/ssd1331/compare/v0.2.1...v0.2.2
136140
[0.2.1]: https://github.com/jamwaffles/ssd1331/compare/v0.2.0...v0.2.1
137141
[0.2.0]: https://github.com/jamwaffles/ssd1331/compare/v0.2.0-alpha.2...v0.2.0

Cargo.toml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,25 @@ circle-ci = { repository = "jamwaffles/ssd1331", branch = "master" }
2121
[dependencies]
2222
embedded-hal = "0.2.3"
2323

24-
[dependencies.embedded-graphics]
24+
[dependencies.embedded-graphics-core]
2525
optional = true
26-
version = "0.6.0"
26+
version = "0.3.2"
2727

2828
[dev-dependencies]
2929
cortex-m = "0.6.1"
3030
cortex-m-rt = "0.6.11"
3131
panic-semihosting = "0.5.3"
32-
33-
[dev-dependencies.tinybmp]
34-
version = "0.2.2"
35-
features = [ "graphics" ]
32+
embedded-graphics = "0.7.1"
33+
tinybmp = "0.3.1"
3634

3735
[dev-dependencies.stm32f1xx-hal]
3836
version = "0.5.2"
3937
features = [ "rt", "stm32f103" ]
4038

4139
[features]
4240
default = ["graphics"]
43-
graphics = ["embedded-graphics"]
41+
graphics = ["embedded-graphics-core"]
42+
4443
[profile.dev]
4544
codegen-units = 1
4645
incremental = false

src/display.rs

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ const BUF_SIZE: usize = 96 * 64 * 2;
2020
///
2121
/// ```rust
2222
/// use embedded_graphics::{
23-
/// fonts::{Font6x8, Text},
2423
/// geometry::Point,
2524
/// image::{Image, ImageRawLE},
25+
/// mono_font::{
26+
/// ascii::{FONT_6X10, FONT_9X18},
27+
/// MonoTextStyleBuilder,
28+
/// },
2629
/// pixelcolor::Rgb565,
2730
/// prelude::*,
28-
/// primitives::{Circle, Line, Rectangle},
29-
/// style::{PrimitiveStyleBuilder, TextStyleBuilder},
31+
/// primitives::{Circle, Line, PrimitiveStyle, Rectangle, Triangle},
32+
/// text::{Baseline, Text},
3033
/// };
3134
/// use ssd1331::{DisplayRotation::Rotate0, Ssd1331};
3235
/// # use ssd1331::test_helpers::{Pin, Spi};
@@ -36,50 +39,44 @@ const BUF_SIZE: usize = 96 * 64 * 2;
3639
/// let dc = Pin;
3740
///
3841
/// let mut display = Ssd1331::new(spi, dc, Rotate0);
39-
/// let raw = ImageRawLE::new(include_bytes!("../examples/ferris.raw"), 86, 64);
42+
/// let raw = ImageRawLE::new(include_bytes!("../examples/ferris.raw"), 86);
4043
///
41-
/// let image: Image<ImageRawLE<Rgb565>, Rgb565> = Image::new(&raw, Point::zero());
44+
/// let image: Image<ImageRawLE<Rgb565>> = Image::new(&raw, Point::zero());
4245
///
4346
/// // Initialise and clear the display
4447
/// display.init().unwrap();
4548
/// display.flush().unwrap();
4649
///
47-
/// Line::new(Point::new(0, 0), Point::new(16, 16))
48-
/// .into_styled(
49-
/// PrimitiveStyleBuilder::new()
50-
/// .stroke_color(Rgb565::RED)
51-
/// .stroke_width(1)
52-
/// .build(),
53-
/// )
54-
/// .draw(&mut display);
50+
/// Triangle::new(
51+
/// Point::new(8, 16 + 16),
52+
/// Point::new(8 + 16, 16 + 16),
53+
/// Point::new(8 + 8, 16),
54+
/// )
55+
/// .into_styled(PrimitiveStyle::with_stroke(Rgb565::RED, 1))
56+
/// .draw(&mut display)
57+
/// .unwrap();
5558
///
56-
/// Rectangle::new(Point::new(24, 0), Point::new(40, 16))
57-
/// .into_styled(
58-
/// PrimitiveStyleBuilder::new()
59-
/// .stroke_color(Rgb565::new(255, 127, 0))
60-
/// .stroke_width(1)
61-
/// .build(),
62-
/// )
63-
/// .draw(&mut display);
59+
/// Rectangle::with_corners(Point::new(36, 16), Point::new(36 + 16, 16 + 16))
60+
/// .into_styled(PrimitiveStyle::with_stroke(Rgb565::GREEN, 1))
61+
/// .draw(&mut display)
62+
/// .unwrap();
6463
///
65-
/// Circle::new(Point::new(64, 8), 8)
66-
/// .into_styled(
67-
/// PrimitiveStyleBuilder::new()
68-
/// .stroke_color(Rgb565::GREEN)
69-
/// .stroke_width(1)
70-
/// .build(),
71-
/// )
72-
/// .draw(&mut display);
64+
/// Circle::new(Point::new(72, 16 + 8), 8)
65+
/// .into_styled(PrimitiveStyle::with_stroke(Rgb565::BLUE, 1))
66+
/// .draw(&mut display)
67+
/// .unwrap();
7368
///
7469
/// image.draw(&mut display);
7570
///
76-
/// Text::new("Hello Rust!", Point::new(24, 24))
77-
/// .into_styled(
78-
/// TextStyleBuilder::new(Font6x8)
79-
/// .text_color(Rgb565::RED)
80-
/// .build(),
81-
/// )
82-
/// .draw(&mut display);
71+
/// // Red with a small amount of green creates a deep orange colour
72+
/// let rust_style = MonoTextStyleBuilder::new()
73+
/// .font(&FONT_9X18)
74+
/// .text_color(Rgb565::RED)
75+
/// .build();
76+
///
77+
/// Text::with_baseline("Hello Rust!", Point::new(0, 0), rust_style, Baseline::Top)
78+
/// .draw(&mut display)
79+
/// .unwrap();
8380
///
8481
/// // Render graphics objects to the screen
8582
/// display.flush().unwrap();
@@ -202,8 +199,7 @@ where
202199
Ok(())
203200
}
204201

205-
/// Turn a pixel on or off. A non-zero `value` is treated as on, `0` as off. If the X and Y
206-
/// coordinates are out of the bounds of the display, this method call is a noop.
202+
/// Set the value for an individual pixel.
207203
pub fn set_pixel(&mut self, x: u32, y: u32, value: u16) {
208204
let idx = match self.display_rotation {
209205
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
@@ -363,47 +359,52 @@ where
363359
}
364360

365361
#[cfg(feature = "graphics")]
366-
use core::convert::TryInto;
367-
#[cfg(feature = "graphics")]
368-
use embedded_graphics::{
369-
drawable,
362+
use embedded_graphics_core::{
363+
draw_target::DrawTarget,
370364
geometry::Size,
365+
geometry::{Dimensions, OriginDimensions},
371366
pixelcolor::{
372367
raw::{RawData, RawU16},
373368
Rgb565,
374369
},
375-
DrawTarget,
370+
Pixel,
376371
};
377372

378373
#[cfg(feature = "graphics")]
379-
impl<SPI, DC> DrawTarget<Rgb565> for Ssd1331<SPI, DC>
374+
impl<SPI, DC> DrawTarget for Ssd1331<SPI, DC>
380375
where
381376
SPI: hal::blocking::spi::Write<u8>,
382377
DC: OutputPin,
383378
{
379+
type Color = Rgb565;
384380
type Error = core::convert::Infallible;
385381

386-
fn draw_pixel(&mut self, pixel: drawable::Pixel<Rgb565>) -> Result<(), Self::Error> {
387-
let drawable::Pixel(pos, color) = pixel;
388-
389-
// Guard against negative values. All positive i32 values from `pos` can be represented in
390-
// the `u32`s that `set_pixel()` accepts.
391-
if pos.x < 0 || pos.y < 0 {
392-
return Ok(());
393-
}
382+
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
383+
where
384+
I: IntoIterator<Item = Pixel<Self::Color>>,
385+
{
386+
let bb = self.bounding_box();
394387

395-
self.set_pixel(
396-
(pos.x).try_into().unwrap(),
397-
(pos.y).try_into().unwrap(),
398-
RawU16::from(color).into_inner(),
399-
);
388+
pixels
389+
.into_iter()
390+
.filter(|Pixel(pos, _color)| bb.contains(*pos))
391+
.for_each(|Pixel(pos, color)| {
392+
self.set_pixel(pos.x as u32, pos.y as u32, RawU16::from(color).into_inner())
393+
});
400394

401395
Ok(())
402396
}
397+
}
403398

399+
#[cfg(feature = "graphics")]
400+
impl<SPI, DC> OriginDimensions for Ssd1331<SPI, DC>
401+
where
402+
SPI: hal::blocking::spi::Write<u8>,
403+
DC: OutputPin,
404+
{
404405
fn size(&self) -> Size {
405406
let (w, h) = self.dimensions();
406407

407-
Size::new(w as u32, h as u32)
408+
Size::new(w.into(), h.into())
408409
}
409410
}

src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
//!
4747
//! ```rust
4848
//! # use ssd1331::test_helpers::{Spi, Pin};
49-
//! use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*};
49+
//! use embedded_graphics::{geometry::Point, image::Image, pixelcolor::Rgb565, prelude::*};
5050
//! use ssd1331::{DisplayRotation::Rotate0, Ssd1331};
5151
//! use tinybmp::Bmp;
5252
//!
@@ -55,16 +55,23 @@
5555
//! let dc = Pin;
5656
//!
5757
//! let mut display = Ssd1331::new(spi, dc, Rotate0);
58-
//! display.init();
58+
//! display.init().unwrap();
59+
//! display.flush().unwrap();
60+
//!
61+
//! let (w, h) = display.dimensions();
5962
//!
60-
//! let bmp = Bmp::from_slice(include_bytes!("../examples/rust-pride.bmp")).unwrap();
63+
//! let bmp = Bmp::from_slice(include_bytes!("../examples/rust-pride.bmp"))
64+
//! .expect("Failed to load BMP image");
6165
//!
62-
//! let im: Image<Bmp, Rgb565> = Image::new(&bmp, Point::zero());
66+
//! let im: Image<Bmp<Rgb565>> = Image::new(&bmp, Point::zero());
6367
//!
64-
//! // Center the image on the display
65-
//! let moved = im.translate(Point::new((96 - bmp.width() as i32) / 2, 0));
68+
//! // Position image in the center of the display
69+
//! let moved = im.translate(Point::new(
70+
//! (w as u32 - bmp.size().width) as i32 / 2,
71+
//! (h as u32 - bmp.size().height) as i32 / 2,
72+
//! ));
6673
//!
67-
//! moved.draw(&mut display);
74+
//! moved.draw(&mut display).unwrap();
6875
//!
6976
//! display.flush().unwrap();
7077
//! ```

0 commit comments

Comments
 (0)