-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactory.go
70 lines (59 loc) · 1.83 KB
/
factory.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
package factory
import (
"context"
"time"
"github.com/naueramant/go-3d-printer/firmware"
"github.com/naueramant/go-3d-printer/firmware/generic"
"github.com/naueramant/go-3d-printer/firmware/marlin"
"github.com/naueramant/go-3d-printer/firmware/prusa"
"github.com/naueramant/go-3d-printer/firmware/reprap"
"github.com/naueramant/go-3d-printer/firmware/smoothie"
"github.com/naueramant/go-3d-printer/printer"
"github.com/naueramant/go-3d-printer/serial"
"github.com/pkg/errors"
)
const (
DetectionTimeout = 2 * time.Second
DefaultBaudRate = 115200
)
func AutoConnect(ctx context.Context) (p printer.Printer, err error) {
devices, err := serial.GetSerialDevices()
if err != nil {
return nil, err
}
for _, d := range devices {
p, err := Connect(ctx, d, DefaultBaudRate)
if err == nil {
return p, nil
}
}
return nil, errors.New("Not printers found")
}
func Connect(ctx context.Context, device string, baudrate int) (p printer.Printer, err error) {
s, err := serial.NewConnection(device, baudrate)
if err != nil {
return nil, err
}
f, err := firmware.Detect(ctx, s, DetectionTimeout)
if err != nil {
return nil, err
}
return newPrinter(ctx, s, f)
}
func newPrinter(ctx context.Context, connection *serial.Connection, firmware printer.FirmwareType) (p printer.Printer, err error) {
switch firmware {
case printer.FirmwareTypeGeneric:
return generic.New(ctx, connection), nil
case printer.FirmwareTypeMarlin:
return marlin.New(ctx, connection), nil
case printer.FirmwareTypeRepRap:
return reprap.New(ctx, connection), nil
case printer.FirmwareTypeRepetier:
return reprap.New(ctx, connection), nil
case printer.FirmwareTypeSmoothie:
return smoothie.New(ctx, connection), nil
case printer.FirmwareTypePrusa:
return prusa.New(ctx, connection), nil
}
return nil, errors.New("Unknown printer firmware type")
}