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

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