From c818e7618e5afe32b66f6da0c73762fe6a61b743 Mon Sep 17 00:00:00 2001
From: Adukra <43076626+Adukra@users.noreply.github.com>
Date: Fri, 7 Sep 2018 18:15:23 -0400
Subject: [PATCH 1/4] UI Panel for GCode

---
 index.html | 19 +++++++++++++++++++
 js/main.js | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/index.html b/index.html
index aea2dad..37e2064 100644
--- a/index.html
+++ b/index.html
@@ -341,6 +341,25 @@ <h3 class="panel-title">Colors</h3>
                     </div>
                 </div><!-- #colors -->
 
+                <div id="gcode" class="panel panel-default">
+                    <div class="panel-heading">
+                        <button class="panel-toggle btn btn-default btn-xs pull-right" type="button" data-toggle="collapse" data-target="#gcode-body">
+                            <span class="glyphicon glyphicon-menu-down"></span>
+                        </button>
+                        <h3 class="panel-title">GCode</h3>
+                    </div>
+                    <div id="gcode-body" class="panel-body collapse">
+                        <div class="form-group">
+                            <label for="start" style="width: 100%; float: none;" >Start</label>
+                            <textarea id="start" class="form-control" rows="5" style="width: 100%; float: none;" ></textarea>
+                        </div>
+                        <div class="form-group">
+                            <label for="end" style="width: 100%; float: none;" >End</label>
+                            <textarea id="end" class="form-control" rows="5" style="width: 100%; float: none;" ></textarea>
+                        </div>
+                    </div>
+                </div><!-- #gcode -->
+
             </div><!-- #sidebar -->
 
             <div id="main">
diff --git a/js/main.js b/js/main.js
index 281e8a3..339e824 100644
--- a/js/main.js
+++ b/js/main.js
@@ -85,6 +85,14 @@ var settings = new SLAcer.Settings({
             position : 7
         }
     },
+    gcode: {
+        start : 'G21;\nG91;\nM17;\nM106 S0;\nG28 Z0;\n;W:480;\n;H:854;\n',
+        end: 'M106 S0;\nG1 Z20.0;\nG4 S300;\nM18;',
+        panel: {
+            collapsed: true,
+            position : 8
+        }
+    },
     viewer3d: {
         color: 0xffffff
     }
@@ -1072,6 +1080,33 @@ $sliceColor.colorpicker().on('changeColor.colorpicker', function(e) {
     }
 });
 
+// gcode
+var $gcodeBody = initPanel('gcode');
+var $gcodeStart = $gcodeBody.find('#start');
+var $gcodeEnd = $gcodeBody.find('#end');
+
+function updateGcodeUI() {
+    var gcode = settings.get('gcode');
+
+    $gcodeStart.val(gcode.start);
+    $gcodeEnd.val(gcode.end);
+}
+
+function updateGcodeSettings() {
+    var startText = $gcodeStart.val();
+    var endText = $gcodeEnd.val();
+
+    settings.set('gcode', {
+        start   : $gcodeStart.val(),
+        end     : $gcodeEnd.val(),
+    });
+}
+
+$gcodeStart.on('change', updateGcodeSettings);
+$gcodeEnd.on('change', updateGcodeSettings);
+
+updateGcodeUI();
+
 // Alert
 var $alertPanel   = $('#alert');
 var $alertMessage = $alertPanel.find('.message');

From c648543123143c318caa3fc8281562383c9b7150 Mon Sep 17 00:00:00 2001
From: Adukra <43076626+Adukra@users.noreply.github.com>
Date: Fri, 7 Sep 2018 18:26:41 -0400
Subject: [PATCH 2/4] use settings when generating GCode

now uses gcode.start for beginning of generated gcode and gcode.end at the end
---
 js/main.js | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/js/main.js b/js/main.js
index 339e824..18465d2 100644
--- a/js/main.js
+++ b/js/main.js
@@ -778,7 +778,9 @@ function slice() {
 function endSlicing() {
 
     if(WOWExport) {// GCode & export logic
-        wowFile += "M106 S0;\nG1 Z20.0;\nG4 S300;\nM18;"
+        // add gcode footer from settings
+        var gcode = settings.get('gcode');
+        wowFile += gcode.end;
 
         zipFile.file("print.wow", wowFile, {binary: true});
 
@@ -864,7 +866,9 @@ function startSlicing() {
         */
         //console.log(textDecoder.encode(binary_layer));
 
-        wowFile += "G21;\nG91;\nM17;\nM106 S0;\nG28 Z0;\n;W:480;\n;H:854;\n"
+        // use gcode header from settings
+        var gcode = settings.get('gcode');
+        wowFile += gcode.start;
     }
 
     slicesNumber && slice();

From 7b304172eacb1cf70f731d7c720fecf9d8502d25 Mon Sep 17 00:00:00 2001
From: Adukra <43076626+Adukra@users.noreply.github.com>
Date: Fri, 7 Sep 2018 19:02:08 -0400
Subject: [PATCH 3/4] reset to default option for gcode

a button for resetting gcode to default seemed wise after some testing
---
 css/main.css |  3 ++-
 index.html   |  6 ++++--
 js/main.js   | 26 ++++++++++++++++++++++++--
 3 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/css/main.css b/css/main.css
index ee1457a..9e65683 100644
--- a/css/main.css
+++ b/css/main.css
@@ -54,7 +54,8 @@
 }
 
 #sidebar .info-value,
-#sidebar .form-control {
+#sidebar .form-control,
+#sidebar .btn-reset {
     width: 40%;
     float: left;
 }
diff --git a/index.html b/index.html
index 37e2064..519c619 100644
--- a/index.html
+++ b/index.html
@@ -350,11 +350,13 @@ <h3 class="panel-title">GCode</h3>
                     </div>
                     <div id="gcode-body" class="panel-body collapse">
                         <div class="form-group">
-                            <label for="start" style="width: 100%; float: none;" >Start</label>
+                            <label for="resetStart" >Start</label>
+                            <span class="btn btn-default btn-xs btn-reset" id="resetStart" title="Reset to default Start GCode" >Reset</span>
                             <textarea id="start" class="form-control" rows="5" style="width: 100%; float: none;" ></textarea>
                         </div>
                         <div class="form-group">
-                            <label for="end" style="width: 100%; float: none;" >End</label>
+                            <label for="resetEnd" >End</label>
+                            <span class="btn btn-default btn-xs btn-reset" id="resetEnd" title="Reset to default End GCode" >Reset</span>
                             <textarea id="end" class="form-control" rows="5" style="width: 100%; float: none;" ></textarea>
                         </div>
                     </div>
diff --git a/js/main.js b/js/main.js
index 18465d2..379e6f1 100644
--- a/js/main.js
+++ b/js/main.js
@@ -86,8 +86,8 @@ var settings = new SLAcer.Settings({
         }
     },
     gcode: {
-        start : 'G21;\nG91;\nM17;\nM106 S0;\nG28 Z0;\n;W:480;\n;H:854;\n',
-        end: 'M106 S0;\nG1 Z20.0;\nG4 S300;\nM18;',
+        start : getDefaultGcodeStart(),
+        end: getDefaultGcodeEnd(),
         panel: {
             collapsed: true,
             position : 8
@@ -1088,6 +1088,8 @@ $sliceColor.colorpicker().on('changeColor.colorpicker', function(e) {
 var $gcodeBody = initPanel('gcode');
 var $gcodeStart = $gcodeBody.find('#start');
 var $gcodeEnd = $gcodeBody.find('#end');
+var $gcodeResetStart = $gcodeBody.find('#resetStart');
+var $gcodeResetEnd = $gcodeBody.find('#resetEnd');
 
 function updateGcodeUI() {
     var gcode = settings.get('gcode');
@@ -1106,8 +1108,28 @@ function updateGcodeSettings() {
     });
 }
 
+function resetGcodeStart() {
+    $gcodeStart.val(getDefaultGcodeStart());
+    updateGcodeSettings();
+}
+
+function resetGcodeEnd() {
+    $gcodeEnd.val(getDefaultGcodeEnd());
+    updateGcodeSettings();
+}
+
+function getDefaultGcodeStart() {
+    return 'G21;\nG91;\nM17;\nM106 S0;\nG28 Z0;\n;W:480;\n;H:854;\n';
+}
+
+function getDefaultGcodeEnd() {
+    return 'M106 S0;\nG1 Z20.0;\nG4 S300;\nM18;';
+}
+
 $gcodeStart.on('change', updateGcodeSettings);
 $gcodeEnd.on('change', updateGcodeSettings);
+$gcodeResetStart.on('click', resetGcodeStart);
+$gcodeResetEnd.on('click', resetGcodeEnd);
 
 updateGcodeUI();
 

From be9c74a850e31cb0de790b29068d2dfaf4146b9f Mon Sep 17 00:00:00 2001
From: Adukra <43076626+Adukra@users.noreply.github.com>
Date: Fri, 7 Sep 2018 19:10:37 -0400
Subject: [PATCH 4/4] comments

was double-checking that I have the right start and end gcode defaults and added links to documentation in comments
---
 js/main.js | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/js/main.js b/js/main.js
index 379e6f1..532648c 100644
--- a/js/main.js
+++ b/js/main.js
@@ -1119,11 +1119,11 @@ function resetGcodeEnd() {
 }
 
 function getDefaultGcodeStart() {
-    return 'G21;\nG91;\nM17;\nM106 S0;\nG28 Z0;\n;W:480;\n;H:854;\n';
+    return 'G21;\nG91;\nM17;\nM106 S0;\nG28 Z0;\n;W:480;\n;H:854;\n'; // see https://github.com/bastirichter/Sparkmaker/blob/master/file_format.md for details
 }
 
 function getDefaultGcodeEnd() {
-    return 'M106 S0;\nG1 Z20.0;\nG4 S300;\nM18;';
+    return 'M106 S0;\nG1 Z20.0;\nG4 S300;\nM18;'; // see https://github.com/bastirichter/Sparkmaker/blob/master/file_format.md for details
 }
 
 $gcodeStart.on('change', updateGcodeSettings);
