Merge pull request #137 from hybridgroup/godoc-leap
Adding godocs information to leap package
This commit is contained in:
commit
1f223ee816
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
|
||||
This package provides the Gobot adaptor and driver for the Leap Motion (https://www.leapmotion.com/)
|
||||
|
||||
Installing:
|
||||
|
||||
* First install the [Leap Motion Software](https://www.leapmotion.com/setup).
|
||||
* Then install the package:
|
||||
|
||||
go get github.com/hybridgroup/gobot && go install github.com/hybridgroup/gobot/platforms/leap
|
||||
|
||||
Example:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/leap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
leapMotionAdaptor := leap.NewLeapMotionAdaptor("leap", "127.0.0.1:6437")
|
||||
l := leap.NewLeapMotionDriver(leapMotionAdaptor, "leap")
|
||||
|
||||
work := func() {
|
||||
gobot.On(l.Event("message"), func(data interface{}) {
|
||||
fmt.Println(data.(leap.Frame))
|
||||
})
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("leapBot",
|
||||
[]gobot.Connection{leapMotionAdaptor},
|
||||
[]gobot.Device{l},
|
||||
work,
|
||||
)
|
||||
|
||||
gbot.AddRobot(robot)
|
||||
|
||||
gbot.Start()
|
||||
}
|
||||
|
||||
For more information refer to the leap README:
|
||||
https://github.com/hybridgroup/gobot/blob/master/platforms/leap/README.md
|
||||
*/
|
||||
package leap
|
|
@ -12,6 +12,7 @@ type LeapMotionAdaptor struct {
|
|||
connect func(*LeapMotionAdaptor)
|
||||
}
|
||||
|
||||
// NewLeapMotionAdaptor creates a new leap motion adaptor using specified name and port
|
||||
func NewLeapMotionAdaptor(name string, port string) *LeapMotionAdaptor {
|
||||
return &LeapMotionAdaptor{
|
||||
Adaptor: *gobot.NewAdaptor(
|
||||
|
@ -31,9 +32,12 @@ func NewLeapMotionAdaptor(name string, port string) *LeapMotionAdaptor {
|
|||
}
|
||||
}
|
||||
|
||||
// Connect returns true if connection to leap motion is established succesfully
|
||||
func (l *LeapMotionAdaptor) Connect() bool {
|
||||
l.connect(l)
|
||||
l.SetConnected(true)
|
||||
return true
|
||||
}
|
||||
|
||||
// Finalize ends connection to leap motion
|
||||
func (l *LeapMotionAdaptor) Finalize() bool { return true }
|
||||
|
|
|
@ -11,6 +11,10 @@ type LeapMotionDriver struct {
|
|||
gobot.Driver
|
||||
}
|
||||
|
||||
// NewLeapMotionDriver creates a new leap motion driver with specified name
|
||||
//
|
||||
// Adds the following events:
|
||||
// "message" - Gets triggered when receiving a message from leap motion
|
||||
func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
|
||||
l := &LeapMotionDriver{
|
||||
Driver: *gobot.NewDriver(
|
||||
|
@ -24,9 +28,16 @@ func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
|
|||
return l
|
||||
}
|
||||
|
||||
// adaptor returns leap motion adaptor
|
||||
func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
|
||||
return l.Adaptor().(*LeapMotionAdaptor)
|
||||
}
|
||||
|
||||
// Start inits leap motion driver by enabling gestures
|
||||
// and listening from incoming messages.
|
||||
//
|
||||
// Publishes the following events:
|
||||
// "message" - Sends parsed data of received frames.
|
||||
func (l *LeapMotionDriver) Start() bool {
|
||||
enableGestures := map[string]bool{"enableGestures": true}
|
||||
b, _ := json.Marshal(enableGestures)
|
||||
|
@ -45,5 +56,9 @@ func (l *LeapMotionDriver) Start() bool {
|
|||
|
||||
return true
|
||||
}
|
||||
|
||||
// Init returns true if driver is initialized correctly
|
||||
func (l *LeapMotionDriver) Init() bool { return true }
|
||||
|
||||
// Halt returns true if driver is halted succesfully
|
||||
func (l *LeapMotionDriver) Halt() bool { return true }
|
||||
|
|
|
@ -52,6 +52,7 @@ type InteractionBox struct {
|
|||
Size []float64 `json:"size"`
|
||||
}
|
||||
|
||||
// Base representation returned that holds every other objects
|
||||
type Frame struct {
|
||||
CurrentFrameRate float64 `json:"currentFrameRate"`
|
||||
Gestures []Gesture `json:"gestures"`
|
||||
|
@ -65,22 +66,29 @@ type Frame struct {
|
|||
Timestamp int `json:"timestamp"`
|
||||
}
|
||||
|
||||
// X returns hand x value
|
||||
func (h *Hand) X() float64 {
|
||||
return h.PalmPosition[0]
|
||||
}
|
||||
|
||||
// Y returns hand y value
|
||||
func (h *Hand) Y() float64 {
|
||||
return h.PalmPosition[1]
|
||||
}
|
||||
|
||||
// Z returns hand z value
|
||||
func (h *Hand) Z() float64 {
|
||||
return h.PalmPosition[2]
|
||||
}
|
||||
|
||||
// ParseFrame converts json data to a Frame
|
||||
func (l *LeapMotionDriver) ParseFrame(data []byte) Frame {
|
||||
var frame Frame
|
||||
json.Unmarshal(data, &frame)
|
||||
return frame
|
||||
}
|
||||
|
||||
// isAFrame returns true if data contains a frame representation
|
||||
func (l *LeapMotionDriver) isAFrame(data []byte) bool {
|
||||
match, _ := regexp.Match("currentFrameRate", data)
|
||||
return match
|
||||
|
|
Loading…
Reference in New Issue