gobot.io/x/gobot@v1.16.0/examples/bb8-collision.go (about)

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