Ver Fonte

Add step to randomized pulse width

Kevin Lee há 10 meses atrás
pai
commit
873813679d
1 ficheiros alterados com 19 adições e 14 exclusões
  1. 19 14
      src/config/pen.rs

+ 19 - 14
src/config/pen.rs

@@ -327,7 +327,7 @@ pub struct RandomizePen {
     speed: Option<(f64, f64, f64)>,
     power: Option<(f64, f64, f64)>,
     frequency: Option<(u32, u32, u32)>,
-    pulse_width: Option<(PulseWidth, PulseWidth)>,
+    pulse_width: Option<(PulseWidth, PulseWidth, usize)>,
 }
 
 impl RandomizePen {
@@ -344,34 +344,39 @@ impl RandomizePen {
             .take(self.count)
             .enumerate()
         {
-            if let Some((min, max, incr)) = self.speed {
-                let offset: usize = rand::thread_rng().gen_range(0..=((max - min) / incr) as usize);
-                let value: f64 = min + incr * offset as f64;
+            if let Some((min, max, step)) = self.speed {
+                let offset: usize = rand::thread_rng().gen_range(0..=((max - min) / step) as usize);
+                let value: f64 = min + step * offset as f64;
                 debug!("Randomizing speed for pen #{} to {}", index, value);
                 *pen.speed = value;
             }
 
-            if let Some((min, max, incr)) = self.power {
-                let offset: usize = rand::thread_rng().gen_range(0..=((max - min) / incr) as usize);
-                let value: f64 = min + incr * offset as f64;
+            if let Some((min, max, step)) = self.power {
+                let offset: usize = rand::thread_rng().gen_range(0..=((max - min) / step) as usize);
+                let value: f64 = min + step * offset as f64;
                 debug!("Randomizing power for pen #{} to {}", index, value);
                 *pen.power = value;
             }
 
-            if let Some((min, max, incr)) = self.frequency {
-                let offset: usize = rand::thread_rng().gen_range(0..=((max - min) / incr) as usize);
-                let value: u32 = min + incr * offset as u32;
+            if let Some((min, max, step)) = self.frequency {
+                let offset: usize = rand::thread_rng().gen_range(0..=((max - min) / step) as usize);
+                let value: u32 = min + step * offset as u32;
                 debug!("Randomizing frequency for pen #{} to {}", index, value);
                 *pen.frequency = value;
                 *pen.frequency_2 = value.try_into().unwrap();
             }
 
-            if let Some((min, max)) = self.pulse_width {
+            if let Some((min, max, step)) = self.pulse_width {
                 let mut pw = PulseWidth::iter();
-                let mut v: Vec<PulseWidth> = vec![pw.find(|x| *x == min).unwrap()];
-                v.extend(pw.take_while_inclusive(|x| *x != max).collect_vec());
+                let mut values: Vec<PulseWidth> = vec![pw.find(|x| *x == min).unwrap()];
+                values.extend(
+                    pw.skip(step - 1)
+                        .step_by(step)
+                        .take_while_inclusive(|x| *x != max)
+                        .collect_vec(),
+                );
 
-                let width: &PulseWidth = v
+                let width: &PulseWidth = values
                     .choose_multiple(&mut rand::thread_rng(), 1)
                     .next()
                     .unwrap();