-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
83 lines (71 loc) · 1.9 KB
/
controller.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
package adalight
import (
"github.com/tarm/serial"
"log"
"time"
)
// Controller represents the top-level entrypoint to an Adalight based LED Strip
type Controller struct {
portcfg *serial.Config
strip *Strip
fps int
}
// New creates and returns a new Strip to use
func New(port string, baud int, ledCount int) *Controller {
c := &Controller{&serial.Config{Name: port, Baud: baud}, newStrip(ledCount), estimateFps(ledCount, baud)}
return c
}
// Strip returns the LED Strip model owned by this controller
func (c *Controller) Strip() *Strip {
return c.strip
}
// Run runs a given Effect
func (c *Controller) Run(e Effect) error {
port, err := serial.OpenPort(c.portcfg)
if err != nil {
return err
}
defer func() {
if ferr := port.Close(); ferr != nil {
err = ferr
}
}()
// Initialize the Effect and Frame Time param
effectName := e.Init(c.strip, c.fps)
t := time.Now()
hdr := buildHeader(c.strip.length)
ft := 1.0 / float64(c.fps)
fd := time.Duration(ft * float64(time.Second))
log.Println("Initialized Effect", effectName, "at", t, "with duration", fd.Seconds())
for f := 0; ; f++ {
// Get the next frame from the effect
done := e.Frame(f)
t = t.Add(fd)
// Wait for the timer
time.Sleep(time.Until(t))
// Write the header and then the pixels
if _, err := port.Write(hdr); err != nil {
return err
}
if _, err := port.Write(c.strip.pixels); err != nil {
return err
}
// If the Effect reports that it is done, break out of the after writing the final frame
if done {
break
}
}
return nil
}
func buildHeader(cnt int) []byte {
countHi := (byte)((cnt - 1) >> 8)
countLo := (byte)((cnt - 1) & 0xFF)
checksum := countHi ^ countLo ^ 0x55
return []byte{'A', 'd', 'a', countHi, countLo, checksum}
}
func estimateFps(pixels int, bps int) int {
bits := (6 + (pixels * 3)) * 8
max := float64(bps) / float64(bits)
scaled := int(max * .75)
return (scaled / 5) * 5
}