1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
require 'ffi/freenect'
require 'freenect/device'
module Freenect
class ContextError < StandardError
end
class Context
def initialize(usb_ctx=nil)
ctx_p = FFI::MemoryPointer.new(:pointer)
if ::FFI::Freenect.freenect_init(ctx_p, usb_ctx) != 0
raise ContextError, "freenect_init() returned nonzero"
elsif ctx_p.null?
raise ContextError, "freenect_init() produced a NULL context"
end
@ctx = ctx_p.read_pointer
end
def context
if @ctx_closed
raise ContextError, "This context has been shut down and can no longer be used"
else
return @ctx
end
end
def num_devices
::FFI::Freenect.freenect_num_devices(self.context)
end
def open_device(idx)
return Device.new(self, idx)
end
alias [] open_device
def set_log_level(loglevel)
::FFI::Freenect.freenect_set_log_level(self.context, loglevel)
end
alias log_level= set_log_level
def set_log_callback(&block)
::FFI::Freenect.freenect_set_log_callback(self.context, block)
end
def process_events
::FFI::Freenect.freenect_process_events(self.context)
end
def close
unless closed?
if ::FFI::Freenect.freenect_shutdown(@ctx) != 0
raise ContextError, "freenect_shutdown() returned nonzero"
end
@ctx_closed = true
end
end
alias shutdown close
def closed?
@ctx_closed == true
end
end
end
|