rust-embedded / rust-sysfs-pwm

Linux PWM Access via Sysfs in Rust
Apache License 2.0
47 stars 16 forks source link

Added get_duty_cycle and set_duty_cycle functions #13

Closed Andful closed 4 years ago

Andful commented 4 years ago

README.md documents functions get_duty_cycle and set_duty_cycle but does not contain its implementations. This pull request should fix that.

rust-highfive commented 4 years ago

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @ryankurte (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

Andful commented 4 years ago

I just saw from commit c3c0d3e020606dea2910d6aa20294d0a6fa09982 that the functions were removed. Either we add the functions back or we should remove the functions from the README.md file.

posborne commented 4 years ago

Hey @Andful, were you able to test this change on hardware to confirm its function. I don't have a setup handy for PWM testing but am find with inclusion if we have confirmed that its working.

Andful commented 4 years ago

@posborne yes. I ran the code in a Onion Omega 2. You can see the program running on this video. I ran the following program.

use sysfs_pwm::Pwm; 
use std::error::Error;

const ONION_OMEGA_MIN_PERIOD : u32 = 25;

const PWM_CHIP: u32 = 0;
const PWM_NUMBER: u32 = 0;

fn main() -> Result<(), Box<dyn Error>> {
    let pwm = Pwm::new(PWM_CHIP, PWM_NUMBER)?;
    let args: Vec<String> = std::env::args().collect();

    let period = ONION_OMEGA_MIN_PERIOD * (args[1].parse::<u32>()?);

    pwm.with_exported(|| {
        pwm.enable(true)?;
        pwm.set_duty_cycle(0.0)?;
        pwm.set_period_ns(period)?;
        let mut values = (0..=period).map(|x| (x as f32)/(period as f32)).collect::<Vec::<f32>>();
        loop{
            for p in values.iter() {
                pwm.set_duty_cycle(*p)?;
                let read_value = pwm.get_duty_cycle()?;
                assert!((read_value*(period as f32) - p*(period as f32)).abs() <= 0.5);
            }
            values.reverse();
        }
    })?;

    Ok(())
}
bors[bot] commented 4 years ago

Build succeeded: