Implement sensor reading
This commit is contained in:
parent
117a9816ed
commit
988204d9fa
70
src/main.rs
70
src/main.rs
@ -1,26 +1,76 @@
|
|||||||
|
//! Example on how to read a 24C/24LC i2c eeprom.
|
||||||
|
//!
|
||||||
|
//! Connect SDA to P0.03, SCL to P0.04
|
||||||
|
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use defmt::info;
|
|
||||||
use embassy_executor::Spawner;
|
|
||||||
use embassy_nrf::gpio::{Level, Output, OutputDrive};
|
|
||||||
use embassy_time::Timer;
|
use embassy_time::Timer;
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
|
||||||
use nrf_softdevice::{raw, Softdevice};
|
use nrf_softdevice::{raw, Softdevice};
|
||||||
|
|
||||||
|
use defmt::*;
|
||||||
|
use embassy_executor::Spawner;
|
||||||
|
use embassy_nrf::twim::{self, Twim};
|
||||||
|
use embassy_nrf::{bind_interrupts, peripherals};
|
||||||
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
const ADDRESS: u8 = 0x44;
|
||||||
|
|
||||||
|
bind_interrupts!(struct Irqs {
|
||||||
|
TWIM0_TWIS0_TWI0 => twim::InterruptHandler<peripherals::TWI0>;
|
||||||
|
});
|
||||||
|
|
||||||
#[embassy_executor::main]
|
#[embassy_executor::main]
|
||||||
async fn main(_spawner: Spawner) {
|
async fn main(_spawner: Spawner) {
|
||||||
let p = embassy_nrf::init(Default::default());
|
let p = embassy_nrf::init(Default::default());
|
||||||
let mut led = Output::new(p.P0_20, Level::Low, OutputDrive::Standard);
|
|
||||||
|
|
||||||
info!("Starting blinky");
|
info!("Initializing TWI...");
|
||||||
|
|
||||||
|
let config = twim::Config::default();
|
||||||
|
let mut twi = Twim::new(p.TWI0, Irqs, p.P0_14, p.P0_13, config);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
led.set_high();
|
unwrap!(twi.blocking_write(ADDRESS, &mut [0xFD]));
|
||||||
|
|
||||||
Timer::after_millis(300).await;
|
Timer::after_millis(300).await;
|
||||||
led.set_low();
|
|
||||||
Timer::after_millis(300).await;
|
let mut buf = [0u8; 6];
|
||||||
info!("Blinked");
|
unwrap!(twi.blocking_read(ADDRESS, &mut buf));
|
||||||
|
|
||||||
|
let temp_int = u16::from_be_bytes([buf[0], buf[1]]);
|
||||||
|
let temp_float: f32 = -45f32 + 175f32 * (temp_int as f32 / (2u32.pow(16) - 1) as f32);
|
||||||
|
|
||||||
|
let rh_int = u16::from_be_bytes([buf[3], buf[4]]);
|
||||||
|
let rh_float: f32 = -6f32 + 125f32 * (rh_int as f32 / (2u32.pow(16) - 1) as f32);
|
||||||
|
println!("temp_float: {}", temp_float);
|
||||||
|
println!("rh_float: {}", rh_float);
|
||||||
|
|
||||||
|
Timer::after_millis(1000).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #![no_std]
|
||||||
|
// #![no_main]
|
||||||
|
//
|
||||||
|
// use defmt::info;
|
||||||
|
// use embassy_executor::Spawner;
|
||||||
|
// use embassy_nrf::gpio::{Level, Output, OutputDrive};
|
||||||
|
// use embassy_time::Timer;
|
||||||
|
// use nrf_softdevice::{raw, Softdevice};
|
||||||
|
// use {defmt_rtt as _, panic_probe as _};
|
||||||
|
//
|
||||||
|
// #[embassy_executor::main]
|
||||||
|
// async fn main(_spawner: Spawner) {
|
||||||
|
// let p = embassy_nrf::init(Default::default());
|
||||||
|
// let mut led = Output::new(p.P0_20, Level::Low, OutputDrive::Standard);
|
||||||
|
//
|
||||||
|
// info!("Starting blinky");
|
||||||
|
//
|
||||||
|
// loop {
|
||||||
|
// led.set_high();
|
||||||
|
// Timer::after_millis(300).await;
|
||||||
|
// led.set_low();
|
||||||
|
// Timer::after_millis(300).await;
|
||||||
|
// info!("Blinked");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user