-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshtc3_and_icm42670.rs
More file actions
78 lines (62 loc) · 2.38 KB
/
shtc3_and_icm42670.rs
File metadata and controls
78 lines (62 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#![no_std]
#![no_main]
use core::cell::RefCell;
use embassy_executor::Spawner;
use embassy_time::Timer;
use embedded_hal_bus::i2c::RefCellDevice;
use embedded_io::Write;
use esp_hal::{
i2c::master::I2c, interrupt::software::SoftwareInterruptControl, timer::timg::TimerGroup,
};
use icm42670::{Address, Icm42670, prelude::*};
use defmt::info;
use esp_backtrace as _;
use esp_println as _;
esp_bootloader_esp_idf::esp_app_desc!();
#[esp_rtos::main]
async fn main(_spawner: Spawner) {
info!("Embassy blinky example started!");
// Inititalize HAL and obtain peripherals object
let peripherals = esp_hal::init(esp_hal::Config::default());
// Configure esp_rtos (similar to embassy-executor for Arm)
// This lets us run async code
let sw_int = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
let timg0 = TimerGroup::new(peripherals.TIMG0);
esp_rtos::start(timg0.timer0, sw_int.software_interrupt0);
let i2c = I2c::new(peripherals.I2C0, esp_hal::i2c::master::Config::default())
.unwrap()
.with_sda(peripherals.GPIO10)
.with_scl(peripherals.GPIO8);
let i2c_ref_cell = RefCell::new(i2c);
let mut icm = Icm42670::new(RefCellDevice::new(&i2c_ref_cell), Address::Primary).unwrap();
let mut sht = shtcx::shtc3(RefCellDevice::new(&i2c_ref_cell));
loop {
let accel_norm = icm.accel_norm().unwrap();
let gyro_norm = icm.gyro_norm().unwrap();
let mut buf = [0u8; 1000];
// Use write! instead of defmt for float precision settings
write!(
&mut buf[..],
"ACCEL = X: {:+.04} Y: {:+.04} Z: {:+.04}\tGYRO = X: {:+.04} Y: {:+.04} Z: {:+.04}\tTEMP = {:+0.2}",
accel_norm.x,
accel_norm.y,
accel_norm.z,
gyro_norm.x,
gyro_norm.y,
gyro_norm.z,
icm.temperature().unwrap()
).unwrap();
info!("{}", str::from_utf8(&buf).unwrap());
match sht.measure(shtcx::PowerMode::NormalMode, &mut embassy_time::Delay) {
Ok(measurement) => {
info!(
"SHT Temperature: {} °C, Humidity: {} %",
measurement.temperature.as_degrees_celsius(),
measurement.humidity.as_percent()
);
}
Err(_) => info!("Error reading SHTC3"),
}
Timer::after_millis(500).await;
}
}