|
| 1 | +use embedded_hal_bus::i2c::RefCellDevice; |
| 2 | +use linux_embedded_hal::I2cdev; |
| 3 | +use pac194x::{AddrSelect, PAC194X}; |
| 4 | +use std::cell::RefCell; |
| 5 | +use std::{thread, time::Duration}; |
| 6 | + |
| 7 | +const SENSE_RESISTOR: f32 = 0.5; |
| 8 | + |
| 9 | +fn main() { |
| 10 | + let i2c = RefCell::new(I2cdev::new("/dev/i2c-3").unwrap()); |
| 11 | + |
| 12 | + let bus_handle1 = RefCellDevice::new(&i2c); |
| 13 | + let mut sensor1 = PAC194X::new(bus_handle1, AddrSelect::GND).unwrap(); |
| 14 | + |
| 15 | + let bus_handle2 = RefCellDevice::new(&i2c); |
| 16 | + let mut sensor2 = PAC194X::new(bus_handle2, AddrSelect::_499).unwrap(); |
| 17 | + |
| 18 | + loop { |
| 19 | + print!("Sensor 1 "); |
| 20 | + for channel in 1..5 { |
| 21 | + let bus_voltage = sensor1.read_bus_voltage_n(channel).unwrap(); |
| 22 | + let sense_voltage = sensor1.read_sense_voltage_n(channel).unwrap(); |
| 23 | + print!( |
| 24 | + "CH{} {:5.2}V, {:5.2}A, ", |
| 25 | + channel, |
| 26 | + bus_voltage, |
| 27 | + sense_voltage / SENSE_RESISTOR |
| 28 | + ); |
| 29 | + } |
| 30 | + println!(); |
| 31 | + print!("Sensor 2 "); |
| 32 | + for channel in 1..5 { |
| 33 | + let bus_voltage = sensor2.read_bus_voltage_n(channel).unwrap(); |
| 34 | + let sense_voltage = sensor2.read_sense_voltage_n(channel).unwrap(); |
| 35 | + print!( |
| 36 | + "CH{} {:5.2}V, {:5.2}A, ", |
| 37 | + channel, |
| 38 | + bus_voltage, |
| 39 | + sense_voltage / SENSE_RESISTOR |
| 40 | + ); |
| 41 | + } |
| 42 | + println!(); |
| 43 | + println!(); |
| 44 | + sensor1.refresh().unwrap(); |
| 45 | + thread::sleep(Duration::from_millis(100)); |
| 46 | + sensor1.refresh_v().unwrap(); |
| 47 | + thread::sleep(Duration::from_millis(100)); |
| 48 | + sensor2.refresh().unwrap(); |
| 49 | + thread::sleep(Duration::from_millis(100)); |
| 50 | + sensor2.refresh_v().unwrap(); |
| 51 | + |
| 52 | + } |
| 53 | +} |
0 commit comments