Skip to content

Commit

Permalink
update to latest e-h
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankurte committed Aug 15, 2024
1 parent c552bf2 commit ee93aae
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 164 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ edition = "2021"
license = "MPL-2.0"

[features]
util = [ "structopt", "linux-embedded-hal", "simplelog", "humantime" ]
util = [ "clap", "linux-embedded-hal", "simplelog", "humantime" ]
default = [ "util" ]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
embedded-hal = { version = "0.2.3", features = ["unproven"] }
log = "0.4.8"
embedded-hal = { version = "1.0.0" }
linux-embedded-hal = { version = "0.4.0", optional = true }

structopt = { version = "0.2.15", optional = true }
linux-embedded-hal = { version = "0.2.2", optional = true }
log = "0.4.8"
clap = { version = "4.2.1", features = [ "derive", "env" ], optional = true }
simplelog = { version = "0.5.3", optional = true }
humantime = { version = "1.2.0", optional = true }
humantime = { version = "2.1.0", optional = true }

[dev-dependencies]
color-backtrace = "0.1.3"
embedded-hal-mock = "0.7.0"
color-backtrace = "0.6.1"
embedded-hal-mock = "0.11.1"
assert_approx_eq = "1.1.0"


Expand Down
33 changes: 14 additions & 19 deletions src/base.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Base communication implementation for interacting with Scd30 device
//!
//!
//! Copyright 2019 Ryan Kurte

use core::fmt::Debug;

use embedded_hal::blocking::i2c;
use embedded_hal::i2c;
use log::trace;

use crate::{Error};
use crate::device::*;
use crate::Error;

/// Base API for reading and writing to the device
/// This should not be required by consumers, but is exposed to support alternate use (or in future provide ModBus support)
Expand Down Expand Up @@ -38,39 +38,35 @@ pub fn crc8(data: &[u8]) -> u8 {
}
}

// Apply final xor
// Apply final xor
crc ^ CRC_XOR
}

/// Base implementation for I2C devices
impl <Conn, Err> Base<Err> for Conn where
Conn: i2c::Read<Error=Err> + i2c::Write<Error=Err> + i2c::WriteRead<Error=Err>,
impl<Conn, Err> Base<Err> for Conn
where
Conn: i2c::I2c<Error = Err>,
Err: Debug,
{
fn write_command(&mut self, command: Command, data: Option<u16>) -> Result<(), Error<Err>> {
let c = command as u16;

let mut buff: [u8; 5] = [
(c >> 8) as u8,
(c & 0xFF) as u8,
0,
0,
0,
];
let mut buff: [u8; 5] = [(c >> 8) as u8, (c & 0xFF) as u8, 0, 0, 0];

let len = match data {
Some(d) => {
buff[2] = (d >> 8) as u8;
buff[3] = (d & 0xFF) as u8;
buff[4] = crc8(&buff[2..4]);
5
},
}
None => 2,
};

trace!("Writing command: {:?} data: {:?}", c, data);

self.write(DEFAULT_ADDRESS | I2C_WRITE_FLAG, &buff[..len]).map_err(|e| Error::Conn(e) )
self.write(DEFAULT_ADDRESS | I2C_WRITE_FLAG, &buff[..len])
.map_err(|e| Error::Conn(e))
}

fn read_command(&mut self, command: Command, data: &mut [u8]) -> Result<(), Error<Err>> {
Expand All @@ -82,11 +78,11 @@ impl <Conn, Err> Base<Err> for Conn where

// First write the read command
self.write(DEFAULT_ADDRESS | I2C_WRITE_FLAG, &cmd)
.map_err(|e| Error::Conn(e) )?;
.map_err(|e| Error::Conn(e))?;

// Then, read the data back
self.read(DEFAULT_ADDRESS | I2C_READ_FLAG, data)
.map_err(|e| Error::Conn(e) )?;
.map_err(|e| Error::Conn(e))?;

// Note: this two-phase approach is specified in the datasheet

Expand All @@ -113,6 +109,5 @@ mod test {
let v = crc8(&t.0);
assert_eq!(v, t.1);
}

}
}
}
6 changes: 2 additions & 4 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! Scd30 device definitions
//!
//!
//! Copyright 2019 Ryan Kurte


/// Scd30 default I2C address
/// (note this is shifted left 1 bit on the wire)
pub const DEFAULT_ADDRESS: u8 = 0x61;

pub const I2C_WRITE_FLAG: u8 = 0x00;
pub const I2C_READ_FLAG: u8 = 0x01;
pub const I2C_READ_FLAG: u8 = 0x01;

pub const CRC_POLY: u8 = 0x31;
pub const CRC_INIT: u8 = 0xff;
Expand Down Expand Up @@ -62,4 +61,3 @@ pub enum Command {

GetFirmwareVersion = 0xD100,
}

Loading

0 comments on commit ee93aae

Please sign in to comment.