Skip to content

Commit 01c1f33

Browse files
algrszarvox
authored andcommitted
Updated Freenect singleton to query the new FrameMode system.
Signed-off-by: Alex Weiss <[email protected]>
1 parent 6f9b50a commit 01c1f33

File tree

3 files changed

+123
-97
lines changed

3 files changed

+123
-97
lines changed

wrappers/ruby/ffi-libfreenect/lib/ffi/freenect.rb

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module FFI::Freenect
8080

8181
RESOLUTIONS = enum( :low, RESOLUTION_LOW,
8282
:medium, RESOLUTION_MEDIUM,
83-
:high, RESOLUTION_HIGH )
83+
:high, RESOLUTION_HIGH)
8484

8585
DEPTH_11BIT = 0
8686
DEPTH_10BIT = 1
@@ -143,14 +143,40 @@ class FrameModeFormat < FFI::Union
143143
class FrameMode < FFI::Struct
144144
layout :reserved, :uint32,
145145
:resolution, RESOLUTIONS,
146-
:union, FrameModeFormat,
146+
:format, FrameModeFormat,
147147
:bytes, :int32,
148148
:width, :int16,
149149
:height, :int16,
150150
:data_bits_per_pixel, :int8,
151151
:padding_bits_per_pixel, :int8,
152152
:framerate, :int8,
153-
:is_valid, :int8,
153+
:is_valid, :int8
154+
def is_valid?
155+
self[:is_valid] != 0
156+
end
157+
def framerate; self[:framerate]; end
158+
def padding_bits_per_pixel; self[:padding_bits_per_pixel]; end
159+
def data_bits_per_pixel; self[:data_bits_per_pixel]; end
160+
def height; self[:height]; end
161+
def width; self[:width]; end
162+
def bytes; self[:bytes]; end
163+
def resolution; self[:resolution]; end
164+
def video_format; self[:format][:video_format]; end
165+
def depth_format; self[:format][:depth_format]; end
166+
attr_accessor :frame_mode_type
167+
def format
168+
case self.frame_mode_type
169+
when :video
170+
self.video_format
171+
when :depth
172+
self.depth_format
173+
else
174+
nil
175+
end
176+
end
177+
def to_s
178+
"#<Freenect::FrameMode #{format} #{resolution} res (#{width}x#{height}) @ #{framerate} Hz>"
179+
end
154180
end
155181

156182
class RawTiltState < FFI::Struct
@@ -192,15 +218,15 @@ class RawTiltState < FFI::Struct
192218
attach_function :freenect_get_mks_accel, [RawTiltState, :pointer, :pointer, :pointer], :void
193219

194220
attach_function :freenect_get_video_mode_count, [], :int
195-
attach_function :freenect_get_video_mode, [:int], FrameMode
196-
attach_function :freenect_get_current_video_mode, [:freenect_device], FrameMode
197-
attach_function :freenect_find_video_mode, [RESOLUTIONS, VIDEO_FORMATS], FrameMode
221+
attach_function :freenect_get_video_mode, [:int], FrameMode.by_value
222+
attach_function :freenect_get_current_video_mode, [:freenect_device], FrameMode.by_value
223+
attach_function :freenect_find_video_mode, [RESOLUTIONS, VIDEO_FORMATS], FrameMode.by_value
198224
attach_function :freenect_set_video_mode, [:freenect_device, FrameMode], :int
199225

200226
attach_function :freenect_get_depth_mode_count, [], :int
201-
attach_function :freenect_get_depth_mode, [:int], FrameMode
202-
attach_function :freenect_get_current_depth_mode, [:freenect_device], FrameMode
203-
attach_function :freenect_find_depth_mode, [RESOLUTIONS, DEPTH_FORMATS], FrameMode
227+
attach_function :freenect_get_depth_mode, [:int], FrameMode.by_value
228+
attach_function :freenect_get_current_depth_mode, [:freenect_device], FrameMode.by_value
229+
attach_function :freenect_find_depth_mode, [RESOLUTIONS, DEPTH_FORMATS], FrameMode.by_value
204230
attach_function :freenect_set_depth_mode, [:freenect_device, FrameMode], :int
205231

206232
attach_function :freenect_sync_get_video, [:pointer, :pointer, :int, VIDEO_FORMATS], :int

wrappers/ruby/ffi-libfreenect/lib/freenect.rb

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,85 @@
77

88
module Freenect
99
include FFI::Freenect
10+
FrameMode = FFI::Freenect::FrameMode
1011

1112
def self.init(*args)
1213
Context.new(*args)
1314
end
1415

15-
def self.lookup_video_format(fmt)
16-
return (fmt.is_a?(Numeric) ? fmt : FFI::Freenect::VIDEO_FORMATS[fmt])
16+
def self.num_video_modes
17+
::FFI::Freenect.freenect_get_video_mode_count()
1718
end
18-
19-
def self.lookup_video_size(fmt)
20-
l_fmt = (fmt.is_a?(Numeric) ? FFI::Freenect::VIDEO_FORMATS[fmt] : fmt)
21-
if l_fmt.nil? or (sz = FFI::Freenect::VIDEO_SIZES[l_fmt]).nil?
22-
return nil
19+
def self.video_mode(a,b=nil)
20+
x = if a.is_a?(Numeric)
21+
::FFI::Freenect.freenect_get_video_mode(a)
2322
else
24-
return sz
23+
::FFI::Freenect.freenect_find_video_mode(a,b)
2524
end
25+
x.frame_mode_type = :video unless x.nil?
26+
x
2627
end
27-
28-
def self.lookup_depth_format(fmt)
29-
return (fmt.is_a?(Numeric) ? fmt : FFI::Freenect::DEPTH_FORMATS[fmt])
28+
def self.video_modes
29+
(0...self.num_video_modes).map do |ii|
30+
self.video_mode(ii)
31+
end
3032
end
31-
32-
def self.lookup_depth_size(fmt)
33-
l_fmt = (fmt.is_a?(Numeric) ? FFI::Freenect::DEPTH_FORMATS[fmt] : fmt)
34-
if l_fmt.nil? or (sz = FFI::Freenect::DEPTH_SIZES[l_fmt]).nil?
35-
return nil
33+
34+
def self.num_depth_modes
35+
::FFI::Freenect.freenect_get_depth_mode_count()
36+
end
37+
def self.depth_mode(a,b=nil)
38+
x = if a.is_a?(Numeric)
39+
::FFI::Freenect.freenect_get_depth_mode(a)
3640
else
37-
return sz
41+
::FFI::Freenect.freenect_find_depth_mode(a,b)
42+
end
43+
x.frame_mode_type = :depth unless x.nil?
44+
x
45+
end
46+
def self.depth_modes
47+
(0...self.num_depth_modes).map do |ii|
48+
self.depth_mode(ii)
3849
end
3950
end
51+
52+
# def self.lookup_video_format(fmt)
53+
# if fmt.is_a?(Numeric)
54+
# unless (p=::FFI::Freenect.freenect_get_video_mode(self.device)).null?
55+
# frame_mode = FrameMode.new(p)
56+
# else
57+
# raise DeviceError, "freenect_get_video_mode() returned a NULL frame_mode"
58+
# end
59+
# else
60+
# unless (p=::FFI::Freenect.freenect_get_video_mode(self.device)).null?
61+
# frame_mode = FrameMode.new(p)
62+
# else
63+
# raise DeviceError, "freenect_get_video_mode() returned a NULL frame_mode"
64+
# end
65+
# end
66+
#
67+
# return frame_mode[:format][:video_format]
68+
# end
69+
#
70+
# def self.lookup_video_size(fmt)
71+
# l_fmt = (fmt.is_a?(Numeric) ? FFI::Freenect::VIDEO_FORMATS[fmt] : fmt)
72+
# if l_fmt.nil? or (sz = FFI::Freenect::VIDEO_SIZES[l_fmt]).nil?
73+
# return nil
74+
# else
75+
# return sz
76+
# end
77+
# end
78+
#
79+
# def self.lookup_depth_format(fmt)
80+
# return (fmt.is_a?(Numeric) ? fmt : FFI::Freenect::DEPTH_FORMATS[fmt])
81+
# end
82+
#
83+
# def self.lookup_depth_size(fmt)
84+
# l_fmt = (fmt.is_a?(Numeric) ? FFI::Freenect::DEPTH_FORMATS[fmt] : fmt)
85+
# if l_fmt.nil? or (sz = FFI::Freenect::DEPTH_SIZES[l_fmt]).nil?
86+
# return nil
87+
# else
88+
# return sz
89+
# end
90+
# end
4091
end

wrappers/ruby/ffi-libfreenect/spec/freenect_spec.rb

Lines changed: 20 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,82 +9,31 @@
99
lambda { ctx.close }.should_not raise_error
1010
end
1111

12-
it "should lookup video format values" do
13-
Freenect.lookup_video_format(:rgb).should == Freenect::VIDEO_RGB
14-
Freenect.lookup_video_format(0).should == Freenect::VIDEO_RGB
15-
16-
17-
Freenect.lookup_video_format(:bayer).should == Freenect::VIDEO_BAYER
18-
Freenect.lookup_video_format(1).should == Freenect::VIDEO_BAYER
19-
20-
Freenect.lookup_video_format(:ir_8bit).should == Freenect::VIDEO_IR_8BIT
21-
Freenect.lookup_video_format(2).should == Freenect::VIDEO_IR_8BIT
22-
23-
Freenect.lookup_video_format(:ir_10bit).should == Freenect::VIDEO_IR_10BIT
24-
Freenect.lookup_video_format(3).should == Freenect::VIDEO_IR_10BIT
25-
26-
Freenect.lookup_video_format(:ir_10bit_packed).should == Freenect::VIDEO_IR_10BIT_PACKED
27-
Freenect.lookup_video_format(4).should == Freenect::VIDEO_IR_10BIT_PACKED
28-
29-
Freenect.lookup_video_format(:yuv_rgb).should == Freenect::VIDEO_YUV_RGB
30-
Freenect.lookup_video_format(5).should == Freenect::VIDEO_YUV_RGB
31-
32-
Freenect.lookup_video_format(:yuv_raw).should == Freenect::VIDEO_YUV_RAW
33-
Freenect.lookup_video_format(6).should == Freenect::VIDEO_YUV_RAW
12+
it "should enumerate the video modes supported by the driver" do
13+
modes = Freenect.video_modes
14+
modes.should be_kind_of(Array)
15+
modes.length.should be > 0
16+
modes.each {|x| x.should be_kind_of(Freenect::FrameMode)}
17+
modes.each {|x| x.frame_mode_type.should == :video}
3418
end
35-
36-
it "should lookup video format size values" do
37-
Freenect.lookup_video_size(:rgb).should == Freenect::RGB_SIZE
38-
Freenect.lookup_video_size(0).should == Freenect::RGB_SIZE
39-
40-
Freenect.lookup_video_size(:bayer).should == Freenect::BAYER_SIZE
41-
Freenect.lookup_video_size(1).should == Freenect::BAYER_SIZE
42-
43-
Freenect.lookup_video_size(:ir_8bit).should == Freenect::IR_8BIT_SIZE
44-
Freenect.lookup_video_size(2).should == Freenect::IR_8BIT_SIZE
45-
46-
Freenect.lookup_video_size(:ir_10bit).should == Freenect::IR_10BIT_SIZE
47-
Freenect.lookup_video_size(3).should == Freenect::IR_10BIT_SIZE
48-
49-
Freenect.lookup_video_size(:ir_10bit_packed).should == Freenect::IR_10BIT_PACKED_SIZE
50-
Freenect.lookup_video_size(4).should == Freenect::IR_10BIT_PACKED_SIZE
51-
52-
Freenect.lookup_video_size(:yuv_rgb).should == Freenect::YUV_RGB_SIZE
53-
Freenect.lookup_video_size(5).should == Freenect::YUV_RGB_SIZE
54-
55-
Freenect.lookup_video_size(:yuv_raw).should == Freenect::YUV_RAW_SIZE
56-
Freenect.lookup_video_size(6).should == Freenect::YUV_RAW_SIZE
57-
19+
it "should be able to find basic video modes" do
20+
mode = Freenect.video_mode(:medium, :rgb)
21+
mode.resolution.should == :medium
22+
mode.format.should == :rgb
5823
end
5924

60-
it "should lookup depth format values" do
61-
Freenect.lookup_depth_format(:depth_11bit).should == Freenect::DEPTH_11BIT
62-
Freenect.lookup_depth_format(0).should == Freenect::DEPTH_11BIT
63-
64-
Freenect.lookup_depth_format(:depth_10bit).should == Freenect::DEPTH_10BIT
65-
Freenect.lookup_depth_format(1).should == Freenect::DEPTH_10BIT
66-
67-
Freenect.lookup_depth_format(:depth_11bit_packed).should == Freenect::DEPTH_11BIT_PACKED
68-
Freenect.lookup_depth_format(2).should == Freenect::DEPTH_11BIT_PACKED
69-
70-
Freenect.lookup_depth_format(:depth_10bit_packed).should == Freenect::DEPTH_10BIT_PACKED
71-
Freenect.lookup_depth_format(3).should == Freenect::DEPTH_10BIT_PACKED
25+
it "should enumerate the depth modes supported by the driver" do
26+
modes = Freenect.depth_modes
27+
modes.should be_kind_of(Array)
28+
modes.length.should be > 0
29+
modes.each {|x| x.should be_kind_of(Freenect::FrameMode)}
30+
modes.each {|x| x.frame_mode_type.should == :depth}
7231
end
73-
74-
it "should lookup depth format size values" do
75-
Freenect.lookup_depth_size(:depth_11bit).should == Freenect::DEPTH_11BIT_SIZE
76-
Freenect.lookup_depth_size(0).should == Freenect::DEPTH_11BIT_SIZE
77-
78-
Freenect.lookup_depth_size(:depth_10bit).should == Freenect::DEPTH_10BIT_SIZE
79-
Freenect.lookup_depth_size(1).should == Freenect::DEPTH_10BIT_SIZE
80-
81-
Freenect.lookup_depth_size(:depth_11bit_packed).should == Freenect::DEPTH_11BIT_PACKED_SIZE
82-
Freenect.lookup_depth_size(2).should == Freenect::DEPTH_11BIT_PACKED_SIZE
83-
84-
Freenect.lookup_depth_size(:depth_10bit_packed).should == Freenect::DEPTH_10BIT_PACKED_SIZE
85-
Freenect.lookup_depth_size(3).should == Freenect::DEPTH_10BIT_PACKED_SIZE
32+
it "should be able to find basic depth modes" do
33+
mode = Freenect.depth_mode(:medium, :depth_11bit)
34+
mode.resolution.should == :medium
35+
mode.format.should == :depth_11bit
8636
end
8737
end
88-
8938
end
9039

0 commit comments

Comments
 (0)