gobot.io/x/gobot/v2@v2.1.0/examples/sphero_dpad.go (about)

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