Skip to content

Commit 4d3c590

Browse files
algrszarvox
authored andcommitted
wrappers/ruby: Update examples
Updated examples & got the last few tests passing Signed-off-by: Alex Weiss <[email protected]>
1 parent b674a8a commit 4d3c590

File tree

6 files changed

+44
-27
lines changed

6 files changed

+44
-27
lines changed

wrappers/ruby/ffi-libfreenect/README.rdoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ FFI-based Ruby wrapper for the OpenKinect libfreenect library.
5151
#
5252
# The example below shows how to write a single video frame to a PPM file.
5353

54-
dev.set_depth_format(Freenect::DEPTH_11BIT)
55-
dev.set_video_format(Freenect::VIDEO_RGB)
54+
dev.depth_mode = Freenect.depth_mode(:medium, :depth_11bit)
55+
dev.video_mode = Freenect.video_mode(:medium, :rgb)
5656
dev.start_depth()
5757
dev.start_video()
5858

@@ -64,9 +64,9 @@ FFI-based Ruby wrapper for the OpenKinect libfreenect library.
6464
fname = "%u.ppm" % timestamp
6565
STDERR.puts "Writing #{fname}"
6666
File.open(fname, "w") do |f|
67-
f.puts("P6 %d %d 255\n" % [ Freenect::FRAME_W, Freenect::FRAME_H ] )
68-
f.write(video.read_string_length(Freenect::RGB_SIZE))
69-
end
67+
f.puts("P6 %d %d 255\n" % [ dev.video_mode.width, dev.video_mode.height ] )
68+
f.write(video.read_string_length(dev.video_mode.bytes))
69+
end
7070
$snapshot_finished = true
7171
end
7272
end

wrappers/ruby/ffi-libfreenect/examples/record.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,22 @@ def open_dump(type, timestamp, extension)
4343
ctx = Freenect.init()
4444
dev = ctx.open_device(0)
4545

46-
dev.set_depth_format(Freenect::DEPTH_11BIT)
47-
dev.set_video_format(Freenect::VIDEO_RGB)
46+
dev.depth_mode = Freenect.depth_mode(:medium, :depth_11bit)
47+
dev.video_mode = Freenect.video_mode(:medium, :rgb)
4848
dev.start_depth()
4949
dev.start_video()
5050

5151
dev.set_depth_callback do |device, depth, timestamp|
5252
open_dump('d', timestamp, "pgm") do |f|
53-
f.puts("P5 %d %d 65535\n" % [ Freenect::FRAME_W, Freenect::FRAME_H ] )
54-
f.write(depth.read_string_length(Freenect::DEPTH_11BIT_SIZE))
53+
f.puts("P5 %d %d 65535\n" % [ dev.depth_mode.width, dev.depth_mode.height ] )
54+
f.write(depth.read_string_length(dev.depth_mode.bytes))
5555
end
5656
end
5757

5858
dev.set_video_callback do |device, video, timestamp|
5959
open_dump('r', timestamp, 'ppm') do |f|
60-
f.puts("P6 %d %d 255\n" % [ Freenect::FRAME_W, Freenect::FRAME_H ] )
61-
f.write(video.read_string_length(Freenect::RGB_SIZE))
60+
f.puts("P6 %d %d 255\n" % [ dev.video_mode.width, dev.video_mode.height ] )
61+
f.write(video.read_string_length(dev.video_mode.bytes))
6262
end
6363
end
6464

wrappers/ruby/ffi-libfreenect/examples/video_snapshot.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
STDERR.puts "Selecting device 0"
2323
dev = ctx.open_device(0)
2424

25-
dev.set_led(:green) # play with the led
25+
dev.led = :green # play with the led
2626

27-
dev.set_video_format(Freenect::VIDEO_RGB)
28-
dev.start_depth()
27+
dev.video_mode = Freenect.video_mode(:medium, :rgb)
2928
dev.start_video()
3029

30+
# Or to grab depth:
31+
# dev.depth_mode = Freenect.depth_mode(:medium, :depth_11bit)
32+
# dev.start_depth()
33+
34+
3135
$snapshot_finished = nil
3236

3337
STDERR.puts "Attempting snapshot"
@@ -36,8 +40,8 @@
3640
fname = "%i.ppm" % timestamp
3741
STDERR.puts "Writing #{fname}"
3842
File.open(fname, "w") do |f|
39-
f.puts("P6 %d %d 255\n" % [ Freenect::FRAME_W, Freenect::FRAME_H ] )
40-
f.write(video.read_string_length(Freenect::RGB_SIZE))
43+
f.puts("P6 %d %d 255\n" % [ dev.video_mode.width, dev.video_mode.height ] )
44+
f.write(video.read_string_length(dev.video_mode.bytes))
4145
end
4246
$snapshot_finished = true
4347
end
@@ -52,9 +56,11 @@
5256
STDERR.puts "Error: unable to take snapshot. process_events code=#{ret}"
5357
end
5458

55-
dev.set_led(:off)
56-
dev.stop_depth
59+
dev.led = :off
5760
dev.stop_video
61+
# Or
62+
# dev.stop_depth
63+
5864
dev.close
5965
ctx.close
6066

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ class RawTiltState < FFI::Struct
185185
:accelerometer_z, :int16,
186186
:tilt_angle, :int8,
187187
:tilt_status, TILT_STATUS_CODES
188+
def accelerometer
189+
[self[:accelerometer_x], self[:accelerometer_y], self[:accelerometer_z]]
190+
end
191+
def angle; self[:tilt_angle]; end
192+
def status; self[:tilt_status]; end
193+
def to_s
194+
"#<Freenect::RawTiltState #{status} @ #{angle} deg, accel: (#{accelerometer.join(',')})>"
195+
end
188196
end
189197

190198
callback :freenect_log_cb, [:freenect_context, LOGLEVELS, :string], :void
@@ -221,13 +229,13 @@ class RawTiltState < FFI::Struct
221229
attach_function :freenect_get_video_mode, [:int], FrameMode.by_value
222230
attach_function :freenect_get_current_video_mode, [:freenect_device], FrameMode.by_value
223231
attach_function :freenect_find_video_mode, [RESOLUTIONS, VIDEO_FORMATS], FrameMode.by_value
224-
attach_function :freenect_set_video_mode, [:freenect_device, FrameMode], :int
232+
attach_function :freenect_set_video_mode, [:freenect_device, FrameMode.by_value], :int
225233

226234
attach_function :freenect_get_depth_mode_count, [], :int
227235
attach_function :freenect_get_depth_mode, [:int], FrameMode.by_value
228236
attach_function :freenect_get_current_depth_mode, [:freenect_device], FrameMode.by_value
229237
attach_function :freenect_find_depth_mode, [RESOLUTIONS, DEPTH_FORMATS], FrameMode.by_value
230-
attach_function :freenect_set_depth_mode, [:freenect_device, FrameMode], :int
238+
attach_function :freenect_set_depth_mode, [:freenect_device, FrameMode.by_value], :int
231239

232240
attach_function :freenect_sync_get_video, [:pointer, :pointer, :int, VIDEO_FORMATS], :int
233241
attach_function :freenect_sync_get_depth, [:pointer, :pointer, :int, DEPTH_FORMATS], :int

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,14 @@ def set_depth_mode(mode)
136136
mode = Freenect.depth_mode(:medium, mode) unless mode.is_a?(Freenect::FrameMode)
137137
raise ArgumentError, "Unkown depth mode #{mode}" if mode.nil?
138138
ret = ::FFI::Freenect.freenect_set_depth_mode(self.device, mode)
139-
raise DeviceError, "Error calling freenect_set_depth_mode(self, #{mode})" unless ret == 0
139+
raise DeviceError, "Error calling freenect_set_depth_mode(self, #{mode}) returned #{ret}" unless ret == 0
140140
end
141141
alias depth_mode= set_depth_mode
142142

143143
# returns the symbolic constant for the current depth format
144144
def depth_mode
145145
x = ::FFI::Freenect.freenect_get_current_depth_mode(self.device)
146+
x = nil if x.height*x.width == 0 or x.framerate == 0
146147
x.frame_mode_type = :depth unless x.nil?
147148
x
148149
end
@@ -153,12 +154,13 @@ def set_video_mode(mode)
153154
mode = Freenect.video_mode(:medium, mode) unless mode.is_a?(Freenect::FrameMode)
154155
raise ArgumentError, "Unkown video mode #{mode}" if mode.nil?
155156
ret = ::FFI::Freenect.freenect_set_video_mode(self.device, mode)
156-
raise DeviceError, "Error calling freenect_set_video_mode(self, #{mode})" unless ret == 0
157+
raise DeviceError, "Error calling freenect_set_video_mode(self, #{mode}) returned #{ret}" unless ret == 0
157158
end
158159
alias video_mode= set_video_mode
159160

160161
def video_mode
161162
x = ::FFI::Freenect.freenect_get_current_video_mode(self.device)
163+
x = nil if x.height*x.width == 0 or x.framerate == 0
162164
x.frame_mode_type = :video unless x.nil?
163165
x
164166
end

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,19 @@
7272
@dev.video_mode.should be_nil # at first
7373
@dev.video_mode = Freenect.video_mode(:medium, :bayer)
7474
@dev.video_mode.format.should == :bayer
75-
@dev.video_mode = Freenect.video_mode(:medium, :rgb)
75+
@dev.video_mode = :rgb
7676
@dev.video_mode.format.should == :rgb
77-
@dev.video_mode.frame_mode_type.shoud == :video
77+
@dev.video_mode.frame_mode_type.should == :video
7878
end
7979

8080

8181
it "should allow the depth_format to be set and retrieved" do
8282
@dev.depth_mode.should be_nil # at first
83-
@dev.depth_mode = :depth_10bit
84-
@dev.depth_mode.should == :depth_10bit
85-
@dev.depth_mode = Freenect::DEPTH_11BIT
83+
@dev.depth_mode = Freenect.depth_mode(:medium, :depth_10bit)
84+
@dev.depth_mode.format.should == :depth_10bit
8685
@dev.depth_mode = :depth_11bit
86+
@dev.depth_mode.format.should == :depth_11bit
87+
@dev.depth_mode.frame_mode_type.should == :depth
8788
end
8889

8990
it "should allow itself to be looked up by it's object reference ID" do

0 commit comments

Comments
 (0)