예제 #1
0
파일: main.go 프로젝트: intfrr/bobblehat
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()
}
예제 #2
0
파일: main.go 프로젝트: intfrr/bobblehat
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()
}
예제 #3
0
파일: main.go 프로젝트: intfrr/bobblehat
func main() {
	fb := screen.NewFrameBuffer()

	var count int
	for {
		count = stick.ReadEvent()
		fmt.Printf("Count is-->%d", count)

		rand.Seed(time.Now().UnixNano())
		direction := rand.Intn(5)

		switch direction {
		case stick.Right:
			fmt.Println("right")
			for i := 0; i < 8; i++ {
				for j := 4; j < 8; j++ {
					fb.SetPixel(i, j, color.Red)
				}
			}
			screen.Draw(fb)
			break
		case stick.Left:
			fmt.Println("left")
			for i := 0; i < 8; i++ {
				for j := 0; j < 4; j++ {
					fb.SetPixel(i, j, color.Black)
				}
			}
			screen.Draw(fb)
			break
		case stick.Up:
			fmt.Println("up")
			for i := 0; i < 4; i++ {
				for j := 0; j < 8; j++ {
					fb.SetPixel(i, j, color.Blue)
				}
			}
			screen.Draw(fb)
			break
		case stick.Down:
			fmt.Println("down")
			for i := 4; i < 8; i++ {
				for j := 0; j < 8; j++ {
					fb.SetPixel(i, j, color.Green)
				}
			}
			screen.Draw(fb)
			break
		case stick.Enter:
			fmt.Println("enter")
			for i := 0; i < 8; i++ {
				for j := 0; j < 8; j++ {
					fb.SetPixel(i, j, color.White)
				}
			}
			screen.Draw(fb)
			break
		default:
			fmt.Println("waiting to be pressed")
		}
		time.Sleep(10)
	}

}