Skip to content

Commit cec9028

Browse files
committed
chore (examples): change the library for working with graphics, check the correct operation, change the toml
1 parent 82cc6f6 commit cec9028

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ panic-semihosting = "0.5.3"
5454
cortex-m-semihosting = "0.3.3"
5555
arrayvec = { version = "0.5.1", default-features = false }
5656
panic-halt = "0.2.0"
57-
ssd1306 = "0.2.6"
58-
embedded-graphics = "0.4.9"
57+
ssd1306 = "0.3.1"
58+
embedded-graphics = "0.6.2"
5959
usb-device = "0.2.5"
6060
usbd-serial = "0.1.0"
61+
micromath = "1.0.0"
6162

6263
[features]
6364
device-selected = []
@@ -116,6 +117,10 @@ required-features = ["rt", "stm32f411"]
116117
name = "stopwatch-with-ssd1306-and-interrupts"
117118
required-features = ["rt", "stm32f411"]
118119

120+
[[example]]
121+
name = "analog-stopwatch-with-spi-ssd1306"
122+
required-features = ["rt", "stm32f429"]
123+
119124
[[example]]
120125
name = "rng-display"
121126
required-features = ["rt", "stm32f407"]

examples/rng-display.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use panic_semihosting as _;
2222

2323
use cortex_m_rt::ExceptionFrame;
2424
use cortex_m_rt::{entry, exception};
25-
use embedded_graphics::{fonts::Font6x8, prelude::*};
25+
use embedded_graphics::{fonts::Font6x8, fonts::Text, pixelcolor::BinaryColor, style::TextStyleBuilder, prelude::*};
2626

2727
use ssd1306::{prelude::*, Builder as SSD1306Builder};
2828

@@ -78,17 +78,22 @@ fn main() -> ! {
7878
let mut rand_source = dp.RNG.constrain(clocks);
7979
let mut format_buf = ArrayString::<[u8; 20]>::new();
8080
loop {
81+
//display clear
82+
disp.clear();
83+
8184
//this will continuously report an error if RNG_CLK < HCLK/16
8285
let rand_val = rand_source.next_u32();
8386

8487
format_buf.clear();
8588
if fmt::write(&mut format_buf, format_args!("{}", rand_val)).is_ok() {
86-
disp.draw(
87-
Font6x8::render_str(format_buf.as_str())
88-
.with_stroke(Some(1u8.into()))
89-
.translate(Coord::new(HINSET_PIX, VCENTER_PIX))
90-
.into_iter(),
91-
);
89+
90+
let text_style = TextStyleBuilder::new(Font6x8)
91+
.text_color(BinaryColor::On)
92+
.build();
93+
94+
Text::new(format_buf.as_str(), Point::new(HINSET_PIX, VCENTER_PIX))
95+
.into_styled(text_style)
96+
.draw(&mut disp).unwrap();
9297
}
9398
disp.flush().unwrap();
9499
//delay a little while between refreshes so the display is readable

examples/ssd1306-image.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern crate stm32f4xx_hal as hal;
1919

2020
use cortex_m_rt::ExceptionFrame;
2121
use cortex_m_rt::{entry, exception};
22-
use embedded_graphics::{image::Image1BPP, prelude::*};
22+
use embedded_graphics::{image::Image, image::ImageRaw, pixelcolor::BinaryColor, prelude::*};
2323
use ssd1306::{prelude::*, Builder as SSD1306Builder};
2424

2525
use crate::hal::{i2c::I2c, prelude::*, stm32};
@@ -53,8 +53,9 @@ fn main() -> ! {
5353
disp.flush().unwrap();
5454

5555
// Display the rustacean
56-
let im = Image1BPP::new(include_bytes!("./ssd1306-image.data"), 128, 64);
57-
disp.draw(im.into_iter());
56+
let raw_image: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("./ssd1306-image.data"), 128, 64);
57+
let image: Image<_, BinaryColor> = Image::new(&raw_image, Point::zero());
58+
image.draw(&mut disp).unwrap();
5859
disp.flush().unwrap();
5960

6061
// Set up state for the loop

examples/stopwatch-with-ssd1306-and-interrupts.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ use core::ops::DerefMut;
3737
use cortex_m::interrupt::{free, CriticalSection, Mutex};
3838
use cortex_m_rt::entry;
3939
use embedded_graphics::{
40-
fonts::{Font12x16, Font6x12},
40+
fonts::{Font12x16, Font6x12, Text},
4141
prelude::*,
42+
pixelcolor::BinaryColor,
43+
style::TextStyleBuilder,
4244
};
43-
use ssd1306::{prelude::*, Builder as SSD1306Builder};
45+
use ssd1306::{mode::GraphicsMode, Builder as SSD1306Builder};
4446

4547
// Set up global state. It's all mutexed up for concurrency safety.
4648
static ELAPSED_MS: Mutex<Cell<u32>> = Mutex::new(Cell::new(0u32));
@@ -116,15 +118,22 @@ fn main() -> ! {
116118
StopwatchState::Running => "",
117119
StopwatchState::Stopped => "Stopped",
118120
};
119-
let state_text = Font6x12::render_str(state_msg)
120-
.with_stroke(Some(1u8.into()))
121-
.translate(Coord::new(0, 0));
122-
disp.draw(state_text.into_iter());
123-
124-
let elapsed_text = Font12x16::render_str(format_buf.as_str())
125-
.with_stroke(Some(1u8.into()))
126-
.translate(Coord::new(0, 14));
127-
disp.draw(elapsed_text.into_iter());
121+
122+
let text_style = TextStyleBuilder::new(Font6x12)
123+
.text_color(BinaryColor::On)
124+
.build();
125+
126+
let text_style_format_buf = TextStyleBuilder::new(Font12x16)
127+
.text_color(BinaryColor::On)
128+
.build();
129+
130+
Text::new(state_msg, Point::new(0, 0))
131+
.into_styled(text_style)
132+
.draw(&mut disp).unwrap();
133+
134+
Text::new(format_buf.as_str(), Point::new(0, 14))
135+
.into_styled(text_style_format_buf)
136+
.draw(&mut disp).unwrap();
128137

129138
disp.flush().unwrap();
130139

src/dwt.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ impl Delay {
7272
/// Delay ticks
7373
/// NOTE DCB and DWT need to be set up for this to work, so it is private
7474
fn delay_ticks(mut start: u32, ticks: u64) {
75-
if ticks < (u32::MAX / 2) as u64 {
75+
if ticks < (core::u32::MAX / 2) as u64 {
7676
// Simple delay
7777
let ticks = ticks as u32;
7878
while (DWT::get_cycle_count().wrapping_sub(start)) < ticks {}
79-
} else if ticks <= u32::MAX as u64 {
79+
} else if ticks <= core::u32::MAX as u64 {
8080
// Try to avoid race conditions by limiting delay to u32::MAX / 2
8181
let mut ticks = ticks as u32;
82-
ticks -= u32::MAX / 2;
83-
while (DWT::get_cycle_count().wrapping_sub(start)) < u32::MAX / 2 {}
84-
start -= u32::MAX / 2;
82+
ticks -= core::u32::MAX / 2;
83+
while (DWT::get_cycle_count().wrapping_sub(start)) < core::u32::MAX / 2 {}
84+
start -= core::u32::MAX / 2;
8585
while (DWT::get_cycle_count().wrapping_sub(start)) < ticks {}
8686
} else {
8787
// Delay for ticks, then delay for rest * u32::MAX
8888
let mut rest = (ticks >> 32) as u32;
89-
let ticks = (ticks & u32::MAX as u64) as u32;
89+
let ticks = (ticks & core::u32::MAX as u64) as u32;
9090
loop {
9191
while (DWT::get_cycle_count().wrapping_sub(start)) < ticks {}
9292
if rest == 0 {

0 commit comments

Comments
 (0)