-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
128 lines (104 loc) · 3.77 KB
/
main.go
File metadata and controls
128 lines (104 loc) · 3.77 KB
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
package main
import (
"flag"
"io/ioutil"
"log"
"math/rand"
"os"
"os/signal"
"path"
"runtime"
"time"
"github.com/brianewing/redshift/animator"
"github.com/brianewing/redshift/effects"
"github.com/brianewing/redshift/midi"
"github.com/brianewing/redshift/repl"
"github.com/brianewing/redshift/server"
"github.com/brianewing/redshift/strip"
)
var numLeds = flag.Int("leds", 100, "number of leds")
var scriptsDir = flag.String("scriptsDir", "scripts", "scripts directory relative to cwd")
var effectsDir = flag.String("effectsDir", "usereffects", "effect definitions directory relative to cwd")
var animationInterval = flag.Duration("animationInterval", time.Second/60, "interval between animation frames")
var pathToEffectsJson = flag.String("effectsJson", "", "path to effects json file")
var pathToEffectsYaml = flag.String("effectsYaml", "", "path to effects yaml file")
var wsInterval = flag.Duration("wsInterval", 16*time.Millisecond, "ws2811/2812(b) refresh interval")
var wsPin = flag.Int("wsPin", 0, "ws2811/2812(b) gpio pin")
var wsBrightness = flag.Int("wsBrightness", 50, "ws2811/2812(b) brightness")
var midiListDevices = flag.Bool("midiListDevices", false, "prints a list of available midi devices")
var midiDeviceId = flag.Int("midiDeviceId", 0, "midi device id")
var httpAddr = flag.String("httpAddr", "0.0.0.0:9191", "http service address")
var davAddr = flag.String("davAddr", "0.0.0.0:9292", "webdav (scripts) service address")
var effectsDavAddr = flag.String("effectsDavAddr", "0.0.0.0:9393", "webdav (effects) service address")
var opcAddr = flag.String("opcAddr", "0.0.0.0:7890", "opc service address")
var oscAddr = flag.String("oscAddr", "0.0.0.0:9494", "osc service address")
var logToFile = flag.Bool("log", false, "log to redshift.log")
var logDebug = flag.Bool("logDebug", false, "include source code filenames in log messages")
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
rand.Seed(time.Now().UnixNano())
flag.Parse()
if *logDebug {
log.SetFlags(log.Ltime | log.Lshortfile)
}
}
func main() {
if _, err := os.Stat(*scriptsDir); os.IsNotExist(err) {
writePackedScripts(*scriptsDir)
}
if *midiListDevices {
devices := midi.Devices()
println("** MIDI Devices Available **")
for i, device := range devices {
println(" ", i, "-", device.Name)
}
println("")
return
}
ledStrip := strip.New(*numLeds)
opcBuffer := strip.NewBuffer(ledStrip.Size)
wssBuffer := strip.NewBuffer(ledStrip.Size)
animator := &animator.Animator{
Strip: ledStrip,
Effects: getEffects(),
PostEffects: effects.EffectSet{
effects.EffectEnvelope{Effect: effects.NewBlendFromBuffer(opcBuffer)},
effects.EffectEnvelope{Effect: effects.NewBlendFromBuffer(wssBuffer)},
},
}
if *wsPin != 0 {
go ledStrip.RunWs2811(*wsPin, *wsInterval, *wsBrightness)
}
go server.RunWebServer(*httpAddr, animator, wssBuffer)
go server.RunWebDavServer(*davAddr, *scriptsDir, true)
go server.RunWebDavServer(*effectsDavAddr, *effectsDir, false)
go server.RunOpcServer(*opcAddr, animator, opcBuffer)
go server.RunOscServer(*oscAddr)
// if *opcForwardAddr != "" {
// go opc.RunForwarder(*opcForwardAddr, ledStrip)
// }
go repl.Run(animator, os.Stdin, "> ")
go repl.RunReplServer(":9999", animator)
go cleanupOnCtrlC(animator)
animator.Run(*animationInterval)
}
func cleanupOnCtrlC(animator *animator.Animator) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
animator.Finish()
}
//go:generate go-bindata --prefix skel/ skel/...
func writePackedScripts(dest string) error {
if err := os.MkdirAll(dest, 0755); err != nil {
return err
}
scripts, _ := AssetDir("scripts")
for _, name := range scripts {
data, _ := Asset(path.Join("scripts", name))
if err := ioutil.WriteFile(path.Join(dest, name), data, 0744); err != nil {
return err
}
}
return nil
}