Class: LeapMotion::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/leap_motion.rb,
ext/leap_motion/leap_motion.cpp

Constant Summary collapse

EVENTS =
[:init, :connect, :disconnect, :frame, :focus_gained, :focus_lost]

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



16
17
18
19
20
21
22
# File 'lib/leap_motion.rb', line 16

def initialize
  super
  @listeners = Set.new
  @mutex     = Mutex.new
  ios        = EVENTS.map { |event| IO.new send "#{event}_fd" }
  @listener  = start_listener ios
end

Instance Method Details

#add_listener(listener) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/leap_motion.rb', line 40

def add_listener listener
  @mutex.synchronize do
    return false if @listeners.include? listener
    @listeners = @listeners.dup.add listener
    listen!
  end
end

#each_frameObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/leap_motion.rb', line 26

def each_frame
  reader = IO.new frame_fd
  listen!
  loop do
    rs, = IO.select [reader]
    rs.each { |io|
      io.read 1
      yield frame
    }
  end
ensure
  unlisten! if @listeners.empty?
end

#joinObject



24
# File 'lib/leap_motion.rb', line 24

def join; @listener.join; end

#remove_listener(listener) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/leap_motion.rb', line 48

def remove_listener listener
  @mutex.synchronize do
    return false unless @listeners.include? listener
    @listeners = @listeners.dup.delete listener
    unlisten! if @listeners.empty?
  end
end