Skip to content

Commit

Permalink
Start measure CO2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciochina Vitalie committed Aug 25, 2024
1 parent ec03648 commit bfcd1a4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
51 changes: 27 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,35 @@
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;
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<cortex_m::delay::Delay> = None;
static mut GLOBAL_DELAY: Option<cortex_m::delay::Delay> = None;
static mut GLOBAL_DELAY: Option<Delay> = None;

#[entry]
fn main() -> ! {
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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<Sunrise<rp_pico::hal::I2C<pac::I2C1, (Pin<rp_pico::hal::gpio::bank0::Gpio18, rp_pico::hal::gpio::FunctionI2c, rp_pico::hal::gpio::PullUp>, Pin<rp_pico::hal::gpio::bank0::Gpio19, rp_pico::hal::gpio::FunctionI2c, rp_pico::hal::gpio::PullUp>)>, cortex_m::delay::Delay, Pin<rp_pico::hal::gpio::bank0::Gpio20, rp_pico::hal::gpio::FunctionSio<rp_pico::hal::gpio::SioOutput>, rp_pico::hal::gpio::PullDown>, rp_pico::hal::gpio::AsInputPin<rp_pico::hal::gpio::bank0::Gpio21, rp_pico::hal::gpio::FunctionNull, rp_pico::hal::gpio::PullDown>> > = None;
let nrdy_pin =pins.gpio21.into_pull_up_input();

//let mut test:Option<Sunrise<rp_pico::hal::I2C<pac::I2C1, (Pin<rp_pico::hal::gpio::bank0::Gpio18, rp_pico::hal::gpio::FunctionI2c, rp_pico::hal::gpio::PullUp>, Pin<rp_pico::hal::gpio::bank0::Gpio19, rp_pico::hal::gpio::FunctionI2c, rp_pico::hal::gpio::PullUp>)>, cortex_m::delay::Delay, Pin<rp_pico::hal::gpio::bank0::Gpio20, rp_pico::hal::gpio::FunctionSio<rp_pico::hal::gpio::SioOutput>, rp_pico::hal::gpio::PullDown>, rp_pico::hal::gpio::AsInputPin<rp_pico::hal::gpio::bank0::Gpio21, rp_pico::hal::gpio::FunctionNull, rp_pico::hal::gpio::PullDown>> > = None;
let mut test:Option<Sunrise<I2C<I2C1, (Pin<Gpio18, FunctionI2c, PullUp>, Pin<Gpio19, FunctionI2c, PullUp>)>, Delay, Pin<Gpio20, FunctionSio<SioOutput>, PullDown>, Pin<Gpio21, FunctionSio<SioInput>, 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);
}
}
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/senseair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum Registers{
MeterControl = 0xA5,
StartMesurement = 0xC3,
AbcTime = 0xC4,
ClearErrorStatus = 0x9D
}

/////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -89,7 +90,12 @@ impl<'a,T, E, D,EN,NRDY> Sunrise<'a,T,D,EN,NRDY> where T: Read<Error = E> + 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<u8, 2> = 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())?;
Expand Down Expand Up @@ -134,20 +140,22 @@ impl<'a,T, E, D,EN,NRDY> Sunrise<'a,T,D,EN,NRDY> where T: Read<Error = E> + Writ
}

pub fn single_measurement_get(&mut self) -> Result<[u8; 8], E> {
let mut vec: Vec<u8, 25> = Vec::new();
let mut vec: Vec<u8, 2> = 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)?;

Expand Down

0 comments on commit bfcd1a4

Please sign in to comment.