diff --git a/Cargo.toml b/Cargo.toml index 9402fee3b..8ca8e2a9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ sk6812_rpi = "0.1" [dev-dependencies] criterion = { version = "0.5.1", features = ["html_reports"] } +rand = "0.8.5" [[bench]] name = "bench" diff --git a/benches/bench.rs b/benches/bench.rs index 0062da434..3257ae2e9 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -24,7 +24,6 @@ fn navigator_benchmark(c: &mut Criterion) { // Benchmark Outputs bench!(set_pwm_enable(false)); bench!(set_pwm_duty_cycle(0, 0.1)); - bench!(set_pwm_frequency(60.0)); bench!(set_neopixel(&[[0, 0, 0]])); bench!(set_led(UserLed::Led1, false)); bench!(set_led_toggle(UserLed::Led1)); diff --git a/examples/raspberry-pi.rs b/examples/raspberry-pi.rs index a9d8d8bde..c0511c180 100644 --- a/examples/raspberry-pi.rs +++ b/examples/raspberry-pi.rs @@ -2,6 +2,8 @@ use navigator_rs::Navigator; use std::thread::sleep; use std::time::Duration; +use rand::seq::SliceRandom; + fn main() { println!("Creating your navigator module!"); let mut nav = Navigator::new(); @@ -10,8 +12,20 @@ fn main() { nav.set_pwm_enable(true); nav.set_pwm_frequency(60.0); + let colors = [ + [255u8, 0, 0], + [0, 255, 0], + [0, 0, 255], + [255, 255, 0], + [0, 255, 255], + [255, 0, 255], + ]; + loop { - nav.set_duty_cycle_all(0.25); - sleep(Duration::from_millis(1000)); + for value in [0.25, 0.5, 0.75, 0.5] { + nav.set_neopixel(&[*colors.choose(&mut rand::thread_rng()).unwrap()]); + nav.set_duty_cycle_all(value); + sleep(Duration::from_millis(1000)); + } } } diff --git a/src/bmp280.rs b/src/bmp280.rs index bd658008b..21a6c5624 100644 --- a/src/bmp280.rs +++ b/src/bmp280.rs @@ -110,7 +110,9 @@ mod tests { #[test] fn test_bmp280_pi_4() { - let mut baro = Bmp280Device::builder().build().unwrap(); + let mut baro = Bmp280Device::builder() + .build() + .expect("Failed to build BMP280"); for _ in 0..10 { println!("BMP280 temperature: {:?}", baro.read_temperature()); println!("BMP280 pressure: {:?}", baro.read_pressure()); diff --git a/src/icm20689.rs b/src/icm20689.rs index 600daf79e..d3eace0c9 100644 --- a/src/icm20689.rs +++ b/src/icm20689.rs @@ -123,7 +123,9 @@ mod tests { #[test] fn test_icm20689_pi_4() { - let mut imu = Icm20689Device::builder().build().unwrap(); + let mut imu = Icm20689Device::builder() + .build() + .expect("Failed to build ICM20689: {error:?}"); for _ in 0..10 { println!("ICM20689 gyroscope: {:?}", imu.read_angular_velocity()); println!("ICM20689 accelerometers: {:?}", imu.read_acceleration()); diff --git a/src/leak.rs b/src/leak.rs index b33d4dae4..3fb8e2290 100644 --- a/src/leak.rs +++ b/src/leak.rs @@ -82,7 +82,9 @@ mod tests { #[test] fn test_leak_pi_4() { - let leak = LeakDetector::builder().build().unwrap(); + let leak = LeakDetector::builder() + .build() + .expect("Failed to build leak detector: {error:?}"); for _ in 0..10 { println!("Leak detector: {}", leak.is_leak_detected().unwrap()); sleep(Duration::from_millis(1000)); diff --git a/src/pca9685.rs b/src/pca9685.rs index 89028cceb..846eac117 100644 --- a/src/pca9685.rs +++ b/src/pca9685.rs @@ -86,16 +86,20 @@ impl Pca9685DeviceBuilder { /// Builds the `Pca9685Device`. pub fn build(self) -> Result> { let device = I2cdev::new(self.i2c_bus)?; - let pwm = Pca9685::new(device, self.address).expect("Failed to open PWM controller"); + 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(30)); - pin.set_direction(Direction::Low)?; + pin.set_direction(Direction::High)?; pin }; + pwm.reset_internal_driver_state(); + pwm.use_external_clock().unwrap(); + pwm.enable().unwrap(); + Ok(Pca9685Device { pwm, oe_pin, @@ -187,18 +191,26 @@ mod tests { //TODO: Not working #[test] fn test_pca9685_pi_4() { - let mut pwm = Pca9685Device::builder().build().unwrap(); + let mut pwm = Pca9685Device::builder() + .build() + .expect("Error building PCA9685"); + println!("PCA9685: Start initial configuration"); - pwm.enable_output(false).unwrap(); - pwm.set_frequency(60.0).unwrap(); - pwm.set_duty_cycle_all(0.5).unwrap(); + + pwm.enable_output(false) + .expect("Error in while enabling output"); + pwm.set_frequency(60.0).expect("Error in setting frequency"); + pwm.set_duty_cycle_all(0.5) + .expect("Error in configuring duty cycle"); + println!("PCA9685: Enabling PWM output"); - pwm.enable_output(true).unwrap(); + pwm.enable_output(true).expect("Error in enabling output"); sleep(Duration::from_millis(1000)); for duty_cycle in [0.0, 1.0, 0.0, 1.0, 0.5] { for channel in 0..16 { println!("PCA9685: Channel {channel} value {duty_cycle:.1}"); - pwm.set_duty_cycle(channel, duty_cycle).unwrap(); + pwm.set_duty_cycle(channel, duty_cycle) + .expect("Error in setting duty cycle: {error:?}"); sleep(Duration::from_millis(100)); } }