Skip to content

Commit d5e520b

Browse files
authored
Upgrade to embedded-graphics 0.7 (#154)
* 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.
1 parent bd30333 commit d5e520b

17 files changed

+97
-102
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
## [Unreleased] - ReleaseDate
88

9+
### Changed
10+
11+
- **(breaking)** [#154](https://github.com/jamwaffles/ssd1306/pull/154) Upgrade to `embedded-graphics` 0.7.
912
- **(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.
1013
- **(breaking)** [#150](https://github.com/jamwaffles/ssd1306/pull/150) `display_on` is now called `set_display_on`.
1114
- **(breaking)** [#150](https://github.com/jamwaffles/ssd1306/pull/150) `TerminalMode::get_position` is now called `position` to conform with Rust API guidelines.

Cargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,16 @@ generic-array = "0.14.2"
2727

2828
[dependencies.embedded-graphics]
2929
optional = true
30-
version = "0.6.0"
30+
version = "0.7.0"
3131

3232
[dev-dependencies]
3333
cortex-m = "0.7.2"
3434
cortex-m-rt = "0.6.12"
3535
cortex-m-rtic = "0.5.3"
3636
panic-halt = "0.2.0"
3737
cast = { version = "0.2.6", default-features = false }
38-
3938
# Used to load BMP images in various examples
40-
[dev-dependencies.tinybmp]
41-
version = "0.2.2"
42-
# Enable embedded-graphics integration
43-
features = [ "graphics" ]
39+
tinybmp = "0.3.0"
4440

4541
# Used by the noise_i2c examples
4642
[dev-dependencies.rand]

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn main() -> ! {
7373
.into_buffered_graphics_mode();
7474
display.init().unwrap();
7575

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

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

@@ -88,7 +88,6 @@ fn main() -> ! {
8888
fn HardFault(ef: &ExceptionFrame) -> ! {
8989
panic!("{:#?}", ef);
9090
}
91-
9291
```
9392

9493
## License

examples/bmp_i2c.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
#![no_main]
2323

2424
use cortex_m_rt::{entry, exception, ExceptionFrame};
25-
use embedded_graphics::{
26-
image::Image,
27-
pixelcolor::{BinaryColor, Rgb565},
28-
prelude::*,
29-
};
25+
use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*};
3026
use panic_halt as _;
3127
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
3228
use stm32f1xx_hal::{
@@ -73,29 +69,15 @@ fn main() -> ! {
7369
.into_buffered_graphics_mode();
7470
display.init().unwrap();
7571

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

79-
// The image is an RGB565 encoded BMP, so specifying the type as `Image<Bmp, Rgb565>` will read
74+
// The image is an RGB565 encoded BMP, so specifying the type as `Image<Bmp<Rgb565>>` will read
8075
// the pixels correctly
81-
let im: Image<Bmp, Rgb565> = Image::new(&bmp, Point::new(32, 0));
82-
83-
// The display uses `BinaryColor` pixels (on/off only). Here, we `map()` over every pixel
84-
// and naively convert the color to an on/off value. The logic below simply converts any
85-
// color that's not black into an "on" pixel.
86-
im.into_iter()
87-
.map(|Pixel(position, color)| {
88-
Pixel(
89-
position,
90-
if color != Rgb565::BLACK {
91-
BinaryColor::On
92-
} else {
93-
BinaryColor::Off
94-
},
95-
)
96-
})
97-
.draw(&mut display)
98-
.unwrap();
76+
let im: Image<Bmp<Rgb565>> = Image::new(&bmp, Point::new(32, 0));
77+
78+
// We use the `color_converted` method here to automatically convert the RGB565 image data into
79+
// BinaryColor values.
80+
im.draw(&mut display.color_converted()).unwrap();
9981

10082
display.flush().unwrap();
10183

examples/graphics.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ use cortex_m_rt::{entry, exception, ExceptionFrame};
2424
use embedded_graphics::{
2525
pixelcolor::BinaryColor,
2626
prelude::*,
27-
primitives::{Circle, Rectangle, Triangle},
28-
style::PrimitiveStyleBuilder,
27+
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
2928
};
3029
use panic_halt as _;
3130
use ssd1306::{prelude::*, Ssd1306};
@@ -91,7 +90,7 @@ fn main() -> ! {
9190
// screen outline
9291
// default display size is 128x64 if you don't pass a _DisplaySize_
9392
// enum to the _Builder_ struct
94-
Rectangle::new(Point::new(0, 0), Point::new(127, 63))
93+
Rectangle::new(Point::new(0, 0), Size::new(127, 63))
9594
.into_styled(style)
9695
.draw(&mut display)
9796
.unwrap();
@@ -107,13 +106,13 @@ fn main() -> ! {
107106
.unwrap();
108107

109108
// square
110-
Rectangle::new(Point::new(52, yoffset), Point::new(52 + 16, 16 + yoffset))
109+
Rectangle::new(Point::new(52, yoffset), Size::new_equal(16))
111110
.into_styled(style)
112111
.draw(&mut display)
113112
.unwrap();
114113

115114
// circle
116-
Circle::new(Point::new(96, yoffset + 8), 8)
115+
Circle::new(Point::new(88, yoffset), 16)
117116
.into_styled(style)
118117
.draw(&mut display)
119118
.unwrap();

examples/graphics_i2c.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use cortex_m_rt::{entry, exception, ExceptionFrame};
2121
use embedded_graphics::{
2222
pixelcolor::BinaryColor,
2323
prelude::*,
24-
primitives::{Circle, Rectangle, Triangle},
25-
style::PrimitiveStyleBuilder,
24+
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
2625
};
2726
use panic_halt as _;
2827
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
@@ -79,7 +78,7 @@ fn main() -> ! {
7978
// screen outline
8079
// default display size is 128x64 if you don't pass a _DisplaySize_
8180
// enum to the _Builder_ struct
82-
Rectangle::new(Point::new(0, 0), Point::new(127, 63))
81+
Rectangle::new(Point::new(0, 0), Size::new(127, 63))
8382
.into_styled(style)
8483
.draw(&mut display)
8584
.unwrap();
@@ -95,13 +94,13 @@ fn main() -> ! {
9594
.unwrap();
9695

9796
// square
98-
Rectangle::new(Point::new(52, yoffset), Point::new(52 + 16, 16 + yoffset))
97+
Rectangle::new(Point::new(52, yoffset), Size::new_equal(16))
9998
.into_styled(style)
10099
.draw(&mut display)
101100
.unwrap();
102101

103102
// circle
104-
Circle::new(Point::new(96, yoffset + 8), 8)
103+
Circle::new(Point::new(88, yoffset), 16)
105104
.into_styled(style)
106105
.draw(&mut display)
107106
.unwrap();

examples/graphics_i2c_128x32.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use cortex_m_rt::{entry, exception, ExceptionFrame};
2121
use embedded_graphics::{
2222
pixelcolor::BinaryColor,
2323
prelude::*,
24-
primitives::{Circle, Rectangle, Triangle},
25-
style::PrimitiveStyleBuilder,
24+
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
2625
};
2726
use panic_halt as _;
2827
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
@@ -79,7 +78,7 @@ fn main() -> ! {
7978
// screen outline
8079
// default display size is 128x64 if you don't pass a _DisplaySize_
8180
// enum to the _Builder_ struct
82-
Rectangle::new(Point::new(0, 0), Point::new(127, 31))
81+
Rectangle::new(Point::new(0, 0), Size::new(127, 31))
8382
.into_styled(style)
8483
.draw(&mut display)
8584
.unwrap();
@@ -95,13 +94,13 @@ fn main() -> ! {
9594
.unwrap();
9695

9796
// square
98-
Rectangle::new(Point::new(52, yoffset), Point::new(52 + 16, 16 + yoffset))
97+
Rectangle::new(Point::new(52, yoffset), Size::new_equal(16))
9998
.into_styled(style)
10099
.draw(&mut display)
101100
.unwrap();
102101

103102
// circle
104-
Circle::new(Point::new(96, yoffset + 8), 8)
103+
Circle::new(Point::new(88, yoffset), 16)
105104
.into_styled(style)
106105
.draw(&mut display)
107106
.unwrap();

examples/graphics_i2c_72x40.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use cortex_m_rt::{entry, exception, ExceptionFrame};
2121
use embedded_graphics::{
2222
pixelcolor::BinaryColor,
2323
prelude::*,
24-
primitives::{Circle, Rectangle, Triangle},
25-
style::PrimitiveStyleBuilder,
24+
primitives::{Circle, PrimitiveStyleBuilder, Rectangle, Triangle},
2625
};
2726
use panic_halt as _;
2827
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
@@ -81,7 +80,7 @@ fn main() -> ! {
8180
// screen outline
8281
// default display size is 128x64 if you don't pass a _DisplaySize_
8382
// enum to the _Builder_ struct
84-
Rectangle::new(Point::new(0, 0), Point::new(71, 39))
83+
Rectangle::with_corners(Point::new(0, 0), Point::new(71, 39))
8584
.into_styled(style)
8685
.draw(&mut display)
8786
.unwrap();
@@ -101,7 +100,7 @@ fn main() -> ! {
101100
let offset = offset + Point::new(spacing, 0);
102101

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

113112
// Circle
114-
Circle::new(Point::new(size / 2, size / 2), size as u32 / 2)
113+
Circle::new(Point::zero(), size as u32)
115114
.translate(offset)
116115
.into_styled(style)
117116
.draw(&mut display)

examples/image_i2c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn main() -> ! {
7474
.into_buffered_graphics_mode();
7575
display.init().unwrap();
7676

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

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

examples/rotation_i2c.rs

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

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

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

8585
let im = Image::new(
8686
&raw,

0 commit comments

Comments
 (0)