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) ⇒ I2CDev

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

path
String

Path to /dev/i2c-* file.

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



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/i2c/driver/i2c-dev.rb', line 23

def initialize(path=nil)
	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
end

Instance Method Details

#i2cget(address, param, length) ⇒ Object

Interface of I2CDevice::Driver



36
37
38
39
40
41
42
43
44
45
# File 'lib/i2c/driver/i2c-dev.rb', line 36

def i2cget(address, param, length)
	i2c = File.open(@path, "r+")
	i2c.ioctl(I2C_SLAVE, address)
	i2c.syswrite(param.chr)
	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



48
49
50
51
52
53
54
55
# File 'lib/i2c/driver/i2c-dev.rb', line 48

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