gobot.io/x/gobot@v1.16.0/examples/sphero.go (about)

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