refactor to support multiple modes
This commit is contained in:
parent
aab533caaf
commit
32d03ddd05
76
main.go
76
main.go
@ -44,6 +44,17 @@ func (lhw *lighthardware) InitPWM() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type lightSet struct {
|
||||
inside *lighthardware
|
||||
inside2 *lighthardware
|
||||
outside *lighthardware
|
||||
}
|
||||
|
||||
type brightnesses struct {
|
||||
inside uint32
|
||||
outside uint32
|
||||
}
|
||||
|
||||
const (
|
||||
period = uint64(1e9 / 500)
|
||||
pressdelay = time.Millisecond * 500
|
||||
@ -147,9 +158,15 @@ func main() {
|
||||
if err != nil {
|
||||
errs <- err
|
||||
} else {
|
||||
lights := lightSet{
|
||||
inside: &insideLight,
|
||||
inside2: &insideLight2,
|
||||
outside: &outsideLight,
|
||||
}
|
||||
|
||||
go cycleBrightness(insidepushed, insidebrightness)
|
||||
go cycleBrightness(outsidepushed, outsidebrightness)
|
||||
go loop(&insideLight, &insideLight2, &outsideLight, insidebrightness, outsidebrightness)
|
||||
go loop(lights, insidebrightness, outsidebrightness, partypushed)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -157,7 +174,7 @@ func main() {
|
||||
println((<-errs).Error())
|
||||
}
|
||||
|
||||
func cycleBrightness(pushchan chan bool, brightnesschan chan uint32) {
|
||||
func cycleBrightness(pushchan <-chan bool, brightnesschan chan<- uint32) {
|
||||
brightnesschan <- 0
|
||||
for {
|
||||
<-pushchan
|
||||
@ -179,43 +196,68 @@ func ledset(lpwm pwm, ledch uint8, brightness uint32) {
|
||||
lpwm.Set(ledch, (lpwm.Top()/brightnesspeak)*brightness)
|
||||
}
|
||||
|
||||
func loop(inside *lighthardware, inside2 *lighthardware, outside *lighthardware, insidebrightness chan uint32, outsidebrightness chan uint32) {
|
||||
var brightIn uint32
|
||||
var brightOut uint32
|
||||
inBrightChange := make(chan bool)
|
||||
outBrightChange := make(chan bool)
|
||||
func loop(lights lightSet, insidebrightness <-chan uint32, outsidebrightness <-chan uint32, partypushed <-chan bool) {
|
||||
inBrightChange := make(chan uint32)
|
||||
outBrightChange := make(chan uint32)
|
||||
partyChange := make(chan bool)
|
||||
lastBrightness := make(chan brightnesses)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
brightIn = <-insidebrightness
|
||||
brightIn := <-insidebrightness
|
||||
select {
|
||||
case inBrightChange <- true:
|
||||
case inBrightChange <- brightIn:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
brightOut = <-outsidebrightness
|
||||
brightOut := <-outsidebrightness
|
||||
select {
|
||||
case outBrightChange <- true:
|
||||
case outBrightChange <- brightOut:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
go normal(lights, inBrightChange, outBrightChange, partyChange, lastBrightness)
|
||||
<-partypushed
|
||||
// go party(lights)
|
||||
// <-partypushed
|
||||
partyChange <- true
|
||||
lb := <-lastBrightness
|
||||
select {
|
||||
case <-inBrightChange:
|
||||
normal(inside, brightIn)
|
||||
normal(inside2, brightIn)
|
||||
case <-outBrightChange:
|
||||
normal(outside, brightOut)
|
||||
case inBrightChange <- lb.inside:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case outBrightChange <- lb.outside:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func normal(light *lighthardware, brightness uint32) {
|
||||
func normal(lights lightSet, inBrightChange <-chan uint32, outBrightChange <-chan uint32, partyChange <-chan bool, lastBrightness chan<- brightnesses) {
|
||||
var brightIn uint32
|
||||
var brightOut uint32
|
||||
|
||||
for {
|
||||
select {
|
||||
case brightIn = <-inBrightChange:
|
||||
setNormal(lights.inside, brightIn)
|
||||
setNormal(lights.inside2, brightIn)
|
||||
case brightOut = <-outBrightChange:
|
||||
setNormal(lights.outside, brightOut)
|
||||
case <-partyChange:
|
||||
lastBrightness <- brightnesses{inside: brightIn, outside: brightOut}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setNormal(light *lighthardware, brightness uint32) {
|
||||
ledset(light.rPwm, light.rCh, brightness)
|
||||
ledset(light.gPwm, light.gCh, brightness)
|
||||
ledset(light.bPwm, light.bCh, brightness)
|
||||
|
Loading…
x
Reference in New Issue
Block a user