diff --git a/src/output_devices.rs b/src/output_devices.rs index d3d1270..441b951 100644 --- a/src/output_devices.rs +++ b/src/output_devices.rs @@ -737,10 +737,11 @@ impl Servo { /// Set servo to any position between min and max. /// value must be between -1 (the minimun position) and +1 (the maximum position). pub fn set_position(&mut self, value: f64) { - if value >= -1.0 && value <= 1.0 { + if (-1.0..=1.0).contains(&value) { // Map value form [-1, 1] to [min_pulse_width, max_pulse_width] linearly let range: f64 = (self.max_pulse_width - self.min_pulse_width) as f64; - let pulse_width: u64 = self.min_pulse_width + (((value + 1.0)/2.0) * range).round() as u64; + let pulse_width: u64 = + self.min_pulse_width + (((value + 1.0) / 2.0) * range).round() as u64; if self .pin .set_pwm( @@ -751,12 +752,11 @@ impl Servo { { println!("Failed to set servo to a new position"); } - } - else { + } else { println!("set_position value must be between -1 and 1"); } } - + /// Set the servo's minimum pulse width pub fn set_min_pulse_width(&mut self, value: u64) { if value >= self.max_pulse_width { @@ -796,12 +796,8 @@ impl Servo { } pub fn detach(&mut self) { - if self - .pin - .clear_pwm() - .is_err() - { + if self.pin.clear_pwm().is_err() { println!("Failed to detach servo") } - } + } }