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

Fix PWM #86

Merged
merged 5 commits into from
Nov 27, 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
18 changes: 16 additions & 2 deletions examples/raspberry-pi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
}
}
}
4 changes: 3 additions & 1 deletion src/bmp280.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion src/icm20689.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}");
Copy link
Member

@joaoantoniocardoso joaoantoniocardoso Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this should be fixed to "Failed to build ICM20689", as this is not a formatted string, same for the others. The error message is automatically shown by the underlying unwrap mechanism.

for _ in 0..10 {
println!("ICM20689 gyroscope: {:?}", imu.read_angular_velocity());
println!("ICM20689 accelerometers: {:?}", imu.read_acceleration());
Expand Down
4 changes: 3 additions & 1 deletion src/leak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
28 changes: 20 additions & 8 deletions src/pca9685.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,20 @@ impl Pca9685DeviceBuilder {
/// Builds the `Pca9685Device`.
pub fn build(self) -> Result<Pca9685Device, Box<dyn Error>> {
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)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering what a low/high direction even means, here it is:

"direction" ... reads as either "in" or "out". This value may
normally be written. Writing as "out" defaults to
initializing the value as low. To ensure glitch free
operation, values "low" and "high" may be written to
configure the GPIO as an output with that initial value.

reference

So here we are configuring the pin to be an output with the initial state as high.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pin
};

pwm.reset_internal_driver_state();
RaulTrombin marked this conversation as resolved.
Show resolved Hide resolved
pwm.use_external_clock().unwrap();
pwm.enable().unwrap();

Ok(Pca9685Device {
pwm,
oe_pin,
Expand Down Expand Up @@ -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));
}
}
Expand Down