hello,
i try to access the I2C bus with ruby on rails. I search the net for some information. The wiringpi gem seems to be a way, but from my point of view it is used to access the GPIO I/O's and not to access the I2C bus. Has someone tryed something like this?
regards
michael
-
- Posts: 4
- Joined: Mon Oct 15, 2012 8:30 am
Re: Use I2C with Ruby
There is a proper i2c device. I'd use that as it's likely to be much simpler than rolling your own with wiringPi.
If you use the device I2C is accessed like any other file.
Have a look at http://www.raspberrypi.org/phpBB3/viewt ... 19#p191519
If you use the device I2C is accessed like any other file.
Have a look at http://www.raspberrypi.org/phpBB3/viewt ... 19#p191519
-
- Posts: 4
- Joined: Mon Oct 15, 2012 8:30 am
Re: Use I2C with Ruby
Hello Joan,
i read all this exaples befor. But there be only python and C exaples. I search for a way to access the I2C with ruby on rails.
regards
Michael
i read all this exaples befor. But there be only python and C exaples. I search for a way to access the I2C with ruby on rails.
regards
Michael
Re: Use I2C with Ruby
The only way is to use the c example via ruby ffi. That needs some c knowlege. I want to acess i2c via ruby too. But i didn't have to right it atm.
Re: Use I2C with Ruby
I'm also trying to learn how to use I2C with Ruby. In my case, I'm trying to use a LSM303 Tilt Compensated Compass from sparkfun. https://www.sparkfun.com/products/10703
Steps I've done so far:
@lsm303 = ::I2C.create("/dev/i2c-0")
@lsm303.read(0x1E, 0x02)
@lsm303.read(0x1E, 0x03)
I've also found the following:
https://github.com/meinside/raspi-adafr ... 8matrix.rb
Steps I've done so far:
- Prepare r-pi for I2C - http://www.skpang.co.uk/blog/archives/575
- Install ruby
- Install I2C gem - http://rubygems.org/gems/i2c
- connect LSM303 to r-pi
- Test connection with i2cdetect
- Experiment in irb
@lsm303 = ::I2C.create("/dev/i2c-0")
@lsm303.read(0x1E, 0x02)
@lsm303.read(0x1E, 0x03)
I've also found the following:
https://github.com/meinside/raspi-adafr ... 8matrix.rb
Re: Use I2C with Ruby
I've also tried a different method from the command line.
here's what I think is the equivalent in ruby:
But my results don't quite match
Code: Select all
pi@raspberrypi:~$ i2cset -y 0 0x1e 0x00 0x14
pi@raspberrypi:~$ i2cset -y 0 0x1e 0x02 0x00
i@raspberrypi:~$ i2cget -y 0 0x1e 0x02
0x00
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x03
0xff
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x04
0x44
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x05
0x01
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x06
0x4f
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x07
0xfe
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x08
0x93
Code: Select all
require "i2c/i2c"
require "i2c/backends/i2c-dev"
@lsm303 = ::I2C.create("/dev/i2c-0")
@lsm303.write(0x1E, 0x00, 0x14)
@lsm303.write(0x1E, 0x02, 0x00)
puts @lsm303.read(0x1E, 0x02)
puts @lsm303.read(0x1E, 0x03)
puts @lsm303.read(0x1E, 0x04)
puts @lsm303.read(0x1E, 0x05)
puts @lsm303.read(0x1E, 0x06)
puts @lsm303.read(0x1E, 0x07)
puts @lsm303.read(0x1E, 0x08)
Code: Select all
?A
R?
??A
R??
?AR??
?AR??
?AR??
Re: Use I2C with Ruby
I think I'm getting closer. I need to read up on the unpack method and the << operator
Code: Select all
require "i2c/i2c"
require "i2c/backends/i2c-dev"
@lsm303 = ::I2C.create("/dev/i2c-0")
@lsm303.write(0x1E, 0x00, 0x14)
@lsm303.write(0x1E, 0x02, 0x00)
@lsm303.read(0x1E, 0x02)
mag_data = Array.new
(3..8).each do |i|
mag_data.push @lsm303.read(0x1E, 0x02)
end
Mx = (mag_data[0] << 8) + mag_data[1]
My = (mag_data[2] << 8) + mag_data[3]
Mz = (mag_data[4] << 8) + mag_data[5]
puts Mx.unpack("C")[0]
puts My.unpack("C")[0]
puts Mz.unpack("C")[0]