コード例 #1
0
ファイル: busylight.go プロジェクト: ingen-fakir/led
func newBusyLight(d hid.Device, setcolorFn func(c color.Color)) *busylightDev {
	closeChan := make(chan struct{})
	colorChan := make(chan color.Color)
	ticker := time.NewTicker(20 * time.Second) // If nothing is send after 30 seconds the device turns off.
	go func() {
		var curColor color.Color = color.Black
		closed := false
		for !closed {
			select {
			case <-ticker.C:
				setcolorFn(curColor)
			case col := <-colorChan:
				curColor = col
				setcolorFn(curColor)
			case <-closeChan:
				ticker.Stop()
				setcolorFn(color.Black) // turn off device
				d.Close()
				closed = true
			}
		}
	}()
	return &busylightDev{closeChan: closeChan, colorChan: colorChan}
}