Skip to content

Commit

Permalink
Upgrade to embedded-graphics 0.7 (#154)
Browse files Browse the repository at this point in the history
* Upgrade to embedded-graphics 0.7

* Changelog entry

* Use black and white Rust BMP image

The darker colours in the rainbow rust-pride.bmp were being converted to
black pixels when going from RGB565 -> BinaryColor, so only half the
logo would show up. I've replaced the BMP with a black and white version
which is what actually shows on the display.
  • Loading branch information
jamwaffles authored Jun 7, 2021
1 parent bd30333 commit d5e520b
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 102 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

## [Unreleased] - ReleaseDate

### Changed

- **(breaking)** [#154](https://github.com/jamwaffles/ssd1306/pull/154) Upgrade to `embedded-graphics` 0.7.
- **(breaking)** [#150](https://github.com/jamwaffles/ssd1306/pull/150) `BufferedGraphicsMode::set_pixel` now accepts a `bool` instead of a `u8` for the pixel color value.
- **(breaking)** [#150](https://github.com/jamwaffles/ssd1306/pull/150) `display_on` is now called `set_display_on`.
- **(breaking)** [#150](https://github.com/jamwaffles/ssd1306/pull/150) `TerminalMode::get_position` is now called `position` to conform with Rust API guidelines.
Expand Down
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,16 @@ generic-array = "0.14.2"

[dependencies.embedded-graphics]
optional = true
version = "0.6.0"
version = "0.7.0"

[dev-dependencies]
cortex-m = "0.7.2"
cortex-m-rt = "0.6.12"
cortex-m-rtic = "0.5.3"
panic-halt = "0.2.0"
cast = { version = "0.2.6", default-features = false }

# Used to load BMP images in various examples
[dev-dependencies.tinybmp]
version = "0.2.2"
# Enable embedded-graphics integration
features = [ "graphics" ]
tinybmp = "0.3.0"

# Used by the noise_i2c examples
[dev-dependencies.rand]
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn main() -> ! {
.into_buffered_graphics_mode();
display.init().unwrap();

let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("./rust.raw"), 64, 64);
let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("./rust.raw"), 64);

let im = Image::new(&raw, Point::new(32, 0));

Expand All @@ -88,7 +88,6 @@ fn main() -> ! {
fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}

```

## License
Expand Down
34 changes: 8 additions & 26 deletions examples/bmp_i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
#![no_main]

use cortex_m_rt::{entry, exception, ExceptionFrame};
use embedded_graphics::{
image::Image,
pixelcolor::{BinaryColor, Rgb565},
prelude::*,
};
use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*};
use panic_halt as _;
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
use stm32f1xx_hal::{
Expand Down Expand Up @@ -73,29 +69,15 @@ fn main() -> ! {
.into_buffered_graphics_mode();
display.init().unwrap();

let bmp =
Bmp::from_slice(include_bytes!("./rust-pride.bmp")).expect("Failed to load BMP image");
let bmp = Bmp::from_slice(include_bytes!("./rust.bmp")).expect("Failed to load BMP image");

// The image is an RGB565 encoded BMP, so specifying the type as `Image<Bmp, Rgb565>` will read
// The image is an RGB565 encoded BMP, so specifying the type as `Image<Bmp<Rgb565>>` will read
// the pixels correctly
let im: Image<Bmp, Rgb565> = Image::new(&bmp, Point::new(32, 0));

// The display uses `BinaryColor` pixels (on/off only). Here, we `map()` over every pixel
// and naively convert the color to an on/off value. The logic below simply converts any
// color that's not black into an "on" pixel.
im.into_iter()
.map(|Pixel(position, color)| {
Pixel(
position,
if color != Rgb565::BLACK {
BinaryColor::On
} else {
BinaryColor::Off
},
)
})
.draw(&mut display)
.unwrap();
let im: Image<Bmp<Rgb565>> = Image::new(&bmp, Point::new(32, 0));

// We use the `color_converted` method here to automatically convert the RGB565 image data into
// BinaryColor values.
im.draw(&mut display.color_converted()).unwrap();

display.flush().unwrap();

Expand Down
9 changes: 4 additions & 5 deletions examples/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use cortex_m_rt::{entry, exception, ExceptionFrame};
use embedded_graphics::{
pixelcolor::BinaryColor,
prelude::*,
primitives::{Circle, Rectangle, Triangle},
style::PrimitiveStyleBuilder,
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
};
use panic_halt as _;
use ssd1306::{prelude::*, Ssd1306};
Expand Down Expand Up @@ -91,7 +90,7 @@ fn main() -> ! {
// screen outline
// default display size is 128x64 if you don't pass a _DisplaySize_
// enum to the _Builder_ struct
Rectangle::new(Point::new(0, 0), Point::new(127, 63))
Rectangle::new(Point::new(0, 0), Size::new(127, 63))
.into_styled(style)
.draw(&mut display)
.unwrap();
Expand All @@ -107,13 +106,13 @@ fn main() -> ! {
.unwrap();

// square
Rectangle::new(Point::new(52, yoffset), Point::new(52 + 16, 16 + yoffset))
Rectangle::new(Point::new(52, yoffset), Size::new_equal(16))
.into_styled(style)
.draw(&mut display)
.unwrap();

// circle
Circle::new(Point::new(96, yoffset + 8), 8)
Circle::new(Point::new(88, yoffset), 16)
.into_styled(style)
.draw(&mut display)
.unwrap();
Expand Down
9 changes: 4 additions & 5 deletions examples/graphics_i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use cortex_m_rt::{entry, exception, ExceptionFrame};
use embedded_graphics::{
pixelcolor::BinaryColor,
prelude::*,
primitives::{Circle, Rectangle, Triangle},
style::PrimitiveStyleBuilder,
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
};
use panic_halt as _;
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
Expand Down Expand Up @@ -79,7 +78,7 @@ fn main() -> ! {
// screen outline
// default display size is 128x64 if you don't pass a _DisplaySize_
// enum to the _Builder_ struct
Rectangle::new(Point::new(0, 0), Point::new(127, 63))
Rectangle::new(Point::new(0, 0), Size::new(127, 63))
.into_styled(style)
.draw(&mut display)
.unwrap();
Expand All @@ -95,13 +94,13 @@ fn main() -> ! {
.unwrap();

// square
Rectangle::new(Point::new(52, yoffset), Point::new(52 + 16, 16 + yoffset))
Rectangle::new(Point::new(52, yoffset), Size::new_equal(16))
.into_styled(style)
.draw(&mut display)
.unwrap();

// circle
Circle::new(Point::new(96, yoffset + 8), 8)
Circle::new(Point::new(88, yoffset), 16)
.into_styled(style)
.draw(&mut display)
.unwrap();
Expand Down
9 changes: 4 additions & 5 deletions examples/graphics_i2c_128x32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use cortex_m_rt::{entry, exception, ExceptionFrame};
use embedded_graphics::{
pixelcolor::BinaryColor,
prelude::*,
primitives::{Circle, Rectangle, Triangle},
style::PrimitiveStyleBuilder,
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
};
use panic_halt as _;
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
Expand Down Expand Up @@ -79,7 +78,7 @@ fn main() -> ! {
// screen outline
// default display size is 128x64 if you don't pass a _DisplaySize_
// enum to the _Builder_ struct
Rectangle::new(Point::new(0, 0), Point::new(127, 31))
Rectangle::new(Point::new(0, 0), Size::new(127, 31))
.into_styled(style)
.draw(&mut display)
.unwrap();
Expand All @@ -95,13 +94,13 @@ fn main() -> ! {
.unwrap();

// square
Rectangle::new(Point::new(52, yoffset), Point::new(52 + 16, 16 + yoffset))
Rectangle::new(Point::new(52, yoffset), Size::new_equal(16))
.into_styled(style)
.draw(&mut display)
.unwrap();

// circle
Circle::new(Point::new(96, yoffset + 8), 8)
Circle::new(Point::new(88, yoffset), 16)
.into_styled(style)
.draw(&mut display)
.unwrap();
Expand Down
9 changes: 4 additions & 5 deletions examples/graphics_i2c_72x40.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use cortex_m_rt::{entry, exception, ExceptionFrame};
use embedded_graphics::{
pixelcolor::BinaryColor,
prelude::*,
primitives::{Circle, Rectangle, Triangle},
style::PrimitiveStyleBuilder,
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
};
use panic_halt as _;
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
Expand Down Expand Up @@ -81,7 +80,7 @@ fn main() -> ! {
// screen outline
// default display size is 128x64 if you don't pass a _DisplaySize_
// enum to the _Builder_ struct
Rectangle::new(Point::new(0, 0), Point::new(71, 39))
Rectangle::with_corners(Point::new(0, 0), Point::new(71, 39))
.into_styled(style)
.draw(&mut display)
.unwrap();
Expand All @@ -101,7 +100,7 @@ fn main() -> ! {
let offset = offset + Point::new(spacing, 0);

// Draw a square
Rectangle::new(Point::new(0, 0), Point::new(size, size))
Rectangle::new(Point::new(0, 0), Size::new_equal(size as u32))
.translate(offset)
.into_styled(style)
.draw(&mut display)
Expand All @@ -111,7 +110,7 @@ fn main() -> ! {
let offset = offset + Point::new(spacing, 0);

// Circle
Circle::new(Point::new(size / 2, size / 2), size as u32 / 2)
Circle::new(Point::zero(), size as u32)
.translate(offset)
.into_styled(style)
.draw(&mut display)
Expand Down
2 changes: 1 addition & 1 deletion examples/image_i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn main() -> ! {
.into_buffered_graphics_mode();
display.init().unwrap();

let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("./rust.raw"), 64, 64);
let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("./rust.raw"), 64);

let im = Image::new(&raw, Point::new(32, 0));

Expand Down
2 changes: 1 addition & 1 deletion examples/rotation_i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn main() -> ! {

let (w, h) = display.dimensions();

let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("./rust.raw"), 64, 64);
let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("./rust.raw"), 64);

let im = Image::new(
&raw,
Expand Down
21 changes: 12 additions & 9 deletions examples/rtic_brightness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#![no_std]
#![no_main]

use core::convert::TryFrom;
use display_interface_spi::SPIInterfaceNoCS;
use embedded_graphics::{
geometry::Point, image::Image, pixelcolor::BinaryColor, prelude::*, primitives::Rectangle,
geometry::Point,
image::Image,
pixelcolor::{BinaryColor, Rgb565},
prelude::*,
primitives::{PrimitiveStyle, Rectangle},
};
use panic_halt as _;
use rtic::app;
Expand Down Expand Up @@ -49,7 +52,7 @@ const APP: () = {
timer: CountDownTimer<pac::TIM1>,
top_left: Point,
velocity: Point,
bmp: Bmp<'static>,
bmp: Bmp<Rgb565, 'static>,
brightness: Brightness,
}

Expand Down Expand Up @@ -135,13 +138,11 @@ const APP: () = {
..
} = cx.resources;

let bottom_right = *top_left + Point::try_from(bmp.dimensions()).unwrap();
let bottom_right = *top_left + bmp.bounding_box().size;

// Erase previous image position with a filled black rectangle
Rectangle::new(*top_left, bottom_right)
.into_styled(embedded_graphics::primitive_style!(
fill_color = BinaryColor::Off
))
Rectangle::with_corners(*top_left, bottom_right)
.into_styled(PrimitiveStyle::with_fill(BinaryColor::Off))
.draw(display)
.unwrap();

Expand Down Expand Up @@ -178,7 +179,9 @@ const APP: () = {
*top_left += *velocity;

// Draw image at new position
Image::new(bmp, *top_left).draw(display).unwrap();
Image::new(bmp, *top_left)
.draw(&mut display.color_converted())
.unwrap();

// Write changes to the display
display.flush().unwrap();
Expand Down
21 changes: 12 additions & 9 deletions examples/rtic_dvd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
#![no_std]
#![no_main]

use core::convert::TryFrom;
use display_interface_spi::SPIInterfaceNoCS;
use embedded_graphics::{
geometry::Point, image::Image, pixelcolor::BinaryColor, prelude::*, primitives::Rectangle,
geometry::Point,
image::Image,
pixelcolor::{BinaryColor, Rgb565},
prelude::*,
primitives::{PrimitiveStyle, Rectangle},
};
use panic_halt as _;
use rtic::app;
Expand Down Expand Up @@ -50,7 +53,7 @@ const APP: () = {
timer: CountDownTimer<pac::TIM1>,
top_left: Point,
velocity: Point,
bmp: Bmp<'static>,
bmp: Bmp<Rgb565, 'static>,
}

#[init]
Expand Down Expand Up @@ -133,13 +136,11 @@ const APP: () = {
..
} = cx.resources;

let bottom_right = *top_left + Point::try_from(bmp.dimensions()).unwrap();
let bottom_right = *top_left + bmp.bounding_box().size;

// Erase previous image position with a filled black rectangle
Rectangle::new(*top_left, bottom_right)
.into_styled(embedded_graphics::primitive_style!(
fill_color = BinaryColor::Off
))
Rectangle::with_corners(*top_left, bottom_right)
.into_styled(PrimitiveStyle::with_fill(BinaryColor::Off))
.draw(display)
.unwrap();

Expand All @@ -158,7 +159,9 @@ const APP: () = {
*top_left += *velocity;

// Draw image at new position
Image::new(bmp, *top_left).draw(display).unwrap();
Image::new(bmp, *top_left)
.draw(&mut display.color_converted())
.unwrap();

// Write changes to the display
display.flush().unwrap();
Expand Down
Binary file removed examples/rust-pride.bmp
Binary file not shown.
Binary file added examples/rust.bmp
Binary file not shown.
Loading

0 comments on commit d5e520b

Please sign in to comment.