gobot.io/x/gobot/v2@v2.1.0/examples/bb8-collision.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  /*
     8   How to run
     9   Pass the Bluetooth address or name as the first param:
    10  
    11  	go run examples/bb8-collision.go BB-1234
    12  
    13   NOTE: sudo is required to use BLE in Linux
    14  */
    15  
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  
    22  	"gobot.io/x/gobot/v2"
    23  	"gobot.io/x/gobot/v2/platforms/ble"
    24  	"gobot.io/x/gobot/v2/platforms/sphero/bb8"
    25  )
    26  
    27  func main() {
    28  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    29  	bb := bb8.NewDriver(bleAdaptor)
    30  
    31  	work := func() {
    32  
    33  		bb.On("collision", func(data interface{}) {
    34  			fmt.Printf("collision detected = %+v \n", data)
    35  			bb.SetRGB(255, 0, 0)
    36  		})
    37  
    38  		bb.SetRGB(0, 255, 0)
    39  		bb.Roll(80, 0)
    40  	}
    41  
    42  	robot := gobot.NewRobot("bb8",
    43  		[]gobot.Connection{bleAdaptor},
    44  		[]gobot.Device{bb},
    45  		work,
    46  	)
    47  
    48  	robot.Start()
    49  
    50  }