func main() { var path string flag.StringVar(&path, "path", "/dev/input/event0", "path to the event device") // Parse command line flags flag.Parse() // Open the input device (and defer closing it) input, err := stick.Open(path) if err != nil { fmt.Printf("Unable to open input device: %s\nError: %v\n", path, err) os.Exit(1) } // Clear the screen screen.Clear() // Print the name of the input device fmt.Println(input.Name()) // Set up a signals channel (stop the loop using Ctrl-C) signals := make(chan os.Signal, 1) signal.Notify(signals, os.Interrupt, os.Kill) // Loop forever for { select { case <-signals: fmt.Println("") screen.Clear() // Exit the loop return case e := <-input.Events: fb := screen.NewFrameBuffer() switch e.Code { case stick.Enter: fmt.Println("⏎") case stick.Up: fmt.Println("↑") draw(fb, 0, 0, 8, 4, color.New(255, 255, 0)) case stick.Down: fmt.Println("↓") draw(fb, 0, 4, 8, 8, color.New(255, 0, 0)) case stick.Left: fmt.Println("←") draw(fb, 0, 0, 4, 8, color.New(0, 0, 255)) case stick.Right: fmt.Println("→") draw(fb, 4, 0, 8, 8, color.New(0, 255, 0)) } screen.Draw(fb) } } }
func main() { // create a new frame buffer fb := screen.NewFrameBuffer() // turn on LEDs on the first row fb.SetPixel(0, 0, color.Red) fb.SetPixel(1, 0, color.Green) fb.SetPixel(2, 0, color.Blue) fb.SetPixel(3, 0, color.New(255, 0, 255)) // Magenta fb.SetPixel(4, 0, color.New(255, 255, 0)) // Yellow fb.SetPixel(5, 0, color.New(0, 255, 255)) // Cyan fb.SetPixel(6, 0, color.White) // draw gradients var c uint8 for x := 0; x < 8; x++ { c = uint8((x+1)*32 - 1) fb.SetPixel(x, 1, color.New(c, c, c)) fb.SetPixel(x, 2, color.New(c, 0, 0)) fb.SetPixel(x, 3, color.New(0, c, 0)) fb.SetPixel(x, 4, color.New(0, 0, c)) fb.SetPixel(x, 5, color.New(c, 0, c)) fb.SetPixel(x, 6, color.New(c, c, 0)) fb.SetPixel(x, 7, color.New(0, c, c)) } // draw the frame buffer to the screen screen.Draw(fb) // wait for Ctrl-C, then clear the screen quit := make(chan os.Signal, 1) signal.Notify(quit, syscall.SIGUSR1, syscall.SIGINT, syscall.SIGTERM) <-quit screen.Clear() }
func main() { // load a PNG specified on the command line (16x16 recommended). if len(os.Args) < 2 { log.Fatal("specify a png image to load.") } tx, err := texture.Load(os.Args[1]) if err != nil { log.Fatal(err) } // create a new frame buffer fb := screen.NewFrameBuffer() // setup a channel to handle Ctrl-C events quit := make(chan os.Signal, 1) signal.Notify(quit, syscall.SIGUSR1, syscall.SIGINT, syscall.SIGTERM) // scroll around the edges of the image. var xo, yo, state int loop: for { // this is to pause for 33 milliseconds (30fps) // except during state changes, which will pause for half-a-second. sleep := 33 switch state { case 0: // nothing (initial state) state = 1 sleep = 500 case 1: // down yo++ if yo == tx.Height()-8 { state = 2 sleep = 500 } case 2: // right xo++ if xo == tx.Width()-8 { state = 3 sleep = 500 } case 3: // up yo-- if yo == 0 { state = 4 sleep = 500 } case 4: // left xo-- if xo == 0 { state = 1 sleep = 500 } } // Blit texture to the frame buffer. texture.Blit(fb.Texture, 0, 0, tx, xo, yo, 8, 8) // Draw frame buffer to the screen. screen.Draw(fb) // Check if Ctrl-C was pressed // while waiting a few milliseconds. timer := time.NewTimer(time.Millisecond * time.Duration(sleep)) select { case <-quit: timer.Stop() break loop case <-timer.C: } } screen.Clear() }