瀏覽代碼

Add pen setting validation

Kevin Lee 10 月之前
父節點
當前提交
914d7bc01a
共有 4 個文件被更改,包括 100 次插入2 次删除
  1. 1 1
      README.md
  2. 5 0
      src/config/pen.rs
  3. 50 1
      src/ezcad/pen.rs
  4. 44 0
      src/ezcad/types.rs

+ 1 - 1
README.md

@@ -95,7 +95,7 @@ Op:
     Speed: [100, 1000, 100] # [min, max, step]
     Power: [10, 100, 5] # [min, max, step]
     Frequency: [20000, 100000, 1000] # [min, max, step]
-    PulseWidth: [2, 250, 2] # [min, max, step]
+    PulseWidth: [2, 350, 2] # [min, max, step]
 ```
 
 Exporting a pen to a file:

+ 5 - 0
src/config/pen.rs

@@ -106,6 +106,7 @@ impl PatchPen {
         debug!("Patching pen #{}", self.pen);
         let pen: &mut Pen = pens.get_mut(self.pen).expect("Invalid pen index");
         self.patch.patch(pen);
+        pen.check_settings();
     }
 }
 
@@ -290,6 +291,8 @@ impl PatternField {
 
             // Always enable custom settings for pen
             *dst.use_default = 0;
+
+            dst.check_settings();
         }
     }
 }
@@ -385,6 +388,8 @@ impl RandomizePen {
                 *pen.pulse_width = value;
                 *pen.pulse_width_2 = value.try_into().unwrap();
             }
+
+            pen.check_settings();
         }
     }
 }

+ 50 - 1
src/ezcad/pen.rs

@@ -7,10 +7,11 @@ use std::{
 
 use binrw::{BinRead, BinWrite, BinWriterExt, FilePtr64};
 use diff::Diff;
+use log::{error, warn};
 
 use crate::{
     field_of::FieldOf,
-    types::{Bool, Field, Rgba, WString, WobbleType, F64, U32},
+    types::{Bool, Field, PulseWidth, Rgba, WString, WobbleType, F64, U32},
 };
 
 #[derive(BinRead, Debug)]
@@ -120,6 +121,54 @@ impl Pen {
             .write_all(buffer.into_inner().as_slice())
             .expect("Failed to write to output file");
     }
+
+    pub fn check_settings(&self) -> bool {
+        let mut ret: bool = true;
+
+        if *self.frequency != *self.frequency_2 as u32 {
+            error!(
+                "Mismatch pen internal frequency setting: ({}, {:.3})",
+                *self.frequency, *self.frequency_2
+            );
+            ret = false;
+        }
+
+        if *self.pulse_width != *self.pulse_width_2 as u32 {
+            error!(
+                "Mismatch pen internal pulse width setting: ({}, {:.3})",
+                *self.pulse_width, *self.pulse_width_2
+            );
+            ret = false;
+        }
+
+        match PulseWidth::try_from(*self.pulse_width) {
+            Ok(pw) => match *self.frequency {
+                freq if freq < pw.min_freq() => {
+                    warn!(
+                        "Pen frequency of {} lower than pulse width minimum frequency of {}",
+                        *self.frequency,
+                        pw.min_freq()
+                    );
+                    ret = false;
+                }
+                freq if freq > pw.max_freq() => {
+                    warn!(
+                        "Pen frequency of {} higher than pulse width maximum frequency of {}",
+                        *self.frequency,
+                        pw.max_freq()
+                    );
+                    ret = false;
+                }
+                _ => (),
+            },
+            Err(_) => {
+                warn!("Invalid pen pulse width value: {}", *self.pulse_width);
+                ret = false;
+            }
+        }
+
+        ret
+    }
 }
 
 // Custom Debug implementation to only print known fields

+ 44 - 0
src/ezcad/types.rs

@@ -291,3 +291,47 @@ pub enum PulseWidth {
     Ns350 = 350,
     Ns500 = 500,
 }
+
+impl PulseWidth {
+    pub fn min_freq(&self) -> u32 {
+        match self {
+            PulseWidth::Ns2 => 850_000,
+            PulseWidth::Ns4 => 500_000,
+            PulseWidth::Ns6 => 320_000,
+            PulseWidth::Ns8 => 250_000,
+            PulseWidth::Ns12 => 170_000,
+            PulseWidth::Ns20 => 115_000,
+            PulseWidth::Ns30 => 90_000,
+            PulseWidth::Ns45 => 75_000,
+            PulseWidth::Ns60 => 65_000,
+            PulseWidth::Ns80 => 60_000,
+            PulseWidth::Ns100 => 45_000,
+            PulseWidth::Ns150 => 30_000,
+            PulseWidth::Ns200 => 25_000,
+            PulseWidth::Ns250 => 25_000,
+            PulseWidth::Ns350 => 25_000,
+            PulseWidth::Ns500 => 25_000,
+        }
+    }
+
+    pub fn max_freq(&self) -> u32 {
+        match self {
+            PulseWidth::Ns2 => 4_000_000,
+            PulseWidth::Ns4 => 4_000_000,
+            PulseWidth::Ns6 => 4_000_000,
+            PulseWidth::Ns8 => 4_000_000,
+            PulseWidth::Ns12 => 3_000_000,
+            PulseWidth::Ns20 => 3_000_000,
+            PulseWidth::Ns30 => 3_000_000,
+            PulseWidth::Ns45 => 2_000_000,
+            PulseWidth::Ns60 => 2_000_000,
+            PulseWidth::Ns80 => 2_000_000,
+            PulseWidth::Ns100 => 1_000_000,
+            PulseWidth::Ns150 => 1_000_000,
+            PulseWidth::Ns200 => 1_000_000,
+            PulseWidth::Ns250 => 900_000,
+            PulseWidth::Ns350 => 600_000,
+            PulseWidth::Ns500 => 500_000,
+        }
+    }
+}