class I2CDevice::Driver::I2CDev

Constants

I2C_FUNCS
I2C_MDELAY
I2C_RDWR
I2C_RETRIES

ioctl command Ref. www.kernel.org/pub/linux/kernel/people/marcelo/linux-2.4/include/linux/i2c.h

I2C_SLAVE
I2C_SLAVE_FORCE
I2C_SMBUS
I2C_TENBIT
I2C_TIMEOUT
I2C_UDELAY

Public Class Methods

new(path=nil, force=false) click to toggle source

This depends on /dev/i2c-* (i2c-dev) feature on Linux. You may load i2c-dev kernel module.

path
String

Path to /dev/i2c-* file.

force
Boolean

Force the driver to read or set values even if the device is in use.

This is dangerous, as it can seriously confuse the kernel driver in question. It can also cause i2cget and i2cset to writ to the wrong register. Use at your own risk and only if you know what you're doing.

If path is not specified, this method use Dir.glob("/dev/i2c-*").last for path

# File lib/templates/grove_pi/i2c/driver/i2c-dev.rb, line 26
    def initialize(path=nil, force=false)
            if path.nil?
                    path = Dir.glob("/dev/i2c-*").sort.last
            end

            unless File.exist?(path)
                    raise I2CDevice::I2CIOError, "/dev/i2c-0 is required"
            end

            @path = path
@slave_command = force ? I2C_SLAVE_FORCE : I2C_SLAVE
    end

Public Instance Methods

i2cget(address, param, length) click to toggle source

Interface of I2CDevice::Driver

# File lib/templates/grove_pi/i2c/driver/i2c-dev.rb, line 40
def i2cget(address, param, length)
        i2c = File.open(@path, "r+")
        begin
        i2c.ioctl(@slave_command, address)
        i2c.syswrite(param.chr) unless param.nil?
        ret = i2c.sysread(length)
        i2c.close
  ret
rescue => e
  puts e.message
end
rescue Errno::EIO => e
        raise I2CDevice::I2CIOError, e.message
end
i2cset(address, *data) click to toggle source

Interface of I2CDevice::Driver

# File lib/templates/grove_pi/i2c/driver/i2c-dev.rb, line 56
def i2cset(address, *data)
        i2c = File.open(@path, "r+")
        begin
        i2c.ioctl(@slave_command, address)
        i2c.syswrite(data.pack("C*"))
        i2c.close
rescue => e
  puts e.message
end
rescue Errno::EIO => e
        raise I2CDevice::I2CIOError, e.message
end