remove time.Sleep() calls
- time.Sleep() blocks the entire program, other goroutines won't run. Instead, watch the button channels and filter out any extra presses within too short of a time.
This commit is contained in:
parent
3c4b177360
commit
67607347e4
30
main.go
30
main.go
@ -68,6 +68,9 @@ const (
|
|||||||
func main() {
|
func main() {
|
||||||
errs := make(chan error)
|
errs := make(chan error)
|
||||||
|
|
||||||
|
outsidepushed_raw := make(chan bool)
|
||||||
|
insidepushed_raw := make(chan bool)
|
||||||
|
partypushed_raw := make(chan bool)
|
||||||
outsidepushed := make(chan bool)
|
outsidepushed := make(chan bool)
|
||||||
insidepushed := make(chan bool)
|
insidepushed := make(chan bool)
|
||||||
partypushed := make(chan bool)
|
partypushed := make(chan bool)
|
||||||
@ -85,23 +88,27 @@ func main() {
|
|||||||
|
|
||||||
outsidebutton.SetInterrupt(machine.PinFalling, func(p machine.Pin) {
|
outsidebutton.SetInterrupt(machine.PinFalling, func(p machine.Pin) {
|
||||||
select {
|
select {
|
||||||
case outsidepushed <- true:
|
case outsidepushed_raw <- true:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
insidebutton.SetInterrupt(machine.PinFalling, func(p machine.Pin) {
|
insidebutton.SetInterrupt(machine.PinFalling, func(p machine.Pin) {
|
||||||
select {
|
select {
|
||||||
case insidepushed <- true:
|
case insidepushed_raw <- true:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
partybutton.SetInterrupt(machine.PinFalling, func(p machine.Pin) {
|
partybutton.SetInterrupt(machine.PinFalling, func(p machine.Pin) {
|
||||||
select {
|
select {
|
||||||
case partypushed <- true:
|
case partypushed_raw <- true:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
go delayChan(outsidepushed_raw, outsidepushed)
|
||||||
|
go delayChan(insidepushed_raw, insidepushed)
|
||||||
|
go delayChan(partypushed_raw, partypushed)
|
||||||
|
|
||||||
pwm2 := machine.PWM2
|
pwm2 := machine.PWM2
|
||||||
pwm2.Configure(machine.PWMConfig{
|
pwm2.Configure(machine.PWMConfig{
|
||||||
Period: period,
|
Period: period,
|
||||||
@ -196,21 +203,30 @@ func main() {
|
|||||||
println((<-errs).Error())
|
println((<-errs).Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func delayChan(inchan <-chan bool, outchan chan<- bool) {
|
||||||
|
var last time.Time
|
||||||
|
|
||||||
|
for {
|
||||||
|
val := <-inchan
|
||||||
|
now := time.Now()
|
||||||
|
if now.Sub(last) > pressdelay {
|
||||||
|
last = now
|
||||||
|
outchan <- val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func cycleBrightness(pushchan <-chan bool, brightnesschan chan<- uint32) {
|
func cycleBrightness(pushchan <-chan bool, brightnesschan chan<- uint32) {
|
||||||
brightnesschan <- 0
|
brightnesschan <- 0
|
||||||
for {
|
for {
|
||||||
<-pushchan
|
<-pushchan
|
||||||
brightnesschan <- 255
|
brightnesschan <- 255
|
||||||
time.Sleep(pressdelay)
|
|
||||||
<-pushchan
|
<-pushchan
|
||||||
brightnesschan <- 120
|
brightnesschan <- 120
|
||||||
time.Sleep(pressdelay)
|
|
||||||
<-pushchan
|
<-pushchan
|
||||||
brightnesschan <- 30
|
brightnesschan <- 30
|
||||||
time.Sleep(pressdelay)
|
|
||||||
<-pushchan
|
<-pushchan
|
||||||
brightnesschan <- 0
|
brightnesschan <- 0
|
||||||
time.Sleep(pressdelay)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user