class I2C::Dev

Constants

I2C_SLAVE

see i2c-dev.h

Attributes

comsMutex[R]

Public Class Methods

create(device_path) click to toggle source
# File lib/i2c/backends/i2c-dev.rb, line 13
def self.create(device_path)
        raise Errno::ENOENT, "Device #{device_path} not found." unless File.exists?(device_path)
        @instances ||= Hash.new
        @instances[device_path] = Dev.new(device_path) unless @instances.has_key?(device_path)
        @instances[device_path]
end
new(device_path) click to toggle source
# File lib/i2c/backends/i2c-dev.rb, line 97
def initialize(device_path)
        @comsMutex = Mutex.new();

        @device = File.new(device_path, 'r+')
        # change the sys* functions of the file object to meet our requirements
        class << @device
                alias :syswrite_orig :syswrite
                def syswrite(var)
                        begin
                                syswrite_orig var
                        rescue Errno::EREMOTEIO, Errno::EIO
                                raise AckError, "No acknowledge received"
                        end
                end
                alias :sysread_orig :sysread
                def sysread(var)
                        begin
                                sysread_orig var
                        rescue Errno::EREMOTEIO, Errno::EIO
                                raise AckError, "No acknowledge received"
                        end
                end
        end # virtual class
end

Public Instance Methods

read(address, size, *params) click to toggle source

this tries to lock the coms mutex (unless already held), then sends *params, if given, and then tries to read size bytes. The result is a String which can be treated with String#unpack afterwards

# File lib/i2c/backends/i2c-dev.rb, line 48
def read(address, size, *params)
        if(@comsMutex.owned?)
                keepLock = true;
        else
                @comsMutex.lock;
        end

        begin
                setup_device(address);
                raw_write(params) unless params.empty?
                result = raw_read(size);
        ensure
                @comsMutex.unlock() unless keepLock;
                return result;
        end
end
read_byte(address) click to toggle source

Read a byte from the current address. Return a one char String which can be treated with String#unpack

# File lib/i2c/backends/i2c-dev.rb, line 67
def read_byte(address)
        read(address, 1);
end
write(address, *params) click to toggle source

this tries to lock the coms mutex, unless already held, then sends every param, begining with params[0] If the current param is a Fixnum, it is treated as one byte. If the param is a String, this string will be sent byte by byte. You can use Array#pack to create a string from an array For Fixnum there is a convenient function to_short which transforms the number to a string this way: 12345.to_short == [12345].pack(“s”)

# File lib/i2c/backends/i2c-dev.rb, line 29
def write(address, *params)
        if(@comsMutex.owned?)
                keepLock = true;
        else
                @comsMutex.lock;
        end

        begin
                setup_device(address);
                raw_write(params);
        ensure
                @comsMutex.unlock() unless keepLock;
        end
end

Private Instance Methods

raw_read(size) click to toggle source

Read size bytes from @device, if possible. Raise an error otherwise

# File lib/i2c/backends/i2c-dev.rb, line 78
def raw_read(size)
        return @device.sysread(size)
end
raw_write(params) click to toggle source

Write “params” to @device, unrolling them first should they be an array. params should be a string, formatted with Array.pack as explained for write()

# File lib/i2c/backends/i2c-dev.rb, line 84
def raw_write(params)
        data = String.new();
        data.force_encoding("US-ASCII")

        if(params.is_a? Array)
                params.each do |i| data << i; end
        else
                data << params;
        end

        @device.syswrite(data);
end
setup_device(address) click to toggle source

Set up @device for a I2C communication to address

# File lib/i2c/backends/i2c-dev.rb, line 73
def setup_device(address)
        @device.ioctl(I2C_SLAVE, address);
end