This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
129 lines (112 loc) · 2.27 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
120
121
122
123
124
125
126
127
128
129
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/bauersimon/ScreenToArtNet/dmx"
"github.com/bauersimon/ScreenToArtNet/ambilight"
"github.com/bauersimon/ScreenToArtNet/capture"
)
func run() error {
areas, universes, mapping, err := ambilight.ReadConfig(*args.Config)
if err != nil {
return err
}
s := capture.NewScreen(
areas,
capture.CaptureConfig{
Spacing: *args.Spacing,
Threshold: *args.Threshold,
Monitor: *args.Screen,
},
)
c, err := dmx.NewArtNetController(
*args.Src,
*args.Dst,
)
if err != nil {
return err
}
a := &ambilight.Ambilight{
Controller: c,
Screen: s,
Universes: universes,
Mappings: mapping,
Config: ambilight.AmbilightConfiguration{
Sleep: *args.Pause,
},
}
return a.Go()
}
func preview() error {
areas, _, _, err := ambilight.ReadConfig(*args.Config)
if err != nil {
return err
}
s := capture.NewScreen(
areas,
capture.CaptureConfig{
Monitor: *args.Screen,
},
)
cwd, err := os.Getwd()
if err != nil {
return err
}
return s.SavePreview(filepath.Join(cwd, "preview"))
}
var args = struct {
Mode *string
Src *string
Dst *string
Pause *int
Screen *int
Spacing *int
Threshold *int
Config *string
}{
flag.String("mode", "run", "tool mode {run|preview}"),
flag.String("src", "", "artnet source"),
flag.String("dst", "", "artnet destination"),
flag.Int("pause", 0, "pause time in ms"),
flag.Int("screen", 0, "screen identifier"),
flag.Int("spacing", 1, "spacing of pixels for averaging"),
flag.Int("threshold", 0, "threshold of color (0<255)"),
flag.String("config", "config.json", "config file"),
}
func main() {
if len(os.Args) == 1 {
flag.PrintDefaults()
return
}
flag.Parse()
// Make sure we clean everything up.
abort := make(chan os.Signal)
signal.Notify(abort, os.Interrupt, syscall.SIGTERM)
go func() {
s := <-abort
fmt.Printf("\r%v received, stopping...\n", s)
os.Exit(0)
}()
switch *args.Mode {
case "run":
err := run()
if err != nil {
crash(err)
}
case "preview":
err := preview()
if err != nil {
crash(err)
}
default:
fmt.Printf("unknown mode: %s", *args.Mode)
}
}
func crash(err error) {
fmt.Printf("encountered error:\n%s\n", err.Error())
os.Exit(-1)
}