// do_blink listens on the provided channel for specified color and blinks it at an // interval. func do_blink(color <-chan byte) { var c byte var intensity byte tick := time.NewTicker(BLINK_INTERVAL) LOOP: for { select { case c_new, ok := <-color: if !ok { break LOOP } piglow.Ring(c, 0) c = c_new case <-tick.C: if intensity == 0 { intensity = INTENSITY_MAX } else { intensity = 0 } piglow.Ring(c, intensity) } } err := piglow.ShutDown() if err != nil { log.Printf("failed to shutdown piglow: %v", err) } }
func main() { if ok := piglow.HasPiGlow(); !ok { log.Fatalf("piglow not available") } colors := make(chan byte) go do_blink(colors) sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) interval := time.NewTicker(Refresh) MAINLOOP: for { select { case <-interval.C: colors <- piglow.Orange current, err := get_juju_status() if err != nil { log.Printf("error: %v", err) continue } color := piglow.Green if len(current.Status[WatchedBranch]) != 0 { color = piglow.Red } colors <- color case <-sigChan: break MAINLOOP } } log.Printf("got sig kill - shutting down") err := piglow.ShutDown() if err != nil { log.Printf("failed to shutdown piglow: %v", err) os.Exit(1) } os.Exit(0) }
// main is the entry point of this program. It accepts two command-line // parameters: // // - brightness: Percentage of max LED brightness (between 0 and 1.0). // - period: CPU poll period (in milliseconds). func main() { signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) pollPeriodParameter := flag.Int("period", 200, "CPU poll period (in milliseconds)") brightnessParameter := flag.Float64("brightness", 0.02, "LED brightness (% of max brightness)") flag.Parse() if *brightnessParameter < 0 || *brightnessParameter > 1.0 { log.Fatalf("Brightness must be a value between 0 and 1.0 (got %v)", *brightnessParameter) } // Make sure we have a PiGlow to use. If we don't, provide a helpful error // message and exit, but wait several seconds before doing so in case systemd // respawns us. We don't want to hit its start limit. if !piglow.HasPiGlow() { log.Println("Unable to access PiGlow. Perhaps you need to hw-assign it?") time.Sleep(6 * time.Second) os.Exit(1) } // Initialize the CPU info so we can start out with a history previousStat := cpuStats() ticker := time.NewTicker(time.Duration(*pollPeriodParameter) * time.Millisecond) // Run this loop in a go routine so we can stop on demand. go func() { for _ = range ticker.C { currentStat := cpuStats() // Calculate previous idle and total ticks previousIdle := previousStat.Idle + previousStat.IOWait previousTotal := previousStat.User + previousStat.Nice + previousStat.System + previousStat.Idle + previousStat.IOWait + previousStat.IRQ + previousStat.SoftIRQ // Calculate current idle and total ticks currentIdle := currentStat.Idle + currentStat.IOWait currentTotal := currentStat.User + currentStat.Nice + currentStat.System + currentStat.Idle + currentStat.IOWait + currentStat.IRQ + currentStat.SoftIRQ // Calculating idle here since it's less typing. We'll invert it later. percentIdle := float64(currentIdle-previousIdle) / float64(currentTotal-previousTotal) displayUtilization(1.0-percentIdle, *brightnessParameter) previousStat = currentStat } }() <-signals // Wait for request to stop ticker.Stop() // We've been asked to stop piglow.ShutDown() // Turn off all LEDs }