diff --git a/README.rdoc b/README.rdoc index 2d53849..46545e4 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,6 +1,6 @@ = SANE FFI -Scanner Access Now Easier in Ruby. +Scanner Access Now Easier in Ruby using FFI. This gem provides bindings == Copyright diff --git a/lib/sane.rb b/lib/sane.rb index 9bb1fe1..f2db7dc 100644 --- a/lib/sane.rb +++ b/lib/sane.rb @@ -155,6 +155,14 @@ class Sane API::Parameters.new(parameters_pointer).to_hash end + def set_io_mode(handle, non_blocking) + check_status!(API.sane_set_io_mode(handle, non_blocking ? 1 : 0)) + end + + def cancel(handle) + API.sane_cancel(handle) + end + def ensure_not_initialized! raise("SANE library is already initialized") if initialized? end diff --git a/lib/sane/device.rb b/lib/sane/device.rb index 8e24acd..3ded72b 100644 --- a/lib/sane/device.rb +++ b/lib/sane/device.rb @@ -1,5 +1,7 @@ class Sane class Device + attr_reader :name, :vendor, :model, :type + def initialize(options) @name = options[:name] @vendor = options[:vendor] @@ -44,6 +46,21 @@ class Sane Sane.instance.send(:read, @handle) end + def cancel + ensure_open! + Sane.instance.send(:cancel, @handle) + end + + def sync + ensure_open! + Sane.instance.send(:set_io_mode, @handle, false) + end + + def async + ensure_open! + Sane.instance.send(:set_io_mode, @handle, true) + end + def option_count ensure_open! Sane.instance.send(:get_option, @handle, 0) @@ -76,16 +93,18 @@ class Sane option_count.times.map do |i| begin self[i] - rescue Error - nil # we can't read values of some options (i.e. buttons), ignore them + rescue Error => e + if e.status == :inval + nil # we can't read values of some options (i.e. buttons), ignore them + else + raise e + end end end end def options - result = {} - option_count.times { |i| hash[option_names[i]] = option_values[i] } - result + Hash[*option_names.zip(option_values).flatten] end def option_lookup(option_name) diff --git a/lib/sane/version.rb b/lib/sane/version.rb index 478b082..d3dfee4 100644 --- a/lib/sane/version.rb +++ b/lib/sane/version.rb @@ -1,3 +1,3 @@ -module Sane +class Sane VERSION = "0.1.0" end