From bfcd1a4ebb1f867e4cf17c439f3dd9626127a766 Mon Sep 17 00:00:00 2001 From: Ciochina Vitalie Date: Sun, 25 Aug 2024 17:16:05 +0300 Subject: [PATCH] Start measure CO2 --- Cargo.toml | 1 - src/main.rs | 51 ++++++++++++++++++++++++++----------------------- src/senseair.rs | 14 +++++++++++--- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2125971..dbd9fcb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,6 @@ rp-pico = "0.9" heapless = "0.7.4" byteorder = { version = "1", default-features = false } lps22hb = "0.1.0" -shared-bus = "0.3.1" # but you can use any BSP. Uncomment this to use the pro_micro_rp2040 BSP instead # sparkfun-pro-micro-rp2040 = "0.8" diff --git a/src/main.rs b/src/main.rs index da0d29f..fa75516 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,9 +8,9 @@ mod senseair; use core::cell::RefCell; use cortex_m::interrupt::{self, Mutex}; +use cortex_m::delay::Delay; - -use bsp::entry; +use rp_pico::entry; use defmt::*; use defmt_rtt as _; use embedded_hal::digital::v2::OutputPin; @@ -18,28 +18,25 @@ use panic_probe as _; use embedded_hal::blocking::i2c::{Read, Write}; -use rp_pico::hal::gpio::PinState; -use rp_pico::pac::adc::result; -// Provide an alias for our BSP so we can switch targets quickly. -// Uncomment the BSP you included in Cargo.toml, the rest of the code does not need to change. -use rp_pico as bsp; -use bsp::hal::fugit::RateExtU32; +use rp_pico::hal::fugit::RateExtU32; use lps22hb::*; use lps22hb::interface::{I2cInterface,i2c::I2cAddress}; -use bsp::hal::{ +use rp_pico::hal::{ clocks::{init_clocks_and_plls, Clock}, pac, + I2C, + pac::I2C1, sio::Sio, watchdog::Watchdog, - gpio::{FunctionI2C, Pin,OutputOverride}, + gpio::{FunctionI2C,FunctionI2c,Pin,OutputOverride,PinState,PullUp,PullDown,bank0::{Gpio18,Gpio19,Gpio20,Gpio21},FunctionSio,SioOutput,SioInput}, }; use senseair::Sunrise; const ADDRESS: u8 = 0x68; //static mut GLOBAL_DELAY:Option = None; -static mut GLOBAL_DELAY: Option = None; +static mut GLOBAL_DELAY: Option = None; #[entry] fn main() -> ! { @@ -63,13 +60,13 @@ fn main() -> ! { .ok() .unwrap(); - //let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); + //let mut delay = Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); unsafe{ - GLOBAL_DELAY = Some(cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz())); + GLOBAL_DELAY = Some(Delay::new(core.SYST, clocks.system_clock.freq().to_Hz())); } - let pins = bsp::Pins::new( + let pins = rp_pico::Pins::new( pac.IO_BANK0, pac.PADS_BANK0, sio.gpio_bank0, @@ -81,7 +78,7 @@ fn main() -> ! { let scl: Pin<_, FunctionI2C, _> = pins.gpio19.reconfigure(); - let mut i2c = bsp::hal::I2C::i2c1( + let i2c = rp_pico::hal::I2C::i2c1( pac.I2C1, sda, scl, // Try `not_an_scl_pin` here @@ -93,28 +90,34 @@ fn main() -> ! { let en_pin = pins.gpio20.into_push_pull_output_in_state(PinState::Low); - let nrdy_pin =pins.gpio21.as_input(); - - let mut test:Option, Pin)>, cortex_m::delay::Delay, Pin, rp_pico::hal::gpio::PullDown>, rp_pico::hal::gpio::AsInputPin> > = None; + let nrdy_pin =pins.gpio21.into_pull_up_input(); + //let mut test:Option, Pin)>, cortex_m::delay::Delay, Pin, rp_pico::hal::gpio::PullDown>, rp_pico::hal::gpio::AsInputPin> > = None; + let mut test:Option, Pin)>, Delay, Pin, PullDown>, Pin, PullUp>>> = None; unsafe { if let Some(delay) = GLOBAL_DELAY.as_mut() { test = Some(Sunrise::new(i2c, delay, en_pin, nrdy_pin)); } } + let mut sensor_CO2 = test.take().unwrap(); + // let mut sensor_CO2 = Sunrise::new(i2c, & mut delay, en_pin, nrdy_pin); - // let mut sensor_CO2 = Sunrise::new(i2c, & mut delay, en_pin, nrdy_pin); + sensor_CO2.init(false,true,false).unwrap(); - // sensor_CO2.init(false,true,false).unwrap(); - if let Some(value) = test{ - - } loop { - // sensor_CO2.single_measurement_get().unwrap(); + let data = sensor_CO2.single_measurement_get().unwrap(); + + let sensor_data = ((data[6] as u16) << 8) | (data[7] as u16); + info!("Read data is {}",sensor_data); + unsafe { + if let Some(delay) = GLOBAL_DELAY.as_mut() { + delay.delay_ms(15000); + } + } } } diff --git a/src/senseair.rs b/src/senseair.rs index e867088..0447b26 100644 --- a/src/senseair.rs +++ b/src/senseair.rs @@ -30,6 +30,7 @@ pub enum Registers{ MeterControl = 0xA5, StartMesurement = 0xC3, AbcTime = 0xC4, + ClearErrorStatus = 0x9D } ///////////////////////////////////////////////////////////////// @@ -89,7 +90,12 @@ impl<'a,T, E, D,EN,NRDY> Sunrise<'a,T,D,EN,NRDY> where T: Read + Writ vec.extend_from_slice(&self.state_buf).expect(EXPECT_MSG); self.comm.write(self.address, &vec) } - + fn clear_error_status(&mut self) -> Result<(), E> { + let mut vec: Vec = Vec::new(); + vec.extend_from_slice(&(Registers::ClearErrorStatus as u8).to_be_bytes()).expect(EXPECT_MSG); + vec.extend_from_slice(&(0x00 as u8).to_be_bytes()).expect(EXPECT_MSG); + self.comm.write(self.address, &vec) + } fn sensor_state_data_get(&mut self) -> Result<(), E> { self.comm.write(self.address, &(Registers::AbcTime as u8).to_be_bytes())?; @@ -134,20 +140,22 @@ impl<'a,T, E, D,EN,NRDY> Sunrise<'a,T,D,EN,NRDY> where T: Read + Writ } pub fn single_measurement_get(&mut self) -> Result<[u8; 8], E> { - let mut vec: Vec = Vec::new(); + let mut vec: Vec = Vec::new(); let mut buf = [0u8; 8]; let _ = self.en_pin.set_high(); self.delay.delay_ms(35); + self.clear_error_status()?; self.single_measurement_set()?; loop { if let Ok(true) = self.n_rdy_pin.is_low() - { + { break; } } + self.comm.write(self.address, &(Registers::ErrorStatus as u8).to_be_bytes())?; self.comm.read(self.address, &mut buf)?;