gobot.io/x/gobot/v2@v2.1.0/examples/sprkplus_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/sprkplus"
    25  )
    26  
    27  func main() {
    28  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    29  	ball := sprkplus.NewDriver(bleAdaptor)
    30  
    31  	work := func() {
    32  		ball.On("collision", func(data interface{}) {
    33  			fmt.Printf("collision detected = %+v \n", data)
    34  			ball.SetRGB(255, 0, 0)
    35  		})
    36  
    37  		ball.SetRGB(0, 255, 0)
    38  		ball.Roll(80, 0)
    39  	}
    40  
    41  	robot := gobot.NewRobot("sprkplus",
    42  		[]gobot.Connection{bleAdaptor},
    43  		[]gobot.Device{ball},
    44  		work,
    45  	)
    46  
    47  	robot.Start()
    48  }