aboutsummaryrefslogtreecommitdiff
path: root/blinkstick.go
blob: 2d7d18062ab9dfa0c976dbe4c80afb1d03263a61 (plain)
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
package blinkstick

import (
	"bytes"
	"image/color"
	"io"
)

/* Reports:
   1: LED Data [R, G, B]
   2: Name [Binary Data 0..32]
   3: Data [Binary Data 0..32]
   4: Mode set [MODE]: 0 - RGB LED Strip, 1 - Inverse RGB LED Strip, 2 - WS2812
   5: LED Data [CHANNEL, INDEX, R, G, B]
   6: LED Frame [Channel, [G, R, B][0..7]]
   7: LED Frame [Channel, [G, R, B][0..15]]
   8: LED Frame [Channel, [G, R, B][0..31]]
   9: LED Frame [Channel, [G, R, B][0..63]]
*/

type Frame [8]color.Color

func rgb(c color.Color) (uint8, uint8, uint8) {
	if c == nil {
		return 0, 0, 0
	}
	r, g, b, _ := c.RGBA()
	return uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)
}

func SetIndex(w io.Writer, i int, c color.Color) {
	buf := bytes.NewBuffer([]byte{5, 0, byte(i)})
	r, g, b := rgb(c)
	buf.Write([]byte{r, g, b})
	io.Copy(w, buf)
}

func Set(w io.Writer, frame Frame) {
	buf := bytes.NewBuffer([]byte{6, 0})
	for _, c := range frame {
		r, g, b := rgb(c)
		buf.Write([]byte{g, r, b})
	}
	io.Copy(w, buf)
}

func SetAll(w io.Writer, c color.Color) {
	Set(w, Frame{c, c, c, c, c, c, c, c})
}

func Off(w io.Writer) {
	Set(w, Frame{})
}