Skip to content

Commit 7ef2956

Browse files
committed
chore: reformat it, add an example entry to https://github.com/stm32-rs/stm32f4xx-hal/blob/master/Cargo.toml
1 parent cec9028 commit 7ef2956

4 files changed

+79
-68
lines changed

examples/analog-stopwatch-with-spi-ssd1306.rs

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ extern crate panic_semihosting;
1313
extern crate stm32f4xx_hal as hal;
1414

1515
use crate::hal::{
16-
prelude::*,
17-
spi::Spi,
18-
interrupt,
1916
gpio::{gpioa::PA0, Edge, ExtiPin, Input, PullDown},
20-
timer::{Event, Timer},
17+
interrupt,
18+
prelude::*,
2119
rcc::{Clocks, Rcc},
20+
spi::Spi,
2221
stm32,
22+
timer::{Event, Timer},
2323
};
2424

2525
use arrayvec::ArrayString;
@@ -30,21 +30,21 @@ use cortex_m::interrupt::{free, CriticalSection, Mutex};
3030

3131
use hal::spi::{Mode, Phase, Polarity};
3232

33-
use micromath::F32Ext;
34-
use cortex_m_rt::{ExceptionFrame, entry, exception};
33+
use core::f32::consts::{FRAC_PI_2, PI};
34+
use cortex_m_rt::{entry, exception, ExceptionFrame};
3535
use embedded_graphics::{
3636
egcircle,
37+
fonts::{Font6x8, Text},
38+
pixelcolor::BinaryColor,
3739
prelude::*,
38-
fonts::{Font6x8, Text},
3940
primitive_style,
4041
primitives::{Circle, Line},
41-
pixelcolor::BinaryColor,
4242
style::TextStyleBuilder,
4343
style::{PrimitiveStyle, PrimitiveStyleBuilder},
4444
};
45-
use core::f32::consts::{FRAC_PI_2, PI};
45+
use micromath::F32Ext;
4646

47-
use ssd1306::{ mode::GraphicsMode, Builder as SSD1306Builder};
47+
use ssd1306::{mode::GraphicsMode, Builder as SSD1306Builder};
4848
use stm32f4::stm32f429;
4949

5050
// Set up global state. It's all mutexed up for concurrency safety.
@@ -72,17 +72,16 @@ enum StopwatchState {
7272

7373
#[entry]
7474
fn main() -> ! {
75-
7675
let mut dp = stm32f429::Peripherals::take().unwrap();
77-
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
76+
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
7877
dp.RCC.apb2enr.write(|w| w.syscfgen().enabled());
7978

8079
let rcc = dp.RCC.constrain();
8180

8281
let clocks = setup_clocks(rcc);
8382

8483
let gpioa = dp.GPIOA.split();
85-
let gpioe = dp.GPIOE.split();
84+
let gpioe = dp.GPIOE.split();
8685

8786
let mut board_btn = gpioa.pa0.into_pull_down_input();
8887
board_btn.make_interrupt_source(&mut dp.SYSCFG);
@@ -100,10 +99,16 @@ fn main() -> ! {
10099
let miso = gpioe.pe5.into_alternate_af5();
101100
let mosi = gpioe.pe6.into_alternate_af5();
102101

103-
let spi = Spi::spi4(dp.SPI4, (sck, miso, mosi), Mode {
104-
polarity: Polarity::IdleLow,
105-
phase: Phase::CaptureOnFirstTransition,
106-
}, stm32f4xx_hal::time::KiloHertz(2000).into(),clocks);
102+
let spi = Spi::spi4(
103+
dp.SPI4,
104+
(sck, miso, mosi),
105+
Mode {
106+
polarity: Polarity::IdleLow,
107+
phase: Phase::CaptureOnFirstTransition,
108+
},
109+
stm32f4xx_hal::time::KiloHertz(2000).into(),
110+
clocks,
111+
);
107112

108113
// Set up the LEDs. On the stm32f429i-disco they are connected to pin PG13 and PG14.
109114
let gpiog = dp.GPIOG.split();
@@ -123,25 +128,24 @@ fn main() -> ! {
123128
disp.init().unwrap();
124129
disp.flush().unwrap();
125130

126-
// Create a 1ms periodic interrupt from TIM2
127-
let mut timer = Timer::tim2(dp.TIM2, 1.khz(), clocks);
128-
timer.listen(Event::TimeOut);
131+
// Create a 1ms periodic interrupt from TIM2
132+
let mut timer = Timer::tim2(dp.TIM2, 1.khz(), clocks);
133+
timer.listen(Event::TimeOut);
129134

130-
free(|cs| {
131-
TIMER_TIM2.borrow(cs).replace(Some(timer));
132-
BUTTON.borrow(cs).replace(Some(board_btn));
133-
});
135+
free(|cs| {
136+
TIMER_TIM2.borrow(cs).replace(Some(timer));
137+
BUTTON.borrow(cs).replace(Some(board_btn));
138+
});
134139

135-
// Enable interrupts
136-
stm32::NVIC::unpend(hal::stm32::Interrupt::TIM2);
137-
stm32::NVIC::unpend(hal::stm32::Interrupt::EXTI0);
138-
unsafe {
139-
stm32::NVIC::unmask(hal::stm32::Interrupt::EXTI0);
140-
};
140+
// Enable interrupts
141+
stm32::NVIC::unpend(hal::stm32::Interrupt::TIM2);
142+
stm32::NVIC::unpend(hal::stm32::Interrupt::EXTI0);
143+
unsafe {
144+
stm32::NVIC::unmask(hal::stm32::Interrupt::EXTI0);
145+
};
141146

142147
let mut state_led = false;
143-
loop{
144-
148+
loop {
145149
let elapsed = free(|cs| ELAPSED_MS.borrow(cs).get());
146150

147151
let mut format_buf = ArrayString::<[u8; 10]>::new();
@@ -162,37 +166,41 @@ fn main() -> ! {
162166
StopwatchState::Ready => {
163167
led3.set_high().unwrap();
164168
led4.set_low().unwrap();
165-
},
169+
}
166170
StopwatchState::Running => {
167171
if state_led {
168172
led4.set_low().unwrap();
169173
led3.set_high().unwrap();
170174
} else {
171175
led4.set_low().unwrap();
172-
led3.set_low().unwrap();
176+
led3.set_low().unwrap();
173177
}
174-
},
178+
}
175179
StopwatchState::Stopped => {
176180
led3.set_low().unwrap();
177181
led4.set_high().unwrap();
178-
},
182+
}
179183
};
180184

181185
let text_style = TextStyleBuilder::new(Font6x8)
182186
.text_color(BinaryColor::On)
183187
.build();
184-
188+
185189
Text::new(state_msg, Point::new(0, 0))
186190
.into_styled(text_style)
187-
.draw(&mut disp).unwrap();
188-
189-
Text::new(format_buf.as_str(), Point::new((128 / 2) - 1, 0 ))
191+
.draw(&mut disp)
192+
.unwrap();
193+
194+
Text::new(format_buf.as_str(), Point::new((128 / 2) - 1, 0))
190195
.into_styled(text_style)
191-
.draw(&mut disp).unwrap();
196+
.draw(&mut disp)
197+
.unwrap();
192198

193199
draw_face().draw(&mut disp).unwrap();
194-
draw_seconds_hand(elapsed_to_s(elapsed)).draw(&mut disp).unwrap();
195-
200+
draw_seconds_hand(elapsed_to_s(elapsed))
201+
.draw(&mut disp)
202+
.unwrap();
203+
196204
disp.flush().unwrap();
197205

198206
delay.delay_ms(100u32);
@@ -223,7 +231,7 @@ fn EXTI0() {
223231
// cases but this one only starts and stops TIM2 interrupts
224232
match state {
225233
StopwatchState::Ready => {
226-
ELAPSED_RESET_MS.borrow(cs).replace(0);
234+
ELAPSED_RESET_MS.borrow(cs).replace(0);
227235
stopwatch_start(cs);
228236
STATE.borrow(cs).replace(StopwatchState::Running);
229237
}
@@ -236,17 +244,17 @@ fn EXTI0() {
236244
StopwatchState::Stopped => {
237245
let cell_reset = ELAPSED_RESET_MS.borrow(cs);
238246
let val_reset = cell_reset.get();
239-
240-
if val_reset > 500_u32{
247+
248+
if val_reset > 500_u32 {
241249
ELAPSED_MS.borrow(cs).replace(0);
242250
stopwatch_reset_stop(cs);
243251
STATE.borrow(cs).replace(StopwatchState::Ready);
244-
} else{
252+
} else {
245253
stopwatch_reset_stop(cs);
246254
stopwatch_continue(cs);
247255
STATE.borrow(cs).replace(StopwatchState::Running);
248-
}
249-
}
256+
}
257+
}
250258
}
251259
}
252260
});
@@ -261,22 +269,21 @@ fn TIM2() {
261269

262270
let cell = ELAPSED_MS.borrow(cs);
263271
let cell_reset = ELAPSED_RESET_MS.borrow(cs);
264-
let val = cell.get();
272+
let val = cell.get();
265273
let val_reset = cell_reset.get();
266274

267275
match STATE.borrow(cs).get() {
268-
StopwatchState::Ready => {
276+
StopwatchState::Ready => {
269277
cell.replace(val + 1);
270278
}
271279
StopwatchState::Running => {
272280
cell.replace(val + 1);
273281
}
274282
StopwatchState::Stopped => {
275-
276283
let mut btn_ref = BUTTON.borrow(cs).borrow_mut();
277284
if let Some(ref mut btn) = btn_ref.deref_mut() {
278285
if btn.is_high().unwrap() {
279-
cell_reset.replace(val_reset + 1);
286+
cell_reset.replace(val_reset + 1);
280287
}
281288
}
282289
}
@@ -403,4 +410,3 @@ fn draw_seconds_hand(seconds: u32) -> impl Iterator<Item = Pixel<BinaryColor>> {
403410
fn HardFault(ef: &ExceptionFrame) -> ! {
404411
panic!("{:#?}", ef);
405412
}
406-

examples/rng-display.rs

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

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

2729
use ssd1306::{prelude::*, Builder as SSD1306Builder};
2830

@@ -80,20 +82,20 @@ fn main() -> ! {
8082
loop {
8183
//display clear
8284
disp.clear();
83-
85+
8486
//this will continuously report an error if RNG_CLK < HCLK/16
8587
let rand_val = rand_source.next_u32();
8688

8789
format_buf.clear();
8890
if fmt::write(&mut format_buf, format_args!("{}", rand_val)).is_ok() {
89-
9091
let text_style = TextStyleBuilder::new(Font6x8)
9192
.text_color(BinaryColor::On)
9293
.build();
93-
94+
9495
Text::new(format_buf.as_str(), Point::new(HINSET_PIX, VCENTER_PIX))
9596
.into_styled(text_style)
96-
.draw(&mut disp).unwrap();
97+
.draw(&mut disp)
98+
.unwrap();
9799
}
98100
disp.flush().unwrap();
99101
//delay a little while between refreshes so the display is readable

examples/ssd1306-image.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ fn main() -> ! {
5353
disp.flush().unwrap();
5454

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

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ use cortex_m::interrupt::{free, CriticalSection, Mutex};
3838
use cortex_m_rt::entry;
3939
use embedded_graphics::{
4040
fonts::{Font12x16, Font6x12, Text},
41+
pixelcolor::BinaryColor,
4142
prelude::*,
42-
pixelcolor::BinaryColor,
4343
style::TextStyleBuilder,
4444
};
4545
use ssd1306::{mode::GraphicsMode, Builder as SSD1306Builder};
@@ -118,22 +118,24 @@ fn main() -> ! {
118118
StopwatchState::Running => "",
119119
StopwatchState::Stopped => "Stopped",
120120
};
121-
121+
122122
let text_style = TextStyleBuilder::new(Font6x12)
123123
.text_color(BinaryColor::On)
124124
.build();
125-
125+
126126
let text_style_format_buf = TextStyleBuilder::new(Font12x16)
127127
.text_color(BinaryColor::On)
128128
.build();
129-
129+
130130
Text::new(state_msg, Point::new(0, 0))
131-
.into_styled(text_style)
132-
.draw(&mut disp).unwrap();
133-
131+
.into_styled(text_style)
132+
.draw(&mut disp)
133+
.unwrap();
134+
134135
Text::new(format_buf.as_str(), Point::new(0, 14))
135136
.into_styled(text_style_format_buf)
136-
.draw(&mut disp).unwrap();
137+
.draw(&mut disp)
138+
.unwrap();
137139

138140
disp.flush().unwrap();
139141

0 commit comments

Comments
 (0)