gobot.io/x/gobot/v2@v2.1.0/examples/leap_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  	"math"
    11  
    12  	"gobot.io/x/gobot/v2"
    13  	"gobot.io/x/gobot/v2/platforms/leap"
    14  	"gobot.io/x/gobot/v2/platforms/sphero"
    15  )
    16  
    17  func main() {
    18  	leapAdaptor := leap.NewAdaptor("127.0.0.1:6437")
    19  	spheroAdaptor := sphero.NewAdaptor("/dev/tty.Sphero-YBW-RN-SPP")
    20  
    21  	leapDriver := leap.NewDriver(leapAdaptor)
    22  	spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
    23  
    24  	work := func() {
    25  		leapDriver.On(leap.MessageEvent, func(data interface{}) {
    26  			hands := data.(leap.Frame).Hands
    27  
    28  			if len(hands) > 0 {
    29  				x := math.Abs(hands[0].Direction[0])
    30  				y := math.Abs(hands[0].Direction[1])
    31  				z := math.Abs(hands[0].Direction[2])
    32  				spheroDriver.SetRGB(scale(x), scale(y), scale(z))
    33  			}
    34  		})
    35  	}
    36  
    37  	robot := gobot.NewRobot("leapBot",
    38  		[]gobot.Connection{leapAdaptor, spheroAdaptor},
    39  		[]gobot.Device{leapDriver, spheroDriver},
    40  		work,
    41  	)
    42  
    43  	robot.Start()
    44  }
    45  
    46  func scale(position float64) uint8 {
    47  	return uint8(gobot.ToScale(gobot.FromScale(position, 0, 1), 0, 255))
    48  }