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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"gobot.io/x/gobot"
     9  	"gobot.io/x/gobot/api"
    10  	"gobot.io/x/gobot/platforms/sphero"
    11  )
    12  
    13  func main() {
    14  	master := gobot.NewMaster()
    15  	api.NewAPI(master).Start()
    16  
    17  	spheros := map[string]string{
    18  		"Sphero-BPO": "/dev/rfcomm0",
    19  	}
    20  
    21  	for name, port := range spheros {
    22  		spheroAdaptor := sphero.NewAdaptor(port)
    23  		spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
    24  
    25  		work := func() {
    26  			spheroDriver.SetRGB(uint8(255), uint8(0), uint8(0))
    27  		}
    28  
    29  		robot := gobot.NewRobot(name,
    30  			[]gobot.Connection{spheroAdaptor},
    31  			[]gobot.Device{spheroDriver},
    32  			work,
    33  		)
    34  		robot.AddCommand("turn_blue", func(params map[string]interface{}) interface{} {
    35  			spheroDriver.SetRGB(uint8(0), uint8(0), uint8(255))
    36  			return nil
    37  		})
    38  
    39  		master.AddRobot(robot)
    40  	}
    41  
    42  	master.Start()
    43  }