Skip to content

Commit 6a01c88

Browse files
committedNov 16, 2024
OLED display
1 parent b2dd3e4 commit 6a01c88

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed
 

‎Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ stm32f1xx-hal = { version = "0.10.0", features = ["stm32f103", "rt"] }
2121
# provies a panic-handler (halting cpu)
2222
# (required when not using stdlib)
2323
panic-halt = "1.0.0"
24+
25+
embedded-graphics = "0.8.1"
26+
sh1106 = "0.5.0"

‎src/main.rs

+48-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ extern crate panic_halt;
88

99
use cortex_m_rt::entry;
1010
use nb::block;
11-
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
11+
use stm32f1xx_hal::{
12+
i2c::{BlockingI2c, DutyCycle, Mode},
13+
pac,
14+
prelude::*,
15+
timer::Timer,
16+
};
17+
18+
use embedded_graphics::{
19+
mono_font::{ascii::FONT_6X10, MonoTextStyle},
20+
pixelcolor::BinaryColor,
21+
prelude::*,
22+
text::Text,
23+
};
24+
use sh1106::{prelude::*, Builder};
1225

1326
// use `main` as the entry point of this application
1427
#[entry]
@@ -25,6 +38,8 @@ fn main() -> ! {
2538
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in `clocks`
2639
let clocks = rcc.cfgr.freeze(&mut flash.acr);
2740

41+
let mut afio = dp.AFIO.constrain();
42+
2843
// Configure GPIOA PA0 and PA2 as output (push-pull)
2944
let mut gpioa = dp.GPIOA.split();
3045
let mut led_a0 = gpioa.pa0.into_push_pull_output(&mut gpioa.crl);
@@ -35,6 +50,36 @@ fn main() -> ! {
3550
let button_b1 = gpiob.pb1.into_floating_input(&mut gpiob.crl);
3651
let button_b11 = gpiob.pb11.into_floating_input(&mut gpiob.crh);
3752

53+
let scl = gpiob.pb8.into_alternate_open_drain(&mut gpiob.crh);
54+
let sda = gpiob.pb9.into_alternate_open_drain(&mut gpiob.crh);
55+
56+
let i2c = BlockingI2c::i2c1(
57+
dp.I2C1,
58+
(scl, sda),
59+
&mut afio.mapr,
60+
Mode::Fast {
61+
frequency: 100.kHz().into(),
62+
duty_cycle: DutyCycle::Ratio2to1,
63+
},
64+
clocks,
65+
1000,
66+
10,
67+
1000,
68+
1000,
69+
);
70+
71+
let mut display: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();
72+
73+
display.init().unwrap();
74+
display.flush().unwrap();
75+
76+
let style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
77+
Text::new("Hello world!", Point::zero(), style)
78+
.draw(&mut display)
79+
.unwrap();
80+
81+
display.flush().unwrap();
82+
3883
// Configure the syst timer to trigger an update every 0.1 second
3984
let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
4085
timer.start(100.Hz()).unwrap();
@@ -46,26 +91,14 @@ fn main() -> ! {
4691
if button_b1.is_low() {
4792
while button_b1.is_low() {} // do nothing while button is held down
4893

49-
if led_a0.is_set_low() {
50-
// if PA0 is currently on
51-
led_a0.set_high(); // Set PA0 to high, turning it off
52-
} else {
53-
// if PA0 is currently off
54-
led_a0.set_low(); // Set PA0 to low, turning it on
55-
}
94+
led_a0.toggle() // Set PA0 to the opposite state
5695
}
5796

5897
// read PB11
5998
if button_b11.is_low() {
6099
while button_b11.is_low() {} // do nothing while button is held down
61100

62-
if led_a2.is_set_low() {
63-
// if PA2 is currently on
64-
led_a2.set_high(); // Set PA2 to high, turning it off
65-
} else {
66-
// if PA2 is currently off
67-
led_a2.set_low(); // Set PA2 to low, turning it on
68-
}
101+
led_a2.toggle() // Set PA0 to the opposite state
69102
}
70103
}
71104
}

0 commit comments

Comments
 (0)
Please sign in to comment.