gobot.io/x/gobot/v2@v2.1.0/examples/sphero_master.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/platforms/sphero"
    14  )
    15  
    16  func main() {
    17  	master := gobot.NewMaster()
    18  
    19  	spheros := map[string]string{
    20  		"Sphero-BPO": "/dev/rfcomm0",
    21  	}
    22  
    23  	for name, port := range spheros {
    24  		spheroAdaptor := sphero.NewAdaptor(port)
    25  		spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
    26  
    27  		work := func() {
    28  			spheroDriver.SetRGB(uint8(255), uint8(0), uint8(0))
    29  		}
    30  
    31  		robot := gobot.NewRobot(name,
    32  			[]gobot.Connection{spheroAdaptor},
    33  			[]gobot.Device{spheroDriver},
    34  			work,
    35  		)
    36  
    37  		master.AddRobot(robot)
    38  	}
    39  
    40  	robot := gobot.NewRobot("",
    41  		func() {
    42  			gobot.Every(1*time.Second, func() {
    43  				sphero := master.Robot("Sphero-BPO").Device("sphero").(*sphero.SpheroDriver)
    44  				sphero.SetRGB(uint8(gobot.Rand(255)),
    45  					uint8(gobot.Rand(255)),
    46  					uint8(gobot.Rand(255)),
    47  				)
    48  			})
    49  		},
    50  	)
    51  
    52  	master.AddRobot(robot)
    53  
    54  	master.Start()
    55  }