working draft
This commit is contained in:
parent
4a6b2d5739
commit
cf3c2b8c6e
3 changed files with 162 additions and 27 deletions
|
@ -12,6 +12,8 @@ class Sane
|
|||
|
||||
class Device < FFI::Struct
|
||||
layout :name, :string, :vendor, :string, :model, :string, :type, :string
|
||||
|
||||
def to_hash; {name: self[:name], vendor: self[:vendor], model: self[:model], type: self[:type]} end
|
||||
end
|
||||
|
||||
class OptionDescriptor < FFI::Struct
|
||||
|
@ -19,10 +21,14 @@ class Sane
|
|||
layout :string_list, :pointer, :word_list, :pointer, :range, :pointer
|
||||
end
|
||||
layout :name, :string, :title, :string, :desc, :string, :type, :value_type, :unit, :unit, :size, :int, :cap, :int, :constraint_type, ConstraintType
|
||||
|
||||
def to_hash; {name: self[:name], title: self[:title], desc: self[:desc], type: self[:type], unit: self[:unit], size: self[:size], cap: self[:cap]} end
|
||||
end
|
||||
|
||||
class Parameters < FFI::Struct
|
||||
layout :format, :frame, :last_frame, :int, :bytes_per_line, :int, :pixels_per_line, :int, :lines, :int, :depth, :int
|
||||
|
||||
def to_hash; {format: self[:format], last_frame: self[:last_frame], bytes_per_line: self[:bytes_per_line], pixels_per_line: self[:pixels_per_line], lines: self[:lines], depth: self[:depth]} end
|
||||
end
|
||||
|
||||
# extern SANE_Status sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize);
|
||||
|
|
|
@ -1,29 +1,108 @@
|
|||
class Sane
|
||||
class Device
|
||||
def initialize(name)
|
||||
@name = name
|
||||
def initialize(options)
|
||||
@name = options[:name]
|
||||
@vendor = options[:vendor]
|
||||
@model = options[:model]
|
||||
@type = options[:type]
|
||||
@handle = nil
|
||||
end
|
||||
|
||||
def open
|
||||
@handle = Sane.open(@name) if closed?
|
||||
if block_given?
|
||||
yield self
|
||||
close
|
||||
end
|
||||
end
|
||||
|
||||
def closed?
|
||||
@handle.nil?
|
||||
end
|
||||
|
||||
def opened?
|
||||
def open?
|
||||
!closed?
|
||||
end
|
||||
|
||||
def open
|
||||
ensure_closed!
|
||||
@handle = Sane.instance.send(:open, @name)
|
||||
if block_given?
|
||||
begin
|
||||
yield(self)
|
||||
ensure
|
||||
close
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def close
|
||||
Sane.close(@handle) if opened?
|
||||
ensure_open!
|
||||
Sane.instance.send(:close, @handle)
|
||||
@handle = nil
|
||||
end
|
||||
|
||||
def start
|
||||
ensure_open!
|
||||
Sane.instance.start(@handle)
|
||||
end
|
||||
|
||||
def read
|
||||
ensure_open!
|
||||
Sane.instance.send(:read, @handle)
|
||||
end
|
||||
|
||||
def option_count
|
||||
ensure_open!
|
||||
Sane.instance.send(:get_option, @handle, 0)
|
||||
end
|
||||
|
||||
def parameters
|
||||
ensure_open!
|
||||
Sane.instance.send(:get_parameters, @handle)
|
||||
end
|
||||
|
||||
def [](option)
|
||||
ensure_open!
|
||||
Sane.instance.send(:get_option, @handle, option_lookup(option))
|
||||
end
|
||||
|
||||
def []=(option, value)
|
||||
ensure_open!
|
||||
Sane.instance.send(:set_option, @handle, option_lookup(option), value)
|
||||
end
|
||||
|
||||
def option_descriptors
|
||||
option_count.times.map { |i| Sane.instance.send(:get_option_descriptor, @handle, i) }
|
||||
end
|
||||
|
||||
def option_names
|
||||
option_descriptors.map { |option| option[:name] }
|
||||
end
|
||||
|
||||
def option_values
|
||||
option_count.times.map do |i|
|
||||
begin
|
||||
self[i]
|
||||
rescue Error
|
||||
nil # we can't get values of some options, ignore them
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def options
|
||||
{}.tap { |hash| option_count.times { |i| hash[option_names[i]] = option_values[i] } }
|
||||
end
|
||||
|
||||
def option_lookup(option_name)
|
||||
return option_name if (0..option_count).include?(option_name)
|
||||
option_descriptors.index { |option| option[:name] == option_name.to_s } or raise ArgumentError, "Option not found: #{option_name}"
|
||||
end
|
||||
|
||||
def describe(option)
|
||||
option_descriptors[option_lookup(option)]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_closed!
|
||||
raise "Device is already open" if open?
|
||||
end
|
||||
|
||||
def ensure_open!
|
||||
raise "Device is closed" if closed?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue