Last active
June 4, 2016 07:43
-
-
Save yasulab/165fc86f5a9e42901404889eb26e69e9 to your computer and use it in GitHub Desktop.
Get temperature from ADT7410
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Sample Output | |
# pi@raspberrypi:~ $ ruby adt7410.rb | |
# Path: /dev/i2c-1 | |
# Temp: 29.26 | |
require 'i2c' | |
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 | |
def get_path(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 | |
end | |
# Interface of I2CDevice::Driver | |
def i2cget(address, param, length) | |
i2c = File.open(@path, "r+") | |
i2c.ioctl(I2C_SLAVE, 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 | |
# Interface of I2CDevice::Driverdef i2cset(address, *data) | |
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 | |
@path = get_path | |
puts "Path: " + @path | |
@device = I2C.create @path | |
@address = 0x48 | |
data = i2cget(@address, 0x00, 2).unpack("C*") | |
temp = data[0] << 8 | data[1] | |
puts "Temp: %6.2f" % (temp / 128.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment