Repository: ezrec/uv3dp (go)

Issue:
Suggested fixes for Elfin/.cws
My Elfin doesn't parse the gcode file that uv3dp currently writes for .cws.  After some hours of troubleshooting, I managed to come up with the attached, working version.

First and most straightforward, this message in the header needs to be preceded with a semicolon:

https://github.com/ezrec/uv3dp/blob/1e75eb433fe069bebcdb16d228233e8ca2067dc4/cws/format.go#L147

Also, the Elfin seems to not be able to parse items in the header if they are inside of parentheses.  Removing all the parenthesis (after adding the semicolon above) finally enabled the Elfin to print the archive.

Furthermore, the only thing that *must* not be in parenthesis in order to print, is "Number of Slices".  If you *only* take the parenthesis away on that one line, my Elfin will print the archive.  This does not mean that there are not other things that the Elfin would otherwise read and use if it could parse those things as well.  But I have not tested for that, and imagine it would be difficult to do so.

[working-elfin-cws.zip](https://github.com/ezrec/uv3dp/files/5026786/working-elfin-cws.zip)





Pull request description:
cws: Correct some issues with formatting
Summary:
- Fixes #95

Patch:
diff --git a/cws/format.go b/cws/format.go
index 62d9862..f2ff921 100644
--- a/cws/format.go
+++ b/cws/format.go
@@ -144,7 +144,7 @@ func (cc *cwsConfig) Save(gcode io.Writer) (err error) {
 		}
 	}
 
-	_, err = fmt.Fprintf(gcode, "(****Machine Configuration ******)\n")
+	_, err = fmt.Fprintf(gcode, ";(****Machine Configuration ******)\n")
 	if err != nil {
 		return
 	}
@@ -177,6 +177,8 @@ func (cc *cwsConfig) Save(gcode io.Writer) (err error) {
 		return
 	}
 
+	_, err = fmt.Fprintf(gcode, ";Number of Slices = %v\n", cc.Layers)
+
 	return
 }
 
@@ -281,6 +283,14 @@ func (sf *CWSFormat) Encode(writer uv3dp.Writer, printable uv3dp.Printable) (err
 	exp := &prop.Exposure
 	bot := &prop.Bottom.Exposure
 
+	if exp.LightPWM == 0 {
+		exp.LightPWM = 255
+	}
+
+	if bot.LightPWM == 0 {
+		bot.LightPWM = 255
+	}
+
 	uv3dp.WithEachLayer(printable, func(n int, layer uv3dp.Layer) {
 		filename := fmt.Sprintf("%s%04d.png", jobName, n)
 
@@ -344,7 +354,7 @@ G28
 G21 ;Set units to be mm
 G91 ;Relative Positioning
 M17 ;Enable motors
-<Slice> Blank
+;<Slice> Blank
 M106 S0
 `)
 
@@ -361,7 +371,7 @@ M106 S0
 
 		// Create all the layers
 		fmt.Fprintf(gcode, "\n;<Slice> %v\n", n)
-		fmt.Fprintf(gcode, "M106 S255\n;<Delay> %v\n", int(layer.Exposure.LightOnTime*1000.0))
+		fmt.Fprintf(gcode, "M106 S%v\n;<Delay> %v\n", layer.Exposure.LightPWM, int(layer.Exposure.LightOnTime*1000.0))
 		fmt.Fprintf(gcode, "M106 S0\n;<Slice> Blank\n")
 		fmt.Fprintf(gcode, "G1 Z%1.3f F%v\n", layer.Exposure.LiftHeight, int(layer.Exposure.LiftSpeed))
 		priorZ = layer.Z
@@ -521,5 +531,9 @@ func (cws *CWS) Layer(index int) (layer uv3dp.Layer) {
 	layer.Image = pngImage.(*image.Gray)
 	layer.Exposure = cws.properties.LayerExposure(index)
 
+	if layer.Exposure.LightPWM == 0 {
+		layer.Exposure.LightPWM = 255
+	}
+
 	return
 }


Test patch:
diff --git a/cws/format_test.go b/cws/format_test.go
index b6c5c98..596d059 100644
--- a/cws/format_test.go
+++ b/cws/format_test.go
@@ -53,6 +53,7 @@ var (
 			LiftSpeed:     120.0,
 			RetractHeight: 3.3,
 			RetractSpeed:  200.0,
+			LightPWM:      100,
 		},
 		Bottom: uv3dp.Bottom{
 			Count: 2,
@@ -63,6 +64,7 @@ var (
 				LiftSpeed:     120.0,
 				RetractHeight: 3.3,
 				RetractSpeed:  200.0,
+				LightPWM:      255,
 			},
 		},
 		Preview: map[uv3dp.PreviewType]image.Image{
@@ -99,7 +101,7 @@ const (
 ;(Flip X                  = True )
 ;(Flip Y                  = True )
 ;(Number of Slices        = 4 )
-(****Machine Configuration ******)
+;(****Machine Configuration ******)
 ;(Platform X Size         = 5.00mm )
 ;(Platform Y Size         = 10.00mm )
 ;(Platform Z Size         = 0.20mm )
@@ -107,12 +109,13 @@ const (
 ;(Max Y Feedrate          = 200mm/min )
 ;(Max Z Feedrate          = 200mm/min )
 ;(Machine Type            = UV_LCD )
+;Number of Slices = 4
 
 G28
 G21 ;Set units to be mm
 G91 ;Relative Positioning
 M17 ;Enable motors
-<Slice> Blank
+;<Slice> Blank
 M106 S0
 
 ;<Slice> 0
@@ -134,7 +137,7 @@ G1 Z-5.450 F120
 ;<Delay> 6000
 
 ;<Slice> 2
-M106 S255
+M106 S100
 ;<Delay> 16500
 M106 S0
 ;<Slice> Blank
@@ -143,7 +146,7 @@ G1 Z-5.450 F120
 ;<Delay> 6000
 
 ;<Slice> 3
-M106 S255
+M106 S100
 ;<Delay> 16500
 M106 S0
 ;<Slice> Blank