Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support to raspberry pi 5 #90

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/icm20689.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use std::error::Error;
use std::{error::Error, thread::sleep, time::Duration};

use icm20689::{self, Builder as ImuBuilder, SpiInterface, ICM20689};
use linux_embedded_hal::spidev::{SpiModeFlags, SpidevOptions};
use linux_embedded_hal::sysfs_gpio::Direction;
use linux_embedded_hal::{Delay, Pin, Spidev};
use linux_embedded_hal::{
gpio_cdev::{Chip, LineRequestFlags},
CdevPin,
};
use linux_embedded_hal::{Delay, Spidev};

use crate::peripherals::{
AccelerometerSensor, AnyHardware, GyroscopeSensor, PeripheralClass, PeripheralInfo, Peripherals,
};

pub struct Icm20689Device {
imu: ICM20689<SpiInterface<Spidev, Pin>>,
imu: ICM20689<SpiInterface<Spidev, CdevPin>>,
info: PeripheralInfo,
}

Expand All @@ -36,7 +39,7 @@ impl AnyHardware for Icm20689Device {

pub struct Icm20689Builder {
spi_device: String,
cs_pin_number: u64,
cs_pin_number: u32,
info: PeripheralInfo,
}

Expand Down Expand Up @@ -67,7 +70,7 @@ impl Icm20689Builder {
/// # Arguments
///
/// * `pin_number` - The SPI pin number (e.g., 16).
pub fn with_cs_pin(mut self, pin_number: u64) -> Self {
pub fn with_cs_pin(mut self, pin_number: u32) -> Self {
self.cs_pin_number = pin_number;
self
}
Expand All @@ -86,11 +89,18 @@ impl Icm20689Builder {
.build();
spi.configure(&options)?;

let cs_pin = Pin::new(self.cs_pin_number);
cs_pin.export()?;
std::thread::sleep(std::time::Duration::from_millis(60));
cs_pin.set_direction(Direction::Out)?;
cs_pin.set_value(1)?;
let cs_pin = {
let mut chip = Chip::new("/dev/gpiochip0").unwrap();
let line = chip
.get_line(self.cs_pin_number)
.unwrap()
.request(LineRequestFlags::OUTPUT, 1, "cs-icm20689")
.expect("Failed to request CS pin");
let pin = CdevPin::new(line)?;
sleep(Duration::from_millis(30));
pin.set_value(1)?;
pin
};

let mut imu = ImuBuilder::new_spi(spi, cs_pin);

Expand Down
23 changes: 15 additions & 8 deletions src/leak.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{error::Error, thread::sleep, time::Duration};

use linux_embedded_hal::sysfs_gpio::{Direction, Pin};
use linux_embedded_hal::gpio_cdev::{Chip, LineHandle, LineRequestFlags};

use crate::peripherals::{AnyHardware, LeakSensor, PeripheralClass, PeripheralInfo, Peripherals};

pub struct LeakDetector {
pin: Pin,
pin: LineHandle,
info: PeripheralInfo,
}

Expand All @@ -26,7 +26,7 @@ impl AnyHardware for LeakDetector {
}

pub struct LeakBuilder {
pin_number: u64,
pin_number: u32,
info: PeripheralInfo,
}

Expand All @@ -46,7 +46,7 @@ impl LeakBuilder {
/// # Arguments
///
/// * `pin_number` - The GPIO pin number.
pub fn with_pin(mut self, pin_number: u64) -> Self {
pub fn with_pin(mut self, pin_number: u32) -> Self {
self.pin_number = pin_number;
self
}
Expand All @@ -57,10 +57,17 @@ impl LeakBuilder {
}

pub fn build(self) -> Result<LeakDetector, Box<dyn Error>> {
let pin = Pin::new(self.pin_number);
pin.export()?;
sleep(Duration::from_millis(60));
pin.set_direction(Direction::In)?;
let pin = {
let mut chip = Chip::new("/dev/gpiochip0")?;
let pin = chip
.get_line(self.pin_number)
.unwrap()
.request(LineRequestFlags::INPUT, 1, "pin-leak")
.expect("Failed to configure LEAK pin");
sleep(Duration::from_millis(30));
pin
};

Ok(LeakDetector {
pin,
info: self.info,
Expand Down
24 changes: 15 additions & 9 deletions src/led.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{thread::sleep, time::Duration};

use linux_embedded_hal::{sysfs_gpio::Direction, Pin};
use linux_embedded_hal::gpio_cdev::{Chip, LineHandle, LineRequestFlags};

use crate::peripherals::{AnyHardware, LedBehaviour, PeripheralClass, PeripheralInfo, Peripherals};

pub struct LedController {
pub leds: Vec<Pin>,
pub leds: Vec<LineHandle>,
pub info: PeripheralInfo,
}

Expand All @@ -26,7 +26,7 @@ impl AnyHardware for LedController {
}

pub struct LedControllerBuilder {
pub pins: Vec<u64>,
pub pins: Vec<u32>,
pub info: PeripheralInfo,
}

Expand All @@ -42,7 +42,7 @@ impl LedControllerBuilder {
}

/// Adds a GPIO pin to control an LED.
pub fn add_led_pin(mut self, pin_number: u64) -> Self {
pub fn add_led_pin(mut self, pin_number: u32) -> Self {
self.pins.push(pin_number);
self
}
Expand All @@ -65,12 +65,18 @@ impl LedControllerBuilder {
self = self.configure_navigator();
}

let mut chip = Chip::new("/dev/gpiochip0").unwrap();
for &pin_number in &self.pins {
let pin = Pin::new(pin_number);
pin.export().expect("Failed to export LED pin");
sleep(Duration::from_millis(60));
pin.set_direction(Direction::Out)
.expect("Failed to set direction");
let pin = chip
.get_line(pin_number)
.unwrap()
.request(
LineRequestFlags::OUTPUT,
1,
&format!("user-led-{pin_number}"),
)
.expect("Failed to request LED pin");
sleep(Duration::from_millis(30));
pin.set_value(1).expect("Failed to set initial LED value");
leds.push(pin);
}
Expand Down
32 changes: 18 additions & 14 deletions src/pca9685.rs
Original file line number Diff line number Diff line change
@@ -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<I2cdev>,
oe_pin: Pin,
oe_pin: LineHandle,
info: PeripheralInfo,
}

Expand All @@ -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,
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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/gpiochip0")?;
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
};

Expand All @@ -110,12 +117,9 @@ impl Pca9685DeviceBuilder {

impl PwmBehaviour for Pca9685Device {
fn enable_output(&mut self, enable: bool) -> Result<(), Box<dyn Error>> {
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(())
}

Expand Down
Loading