class Selfie::Camera

Constants

CAPTURE_COMMAND

Public Class Methods

new(outdir, options = {}) click to toggle source
# File lib/selfie/camera.rb, line 5
def initialize(outdir, options = {})
  @outdir = outdir
  @glob = File.join(@outdir, "snapshot-*.jpg")
  @captured = []
  @pid = nil
end

Public Instance Methods

capture() click to toggle source

Some hacks to make it seem like capture() takes a picture without accumulating a ton of unused images.

# File lib/selfie/camera.rb, line 49
def capture
  sleep 0.2

  images = Dir[@glob]
  return if images.none? || images.last == @captured.last

  @captured << images.last
  FileUtils.rm_f(images - @captured)

  @captured.last
end
off() click to toggle source
# File lib/selfie/camera.rb, line 30
def off
  return false unless @pid

  begin
    Process.kill("TERM", @pid)
    Process.wait(@pid)
  rescue Errno::ESRCH, Errno::ECHILD
    # kill, wait cannot find pid
  end

  # final cleanup
  FileUtils.rm_f(Dir[@glob] - @captured)

  @pid = nil
  true
end
on() click to toggle source

The camera interface we’d like to have (and may at some point). Until then, we fake it.

# File lib/selfie/camera.rb, line 14
def on
  return false if @pid

  # TODO: signals
  begin
    @pid = spawn(CAPTURE_COMMAND, :chdir => @outdir, [:out, :err] => File::NULL)
  rescue SystemCallError => e
    raise Error, "cannot spawn photo capture process: #{e}"
  end

  # Wait for camera to warm up, -w option is not enough :(
  sleep 2.5

  true
end