gobot.io/x/gobot@v1.16.0/examples/sphero_dpad.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"time"
     9  
    10  	"gobot.io/x/gobot"
    11  	"gobot.io/x/gobot/api"
    12  	"gobot.io/x/gobot/platforms/sphero"
    13  )
    14  
    15  func main() {
    16  	master := gobot.NewMaster()
    17  	a := api.NewAPI(master)
    18  	a.Start()
    19  
    20  	conn := sphero.NewAdaptor("/dev/rfcomm0")
    21  	ball := sphero.NewSpheroDriver(conn)
    22  
    23  	robot := gobot.NewRobot("sphero-dpad",
    24  		[]gobot.Connection{conn},
    25  		[]gobot.Device{ball},
    26  	)
    27  
    28  	robot.AddCommand("move", func(params map[string]interface{}) interface{} {
    29  		direction := params["direction"].(string)
    30  
    31  		switch direction {
    32  		case "up":
    33  			ball.Roll(100, 0)
    34  		case "down":
    35  			ball.Roll(100, 180)
    36  		case "left":
    37  			ball.Roll(100, 270)
    38  		case "right":
    39  			ball.Roll(100, 90)
    40  		}
    41  
    42  		time.Sleep(2 * time.Second)
    43  		ball.Stop()
    44  		return "ok"
    45  	})
    46  
    47  	master.AddRobot(robot)
    48  
    49  	master.Start()
    50  }