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() {
|
||||
errs := make(chan error)
|
||||
|
||||
outsidepushed_raw := make(chan bool)
|
||||
insidepushed_raw := make(chan bool)
|
||||
partypushed_raw := make(chan bool)
|
||||
outsidepushed := make(chan bool)
|
||||
insidepushed := make(chan bool)
|
||||
partypushed := make(chan bool)
|
||||
@ -85,23 +88,27 @@ func main() {
|
||||
|
||||
outsidebutton.SetInterrupt(machine.PinFalling, func(p machine.Pin) {
|
||||
select {
|
||||
case outsidepushed <- true:
|
||||
case outsidepushed_raw <- true:
|
||||
default:
|
||||
}
|
||||
})
|
||||
insidebutton.SetInterrupt(machine.PinFalling, func(p machine.Pin) {
|
||||
select {
|
||||
case insidepushed <- true:
|
||||
case insidepushed_raw <- true:
|
||||
default:
|
||||
}
|
||||
})
|
||||
partybutton.SetInterrupt(machine.PinFalling, func(p machine.Pin) {
|
||||
select {
|
||||
case partypushed <- true:
|
||||
case partypushed_raw <- true:
|
||||
default:
|
||||
}
|
||||
})
|
||||
|
||||
go delayChan(outsidepushed_raw, outsidepushed)
|
||||
go delayChan(insidepushed_raw, insidepushed)
|
||||
go delayChan(partypushed_raw, partypushed)
|
||||
|
||||
pwm2 := machine.PWM2
|
||||
pwm2.Configure(machine.PWMConfig{
|
||||
Period: period,
|
||||
@ -196,21 +203,30 @@ func main() {
|
||||
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) {
|
||||
brightnesschan <- 0
|
||||
for {
|
||||
<-pushchan
|
||||
brightnesschan <- 255
|
||||
time.Sleep(pressdelay)
|
||||
<-pushchan
|
||||
brightnesschan <- 120
|
||||
time.Sleep(pressdelay)
|
||||
<-pushchan
|
||||
brightnesschan <- 30
|
||||
time.Sleep(pressdelay)
|
||||
<-pushchan
|
||||
brightnesschan <- 0
|
||||
time.Sleep(pressdelay)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user