Skip to content

Commit

Permalink
added respeaker ledstrip
Browse files Browse the repository at this point in the history
  • Loading branch information
talkkonnect committed Mar 14, 2021
1 parent 4556ad4 commit a4a4f70
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 19 deletions.
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var (
message string
isrepeattx bool = true
NowStreaming bool
MyLedStrip *LedStrip
)

type Talkkonnect struct {
Expand Down Expand Up @@ -262,7 +263,9 @@ func (b *Talkkonnect) ClientStart() {
}

if TargetBoard == "rpi" {
b.LEDOffAll()
if !LedStripEnabled {
b.LEDOffAll()
}
}

if Logging == "screenandfile" {
Expand All @@ -287,6 +290,8 @@ func (b *Talkkonnect) ClientStart() {
if TargetBoard == "rpi" {
log.Println("info: Target Board Set as RPI (gpio enabled) ")
b.initGPIO()
MyLedStrip, _ = NewLedStrip()
log.Printf("info: Led Strip %v %s\n", MyLedStrip.buf, MyLedStrip.display)
} else {
log.Println("info: Target Board Set as PC (gpio disabled) ")
}
Expand Down
36 changes: 27 additions & 9 deletions clientcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ func (b *Talkkonnect) CleanUp() {
oledDisplay(false, 6, 1, "Please Visit")
oledDisplay(false, 7, 1, "www.talkkonnect.com")
}
b.LEDOffAll()
if !LedStripEnabled {
b.LEDOffAll()
} else {
MyLedStripLEDOffAll()
}
}

term.Close()
Expand Down Expand Up @@ -150,7 +154,11 @@ func (b *Talkkonnect) TransmitStart() {
}

if TargetBoard == "rpi" {
b.LEDOn(b.TransmitLED)
if !LedStripEnabled {
b.LEDOn(b.TransmitLED)
} else {
MyLedStripTransmitLEDOn()
}
if LCDEnabled == true {
LcdText[0] = "Online/TX"
LcdText[3] = "TX at " + t.Format("15:04:05")
Expand Down Expand Up @@ -184,8 +192,11 @@ func (b *Talkkonnect) TransmitStop(withBeep bool) {
b.BackLightTimer()

if TargetBoard == "rpi" {
b.LEDOff(b.TransmitLED)

if !LedStripEnabled {
b.LEDOff(b.TransmitLED)
} else {
MyLedStripTransmitLEDOff()
}
if LCDEnabled == true {
LcdText[0] = b.Address
LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
Expand Down Expand Up @@ -295,8 +306,13 @@ func (b *Talkkonnect) ParticipantLEDUpdate(verbose bool) {

if participantCount > 1 {
if TargetBoard == "rpi" {
b.LEDOn(b.ParticipantsLED)
b.LEDOn(b.OnlineLED)
if !LedStripEnabled {
b.LEDOn(b.ParticipantsLED)
b.LEDOn(b.OnlineLED)
} else {
MyLedStripParticipantsLEDOn()
MyLedStripOnlineLEDOn()
}
}

} else {
Expand All @@ -311,9 +327,11 @@ func (b *Talkkonnect) ParticipantLEDUpdate(verbose bool) {
prevParticipantCount = 0

if TargetBoard == "rpi" {

b.LEDOff(b.ParticipantsLED)

if !LedStripEnabled {
b.LEDOff(b.ParticipantsLED)
} else {
MyLedStripParticipantsLEDOff()
}
if LCDEnabled == true {
LcdText = [4]string{b.Address, "Alone in " + b.Client.Self.Channel.Name, "", "nil"}
LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
Expand Down
9 changes: 8 additions & 1 deletion commandkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,14 @@ func (b *Talkkonnect) cmdPanicSimulation() {
}
IsPlayStream = false
b.IsTransmitting = false
b.LEDOff(b.TransmitLED)

if TargetBoard == "rpi" {
if !LedStripEnabled {
b.LEDOff(b.TransmitLED)
} else {
MyLedStripTransmitLEDOff()
}
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions gpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,35 @@ func (b *Talkkonnect) LEDOffAll() {
if VoiceActivityLEDPin > 0 {
LEDOffFunc(b.VoiceActivityLED)
}

}

func MyLedStripLEDOffAll() {
MyLedStrip.ledCtrl(OnlineLED, OffCol)
MyLedStrip.ledCtrl(ParticipantsLED, OffCol)
MyLedStrip.ledCtrl(TransmitLED, OffCol)
}

func MyLedStripOnlineLEDOn() {
MyLedStrip.ledCtrl(OnlineLED, OnlineCol)
}

func MyLedStripOnlineLEDOff() {
MyLedStrip.ledCtrl(OnlineLED, OffCol)
}

func MyLedStripParticipantsLEDOn() {
MyLedStrip.ledCtrl(ParticipantsLED, ParticipantsCol)
}

func MyLedStripParticipantsLEDOff() {
MyLedStrip.ledCtrl(ParticipantsLED, OffCol)
}

func MyLedStripTransmitLEDOn() {
MyLedStrip.ledCtrl(TransmitLED, TransmitCol)
}

func MyLedStripTransmitLEDOff() {
MyLedStrip.ledCtrl(TransmitLED, OffCol)
}
96 changes: 96 additions & 0 deletions ledstrip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package talkkonnect

import (
"strconv"
"log"
"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 {
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 {
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)

log.Printf("debug: LedStrip %v\n", ls.buf)

return err
}

func (ls *LedStrip) closePort() {
ls.spiInterface.Close()
}
15 changes: 10 additions & 5 deletions onevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ func (b *Talkkonnect) OnConnect(e *gumble.ConnectEvent) {
}

if TargetBoard == "rpi" {
b.LEDOn(b.OnlineLED)

if !LedStripEnabled {
b.LEDOn(b.OnlineLED)
} else {
MyLedStripOnlineLEDOn()
}
if LCDEnabled == true {
LcdText = [4]string{"nil", "nil", "nil", "nil"}
LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
Expand Down Expand Up @@ -95,9 +98,11 @@ func (b *Talkkonnect) OnDisconnect(e *gumble.DisconnectEvent) {
IsConnected = false

if TargetBoard == "rpi" {
b.LEDOff(b.OnlineLED)
b.LEDOff(b.ParticipantsLED)
b.LEDOff(b.TransmitLED)
if !LedStripEnabled {
b.LEDOffAll()
} else {
MyLedStripLEDOffAll()
}
}

if !ServerHop {
Expand Down
10 changes: 7 additions & 3 deletions xmlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ import (

//version and release date
const (
talkkonnectVersion string = "1.60.01"
talkkonnectReleased string = "March 12 2021"
talkkonnectVersion string = "1.61.01"
talkkonnectReleased string = "March 13 2021"
)

// Generic Global Variables
Expand Down Expand Up @@ -283,6 +283,7 @@ var (

//indicator light settings
var (
LedStripEnabled bool
VoiceActivityLEDPin uint
ParticipantsLEDPin uint
TransmitLEDPin uint
Expand Down Expand Up @@ -618,6 +619,7 @@ type Document struct {
Hardware struct {
TargetBoard string `xml:"targetboard,attr"`
Lights struct {
LedStripEnabled bool `xml:"ledstripenabled"`
VoiceActivityLedPin string `xml:"voiceactivityledpin"`
ParticipantsLedPin string `xml:"participantsledpin"`
TransmitLedPin string `xml:"transmitledpin"`
Expand Down Expand Up @@ -719,6 +721,7 @@ type Document struct {
} `xml:"global"`
}


func readxmlconfig(file string) error {
xmlFile, err := os.Open(file)
if err != nil {
Expand Down Expand Up @@ -1237,7 +1240,7 @@ func readxmlconfig(file string) error {
PrintMQTT = document.Global.Software.PrintVariables.PrintMQTT

TargetBoard = document.Global.Hardware.TargetBoard

LedStripEnabled = document.Global.Hardware.Lights.LedStripEnabled
// my stupid work around for null uint xml unmarshelling problem with numbers so use strings and convert it 2 times
temp0, _ := strconv.ParseUint(document.Global.Hardware.Lights.VoiceActivityLedPin, 10, 64)
VoiceActivityLEDPin = uint(temp0)
Expand Down Expand Up @@ -1570,6 +1573,7 @@ func printxmlconfig() {

if PrintLeds {
log.Println("info: ------------ LEDS ---------------------- ")
log.Println("info: Led Strip Enabled " + fmt.Sprintf("%v", LedStripEnabled))
log.Println("info: Voice Activity Led Pin " + fmt.Sprintf("%v", VoiceActivityLEDPin))
log.Println("info: Participants Led Pin " + fmt.Sprintf("%v", ParticipantsLEDPin))
log.Println("info: Transmit Led Pin " + fmt.Sprintf("%v", TransmitLEDPin))
Expand Down

0 comments on commit a4a4f70

Please sign in to comment.