Skip to content

Commit de35cce

Browse files
committed
i2c: implement embedded-hal 1 traits
1 parent 7ce7760 commit de35cce

File tree

4 files changed

+216
-127
lines changed

4 files changed

+216
-127
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ cortex-m-log = { version = "0.7", features = ["log-integration"] }
7474
cfg-if = "0.1.10"
7575
mpu6050 = "0.1.4"
7676
bme680 = "0.6.0"
77+
sh1106 = { git = "https://github.com/techmccat/sh1106.git", branch = "hal-1", version = "0.5.0" }
78+
embedded-graphics = "0.8.0"
7779
embedded-sdmmc = "0.3.0"
7880

7981
#TODO: Separate feature sets

examples/i2c-sh1106.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#![deny(warnings)]
2+
#![deny(unsafe_code)]
3+
#![no_main]
4+
#![no_std]
5+
6+
use embedded_graphics::mono_font::MonoTextStyle;
7+
use hal::i2c::Config;
8+
use hal::prelude::*;
9+
use hal::stm32;
10+
use hal::time::RateExtU32;
11+
use stm32g4xx_hal as hal;
12+
13+
use cortex_m_rt::entry;
14+
15+
use sh1106::{prelude::*, Builder};
16+
use embedded_graphics::{
17+
mono_font::ascii::FONT_6X10,
18+
pixelcolor::BinaryColor,
19+
prelude::*,
20+
text::Text,
21+
};
22+
23+
#[macro_use]
24+
mod utils;
25+
26+
#[entry]
27+
fn main() -> ! {
28+
utils::logger::init();
29+
30+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
31+
let mut rcc = dp.RCC.constrain();
32+
let gpiob = dp.GPIOB.split(&mut rcc);
33+
34+
let sda = gpiob.pb9.into_alternate_open_drain();
35+
let scl = gpiob.pb8.into_alternate_open_drain();
36+
37+
let i2c = dp.I2C1.i2c(sda, scl, Config::new(100.kHz()), &mut rcc);
38+
// Alternatively, it is possible to specify the exact timing as follows (see the documentation
39+
// of with_timing() for an explanation of the constant):
40+
//let mut i2c = dp
41+
// .I2C1
42+
// .i2c(sda, scl, Config::with_timing(0x3042_0f13), &mut rcc);
43+
44+
let mut disp: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into();
45+
disp.init().unwrap();
46+
disp.flush().unwrap();
47+
48+
let style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
49+
Text::new("Hello Rust!", Point::new(16, 16), style)
50+
.draw(&mut disp)
51+
.unwrap();
52+
53+
disp.flush().unwrap();
54+
55+
loop {}
56+
}

examples/i2c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use hal::time::RateExtU32;
1010
use stm32g4xx_hal as hal;
1111

1212
use cortex_m_rt::entry;
13-
use log::info;
1413

1514
#[macro_use]
1615
mod utils;
16+
use utils::logger::info;
1717

1818
#[entry]
1919
fn main() -> ! {

0 commit comments

Comments
 (0)