ruby binding and example from Steven Jenkins. I still need to tweak

the Makefiles a bit to integrate it properly
This commit is contained in:
Frank Mori Hess 2004-01-17 16:33:09 +00:00
parent a7ad74bf2d
commit cac5a2eac6
8 changed files with 1476 additions and 93 deletions

47
swig/ruby/README Normal file
View file

@ -0,0 +1,47 @@
This directory contains files for building a Ruby extension library
for Comedi.
Instructions for building:
1. Type 'ruby setup.rb config'. This configures for the local
environment.
2. Type 'ruby setup.rb setup'. This builds the extension library.
3. Type 'ruby setup.rb install'. This installs the extension
library and the file lib/comedi.rb, which provides more
Ruby-like method syntax.
The file 'lib/comedi.rb' provides syntactic sugar in three forms:
1. Method names without the 'comedi_' prefix. The Comedi module
disambiguates the namespace. For example, you can say
'dev = Comedi::open()' instead of 'dev = Comedi::comedi_open()'.
2. Instance methods that take an explicit receiver instead of
expecting the target object as an initial argument. For example:
'comedi_close(dev)' can be written as 'dev.close'.
3. A pre-defined IO object and an accessor method to simplify
reading from the file descriptor associated with the comedi device.
Data from comedi device 'dev' can be read by 'dev.ios.read'.
The file 'demo/cmd' is a straight port of 'cmd.c' from the
Comedilib 'demo' directory. It illustrates the basics of programming
Comedi commands using Ruby.
If a Comedilib function returns a value through a pointer passed as an
input parameter, its Ruby counterpart returns the value as an element
of an Array.
ret, cmd = dev.command_test(cmd)
Because the command object is used by command_test() it appears as an
input parameter as well. If the pointer is used only for output, it is
omitted from the parameter list. For example:
data = dev.data_read(subdevice, channel, range, aref);
Steven Jenkins
steven.jenkins@ieee.org
2004-01-12

View file

@ -10,7 +10,7 @@ cmdtest_messages = [ "success", "invalid source", "source conflict",
"invalid argument", "argument conflict",
"invalid chanlist" ]
class Comedi_t
class SWIG::TYPE_p_comedi_t
def prepare_cmd_lib(subdevice, freq, cmd)
ret, cmd = get_cmd_generic_timed(subdevice, cmd,
@ -35,7 +35,7 @@ end
parse_options($ARGV)
begin
dev = Comedi_t.new($filename)
dev = Comedi::open($filename)
rescue
comedi_perror($filename)
exit 1

0
swig/ruby/ext/MANIFEST Normal file
View file

3
swig/ruby/ext/extconf.rb Normal file
View file

@ -0,0 +1,3 @@
require 'mkmf'
have_library('comedi')
create_makefile("comedi")

118
swig/ruby/lib/comedi.rb Normal file
View file

@ -0,0 +1,118 @@
######################################################################
#
# $Source$
#
# $Revision$
# $Date$
#
# $Author$
#
# Copyright (C) 2003,2004 James Steven Jenkins
#
######################################################################
# This file is syntactic sugar for accessing the Ruby comedilib
# extension library generated by SWIG. The syntactic sugar is in
# three forms:
#
# (1) Method names without the 'comedi_' prefix. The Comedi module
# disambiguates the namespace.
#
# (2) Instance methods that take an explicit receiver instead of
# passing the target object as an initial pointer. For example:
# comedi_close(dev) can be written as dev.close.
#
# (3) A pre-defined IO object and an accessor method to simplify
# reading from the file descriptor associated with the comedi device.
# Data from comedi device dev can be accessed with dev.ios.read.
require 'comedi.so'
include Comedi
include SWIG
module SWIG
# TYPE_p_comedi_t is returned by Comedi::open
class TYPE_p_comedi_t
# create an IO object to access the comedi_t fileno
def ios
@ios = IO.new(fileno, 'r') if @ios.nil?
@ios
end
end
end
module Comedi
private
# wrap_module_method defines Comedi module methods without the
# unnecessary comedi_ prefix.
def wrap_module_method(mod, name)
mod.module_eval <<-EOF
def #{name}(*args)
comedi_#{name}(*args)
end
EOF
end
# wrap_instance_method defines instance methods for any of several
# classes. It removes the comedi_ prefix and allows use of an
# explicit receiver.
def wrap_instance_method(mod, name)
mod.module_eval <<-EOF
def #{name}(*args)
comedi_#{name}(self, *args)
end
EOF
end
# Comedi module methods
%w{ open loglevel perror strerrno errno to_phys from_phys
set_global_oor_behavior parse_calibration_file
}.each do |name|
wrap_module_method(Comedi, name)
end
# Comedi_t_struct instance methods
%w{ close fileno get_n_subdevices get_version_code
get_driver_name get_board_name get_subdevice_type
find_subdevice_by_type get_read_subdevice get_write_device
get_subdevice_flags get_n_channels range_is_chan_specific
maxdata_is_chan_specific get_maxdata get_n_ranges
get_range find_range get_buffer size
get_max_buffer_size set_buffer_size trigger
do_insnlist do_insn lock unlock data_read
data_read_delayed data_read_hint data_write dio_config
dio_read dio_write dio_bitfield get_cmd_src_mask
get_cmd_generic_timed cancel command command_test poll
set_max_buffer_size get_buffer_contents
mark_buffer_read get_buffer_offset apply_calibration
apply_parsed_calibration get_default_calibration_path
}.each do |name|
wrap_instance_method(SWIG::TYPE_p_comedi_t, name)
end
# Comedi_sv_t instance methods
%w{ sv_init sv_update sv_measure
}.each do |name|
wrap_instance_method(Comedi_sv_t, name)
end
# Comedi_calibration_t instance methods
%w{ cleanup_calibration_file
}.each do |name|
wrap_instance_method(Comedi_calibration_t, name)
end
end

1306
swig/ruby/setup.rb Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,91 +0,0 @@
require 'comedi.so'
include Comedi
include SWIG
module Comedi
class Comedi_t
def initialize(name)
@pointer = Comedi::open(name)
raise if @pointer.nil?
@ios = IO.new(fileno, "r")
raise if @ios.nil?
end
attr_reader :pointer, :ios
end
private
def wrap_function(mod, name)
defn = %Q{ def #{name}(*args)
comedi_#{name}(*args)
end
}
mod.module_eval defn
end
def wrap_function_with_receiver(mod, name)
defn = %Q{ def #{name}(*args)
comedi_#{name}(self, *args)
end
}
mod.module_eval defn
end
def wrap_function_with_pointer(mod, name)
defn = %Q{ def #{name}(*args)
comedi_#{name}(self.pointer, *args)
end
}
mod.module_eval defn
end
# Comedi module methods
names = %w{ open loglevel perror strerrno errno to_phys from_phys
set_global_oor_behavior parse_calibration_file }
names.each do |name|
wrap_function(Comedi, name)
end
# Comedi_t instance methods
names = %w{ close fileno get_n_subdevices get_version_code
get_driver_name get_board_name get_subdevice_type
find_subdevice_by_type get_read_subdevice get_write_device
get_subdevice_flags get_n_channels range_is_chan_specific
maxdata_is_chan_specific get_maxdata get_n_ranges
get_range find_range get_buffer size
get_max_buffer_size set_buffer_size trigger
do_insnlist do_insn lock unlock data_read
data_read_delayed data_read_hint data_write dio_config
dio_read dio_write dio_bitfield get_cmd_src_mask
get_cmd_generic_timed cancel command command_test poll
set_max_buffer_size get_buffer_contents
mark_buffer_read get_buffer_offset apply_calibration
apply_parsed_calibration get_default_calibration_path }
names.each do |name|
wrap_function_with_pointer(Comedi_t, name)
end
# Comedi_sv_t instance methods
names = %w{ sv_init sv_update sv_measure }
names.each do |name|
wrap_function_with_receiver(Comedi_sv_t, name)
end
# Comedi_calibration_t instance methods
names = %w{ cleanup_calibration_file }
names.each do |name|
wrap_function_with_receiver(Comedi_calibration_t, name)
end
end