Class: LEAP::Motion::WS

Inherits:
Object
  • Object
show all
Defined in:
lib/leap/motion/ws.rb,
lib/leap/motion/ws/hand.rb,
lib/leap/motion/ws/frame.rb,
lib/leap/motion/ws/gesture.rb,
lib/leap/motion/ws/pointable.rb

Overview

Public: Class to be subclassed to interface with the Leap Motion

Example

class MyLeap < LEAP::Motion::WS
  def initialize(...)
    #...
  end

  def on_frame(frame)
    # ...
  end

  def on_connect
    # ...
  end

  def on_disconnect
    # ...
  end

  def on_error(message)
    # ...
  end
end

on-frame is the only mandatory function.

Defined Under Namespace

Classes: Frame, Gesture, Hand, Pointable

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WS

Public: Initialize a LEAP::Motion::WS

options - The Hash options used to initialize the Leap access (default: => “ws://127.0.0.1:6437”, :enable_gesture => false, :history_size => 1000):

:uri - the String uri for the WebSocket::EventMachine::Client connection (optional)
:enable_gesture - boolean to indicate if we want to enable gesture or not (optional)
:history_size : the Integer size of the history ()


43
44
45
46
# File 'lib/leap/motion/ws.rb', line 43

def initialize(options = {})
  @options = {:uri => "ws://127.0.0.1:6437", :enable_gesture => false, :history_size => 1000}.merge(options)
  @history = LEAP::Motion::Utils::History.new(@options[:history_size])
end

Class Method Details

.start(enable_gesture = false) ⇒ Object

Public: Start the interface

enable_gesture : boolean to indicate if we want to enable gesture or not (default: false)

Examples

class MyLeap < LEAP::Motion::WS
  ...
end
MyLeap.start

Returns the LEAP::Motion::WS subclass object



120
121
122
123
# File 'lib/leap/motion/ws.rb', line 120

def self.start(enable_gesture = false)
  _leap = new
  _leap.start(enable_gesture)
end

Instance Method Details

#gesture!Object

Public: Enable gestures

Return nothing.



61
62
63
64
65
66
# File 'lib/leap/motion/ws.rb', line 61

def gesture!
  unless @ws.nil?
    data = JSON "enableGestures" => true
    options[:enable_gesture] = ws.send data
  end
end

#gestures?Boolean

Public: Return true if gestures are enabled, false otherwise

Returns:

  • (Boolean)


49
50
51
# File 'lib/leap/motion/ws.rb', line 49

def gestures?
  options[:enable_gesture] 
end

#historyObject

Public: Return the Frame History



54
55
56
# File 'lib/leap/motion/ws.rb', line 54

def history
  @history ||= LEAP::Motion::Utils::History.new(options[:history_size])
end

#start(enable_gesture = false) ⇒ Object

Public: Start the interface

enable_gesture : boolean to indicate if we want to enable gesture or not (default: false)

Examples

class MyLeap < LEAP::Motion::WS
  ...
end
MyLeap.new.start

Returns self



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/leap/motion/ws.rb', line 80

def start(enable_gesture = false)
  EM.run do
    ws.onopen do
      on_connect if respond_to? :on_connect
      gesture! if enable_gesture or options[:enable_gesture]
    end

    ws.onmessage do |msg, type|
      message = JSON(msg)
      if message.key?("id") and message.key?("timestamp")
        frame = LEAP::Motion::WS::Frame.new(message)
        history << frame
        on_frame frame
      end
    end

    ws.onerror do |err|
      on_error(err) if respond_to? :on_error
    end

    ws.onclose do
      on_disconnect if respond_to? :on_disconnect
    end
  end

  return self
end

#stopObject

Public: Stop the interface

Examples

class MyLeap < LEAP::Motion::WS
  ...
end
my_leap = MyLeap.start
...
my_leap.stop

Returns self



138
139
140
# File 'lib/leap/motion/ws.rb', line 138

def stop
  EM::stop_event_loop
end