Class: I2CDevice::Driver::I2CDev

Inherits:
Base
  • Object
show all
Defined in:
lib/templates/grove_pi/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



26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#i2cget(address, param, length) ⇒ Object

Interface of I2CDevice::Driver



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object

Interface of I2CDevice::Driver



56
57
58
59
60
61
62
63
64
65
66
67
# 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