gobot.io/x/gobot@v1.16.0/examples/sphero_master.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/platforms/sphero"
    12  )
    13  
    14  func main() {
    15  	master := gobot.NewMaster()
    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  
    35  		master.AddRobot(robot)
    36  	}
    37  
    38  	robot := gobot.NewRobot("",
    39  		func() {
    40  			gobot.Every(1*time.Second, func() {
    41  				sphero := master.Robot("Sphero-BPO").Device("sphero").(*sphero.SpheroDriver)
    42  				sphero.SetRGB(uint8(gobot.Rand(255)),
    43  					uint8(gobot.Rand(255)),
    44  					uint8(gobot.Rand(255)),
    45  				)
    46  			})
    47  		},
    48  	)
    49  
    50  	master.AddRobot(robot)
    51  
    52  	master.Start()
    53  }