Compare commits
2 Commits
bf5ee0ec78
...
acc6fa2ac1
Author | SHA1 | Date | |
---|---|---|---|
|
acc6fa2ac1 | ||
|
014b45d517 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -40,7 +40,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ina3221"
|
name = "ina3221"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"i2cdev",
|
"i2cdev",
|
||||||
]
|
]
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ina3221"
|
name = "ina3221"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
use ina3221;
|
use ina3221;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut ina = ina3221::Ina3221::new(0x40);
|
let mut ina = ina3221::Ina3221::new(0x40, "/dev/i2c-1", 5.0);
|
||||||
let res = match ina.current(ina3221::Channel::One) {
|
let res = match ina.current(ina3221::Channel::One) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => panic!("Can't read i2c: {error:?}"),
|
Err(error) => panic!("Can't read i2c: {error:?}"),
|
||||||
};
|
};
|
||||||
println!("Channel one current: {:?} mA", res);
|
println!("Channel one current: {:?} A", res);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use ina3221;
|
use ina3221;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut ina = ina3221::Ina3221::new(0x40);
|
let mut ina = ina3221::Ina3221::new(0x40, "/dev/i2c-1", 5.0);
|
||||||
let res = match ina.bus_voltage(ina3221::Channel::One) {
|
let res = match ina.bus_voltage(ina3221::Channel::One) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => panic!("Can't read i2c: {error:?}"),
|
Err(error) => panic!("Can't read i2c: {error:?}"),
|
||||||
|
10
src/lib.rs
10
src/lib.rs
@ -17,11 +17,11 @@ pub struct Ina3221 {
|
|||||||
|
|
||||||
impl Ina3221 {
|
impl Ina3221 {
|
||||||
|
|
||||||
pub fn new(slave_address: u16) -> Ina3221 {
|
pub fn new(slave_address: u16, i2c_device: &str, shunt_resistor: f64) -> Ina3221 {
|
||||||
Ina3221 {
|
Ina3221 {
|
||||||
slave_addr: slave_address,
|
slave_addr: slave_address,
|
||||||
i2c_device: format!("/dev/i2c-1"),
|
i2c_device: format!("{}", i2c_device),
|
||||||
shunt_resistor: 1.0,
|
shunt_resistor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ impl Ina3221 {
|
|||||||
|
|
||||||
pub fn current(&mut self, channel: Channel) -> Result<f64, LinuxI2CError> {
|
pub fn current(&mut self, channel: Channel) -> Result<f64, LinuxI2CError> {
|
||||||
let tmp: f64 = self.shunt_voltage(channel)?;
|
let tmp: f64 = self.shunt_voltage(channel)?;
|
||||||
let res: f64 = tmp * self.shunt_resistor;
|
let res: f64 = tmp / self.shunt_resistor * 100.0; // No idea why multiplying with 100 but the result seems to be correct...
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user