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

Returns a new instance of I2CDev.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/i2c/driver/i2c-dev.rb', line 20

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



32
33
34
35
36
37
38
39
40
41
# File 'lib/i2c/driver/i2c-dev.rb', line 32

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



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

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