浏览代码

More pen work

Kevin Lee 1 年之前
父节点
当前提交
b3c6423c46
共有 4 个文件被更改,包括 74 次插入47 次删除
  1. 6 4
      config.yml
  2. 48 37
      src/config.rs
  3. 11 0
      src/ezcad/types.rs
  4. 9 6
      src/main.rs

+ 6 - 4
config.yml

@@ -1,10 +1,12 @@
 Pens: 
   - !Patch
     Pen: 0
-    Patch:
-      Enabled: true
+    Enabled: true
   - !Clone
     From: 0
     To: 1
-    Patch:
-      Enabled: false
+    Enabled: false
+  - !Pattern
+    From: 1
+    To: 3
+    Field: !Speed -100

+ 48 - 37
src/config.rs

@@ -1,4 +1,4 @@
-use ezcad::{pen::Pen, types::Rgba};
+use ezcad::pen::Pen;
 use log::trace;
 use serde::{Deserialize, Serialize};
 
@@ -17,15 +17,10 @@ impl Patch {
     fn patch(&self, id: usize, target: &mut Vec<Pen>) {
         trace!("Patching settings for pen #{}", id);
 
-        let to_patch = target.get_mut(id).unwrap();
+        let to_patch = target.get_mut(id).expect("Invalid pen index");
 
         if let Some(color) = self.color {
-            *to_patch.color = Rgba {
-                red: color.0,
-                green: color.1,
-                blue: color.2,
-                alpha: 0,
-            };
+            *to_patch.color = color.into()
         }
 
         if let Some(enabled) = self.enabled {
@@ -54,44 +49,78 @@ impl Patch {
 #[derive(Debug, Serialize, Deserialize)]
 #[serde(rename_all = "PascalCase")]
 pub struct PatchPen {
-    pen: u8,
+    pen: usize,
+    #[serde(flatten)]
     patch: Patch,
 }
 
 impl PatchPen {
     fn patch(&self, target: &mut Vec<Pen>) {
-        self.patch.patch(self.pen as usize, target);
+        self.patch.patch(self.pen, target);
     }
 }
 
 #[derive(Debug, Serialize, Deserialize)]
 #[serde(rename_all = "PascalCase")]
 pub struct ClonePen {
-    from: u8,
-    to: u8,
+    from: usize,
+    to: usize,
+    #[serde(flatten)]
     patch: Option<Patch>,
 }
 
 impl ClonePen {
     fn clone(&self, target: &mut Vec<Pen>) {
         trace!("Cloning pen #{} to #{}", self.from, self.to);
-
-        let src = target.get(self.from as usize).unwrap().clone();
-        let dst = target.get_mut(self.to as usize).unwrap();
+        assert!(
+            self.to > self.from,
+            "Target pen must be greater than source pen"
+        );
+
+        // Clone pen
+        let src = target.get(self.from).expect("Invalid pen index").clone();
+        let dst = target.get_mut(self.to).expect("Invalid pen index");
         *dst = src;
 
+        // Patch pen if needed
         if let Some(patch) = &self.patch {
-            patch.patch(self.to as usize, target);
+            patch.patch(self.to, target);
         }
     }
 }
 
+#[derive(Debug, Serialize, Deserialize)]
+pub enum PatternField {
+    Loops(i32),
+    Speed(f64),
+    Power(f64),
+    Frequency(i32),
+}
+
 #[derive(Debug, Serialize, Deserialize)]
 #[serde(rename_all = "PascalCase")]
-pub struct PatternPen {}
+pub struct PatternPen {
+    from: usize,
+    to: usize,
+    field: PatternField,
+}
 
 impl PatternPen {
-    fn pattern(&self, target: &mut Vec<Pen>) {}
+    fn pattern(&self, target: &mut Vec<Pen>) {
+        trace!("Patterning from pen #{} to #{}", self.from, self.to);
+        assert!(
+            self.to > self.from,
+            "Target pen(s) must be greater than source pen"
+        );
+
+        // Obtain settings from first pen
+        let src = target.get(self.from).expect("Invalid pen index").clone();
+
+        // Clone to target pen and patch pattern setting
+        for dst in (self.from..=self.to).skip(1) {
+
+        }
+    }
 }
 
 #[derive(Debug, Serialize, Deserialize)]
@@ -111,7 +140,7 @@ impl PatchPens for Vec<PenOperation> {
             match op {
                 PenOperation::Patch(x) => x.patch(target),
                 PenOperation::Clone(x) => x.clone(target),
-                PenOperation::Pattern(_) => todo!(),
+                PenOperation::Pattern(x) => x.pattern(target),
             }
         }
     }
@@ -122,21 +151,3 @@ impl PatchPens for Vec<PenOperation> {
 pub struct Config {
     pub pens: Vec<PenOperation>,
 }
-
-// #[test]
-// fn test() {
-//     let config = Config {
-//         modify_pens: vec![ModifyPen {
-//             id: 0,
-//             color: None,
-//             disabled: None,
-//             loop_count: None,
-//             speed: None,
-//             power: None,
-//             frequency: None,
-//         }],
-//     };
-
-//     dbg!(serde_yaml::to_string(&config).unwrap());
-//     panic!()
-// }

+ 11 - 0
src/ezcad/types.rs

@@ -56,6 +56,17 @@ pub struct Rgba {
     pub alpha: u8,
 }
 
+impl From<(u8, u8, u8)> for Rgba {
+    fn from(value: (u8, u8, u8)) -> Self {
+        Self {
+            red: value.0,
+            green: value.1,
+            blue: value.2,
+            alpha: 0,
+        }
+    }
+}
+
 #[derive(Clone, Debug, PartialEq, BinRead, BinWrite)]
 pub struct Point {
     pub x: f64,

+ 9 - 6
src/main.rs

@@ -36,10 +36,11 @@ fn main() {
         .filter_level(cli.verbose.log_level_filter())
         .init();
 
-    let mut input: File = File::open(cli.input).unwrap();
+    let mut input: File = File::open(cli.input).expect("Failed to open input file");
 
     let time: Instant = Instant::now();
-    let mut file: EzCadHeader = EzCadHeader::read_le(&mut input).unwrap();
+    let mut file: EzCadHeader =
+        EzCadHeader::read_le(&mut input).expect("Failed to parse input file as EZCAD format");
     info!("Decode time: {:?}", time.elapsed());
 
     // Print info on pens with non-default settings
@@ -62,8 +63,8 @@ fn main() {
 
     if let Some(config) = cli.config {
         info!("Processing config file {}", config.to_string_lossy());
-        let config: String = std::fs::read_to_string(config).unwrap();
-        let config: Config = serde_yaml::from_str(&config).unwrap();
+        let config: String = std::fs::read_to_string(config).expect("Failed to open config file");
+        let config: Config = serde_yaml::from_str(&config).expect("Failed to parse config file");
         debug!("{:#?}", config);
 
         // Patch pen settings
@@ -71,9 +72,11 @@ fn main() {
     }
 
     if let Some(output) = cli.output {
-        let mut output = File::create(output).unwrap();
+        let mut output = File::create(output).expect("Failed to open output file");
         let time: Instant = Instant::now();
-        output.write_le(&file).unwrap();
+        output
+            .write_le(&file)
+            .expect("Failed to serialize contents in EZCAD format");
         info!("Encode time: {:?}", time.elapsed());
     }
 }