File: sync.rb

package info (click to toggle)
libfreenect 1%3A0.5.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,156 kB
  • sloc: ansic: 7,417; cpp: 7,265; cs: 2,062; python: 992; ruby: 873; java: 730; xml: 49; sh: 27; makefile: 23
file content (93 lines) | stat: -rw-r--r-- 2,834 bytes parent folder | download | duplicates (6)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

require 'freenect'

module Freenect
  module Sync
    class FormatError < StandardError
    end

    # Synchronous video function (starts the runloop if it isn't running)
    #
    # @param idx 
    #   Device index. Default: 0
    #
    # @param fmt
    #   Video Format. See FFI::Freenect::VIDEO_FORMATS. Default is :rgb
    #
    # @return [Numeric, String]
    #   Returns an array containing a numeric timestamp and the video buffer 
    #   snapshot with a size based on the requested video format.
    #
    # @raise FormatError
    #   An exception is raised if an invalid format is specified.
    #
    # @raise RuntimeError
    #   An exception is raised if an unknown error occurs in the 
    #   freenect_sync_get_video function
    #   
    def self.get_video(idx=nil, fmt=nil)
      idx ||= 0
      fmt ||= :rgb

      if (buf_size = Freenect.lookup_video_size(fmt)).nil?
        raise(FormatError, "Invalid video format: #{fmt.inspect}")
      end

      video_p = FFI::MemoryPointer.new(buf_size)
      timestamp_p = FFI::MemoryPointer.new(:uint32)

      ret = ::FFI::Freenect.freenect_sync_get_video(video_p, timestamp_p, idx, Freenect.lookup_video_format(fmt))

      if ret != 0
        raise("Unknown error in freenect_sync_get_video()") # TODO is errno set or something here?
      else
        return [timestamp_p.read_int, video_p.read_string_length(buf_size)]
      end
    end

    # Synchronous depth function (starts the runloop if it isn't running)
    #
    # @param idx 
    #   Device index. Default: 0
    #
    # @param fmt
    #   Video Format. See FFI::Freenect::DEPTH_FORMATS. Default is :rgb
    #
    # @return [Numeric, String]
    #   Returns an array containing a numeric timestamp and the depth buffer 
    #   snapshot with a size based on the requested video format.
    #
    # @raise FormatError
    #   An exception is raised if an invalid format is specified.
    #
    # @raise RuntimeError
    #   An exception is raised if an unknown error occurs in the 
    #   freenect_sync_get_video function
    #   
    def self.get_depth(idx=0, fmt=:depth_11bit)
      idx ||= 0
      fmt ||= :depth_11bit

      if (buf_size = Freenect.lookup_depth_size(fmt)).nil?
        raise(FormatError, "Invalid depth format: #{fmt.inspect}")
      end

      depth_p = FFI::MemoryPointer.new(buf_size)
      timestamp_p = FFI::MemoryPointer.new(:uint32)

      ret = ::FFI::Freenect.freenect_sync_get_depth(depth_p, timestamp_p, idx, Freenect.lookup_depth_format(fmt))
      if ret != 0
        raise("Unknown error in freenect_sync_get_depth()") # TODO is errno set or something here?
      else
        return [timestamp_p.read_int, video_p.read_string_length(buf_size)]
      end
    end

    # Stops the sync runloop
    def self.stop
      FFI::Freenect.freenect_sync_stop()
    end

  end
end