Skip to content

Commit

Permalink
Merge pull request #6 from knurling-rs/november-16-release
Browse files Browse the repository at this point in the history
November 16 release
  • Loading branch information
Mirabellensaft authored Nov 16, 2020
2 parents d9ae0a3 + f5e1f09 commit 1e010e5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
79 changes: 79 additions & 0 deletions src/bin/11_scd_30_measure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#![no_main]
#![no_std]

use knurling_session_20q4 as _; // global logger + panicking-behavior + memory layout
use knurling_session_20q4::{
scd30,
};

use embedded_hal::blocking::delay::DelayMs;

// access to board peripherals:
use nrf52840_hal::{
self as hal,
gpio::{p0::Parts as P0Parts, Level},
prelude::*,
twim::{self, Twim},
Timer,
};

#[cortex_m_rt::entry]
fn main() -> ! {
// take() returns all peripherals, so we can access them
let board = hal::pac::Peripherals::take().unwrap();
// first peripheral: initialize timer
let mut timer = Timer::new(board.TIMER0);

let pins = P0Parts::new(board.P0);
// onboard led
let mut led_1 = pins.p0_13.into_push_pull_output(Level::Low);

// external led
let led_channel_red = pins.p0_03.degrade();
let led_channel_blue = pins.p0_04.degrade();
let led_channel_green = pins.p0_28.degrade();

// instanciate I2C
let scl = pins.p0_30.degrade();
let sda = pins.p0_31.degrade();

let pins = twim::Pins { scl, sda };
let i2c = Twim::new(board.TWIM0, pins, twim::Frequency::K100);

// set ambient air pressure:
let pressure = 1020_u16;

let mut sensor = scd30::SCD30::init(i2c);

let firmware_version = sensor.get_firmware_version().unwrap();
defmt::info!(
"Firmware Version: {:u8}.{:u8}",
firmware_version[0],
firmware_version[1]
);

sensor.start_measuring(pressure).unwrap();

'ready: loop {
if sensor.data_ready().unwrap() {
defmt::info!("Data ready.");
break 'ready
}

loop {
let result = sensor.get_measurement().unwrap();

let co2 = result.co2;
let temp = result.temperature;
let humidity = result.humidity;

defmt::info!("CO2 {:?} ppm \r\nTemperature {:?} C \r\nHumidity {:?} % \r\n\r\n",
co2, temp, humidity
);

timer.delay_ms(2000_u32);
led_1.set_high().unwrap();
timer.delay_ms(2000_u32);
led_1.set_low().unwrap();
}
}
2 changes: 0 additions & 2 deletions src/bin/2_hello_external_led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,4 @@ fn main() -> ! {

timer.delay_ms(1000_u32);
}

// knurling_session_20q4::exit()
}
8 changes: 4 additions & 4 deletions src/rgb_led/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use nrf52840_hal::{
timer::OneShot,
Timer,
};

// This module is written for common anode rgb leds. For common cathode rgb leds, switch high and low.
pub struct LEDColor {
r: Pin<Output<PushPull>>,
g: Pin<Output<PushPull>>,
Expand Down Expand Up @@ -72,11 +74,9 @@ impl LEDColor {
}
// blinks between two colors
pub fn blinky(&mut self, timer: &mut Timer<TIMER0, OneShot>) {
self.r.set_low().unwrap();
self.b.set_high().unwrap();
self.red();
timer.delay_ms(1000_u32);
self.r.set_high().unwrap();
self.b.set_low().unwrap();
self.blue();
timer.delay_ms(1000_u32);
}
}
14 changes: 10 additions & 4 deletions src/scd30/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ where
Ok([major, minor])
}

pub fn start_measuring(&mut self) -> Result<(), Error> {
let pressure = 0_u16;

pub fn start_continuous_measurement(&mut self, pressure: u16) -> Result<(), Error> {

// command bytes
let mut command: [u8; 5] = [0x00, 0x10, 0x00, 0x00, 0x00];
let argument_bytes = &pressure.to_be_bytes();

command[2] = argument_bytes[0];
command[3] = argument_bytes[1];

let mut crc = Crc::<u8>::new(0x31, 8, 0xff, 0x00, false);

defmt::info!("{:?}", command);

crc.update(&pressure.to_be_bytes());
command[4] = crc.finish();
defmt::info!("{:?}", command);

self.0.write(DEFAULT_ADDRESS, &command)?;

Expand All @@ -56,7 +62,7 @@ where
Ok(u16::from_be_bytes([rd_buffer[0], rd_buffer[1]]) == 1)
}

pub fn get_measurement(&mut self) -> Result<SensorData, Error> {
pub fn read_measurement(&mut self) -> Result<SensorData, Error> {
let command: [u8; 2] = [0x03, 0x00];
let mut rd_buffer = [0u8; 18];

Expand Down

0 comments on commit 1e010e5

Please sign in to comment.