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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"math"
     9  
    10  	"gobot.io/x/gobot"
    11  	"gobot.io/x/gobot/platforms/leap"
    12  	"gobot.io/x/gobot/platforms/sphero"
    13  )
    14  
    15  func main() {
    16  	leapAdaptor := leap.NewAdaptor("127.0.0.1:6437")
    17  	spheroAdaptor := sphero.NewAdaptor("/dev/tty.Sphero-YBW-RN-SPP")
    18  
    19  	leapDriver := leap.NewDriver(leapAdaptor)
    20  	spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
    21  
    22  	work := func() {
    23  		leapDriver.On(leap.MessageEvent, func(data interface{}) {
    24  			hands := data.(leap.Frame).Hands
    25  
    26  			if len(hands) > 0 {
    27  				x := math.Abs(hands[0].Direction[0])
    28  				y := math.Abs(hands[0].Direction[1])
    29  				z := math.Abs(hands[0].Direction[2])
    30  				spheroDriver.SetRGB(scale(x), scale(y), scale(z))
    31  			}
    32  		})
    33  	}
    34  
    35  	robot := gobot.NewRobot("leapBot",
    36  		[]gobot.Connection{leapAdaptor, spheroAdaptor},
    37  		[]gobot.Device{leapDriver, spheroDriver},
    38  		work,
    39  	)
    40  
    41  	robot.Start()
    42  }
    43  
    44  func scale(position float64) uint8 {
    45  	return uint8(gobot.ToScale(gobot.FromScale(position, 0, 1), 0, 255))
    46  }