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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  /*
     6   To run this example, pass the BLE address or BLE name as first param:
     7  
     8   go run examples/ollie_multiple.go 2B-1234 2B-5678
     9  
    10   NOTE: sudo is required to use BLE in Linux
    11  */
    12  
    13  package main
    14  
    15  import (
    16  	"os"
    17  	"time"
    18  
    19  	"gobot.io/x/gobot"
    20  	"gobot.io/x/gobot/api"
    21  	"gobot.io/x/gobot/platforms/ble"
    22  	"gobot.io/x/gobot/platforms/sphero/ollie"
    23  )
    24  
    25  func NewSwarmBot(port string) *gobot.Robot {
    26  	bleAdaptor := ble.NewClientAdaptor(port)
    27  	ollieDriver := ollie.NewDriver(bleAdaptor)
    28  
    29  	work := func() {
    30  		gobot.Every(1*time.Second, func() {
    31  			ollieDriver.SetRGB(uint8(gobot.Rand(255)),
    32  				uint8(gobot.Rand(255)),
    33  				uint8(gobot.Rand(255)),
    34  			)
    35  		})
    36  	}
    37  
    38  	robot := gobot.NewRobot("ollie "+port,
    39  		[]gobot.Connection{bleAdaptor},
    40  		[]gobot.Device{ollieDriver},
    41  		work,
    42  	)
    43  
    44  	return robot
    45  }
    46  
    47  func main() {
    48  	master := gobot.NewMaster()
    49  	api.NewAPI(master).Start()
    50  
    51  	for _, port := range os.Args[1:] {
    52  		bot := NewSwarmBot(port)
    53  		master.AddRobot(bot)
    54  	}
    55  
    56  	master.Start()
    57  }