Exemple #1
0
func main() {
	gbot := gobot.NewGobot()

	keys := keyboard.NewKeyboardDriver("keyboard")

	work := func() {
		keys.On(keyboard.Key, func(data interface{}) {
			key := data.(keyboard.KeyEvent)

			if key.Key == keyboard.A {
				fmt.Println("A pressed!")
			} else {
				fmt.Println("keyboard event!", key, key.Char)
			}
		})
	}

	robot := gobot.NewRobot("keyboardbot",
		[]gobot.Connection{},
		[]gobot.Device{keys},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
Exemple #2
0
func main() {
	var (
		gbot = gobot.NewGobot()
		r    = raspi.NewRaspiAdaptor("raspi")
		keys = keyboard.NewKeyboardDriver("keyboard")
	)

	conf, err := NewMatrixConfig("config.json")
	if err != nil {
		log.Fatalf("Error loading config: %s", err)
	}
	cube, err := NewMatrixCube(r, conf)
	if err != nil {
		log.Fatalf("Error init cube: %s", err)
	}

	work := func() {
		var x, y, z int

		cube.Clear()
		cube.Toggle(x, y, z)

		ticker := time.NewTicker(10 * time.Millisecond)
		gobot.On(keys.Event("key"), func(data interface{}) {
			<-ticker.C
			key := data.(keyboard.KeyEvent)

			lastX, lastY, lastZ := x, y, z
			if key.Key == keyboard.R {
				cube.Clear()
				x, y, z = 0, 0, 0
				cube.Toggle(x, y, z)
				return
			}
			x, y, z = handle(key.Key, x, y, z)
			if lastX != x || lastY != y || lastZ != z {
				cube.Toggle(lastX, lastY, lastZ)
				cube.Toggle(x, y, z)
			}
			println(x, y, z)
		})

	}

	devices := []gobot.Device{}
	for _, elem := range cube.rows {
		devices = append(devices, elem)
	}
	for _, elem := range cube.cols {
		devices = append(devices, elem)
	}
	devices = append(devices, keys)

	robot := gobot.NewRobot("blinkBot",
		[]gobot.Connection{r},
		devices,
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}