Esempio n. 1
0
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)
		}
	}
}
Esempio n. 2
0
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()
}
Esempio n. 3
0
// Load a texture from a PNG.
func Load(name string) (*Texture, error) {
	f, err := os.Open(name)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	img, _, err := image.Decode(f)
	if err != nil {
		return nil, err
	}

	b := img.Bounds().Size()
	tx := New(b.X, b.Y)

	for y := 0; y < b.Y; y++ {
		for x := 0; x < b.X; x++ {
			r, g, b, _ := img.At(x, y).RGBA()
			tx.Pixels[y*tx.width+x] = color.New(uint8(r>>8), uint8(g>>8), uint8(b>>8))
		}
	}
	return tx, nil
}