andec / i2c Goto Github PK
View Code? Open in Web Editor NEWRuby I2C library
Ruby I2C library
Sorry, there is no message facility in github and you didn't leave a mail. So just close this when read.
I have created a site to share experience with ruby on the pi. And this library makes you an "expert"
I was wondering if you would like to write a little article on ruby and serial communication.
BTW: how is that motor coming along ?
Torsten
Hello,
I hava a small problem. When i use your gem (thanks for it), i get theses warnings.
/home/sebastien/.rvm/gems/ruby-1.9.3-p392/gems/i2c-0.2.0/lib/i2c/drivers/mcp23017.rb:16: warning: already initialized constant INPUT
/home/sebastien/.rvm/gems/ruby-1.9.3-p392/gems/i2c-0.2.0/lib/i2c/drivers/mcp23017.rb:17: warning: already initialized constant OUTPUT
/home/sebastien/.rvm/gems/ruby-1.9.3-p392/gems/i2c-0.2.0/lib/i2c/drivers/mcp23017.rb:20: warning: already initialized constant HIGH
/home/sebastien/.rvm/gems/ruby-1.9.3-p392/gems/i2c-0.2.0/lib/i2c/drivers/mcp23017.rb:21: warning: already initialized constant LOW
I think it's because there constant are initialized twice (in both drivers) but i'm not sure.
Good afternoon
What I'm trying:
require 'i2c'
i2c = I2C.create('/dev/i2c-1')
mcp = I2C::Drivers::MCP23017.new(i2c,0x20)
and I get:
TypeError: no implicit conversion of Array into String
at file lib/i2c/backends/i2c-dev.rb:31:in block in write
with Ruby 2.1.10.
I found on that file that there is a data << value
assignation, when data is defined as string in line # 28 :/
I just checked, and the current version of the i2c gem found on rubygems.org is a little outdated, as it is from 2015.
I recommend testing the gem, updating the gemspec's version and then publishing a fresh version ^^
Hi,
this is nice and small, and works, great. Also google and rubygems lead here.
So i thought it may be nice to have a little more explanation in the readme, eg:
I could make a pull request if you want, if the topics are ok
We have a MCP23017 attached to a Raspberry Pi compute module.
The device is setup to use address 0x23. We have the reset pin also wired to the Pi.
As a reference we use the two methods to read input
values of the device. Python and direct shell
We read after the device is reset. As per datasheet it is therefore initialised to input
i2cget -y 1 0x23 0x12
i2cget -y 1 0x23 0x13
and we get the values as expected.
Also with python:
#!/usr/bin/python
import smbus
import time
import sys
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
DEVICE = 0x23 # Device address (A0-A2)
GPIOA = 0x12 # Register for inputs
GPIOB = 0x13 # Register for inputs
stateBankA = bus.read_byte_data(DEVICE,GPIOA)
stateBankB = bus.read_byte_data(DEVICE,GPIOB)
print "state of the GPIO " + bin(stateBankA) + bin(stateBankB)
When a signal is attached to pin 16 we read:
0x00
0x80
same with the python script:
state of the GPIO 0b00b10000000
And it reacts accordingly when I provide a signal to another pin.
We wrote a little ruby script to also read the input values. Our business logic is also written in Ruby so this is the prototype of the final implementation:
#!/usr/bin/env ruby
require 'i2c'
mcp = I2C::Drivers::MCP23017.new("/dev/i2c-1", 0x23)
#(0..15).each do |pin|
# mcp.mode(pin, INPUT)
#end
(0..15).each do |pin|
print "#{mcp[pin]}"
end
print "/n"
First issue: The default state when reading via ruby is not 0000000000000000
but it is 0000000011111111
. There is no input signal. When providing an input to pin 16 the output is 0000000011111110
.
The issue is present both when setting the mode or not.
Why is the second bank inverted?
Second issue: After running the ruby script once, also the output in python and shell is "corrupted".
0x00
0x7f
is now the output of the shell script
state of the GPIO 0b00b1111111
is the output of the Python script.
state of the GPIO 0b00b11111111
is the output with input signal.
I looked in the initialisation and saw there is some writing when doing the initialisation:
https://github.com/andec/i2c/blob/master/lib/i2c/drivers/mcp230xx.rb#L54
I'm trying to make some sense of the reading in the irb
. I don't understand how to do a simple i2cget -y 1 0x23 0x12
would translate to the ruby code:
irb(main):025:0> device.read(0x23,2,0x12).unpack('CC')
=> [0, 128]
irb(main):026:0> device.read(0x23,1,0x12).unpack('CC')
=> [0, nil]
output of i2cget
remains to be
0x00
0x80
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.