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

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"time"
    12  
    13  	"gobot.io/x/gobot/v2"
    14  	"gobot.io/x/gobot/v2/platforms/sphero"
    15  )
    16  
    17  func main() {
    18  	adaptor := sphero.NewAdaptor("/dev/rfcomm0")
    19  	spheroDriver := sphero.NewSpheroDriver(adaptor)
    20  
    21  	work := func() {
    22  		spheroDriver.SetDataStreaming(sphero.DefaultDataStreamingConfig())
    23  
    24  		spheroDriver.On(sphero.Collision, func(data interface{}) {
    25  			fmt.Printf("Collision! %+v\n", data)
    26  		})
    27  
    28  		spheroDriver.On(sphero.SensorData, func(data interface{}) {
    29  			fmt.Printf("Streaming Data! %+v\n", data)
    30  		})
    31  
    32  		gobot.Every(3*time.Second, func() {
    33  			spheroDriver.Roll(30, uint16(gobot.Rand(360)))
    34  		})
    35  
    36  		gobot.Every(1*time.Second, func() {
    37  			r := uint8(gobot.Rand(255))
    38  			g := uint8(gobot.Rand(255))
    39  			b := uint8(gobot.Rand(255))
    40  			spheroDriver.SetRGB(r, g, b)
    41  		})
    42  	}
    43  
    44  	robot := gobot.NewRobot("sphero",
    45  		[]gobot.Connection{adaptor},
    46  		[]gobot.Device{spheroDriver},
    47  		work,
    48  	)
    49  
    50  	robot.Start()
    51  }