Skip to content

Commit

Permalink
Moved to the standard error format, instead of log.Fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisjake committed Sep 4, 2013
1 parent 83c5929 commit 012292a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package gomotion

import (
"code.google.com/p/go.net/websocket"
"log"
)

// The LeapMotionDevice definition. Connecting to a device will return an instance of this struct.
Expand All @@ -14,38 +13,40 @@ type LeapMotionDevice struct {
}

// This function acts as a constructor and connector for the gomotion package.
func GetDevice(url string) *LeapMotionDevice {
func GetDevice(url string) (*LeapMotionDevice, error) {
pipe := make(chan *Frame)
connection, err := websocket.Dial(url, "", "http://localhost")
if err != nil {
log.Fatal(err)
return nil, err
}
return &LeapMotionDevice{pipe, connection}
return &LeapMotionDevice{pipe, connection}, nil
}

// This function starts the listening on the WebSocket. By default it enables Gestures on the LeapMotionDevice.
func (device *LeapMotionDevice) Listen() {
func (device *LeapMotionDevice) Listen() (error) {
var config struct {
enableGestures bool `json:"enableGestures"`
}
config.enableGestures = true
err := websocket.JSON.Send(device.Connection, &config)
if err != nil {
log.Fatal(err)
return err
}
go device.listenRead()
return nil
}

func (device *LeapMotionDevice) listenRead() {
func (device *LeapMotionDevice) listenRead() (error){
for {
var frame Frame
err := websocket.JSON.Receive(device.Connection, &frame)
if err == nil {
device.Pipe <- &frame
} else {
log.Fatal(err)
return err
}
}
return nil
}

// This function closes the internal WebSocket connection on a LeapMotionDevice
Expand Down

0 comments on commit 012292a

Please sign in to comment.