forked from CustomMachines/talkiepi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ledstrip.go
97 lines (83 loc) · 2.08 KB
/
ledstrip.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
package talkiepi
import (
"fmt"
"strconv"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/conn/spi"
"periph.io/x/periph/conn/spi/spireg"
"periph.io/x/periph/devices/apa102"
"periph.io/x/periph/host"
)
const (
numLEDs int = 3
OnlineLED int = 0
ParticipantsLED int = 1
TransmitLED int = 2
OnlineCol string = "FF0000"
ParticipantsCol string = "00FF00"
TransmitCol string = "0000FF"
OffCol string = "000000"
)
type LedStrip struct {
buf []byte
display *apa102.Dev
spiInterface spi.PortCloser
}
func NewLedStrip() (*LedStrip, error) {
var spiID string = "SPI0.0" //SPI port to use
var intensity uint8 = 16 //light intensity [1-255]
var temperature uint16 = 5000 //light temperature in °Kelvin [3500-7500]
var hz physic.Frequency //SPI port speed
var globalPWM bool = false
if _, err := host.Init(); err != nil {
return nil, err
}
// Open the display device.
s, err := spireg.Open(spiID)
if err != nil {
return nil, err
}
//Set port speed
if hz != 0 {
if err := s.LimitSpeed(hz); err != nil {
return nil, err
}
}
if p, ok := s.(spi.Pins); ok {
fmt.Printf("Using pins CLK: %s MOSI: %s MISO: %s", p.CLK(), p.MOSI(), p.MISO())
}
o := apa102.DefaultOpts
o.NumPixels = numLEDs
o.Intensity = intensity
o.Temperature = temperature
o.DisableGlobalPWM = globalPWM
display, err := apa102.New(s, &o)
if err != nil {
return nil, err
}
fmt.Printf("init display: %s\n", display)
buf := make([]byte, numLEDs*3)
return &LedStrip{
buf: buf,
display: display,
spiInterface: s,
}, nil
}
func (ls *LedStrip) ledCtrl(num int, color string) error {
rgb, err := strconv.ParseUint(color, 16, 32)
if err != nil {
return err
}
r := byte(rgb >> 16)
g := byte(rgb >> 8)
b := byte(rgb)
ls.buf[num*numLEDs+0] = r
ls.buf[num*numLEDs+1] = g
ls.buf[num*numLEDs+2] = b
_, err = ls.display.Write(ls.buf)
fmt.Printf("%v\n", ls.buf)
return err
}
func (ls *LedStrip) closePort() {
ls.spiInterface.Close()
}