Skip to content

Commit

Permalink
Refactor API
Browse files Browse the repository at this point in the history
  • Loading branch information
akzhan committed Apr 11, 2012
1 parent 2339190 commit 69b77cb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 54 deletions.
73 changes: 22 additions & 51 deletions lib/ftdi.rb
Expand Up @@ -88,76 +88,47 @@ class Context < FFI::Struct
:module_detach_mode, Ftdi::ModuleDetachMode
)

def initialize
ptr = Ftdi.ftdi_new
raise CannotInitializeContextError.new if ptr.nil?
super(ptr)
end

def error_string
self[:error_str]
end

def check_result(status_code)
if status_code.nonzero?
raise StatusCodeError.new(status_code, error_string)
end
nil
end

# Opens the first device with a given vendor and product ids.
def usb_open(vendor, product)
Ftdi.usb_open(self, vendor, product)
check_result(Ftdi.ftdi_usb_open(self.to_ptr, vendor, product))
end

# Closes the ftdi device.
def usb_close
Ftdi.usb_close(self)
end

# Allocate and initialize a new ftdi context
def self.open
Ftdi.init
Ftdi.ftdi_usb_close(self.to_ptr)
nil
end

# Deinitialize and free an ftdi context.
def close
Ftdi.deinit(self)
def dispose
Ftdi.ftdi_free(self.to_ptr)
nil
end

alias :close :dispose
end

attach_function :ftdi_new, [ ], :pointer
attach_function :ftdi_free, [ :pointer ], :void
attach_function :ftdi_usb_open, [ :pointer, :int, :int ], :int
attach_function :ftdi_usb_close, [ :pointer ], :void

class << self
# Allocate and initialize a new ftdi_context
def init
r = ftdi_new
raise CannotInitializeContextError.new if r.nil?

Context.new(r)
end

def check_context(context)
raise ArgumentError.new("context is nil") if context.nil?
end

def raise_error(status_code, context)
raise StatusCodeError.new(status_code, context.error_string)
end

# Deinitialize and free an ftdi context.
def deinit(context)
check_context(context)

ftdi_free(context.to_ptr)
end

# Opens the first device with a given vendor and product ids.
def usb_open(context, vendor, product)
check_context(context)
r = ftdi_usb_open(context.to_ptr, vendor, product)
unless r.zero?
raise_error(r, context)
end
end

# Closes the ftdi device.
def usb_close(context)
check_context(context)
r = ftdi_usb_close(context.to_ptr)
unless r.zero?
raise_error(r, context)
end
end
end
end

8 changes: 5 additions & 3 deletions test.rb
Expand Up @@ -5,15 +5,17 @@

require 'ftdi'

context = Ftdi::Context.open
context = Ftdi::Context.new

puts "Context is:"
context.members.each { |k| puts "#{k} = #{context[k]}" }

begin
context.usb_open(0x0403, 0x6001)
begin

puts "Context is:"
context.members.each { |k| puts "#{k} = #{context[k]}" }


ensure
context.usb_close
end
Expand Down

0 comments on commit 69b77cb

Please sign in to comment.