-
Notifications
You must be signed in to change notification settings - Fork 6
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
Fix PWM #86
Changes from all commits
179ac24
a3ac817
292c4a3
6d5afd2
d4f1f47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
So here we are configuring the pin to be an There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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)); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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.