Skip to content

Commit 2c99a44

Browse files
committed
- fix: No longer smfix .bin files
- fix: Find the printer in the default broadcast address - fix: disable CGO for linux
1 parent d85e7b0 commit 2c99a44

File tree

10 files changed

+52
-39
lines changed

10 files changed

+52
-39
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ darwin-amd64: $(SRC)
1919
GOOS=darwin GOARCH=amd64 $(CMD) -o $(DIST)$(NAME)-$@ $^
2020

2121
linux-amd64: $(SRC)
22-
GOOS=linux GOARCH=amd64 $(CMD) -o $(DIST)$(NAME)-$@ $^
22+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(CMD) -o $(DIST)$(NAME)-$@ $^
2323

2424
linux-arm7: $(SRC)
25-
GOOS=linux GOARCH=arm GOARM=7 $(CMD) -o $(DIST)$(NAME)-$@ $^
25+
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 $(CMD) -o $(DIST)$(NAME)-$@ $^
2626

2727
linux-arm6: $(SRC)
28-
GOOS=linux GOARCH=arm GOARM=6 $(CMD) -o $(DIST)$(NAME)-$@ $^
28+
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 $(CMD) -o $(DIST)$(NAME)-$@ $^
2929

3030
win64: $(SRC)
3131
GOOS=windows GOARCH=amd64 $(CMD) -o $(DIST)$(NAME)-$@.exe $^

connector.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func (p *Payload) ReadableSize() string {
3232
return humanReadableSize(p.Size)
3333
}
3434

35-
func (p *Payload) GetContent(fix bool) (cont []byte, err error) {
35+
func (p *Payload) GetContent(nofix bool) (cont []byte, err error) {
3636
defer runtime.GC()
37-
if fix {
37+
if nofix || !shouldBeFix(p.Name) {
38+
cont, err = io.ReadAll(p.File)
39+
} else {
3840
cont, err = postProcess(p.File)
3941
p.Size = int64(len(cont))
40-
} else {
41-
cont, err = io.ReadAll(p.File)
4242
}
4343
return cont, err
4444
}

connector_http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ func (hc *HTTPConnector) Upload(payload *Payload) (err error) {
140140
pr, pw := io.Pipe()
141141
go func() {
142142
defer pw.Close()
143-
content, err := payload.GetContent(SmFix)
144-
if SmFix {
143+
content, err := payload.GetContent(NoFix)
144+
if !NoFix {
145145
log.SetOutput(os.Stderr)
146146
if err != nil {
147147
log.Printf("G-Code fix error(ignored): %s", err)

connector_sacp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func (sc *SACPConnector) Disconnect() error {
4848
}
4949

5050
func (sc *SACPConnector) Upload(payload *Payload) (err error) {
51-
content, err := payload.GetContent(SmFix)
52-
if SmFix {
51+
content, err := payload.GetContent(NoFix)
52+
if !NoFix {
5353
if err != nil {
5454
log.Printf("G-Code fix error(ignored): %s", err)
5555
} else {

discover.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,22 @@ func Discover(timeout time.Duration) ([]*Printer, error) {
8383
}
8484

8585
func getBroadcastAddresses() ([]string, error) {
86-
ifs, err := net.InterfaceAddrs()
86+
ifs, err := net.Interfaces()
8787
if err != nil {
8888
return nil, err
8989
}
9090

91-
addrs := make([]string, 0)
92-
for _, address := range ifs {
93-
var ip net.IP
94-
switch typedAddress := address.(type) {
95-
case *net.TCPAddr:
96-
ip = typedAddress.IP.To4()
97-
case *net.UDPAddr:
98-
ip = typedAddress.IP.To4()
99-
case *net.IPNet:
100-
ip = typedAddress.IP.To4()
101-
default:
91+
baddrs := make([]string, 0, len(ifs))
92+
for _, iface := range ifs {
93+
addrs, err := iface.Addrs()
94+
if err != nil {
95+
continue
10296
}
103-
104-
if ip != nil && len(ip) == net.IPv4len && ip.IsGlobalUnicast() {
105-
ip[3] = 255
106-
addrs = append(addrs, ip.String())
97+
for _, addr := range addrs {
98+
if n, ok := addr.(*net.IPNet); ok && !n.IP.IsLoopback() && n.IP.To4() != nil {
99+
baddrs = append(baddrs, n.IP.Mask(n.IP.DefaultMask()).String())
100+
}
107101
}
108102
}
109-
return addrs, nil
103+
return baddrs, nil
110104
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.20
55
require (
66
github.com/gosuri/uilive v0.0.4
77
github.com/imroc/req/v3 v3.11.0
8-
github.com/macdylan/SMFix/fix v0.0.0-20230811055700-930d31056194
8+
github.com/macdylan/SMFix/fix v0.0.0-20230904075958-62dd346d9d73
99
github.com/manifoldco/promptui v0.9.0
1010
gopkg.in/yaml.v3 v3.0.1
1111
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2323
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2424
github.com/macdylan/SMFix/fix v0.0.0-20230811055700-930d31056194 h1:BEwsonfRa4+0lJDSFAxh59aRtB4h434mJC2rx6V+2Yk=
2525
github.com/macdylan/SMFix/fix v0.0.0-20230811055700-930d31056194/go.mod h1:dnB1MevhW7tICqBpQ2aHpVClwLdmSBUNmfV7jpRmiWw=
26+
github.com/macdylan/SMFix/fix v0.0.0-20230904075958-62dd346d9d73 h1:fVzGqq2B1RyucrvZvz9KIfxpfsLVR92VpYARFqyCHaM=
27+
github.com/macdylan/SMFix/fix v0.0.0-20230904075958-62dd346d9d73/go.mod h1:dnB1MevhW7tICqBpQ2aHpVClwLdmSBUNmfV7jpRmiWw=
2628
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
2729
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
2830
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=

main.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ var (
1717
KnownHosts string
1818
DiscoverTimeout time.Duration
1919
OctoPrintListenAddr string
20-
SmFix bool
20+
NoFix bool
2121
Debug bool
2222

23-
_Payloads []*Payload
23+
_Payloads []*Payload
24+
SmFixExtensions = map[string]bool{
25+
".gcode": true,
26+
".nc": false,
27+
".cnc": false,
28+
".bin": false,
29+
}
2430
)
2531

2632
func main() {
@@ -44,7 +50,7 @@ func main() {
4450
flag.StringVar(&KnownHosts, "knownhosts", defaultKnownHosts, "known hosts")
4551
flag.DurationVar(&DiscoverTimeout, "timeout", time.Second*4, "printer discovery timeout")
4652
flag.StringVar(&OctoPrintListenAddr, "octoprint", "", "octoprint listen address, e.g. '-octoprint :8844' then you can upload files to printer by http://localhost:8844")
47-
flag.BoolVar(&SmFix, "fix", true, "enable SMFix(built-in), -fix=0 to disable.")
53+
flag.BoolVar(&NoFix, "nofix", false, "disable SMFix(built-in)")
4854
flag.BoolVar(&Debug, "debug", false, "debug mode")
4955

5056
flag.Usage = flag_usage
@@ -54,7 +60,7 @@ func main() {
5460
log.Println("-- Debug mode")
5561
}
5662

57-
if !SmFix {
63+
if NoFix {
5864
log.Println("smfix disabled")
5965
}
6066

start-octoprint.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ set w64=sm2uploader-win64.exe
1616
set w32=sm2uploader-win32.exe
1717
set cmd=
1818

19-
where /q %w64% && set "cmd=%w64%"
2019
where /q %w32% && set "cmd=%w32%"
20+
where /q %w64% && set "cmd=%w64%"
2121

2222
if "%cmd%"=="" (
2323
echo Can not find %w64% or %w32%

utils.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"bytes"
66
"fmt"
77
"io"
8-
"os"
8+
"path/filepath"
99
"regexp"
1010
"strings"
1111

@@ -33,6 +33,7 @@ func normalizedFilename(filename string) string {
3333
return reFilename.ReplaceAllString(filename, "")
3434
}
3535

36+
/*
3637
func postProcessFile(file_path string) (out []byte, err error) {
3738
var r *os.File
3839
if r, err = os.Open(file_path); err != nil {
@@ -41,6 +42,7 @@ func postProcessFile(file_path string) (out []byte, err error) {
4142
defer r.Close()
4243
return postProcess(r)
4344
}
45+
*/
4446

4547
func postProcess(r io.Reader) (out []byte, err error) {
4648
header, errfix := fix.ExtractHeader(r)
@@ -59,17 +61,21 @@ func postProcess(r io.Reader) (out []byte, err error) {
5961
}
6062

6163
if errfix == nil {
64+
funcs := make([]func([]string) []string, 0, 4)
6265
if !noTrim {
63-
gcodes = fix.GcodeTrimLines(gcodes)
66+
funcs = append(funcs, fix.GcodeTrimLines)
6467
}
6568
if !noShutoff {
66-
gcodes = fix.GcodeFixShutoff(gcodes)
69+
funcs = append(funcs, fix.GcodeFixShutoff)
6770
}
6871
if !noPreheat {
69-
gcodes = fix.GcodeFixPreheat(gcodes)
72+
funcs = append(funcs, fix.GcodeFixPreheat)
7073
}
7174
if !noReinforceTower {
72-
gcodes = fix.GcodeReinforceTower(gcodes)
75+
funcs = append(funcs, fix.GcodeReinforceTower)
76+
}
77+
for _, fn := range funcs {
78+
gcodes = fn(gcodes)
7379
}
7480
}
7581

@@ -81,3 +87,8 @@ func postProcess(r io.Reader) (out []byte, err error) {
8187
return out, errfix
8288
}
8389
}
90+
91+
func shouldBeFix(fpath string) bool {
92+
ext := strings.ToLower(filepath.Ext(fpath))
93+
return SmFixExtensions[ext]
94+
}

0 commit comments

Comments
 (0)