-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
119 lines (110 loc) · 2.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2014 Andrea Masi. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE.txt file.
// to-laser is a gcode post processor for jscut that prepares input
// for a Grbl controlled laser cutter.
//
// Usage
//
// Just redirect gcode via pipe:
//
// cat gcode.nc | tl > laser.gcode
package main
// TODO implements these as parameters
//
// laser on command
// laser off command
// laser power
// power on delay
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
"github.com/eraclitux/cfgp"
)
var Version = "unknown-build"
var BuildTime = "unknown-time"
const (
delayGcode = "G4 P0.5"
laserOnGcode = "M3"
laserOffGcode = "M5 S0"
plungeComment = "; plunge"
retractComment = "; Retract"
)
type Conf struct {
Power int `cfgp:"power,% of laser power [0-100],"`
Version bool `cfgp:"v,show version and exit,"`
}
// parsePower transform laser power from [0-100]% range
// to S[0-100] gcode parameter.
func parsePower(p int) (string, error) {
if p < 0 || p > 100 {
return "", fmt.Errorf("invalid power value: %d", p)
}
i := strconv.Itoa(p)
return "S" + i, nil
}
func main() {
c := Conf{}
err := cfgp.Parse(&c)
if err != nil {
log.Fatal("Unable to parse configuration", err)
}
if c.Version {
fmt.Println("to-laser - jscut post processor for Grbl laser cutters", Version, BuildTime)
return
}
toPlunge := false
toRetract := false
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
line := s.Text()
if toPlunge {
p, err := parsePower(c.Power)
if err != nil {
log.Fatalln(err)
}
fmt.Println(laserOnGcode, p)
fmt.Println(delayGcode)
toPlunge = false
}
if toRetract {
fmt.Println(laserOffGcode)
toRetract = false
}
matched, err := regexp.MatchString(plungeComment, line)
if err != nil {
log.Fatalln(err)
}
if matched {
toPlunge = true
continue
}
matched, err = regexp.MatchString(retractComment, line)
if err != nil {
log.Fatalln(err)
}
if matched {
toRetract = true
continue
}
// If line contains moves on Z axis skip it.
matched, err = regexp.MatchString(".*Z.*", line)
if err != nil {
log.Fatalln(err)
}
if matched {
continue
}
fmt.Println(line)
}
// Error parsing stdin.
if err := s.Err(); err != nil {
log.Fatalln(err)
}
// The End
fmt.Println("M2")
}