From a27bc5ff72103522d1d9711843c57c1287c8a1fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 10 Dec 2024 13:46:21 -0300 Subject: [PATCH 1/7] leak: Add gpiochip in builder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/leak.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/leak.rs b/src/leak.rs index 675fc9067..6f59c7c75 100644 --- a/src/leak.rs +++ b/src/leak.rs @@ -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}; @@ -28,6 +28,7 @@ impl AnyHardware for LeakDetector { pub struct LeakBuilder { pin_number: u32, info: PeripheralInfo, + gpiochip: PathBuf, } impl LeakBuilder { @@ -38,6 +39,7 @@ impl LeakBuilder { peripheral: Peripherals::Leak, class: vec![PeripheralClass::DigitalInput], }, + gpiochip: "/dev/gpiochip0".into(), } } @@ -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> { 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() From e9d66c609981606b8e97a37fbcb1a07a70dd7ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 10 Dec 2024 13:46:32 -0300 Subject: [PATCH 2/7] led: Add gpiochip in builder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/led.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/led.rs b/src/led.rs index d249329e7..e8ca0edf7 100644 --- a/src/led.rs +++ b/src/led.rs @@ -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}; @@ -28,6 +28,7 @@ impl AnyHardware for LedController { pub struct LedControllerBuilder { pub pins: Vec, pub info: PeripheralInfo, + pub gpiochip: PathBuf, } impl LedControllerBuilder { @@ -38,6 +39,7 @@ impl LedControllerBuilder { peripheral: Peripherals::Gpio, class: vec![PeripheralClass::Led], }, + gpiochip: "/dev/gpiochip0".into(), } } @@ -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) @@ -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) From b6947358cd3a70805623e0d8a1e770b08d2ab416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 10 Dec 2024 13:46:53 -0300 Subject: [PATCH 3/7] pca: Add gpiochip in builder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/pca9685.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pca9685.rs b/src/pca9685.rs index 130ba9988..9b380ca55 100644 --- a/src/pca9685.rs +++ b/src/pca9685.rs @@ -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}, @@ -35,6 +35,7 @@ pub struct Pca9685DeviceBuilder { address: PwmAddress, oe_pin_number: u32, info: PeripheralInfo, + gpiochip: PathBuf, } impl Pca9685DeviceBuilder { @@ -47,6 +48,7 @@ impl Pca9685DeviceBuilder { peripheral: Peripherals::Pca9685, class: vec![PeripheralClass::Pwm], }, + gpiochip: "/dev/gpiochip0".into(), } } @@ -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> { 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() From e04570987a2a0b76807f3f9e684a4101df88f7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 10 Dec 2024 13:47:04 -0300 Subject: [PATCH 4/7] lib: Turn BMP390 public MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 405537bd3..c0117292c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; From 5c04e48a671e4df6ffa37c75666465b668679165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 10 Dec 2024 13:47:17 -0300 Subject: [PATCH 5/7] lib: Add build_navigator_v2_pi5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index c0117292c..7b5c5a12b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -400,4 +400,52 @@ impl NavigatorBuilder { Navigator { devices } } + + pub fn build_navigator_v2_pi5(self) -> Navigator { + let gpiochip = "/dev/gpiochip4"; + let devices: Vec> = 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 } + } } From 77e17cfd1c428f5605eca7509c41d8dd0b3bc20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 10 Dec 2024 13:47:31 -0300 Subject: [PATCH 6/7] pca: Add more expect messsages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/pca9685.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pca9685.rs b/src/pca9685.rs index 9b380ca55..f80845ad1 100644 --- a/src/pca9685.rs +++ b/src/pca9685.rs @@ -111,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, From 7407a829423068fe9c4530c8fdc51df29f705f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 10 Dec 2024 13:48:17 -0300 Subject: [PATCH 7/7] examples: Add led-custom-builder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- examples/led-custom-builder.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/led-custom-builder.rs diff --git a/examples/led-custom-builder.rs b/examples/led-custom-builder.rs new file mode 100644 index 000000000..6f6c40398 --- /dev/null +++ b/examples/led-custom-builder.rs @@ -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()); + } +}