forked from talkkonnect/talkkonnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledstrip.go
125 lines (112 loc) · 3.22 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
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
/*
* talkkonnect headless mumble client/gateway with lcd screen and channel control
* Copyright (C) 2018-2019, Suvir Kumar <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* talkkonnect is the based on talkiepi and barnard by Daniel Chote and Tim Cooper
*
* The source code for ledstrip was copied from github.com/CustomMachines and the code was written by Ben Lewis licensed under MPL Version 2 License
* The Initial Developer of the Original Code is
* Ben Lewis
*
* Contributor(s):
*
* Ben Lewis
* Suvir Kumar <[email protected]>
*
* My Blog is at www.talkkonnect.com
* The source code is hosted at github.com/talkkonnect
*
* ledstrip.go -> function in talkkonnect to control the led strip on respeaker hat
*/
package talkkonnect
import (
"errors"
"log"
"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
SOnlineLED int = 0
SVoiceActivityLED int = 1
STransmitLED int = 2
OnlineCol string = "00FF00" //Green
VoiceActivityCol string = "0000FF" //Blue
TransmitCol string = "FF0000" //Red
OffCol string = "000000" //Off
)
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 {
log.Printf("debug: 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
}
log.Printf("debug: 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 {
if !Config.Global.Hardware.LedStripEnabled {
return errors.New("LedStrip Not Enabled in Config")
}
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)
return err
}