2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/keyboard"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/mqtt"
|
2016-12-08 20:24:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
keys := keyboard.NewDriver()
|
|
|
|
mqttAdaptor := mqtt.NewAdaptor("tcp://iot.eclipse.org:1883", "conductor")
|
|
|
|
|
|
|
|
work := func() {
|
2024-02-11 22:34:50 +08:00
|
|
|
_ = keys.On(keyboard.Key, func(data interface{}) {
|
2016-12-08 20:24:03 +08:00
|
|
|
key := data.(keyboard.KeyEvent)
|
|
|
|
|
|
|
|
switch key.Key {
|
|
|
|
case keyboard.ArrowUp:
|
|
|
|
mqttAdaptor.Publish("rover/frente", []byte{})
|
|
|
|
case keyboard.ArrowRight:
|
|
|
|
mqttAdaptor.Publish("rover/derecha", []byte{})
|
|
|
|
case keyboard.ArrowDown:
|
|
|
|
mqttAdaptor.Publish("rover/atras", []byte{})
|
|
|
|
case keyboard.ArrowLeft:
|
|
|
|
mqttAdaptor.Publish("rover/izquierda", []byte{})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("keyboardbot",
|
|
|
|
[]gobot.Connection{mqttAdaptor},
|
|
|
|
[]gobot.Device{keys},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-12-08 20:24:03 +08:00
|
|
|
}
|