diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0865241..cf698c1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,6 +2,7 @@ name: CI Build on: push: + pull_request: env: CARGO_TERM_COLOR: always diff --git a/Cargo.toml b/Cargo.toml index 32d6fef..3b406a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ pastey = "0.2" # For the example [dev-dependencies] +embedded-hal-bus = "0.3.0" linux-embedded-hal = "0.4.1" [dependencies.packed_struct] diff --git a/examples/linux-shared.rs b/examples/linux-shared.rs new file mode 100644 index 0000000..ab366e9 --- /dev/null +++ b/examples/linux-shared.rs @@ -0,0 +1,52 @@ +use embedded_hal_bus::i2c::RefCellDevice; +use linux_embedded_hal::I2cdev; +use pac194x::{AddrSelect, PAC194X}; +use std::cell::RefCell; +use std::{thread, time::Duration}; + +const SENSE_RESISTORS: [f32; 8] = [0.005, 0.010, 0.010, 0.010, 0.002, 0.010, 0.010, 0.010]; + +fn main() { + let i2c = RefCell::new(I2cdev::new("/dev/i2c-3").unwrap()); + + let bus_handle1 = RefCellDevice::new(&i2c); + let mut sensor1 = PAC194X::new(bus_handle1, AddrSelect::GND).unwrap(); + + let bus_handle2 = RefCellDevice::new(&i2c); + let mut sensor2 = PAC194X::new(bus_handle2, AddrSelect::_499).unwrap(); + + loop { + print!("Sensor 1 "); + for channel in 1..5 { + let bus_voltage = sensor1.read_bus_voltage_n(channel).unwrap(); + let sense_voltage = sensor1.read_sense_voltage_n(channel).unwrap(); + print!( + "CH{} {:5.2}V, {:5.2}A, ", + channel, + bus_voltage, + sense_voltage / SENSE_RESISTORS[(channel - 1) as usize] + ); + } + println!(); + print!("Sensor 2 "); + for channel in 1..5 { + let bus_voltage = sensor2.read_bus_voltage_n(channel).unwrap(); + let sense_voltage = sensor2.read_sense_voltage_n(channel).unwrap(); + print!( + "CH{} {:5.2}V, {:5.2}A, ", + channel, + bus_voltage, + sense_voltage / SENSE_RESISTORS[(channel - 1 + 4) as usize] + ); + } + println!(); + println!(); + sensor1.refresh().unwrap(); + thread::sleep(Duration::from_millis(100)); + sensor1.refresh_v().unwrap(); + thread::sleep(Duration::from_millis(100)); + sensor2.refresh().unwrap(); + thread::sleep(Duration::from_millis(100)); + sensor2.refresh_v().unwrap(); + } +} diff --git a/examples/linux.rs b/examples/linux.rs index 056b0f5..ce26b30 100644 --- a/examples/linux.rs +++ b/examples/linux.rs @@ -2,7 +2,7 @@ use linux_embedded_hal::I2cdev; use pac194x::{AddrSelect, PAC194X}; use std::{thread, time::Duration}; -const SENSE_RESISTOR: f32 = 0.5; +const SENSE_RESISTORS: [f32; 4] = [0.005, 0.010, 0.010, 0.010]; fn main() { let i2c = I2cdev::new("/dev/i2c-3").unwrap(); @@ -15,14 +15,15 @@ fn main() { "CH{} {:.2}V, {:.2}A, ", channel, bus_voltage, - sense_voltage / SENSE_RESISTOR + sense_voltage / SENSE_RESISTORS[(channel - 1) as usize] ); } println!(); println!(); + sensor.refresh().unwrap(); + thread::sleep(Duration::from_millis(100)); sensor.refresh_v().unwrap(); - thread::sleep(Duration::from_millis(100)); } }