From 8695c00ebddc401c8d14a54d1e0eb4cac5af159b Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Mon, 17 Nov 2025 18:37:55 +0800 Subject: [PATCH 1/5] Move sample from README to buildable folder Signed-off-by: Daniel Schaefer --- Cargo.toml | 4 ++++ README.md | 23 ++++------------------- examples/linux.rs | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 examples/linux.rs diff --git a/Cargo.toml b/Cargo.toml index 34be231..74cceb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,10 @@ documentation = "https://docs.rs/crate/pac194x/" embedded-hal = "0.2" paste = "1.0" +# For the example +[dev-dependencies] +linux-embedded-hal = "0.3.2" + [dependencies.packed_struct] version = "0.10" default-features = false diff --git a/README.md b/README.md index ca2d8ff..d9ce2f3 100644 --- a/README.md +++ b/README.md @@ -17,25 +17,10 @@ This driver allows you to: ## Usage To use this driver, import this crate and an `embedded_hal` implementation, -then instantiate the appropriate device. - -```rust -use linux_embedded_hal::I2cdev; -use pac194x::{PAC194X, AddrSelect}; - -const SENSE_RESISTOR: f32 = 0.5; - -fn main() { - let i2c = I2cdev::new("/dev/i2c-1").unwrap(); - let mut sensor = PAC194X::new(i2c, AddrSelect::GND).unwrap(); - loop { - let bus_voltage_1 = sensor.read_bus_voltage_n(1).unwrap(); - let sense_voltage_1 = sensor.read_sense_voltage_n(1).unwrap(); - println!("Channel 1 has a bus voltage of: {:.2} V", bus_voltage_1); - println!("Channel 1 is pulling a current of: {:.2} A", sense_voltage_1 / SENSE_RESISTOR); - } -} -``` +then instantiate the appropriate device. See the `examples` folder for example code. + +Run it on Linux with `cargo build --examples linux && sudo ./target/debug/examples/linux`. +It's hardcoded to bus `/dev/i2c-3` and I2C address 0b10000 (grounded). ## Discussion diff --git a/examples/linux.rs b/examples/linux.rs new file mode 100644 index 0000000..56fff10 --- /dev/null +++ b/examples/linux.rs @@ -0,0 +1,15 @@ +use linux_embedded_hal::I2cdev; +use pac194x::{PAC194X, AddrSelect}; + +const SENSE_RESISTOR: f32 = 0.5; + +fn main() { + let i2c = I2cdev::new("/dev/i2c-3").unwrap(); + let mut sensor1 = PAC194X::new(i2c, AddrSelect::GND); + loop { + let bus_voltage_1 = sensor.read_bus_voltage_n(1).unwrap(); + let sense_voltage_1 = sensor.read_sense_voltage_n(1).unwrap(); + println!("Channel 1 has a bus voltage of: {:.2} V", bus_voltage_1); + println!("Channel 1 is pulling a current of: {:.2} A", sense_voltage_1 / SENSE_RESISTOR); + } +} From 269e040fd2baf53132d89d7330bd7d75bd5157b0 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Tue, 18 Nov 2025 18:55:15 +0800 Subject: [PATCH 2/5] example: Refresh sensor values, print all channels If we don't refresh, the sensor data won't update. Signed-off-by: Daniel Schaefer --- examples/linux.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/examples/linux.rs b/examples/linux.rs index 56fff10..db46f66 100644 --- a/examples/linux.rs +++ b/examples/linux.rs @@ -1,15 +1,28 @@ use linux_embedded_hal::I2cdev; -use pac194x::{PAC194X, AddrSelect}; +use pac194x::{AddrSelect, PAC194X}; +use std::{thread, time::Duration}; const SENSE_RESISTOR: f32 = 0.5; fn main() { let i2c = I2cdev::new("/dev/i2c-3").unwrap(); - let mut sensor1 = PAC194X::new(i2c, AddrSelect::GND); + let mut sensor = PAC194X::new(i2c, AddrSelect::GND); loop { - let bus_voltage_1 = sensor.read_bus_voltage_n(1).unwrap(); - let sense_voltage_1 = sensor.read_sense_voltage_n(1).unwrap(); - println!("Channel 1 has a bus voltage of: {:.2} V", bus_voltage_1); - println!("Channel 1 is pulling a current of: {:.2} A", sense_voltage_1 / SENSE_RESISTOR); + for channel in 1..5 { + let bus_voltage = sensor.read_bus_voltage_n(channel).unwrap(); + let sense_voltage = sensor.read_sense_voltage_n(channel).unwrap(); + print!( + "CH{} {:.2}V, {:.2}A, ", + channel, + bus_voltage, + sense_voltage / SENSE_RESISTOR + ); + } + println!(); + println!(); + sensor.refresh().unwrap(); + sensor.refresh_v().unwrap(); + + thread::sleep(Duration::from_millis(100)); } } From a9db1b687a94e75309cd2b96787c1a2d2fb95a50 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Tue, 18 Nov 2025 19:05:10 +0800 Subject: [PATCH 3/5] gh-actions: actions-rs is archived Signed-off-by: Daniel Schaefer --- .github/workflows/branches.yml | 48 +++++++++++++++------------------- .github/workflows/main.yml | 48 +++++++++++++++------------------- 2 files changed, 42 insertions(+), 54 deletions(-) diff --git a/.github/workflows/branches.yml b/.github/workflows/branches.yml index bbdf1bb..ed57e7a 100644 --- a/.github/workflows/branches.yml +++ b/.github/workflows/branches.yml @@ -20,30 +20,24 @@ jobs: - nightly steps: - - uses: actions/checkout@v2 - - - uses: actions-rs/toolchain@v1 - name: installing toolchain - with: - profile: default - toolchain: ${{ matrix.rust }} - override: true - components: rustfmt, clippy - - - uses: actions-rs/cargo@v1 - name: build - with: - command: build - args: --release - - - uses: actions-rs/cargo@v1 - name: fmt - with: - command: fmt - args: --all -- --check - - - uses: actions-rs/cargo@v1 - name: clippy - with: - command: clippy - args: -- -D warnings \ No newline at end of file + - uses: actions/checkout@v4 + + - name: Set up Rust toolchain + run: | + rustup toolchain install ${{ matrix.rust }} + + - name: Build examples + run: | + cargo build --examples + + - name: Build release config + run: | + cargo build --release + + - name: Check formatting + run: | + cargo fmt --all -- --check + + - name: Check formatting + run: | + cargo clippy -- -D warnings diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 25dda45..8b42b47 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,30 +20,24 @@ jobs: - nightly steps: - - uses: actions/checkout@v2 - - - uses: actions-rs/toolchain@v1 - name: installing toolchain - with: - profile: default - toolchain: ${{ matrix.rust }} - override: true - components: rustfmt, clippy - - - uses: actions-rs/cargo@v1 - name: build - with: - command: build - args: --release - - - uses: actions-rs/cargo@v1 - name: fmt - with: - command: fmt - args: --all -- --check - - - uses: actions-rs/cargo@v1 - name: clippy - with: - command: clippy - args: -- -D warnings \ No newline at end of file + - uses: actions/checkout@v4 + + - name: Set up Rust toolchain + run: | + rustup toolchain install ${{ matrix.rust }} + + - name: Build examples + run: | + cargo build --examples + + - name: Build release config + run: | + cargo build --release + + - name: Check formatting + run: | + cargo fmt --all -- --check + + - name: Check formatting + run: | + cargo clippy -- -D warnings From 0ce898b1140bb960b53b21cd4b30a72b37abdbe4 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Tue, 18 Nov 2025 19:14:17 +0800 Subject: [PATCH 4/5] gh-actions: Run CI on all channels on all branches It's very fast, no need to skip it. Signed-off-by: Daniel Schaefer --- .github/workflows/branches.yml | 43 ---------------------------------- .github/workflows/main.yml | 4 +--- 2 files changed, 1 insertion(+), 46 deletions(-) delete mode 100644 .github/workflows/branches.yml diff --git a/.github/workflows/branches.yml b/.github/workflows/branches.yml deleted file mode 100644 index ed57e7a..0000000 --- a/.github/workflows/branches.yml +++ /dev/null @@ -1,43 +0,0 @@ -name: Branches - -on: - push: - branches-ignore: - - "main" - -env: - CARGO_TERM_COLOR: always - -jobs: - ci: - runs-on: ubuntu-latest - - strategy: - matrix: - rust: - - stable - - beta - - nightly - - steps: - - uses: actions/checkout@v4 - - - name: Set up Rust toolchain - run: | - rustup toolchain install ${{ matrix.rust }} - - - name: Build examples - run: | - cargo build --examples - - - name: Build release config - run: | - cargo build --release - - - name: Check formatting - run: | - cargo fmt --all -- --check - - - name: Check formatting - run: | - cargo clippy -- -D warnings diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8b42b47..0865241 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,9 +1,7 @@ -name: Main +name: CI Build on: push: - branches: - - "main" env: CARGO_TERM_COLOR: always From 6658897f10c926c9e94f5c699d5982b33e15ae21 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Tue, 18 Nov 2025 19:15:46 +0800 Subject: [PATCH 5/5] Fix clippy warnings https://rust-lang.github.io/rust-clippy/rust-1.91.0/index.html#doc_lazy_continuation Signed-off-by: Daniel Schaefer --- src/regs.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/regs.rs b/src/regs.rs index 262dfbd..d03036b 100644 --- a/src/regs.rs +++ b/src/regs.rs @@ -261,6 +261,7 @@ pub struct SmbusSettings { /// /// - false = The auto-incrementing pointer will skip over addresses used by/for channels that are inactive (default) /// - true = he auto-incrementing pointer will not skip over addresses used by/for channels that are inactive. + /// /// When these channels are disabled, if a read is performed, it will read FF. pub no_skip: bool, #[packed_field(bits = "0")] @@ -334,31 +335,37 @@ pub struct Slow { #[packed_field(bits = "6")] /// - false = The SLOW pin has not transitioned low to high since the last REFRESH command /// - true = The SLOW pin has transitioned low to high since the last REFRESH command + /// /// The bit is reset to ‘0’ by a REFRESH or REFRESH_G command. pub slow_lh: bool, #[packed_field(bits = "5")] /// - false = The SLOW pin has not transitioned low to high since the last REFRESH command /// - true = The SLOW pin has transitioned low to high since the last REFRESH command + /// /// The bit is reset to ‘0’ by a REFRESH or REFRESH_G command. pub slow_hl: bool, #[packed_field(bits = "4")] /// - false = Disables limited REFRESH function to take place on the rising edge of the SLOW pin /// - true = Enables limited REFRESH function to take place on the rising edge of the SLOW pin + /// /// The bit is not reset automatically, it must be written to be changed. pub r_rise: bool, #[packed_field(bits = "3")] /// - false = Disables limited REFRESH_V function to take place on the rising edge of the SLOW pin /// - true = Enables limited REFRESH_V function to take place on the rising edge of the SLOW pin + /// /// The bit is not reset automatically, it must be written to be changed. pub r_v_rise: bool, #[packed_field(bits = "2")] /// - false = Disables limited REFRESH function to take place on the rising edge of the SLOW pin /// - true = Enables limited REFRESH function to take place on the rising edge of the SLOW pin + /// /// The bit is not reset automatically, it must be written to be changed. pub r_fall: bool, #[packed_field(bits = "1")] /// - false = Disables limited REFRESH_V function to take place on the rising edge of the SLOW pin /// - true = Enables limited REFRESH_V function to take place on the rising edge of the SLOW pin + /// /// The bit is not reset automatically, it must be written to be changed. pub r_v_fall: bool, }