Class: I2CDevice::Driver::I2CDev

Inherits:
Base
  • Object
show all
Defined in:
lib/i2c/driver/i2c-dev.rb

Constant Summary collapse

I2C_RETRIES =
0x0701
I2C_TIMEOUT =
0x0702
I2C_SLAVE =
0x0703
I2C_SLAVE_FORCE =
0x0706
I2C_TENBIT =
0x0704
I2C_FUNCS =
0x0705
I2C_RDWR =
0x0707
I2C_SMBUS =
0x0720
I2C_UDELAY =
0x0705
I2C_MDELAY =
0x0706

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, force = false) ⇒ I2CDev

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


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/i2c/driver/i2c-dev.rb', line 27

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

Instance Method Details

#i2cget(address, param, length) ⇒ Object

Interface of I2CDevice::Driver


41
42
43
44
45
46
47
48
49
50
# File 'lib/i2c/driver/i2c-dev.rb', line 41

def i2cget(address, param, length)
	i2c = File.open(@path, "r+")
	i2c.ioctl(@slave_command, address)
	i2c.syswrite(param.chr) unless param.nil?
	ret = i2c.sysread(length)
	i2c.close
	ret
rescue Errno::EIO => e
	raise I2CDevice::I2CIOError, e.message
end

#i2cset(address, *data) ⇒ Object

Interface of I2CDevice::Driver


53
54
55
56
57
58
59
60
# File 'lib/i2c/driver/i2c-dev.rb', line 53

def i2cset(address, *data)
	i2c = File.open(@path, "r+")
	i2c.ioctl(@slave_command, address)
	i2c.syswrite(data.pack("C*"))
	i2c.close
rescue Errno::EIO => e
	raise I2CDevice::I2CIOError, e.message
end