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

Add final raspberry pi 5 support #95

Merged
merged 7 commits into from
Dec 10, 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
29 changes: 29 additions & 0 deletions examples/led-custom-builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use navigator_rs::{Navigator, UserLed};
use std::thread::sleep;
use std::time::Duration;

use rand::seq::SliceRandom;

fn main() {
let mut nav = Navigator::create().build_navigator_v2_pi5();

let colors = [
[40u8, 0, 0],
[0, 40, 0],
[0, 0, 40],
[40, 40, 0],
[0, 40, 40],
[40, 0, 40],
];

println!("LED show started!");
loop {
nav.set_neopixel(&[*colors.choose(&mut rand::thread_rng()).unwrap()]);
for led in [UserLed::Led1, UserLed::Led2, UserLed::Led3] {
nav.set_led_toggle(led);
sleep(Duration::from_millis(300));
}

println!("{:?}", nav.read_all());
}
}
11 changes: 9 additions & 2 deletions src/leak.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error::Error, thread::sleep, time::Duration};
use std::{error::Error, path::PathBuf, thread::sleep, time::Duration};

use linux_embedded_hal::gpio_cdev::{Chip, LineHandle, LineRequestFlags};

Expand Down Expand Up @@ -28,6 +28,7 @@ impl AnyHardware for LeakDetector {
pub struct LeakBuilder {
pin_number: u32,
info: PeripheralInfo,
gpiochip: PathBuf,
}

impl LeakBuilder {
Expand All @@ -38,6 +39,7 @@ impl LeakBuilder {
peripheral: Peripherals::Leak,
class: vec![PeripheralClass::DigitalInput],
},
gpiochip: "/dev/gpiochip0".into(),
}
}

Expand All @@ -56,9 +58,14 @@ impl LeakBuilder {
self
}

pub fn with_gpiochip(mut self, gpiochip: &str) -> Self {
self.gpiochip = gpiochip.into();
self
}

pub fn build(self) -> Result<LeakDetector, Box<dyn Error>> {
let pin = {
let mut chip = Chip::new("/dev/gpiochip0")?;
let mut chip = Chip::new(self.gpiochip)?;
let pin = chip
.get_line(self.pin_number)
.unwrap()
Expand Down
11 changes: 9 additions & 2 deletions src/led.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{thread::sleep, time::Duration};
use std::{path::PathBuf, thread::sleep, time::Duration};

use linux_embedded_hal::gpio_cdev::{Chip, LineHandle, LineRequestFlags};

Expand Down Expand Up @@ -28,6 +28,7 @@ impl AnyHardware for LedController {
pub struct LedControllerBuilder {
pub pins: Vec<u32>,
pub info: PeripheralInfo,
pub gpiochip: PathBuf,
}

impl LedControllerBuilder {
Expand All @@ -38,6 +39,7 @@ impl LedControllerBuilder {
peripheral: Peripherals::Gpio,
class: vec![PeripheralClass::Led],
},
gpiochip: "/dev/gpiochip0".into(),
}
}

Expand All @@ -52,6 +54,11 @@ impl LedControllerBuilder {
self
}

pub fn with_gpiochip(mut self, gpiochip: &str) -> Self {
self.gpiochip = gpiochip.into();
self
}

/// Configures the GPIO pins for the default Navigator board.
pub fn configure_navigator(self) -> Self {
self.add_led_pin(11).add_led_pin(24).add_led_pin(25)
Expand All @@ -65,7 +72,7 @@ impl LedControllerBuilder {
self = self.configure_navigator();
}

let mut chip = Chip::new("/dev/gpiochip0").unwrap();
let mut chip = Chip::new(self.gpiochip).expect("Failed to open GPIO chip");
for &pin_number in &self.pins {
let pin = chip
.get_line(pin_number)
Expand Down
49 changes: 49 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use peripherals::*;
use ads1115::Ads1115Device;
use ak09915::Ak09915Device;
use bmp280::Bmp280Device;
use bmp390::Bmp390Device;
use icm20689::Icm20689Device;
use leak::LeakDetector;
use led::LedController;
Expand Down Expand Up @@ -399,4 +400,52 @@ impl NavigatorBuilder {

Navigator { devices }
}

pub fn build_navigator_v2_pi5(self) -> Navigator {
let gpiochip = "/dev/gpiochip4";
let devices: Vec<Box<dyn AnyHardware>> = vec![
Box::new(
Ads1115Device::builder()
.build()
.expect("Failed to create Ads1115"),
),
Box::new(
Ak09915Device::builder()
.build()
.expect("Failed to create Ak09915"),
),
Box::new(
Bmp390Device::builder()
.build()
.expect("Failed to create Bmp390"),
),
Box::new(
Icm20689Device::builder()
.build()
.expect("Failed to create Icm20689"),
),
Box::new(
LeakDetector::builder()
.with_gpiochip(gpiochip)
.build()
.expect("Failed to create LedDetector"),
),
Box::new(LedController::builder().with_gpiochip(gpiochip).build()),
Box::new(
Pca9685Device::builder()
.with_gpiochip(gpiochip)
.with_i2c_bus("/dev/i2c-3")
.build()
.expect("Failed to create Pca9685"),
),
Box::new(
RgbController::builder()
.with_led_count(self.rgb_led_strip_size)
.build()
.expect("Failed to create RgbController"),
),
];

Navigator { devices }
}
}
16 changes: 12 additions & 4 deletions src/pca9685.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error::Error, thread::sleep, time::Duration};
use std::{error::Error, path::PathBuf, thread::sleep, time::Duration};

use linux_embedded_hal::{
gpio_cdev::{Chip, LineHandle, LineRequestFlags},
Expand Down Expand Up @@ -35,6 +35,7 @@ pub struct Pca9685DeviceBuilder {
address: PwmAddress,
oe_pin_number: u32,
info: PeripheralInfo,
gpiochip: PathBuf,
}

impl Pca9685DeviceBuilder {
Expand All @@ -47,6 +48,7 @@ impl Pca9685DeviceBuilder {
peripheral: Peripherals::Pca9685,
class: vec![PeripheralClass::Pwm],
},
gpiochip: "/dev/gpiochip0".into(),
}
}

Expand Down Expand Up @@ -86,13 +88,18 @@ impl Pca9685DeviceBuilder {
self
}

pub fn with_gpiochip(mut self, gpiochip: &str) -> Self {
self.gpiochip = gpiochip.into();
self
}

/// Builds the `Pca9685Device`.
pub fn build(self) -> Result<Pca9685Device, Box<dyn Error>> {
let device = I2cdev::new(self.i2c_bus)?;
let mut pwm = Pca9685::new(device, self.address).expect("Failed to open PWM controller");

let oe_pin = {
let mut chip = Chip::new("/dev/gpiochip0")?;
let mut chip = Chip::new(self.gpiochip).expect("Failed to open GPIO chip");
let pin = chip
.get_line(self.oe_pin_number)
.unwrap()
Expand All @@ -104,8 +111,9 @@ impl Pca9685DeviceBuilder {
};

pwm.reset_internal_driver_state();
pwm.use_external_clock().unwrap();
pwm.enable().unwrap();
pwm.use_external_clock()
.expect("Failed to use external clock");
pwm.enable().expect("Failed to enable PWM controller");

Ok(Pca9685Device {
pwm,
Expand Down
Loading