class GPhoto2::Camera

Attributes

model[R]

@return [String]

port[R]

@return [String]

Public Class Methods

all() click to toggle source

@example

cameras = GPhoto2::Camera.all
# => [#<GPhoto2::Camera>, #<GPhoto2::Camera>, ...]

@return [Array<GPhoto2::Camera>] a list of all available devices

# File lib/gphoto2/camera.rb, line 22
def self.all
  context = Context.new

  abilities = CameraAbilitiesList.new(context)
  cameras = abilities.detect

  entries = cameras.to_a.map do |entry|
    model, port = entry.name, entry.value
    Camera.new(model, port)
  end

  context.finalize

  entries
end
first(&blk) click to toggle source

@example

camera = GPhoto2::Camera.first

begin
  # ...
ensure
  camera.finalize
end

# Alternatively, pass a block, which will automatically close the camera.
GPhoto2::Camera.first do |camera|
  # ...
end

@return [GPhoto2::Camera] the first detected camera @raise [RuntimeError] when no devices are detected

# File lib/gphoto2/camera.rb, line 54
def self.first(&blk)
  entries = all
  raise RuntimeError, 'no devices detected' if entries.empty?
  camera = entries.first
  autorelease(camera, &blk)
end
new(model, port) click to toggle source

@param [String] model @param [String] port

Calls superclass method GPhoto2::Camera::Configuration::new
# File lib/gphoto2/camera.rb, line 108
def initialize(model, port)
  super
  @model, @port = model, port
end
open(model, port, &blk) click to toggle source

@example

model = 'Nikon DSC D5100 (PTP mode)'
port = 'usb:250,006'

camera = GPhoto2::Camera.open(model, port)

begin
  # ...
ensure
  camera.finalize
end

# Alternatively, pass a block, which will automatically close the camera.
GPhoto2::Camera.open(model, port) do |camera|
  # ...
end

@param [String] model @param [String] port @return [GPhoto2::Camera]

# File lib/gphoto2/camera.rb, line 81
def self.open(model, port, &blk)
  camera = new(model, port)
  autorelease(camera, &blk)
end
where(condition) click to toggle source

Filters devices by a given condition.

Filter keys can be either ‘model` or `port`. Only the first filter is used.

@example

# Find the cameras whose model names contain Nikon.
cameras = GPhoto2::Camera.where(model: /nikon/i)

# Select a camera by its port.
camera = GPhoto2::Camera.where(port: 'usb:250,004').first

@param [Hash] condition @return [Array<GPhoto2::Camera>]

# File lib/gphoto2/camera.rb, line 100
def self.where(condition)
  name = condition.keys.first
  pattern = condition.values.first
  all.select { |c| c.send(name).match(pattern) }
end

Private Class Methods

autorelease(camera) { |camera| ... } click to toggle source

Ensures the given camera is finalized when passed a block.

If no block is given, the camera is returned and the caller must must manually close it.

# File lib/gphoto2/camera.rb, line 163
def self.autorelease(camera)
  if block_given?
    begin
      yield camera
    ensure
      camera.finalize
    end
  else
    camera
  end
end

Public Instance Methods

abilities() click to toggle source

@return [GPhoto2::CameraAbilities]

# File lib/gphoto2/camera.rb, line 132
def abilities
  @abilities || (init && @abilities)
end
can?(operation) click to toggle source

@example

camera.can? :capture_image
# => true

@see FFI::GPhoto2::CameraOperation @param [CameraOperation] operation @return [Boolean]

# File lib/gphoto2/camera.rb, line 153
def can?(operation)
  (abilities.operations & (CameraOperation[operation] || 0)) != 0
end
close()
Alias for: finalize
context() click to toggle source

@return [GPhoto2::Context]

# File lib/gphoto2/camera.rb, line 142
def context
  @context ||= Context.new
end
exit() click to toggle source

@return [void]

# File lib/gphoto2/camera.rb, line 122
def exit
  _exit
end
finalize() click to toggle source

@return [void]

# File lib/gphoto2/camera.rb, line 114
def finalize
  @context.finalize if @context
  @window.finalize if @window
  unref if @ptr
end
Also aliased as: close
port_info() click to toggle source

@return [GPhoto2::PortInfo]

# File lib/gphoto2/camera.rb, line 137
def port_info
  @port_info || (init && @port_info)
end
ptr() click to toggle source

@return [FFI::GPhoto::Camera]

# File lib/gphoto2/camera.rb, line 127
def ptr
  @ptr || (init && @ptr)
end

Private Instance Methods

_exit() click to toggle source
# File lib/gphoto2/camera.rb, line 188
def _exit
  rc = gp_camera_exit(ptr, context.ptr)
  GPhoto2.check!(rc)
end
init() click to toggle source
# File lib/gphoto2/camera.rb, line 175
def init
  new
  set_abilities(CameraAbilities.find(@model))
  set_port_info(PortInfo.find(@port))
end
new() click to toggle source
# File lib/gphoto2/camera.rb, line 181
def new
  ptr = FFI::MemoryPointer.new(:pointer)
  rc = gp_camera_new(ptr)
  GPhoto2.check!(rc)
  @ptr = FFI::GPhoto2::Camera.new(ptr.read_pointer)
end
set_abilities(abilities) click to toggle source
# File lib/gphoto2/camera.rb, line 199
def set_abilities(abilities)
  rc = gp_camera_set_abilities(ptr, abilities.ptr)
  GPhoto2.check!(rc)
  @abilities = abilities
end
set_port_info(port_info) click to toggle source
# File lib/gphoto2/camera.rb, line 193
def set_port_info(port_info)
  rc = gp_camera_set_port_info(ptr, port_info.ptr)
  GPhoto2.check!(rc)
  @port_info = port_info
end
unref() click to toggle source
# File lib/gphoto2/camera.rb, line 205
def unref
  rc = gp_camera_unref(ptr)
  GPhoto2.check!(rc)
end