-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
79 lines (61 loc) · 1.66 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
package main
import (
"flag"
"fmt"
"image/png"
"log"
"os"
"github.com/piger/gattoprint/internal/bt"
"github.com/piger/gattoprint/internal/commands"
"github.com/piger/gattoprint/internal/graphics"
"tinygo.org/x/bluetooth"
)
var (
flagDeviceName = flag.String("printer-name", "GB03", "Name advertised by the printer")
flagOutput = flag.String("output", "output.png", "Output file name, for preview")
flagNoPrint = flag.Bool("no-print", false, "Disable printing, just create the preview")
)
func run(filename string) error {
goo, err := graphics.ConvertImage(filename)
if err != nil {
return fmt.Errorf("error converting image: %w", err)
}
out, err := os.Create(*flagOutput)
if err != nil {
return fmt.Errorf("error opening preview file for writing: %w", err)
}
defer out.Close()
if err := png.Encode(out, goo); err != nil {
return fmt.Errorf("error encoding preview: %w", err)
}
if *flagNoPrint {
return nil
}
cmds := commands.PrintImage(goo)
var adapter = bluetooth.DefaultAdapter
if err := adapter.Enable(); err != nil {
return fmt.Errorf("error enabling Bluetooth adapter: %w", err)
}
addr, err := bt.FindDevice(*flagDeviceName, adapter)
if err != nil {
return fmt.Errorf("couldn't find printer: %w", err)
}
log.Printf("found %s: %s", *flagDeviceName, addr)
if err := bt.SendCommands(adapter, addr, cmds); err != nil {
return fmt.Errorf("error sending commands to printer: %w", err)
}
return nil
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
fmt.Printf("error: pass an image file\n")
os.Exit(1)
}
filename := args[0]
if err := run(filename); err != nil {
log.Print(err)
os.Exit(1)
}
}