From 2242c3e8f73e94b2c45a35f5b876b05f2997bc0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 27 Nov 2024 17:48:40 -0300 Subject: [PATCH] pca9685: Move to use new gpio_cdev over sysfs_gpio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/pca9685.rs | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/pca9685.rs b/src/pca9685.rs index a8b599765..872e811e1 100644 --- a/src/pca9685.rs +++ b/src/pca9685.rs @@ -1,13 +1,16 @@ use std::{error::Error, thread::sleep, time::Duration}; -use linux_embedded_hal::{sysfs_gpio::Direction, I2cdev, Pin}; +use linux_embedded_hal::{ + gpio_cdev::{Chip, LineHandle, LineRequestFlags}, + I2cdev, +}; use pwm_pca9685::{Address as PwmAddress, Channel, Pca9685}; use crate::peripherals::{AnyHardware, PeripheralClass, PeripheralInfo, Peripherals, PwmBehaviour}; pub struct Pca9685Device { pwm: Pca9685, - oe_pin: Pin, + oe_pin: LineHandle, info: PeripheralInfo, } @@ -30,7 +33,7 @@ impl AnyHardware for Pca9685Device { pub struct Pca9685DeviceBuilder { i2c_bus: String, address: PwmAddress, - oe_pin_number: u64, + oe_pin_number: u32, info: PeripheralInfo, } @@ -72,7 +75,7 @@ impl Pca9685DeviceBuilder { /// # Arguments /// /// * `pin_number` - The GPIO pin number for OE. - pub fn with_oe_pin(mut self, pin_number: u64) -> Self { + pub fn with_oe_pin(mut self, pin_number: u32) -> Self { self.oe_pin_number = pin_number; self } @@ -89,10 +92,14 @@ impl Pca9685DeviceBuilder { let mut pwm = Pca9685::new(device, self.address).expect("Failed to open PWM controller"); let oe_pin = { - let pin = Pin::new(self.oe_pin_number); - pin.export()?; - sleep(Duration::from_millis(60)); - pin.set_direction(Direction::High)?; + let mut chip = Chip::new("/dev/gpiochip4")?; + let pin = chip + .get_line(self.oe_pin_number) + .unwrap() + .request(LineRequestFlags::OUTPUT, 1, "oe-pin-pca9685") + .expect("Failed to request OE pin"); + sleep(Duration::from_millis(30)); + pin.set_value(1)?; pin }; @@ -110,12 +117,9 @@ impl Pca9685DeviceBuilder { impl PwmBehaviour for Pca9685Device { fn enable_output(&mut self, enable: bool) -> Result<(), Box> { - let value = if enable { - Direction::Low - } else { - Direction::High - }; // Active low OE pin - self.oe_pin.set_direction(value).unwrap(); + // Active low OE pin + let value = if enable { 0 } else { 1 }; + self.oe_pin.set_value(value).unwrap(); Ok(()) }