Browse Source

delete the old code when we emit

john 2 months ago
parent
commit
26c7ee709e
1 changed files with 41 additions and 3 deletions
  1. 41 3
      index.js

+ 41 - 3
index.js

@@ -44,6 +44,45 @@ document.body.addEventListener("keydown", async (e) => {
   //}
 });
 
+let gcode = [];
+let gcodeMachine = function*(){
+  let absolute = true;
+  while(gc = yield){
+    if(gc === 'G90' && !absolute){ //we are relative and were asked to go absolute
+      if(gcode[gcode.length-1] === 'G91'){//nothing happened in relative mode so just delete it
+        gcode.pop();
+        //we now need to determine if what the last code was
+        let lastRel = gcode.lastIndexOf('G91');
+        let lastAbs = gcode.lastIndexOf('G90');
+        absolute = lastAbs >= lastRel;
+      }
+      if(!absolute){
+        gcode.push(gc);
+        absolute = true;
+      }
+    }
+    else if(gc === 'G91' && absolute){ //we are relative and were asked to go absolute
+      if(gcode[gcode.length-1] === 'G90'){//nothing happened in mode so just delete it
+        gcode.pop();
+        //we now need to determine if what the last code was
+        let lastRel = gcode.lastIndexOf('G91');
+        let lastAbs = gcode.lastIndexOf('G90');
+        absolute = lastAbs >= lastRel;
+      }
+      if(absolute){
+        gcode.push(gc);
+        absolute = false;
+      }
+    }
+    else{
+      gcode.push(gc);
+    }
+  }
+}();
+function pushGCode(gc){
+  gcodeMachine.next(gc);
+}
+
 const prepro = {
   //reComment: {
   //  regex: /^([^\n;]*)(;[^;\n]*)$/gm,
@@ -55,18 +94,17 @@ const prepro = {
   },
   push:{
     regex: /(([GMFSXYZIJKFP]((-?[0-9]+(\.[0-9]+)?)|(\$\{[^\}]+\}))) ?)+/g,
-    replace: "gcode.push(`$&`);"
+    replace: "pushGCode(`$&`, gcode);"
   }
 }
 
-
 function convert(data){
+  gcode = [];
   for(let pname in prepro){
     let pass = prepro[pname];
     data = data.replace(pass.regex, pass.replace);
   }
 
-  gcode = [];
   try{
     eval(data);
     let o = gcode.join("\n");