Class: I2C::Dev
- Inherits:
-
Object
- Object
- I2C::Dev
- Defined in:
- lib/i2c/backends/i2c-dev.rb
Constant Summary collapse
- I2C_SLAVE =
see i2c-dev.h
0x0703
Class Method Summary collapse
Instance Method Summary collapse
-
#read(address, size, *params) ⇒ Object
this sends *params as the write function and then tries to read
size
bytes. -
#read_byte(address) ⇒ Object
Read a byte from the current address.
-
#write(address, *params) ⇒ Object
sends every param, begining with
params[0]
If the current param is a Fixnum, it is treated as one byte.
Class Method Details
.create(device_path) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/i2c/backends/i2c-dev.rb', line 14 def self.create(device_path) raise Errno::ENOENT, "Device #{device_path} not found." unless File.exists?(device_path) @instances ||= Hash.new @instances[device_path] = Dev.new(device_path) unless @instances.has_key?(device_path) @instances[device_path] end |
Instance Method Details
#read(address, size, *params) ⇒ Object
this sends *params as the write function and then tries to read size
bytes. The result is a String which can be treated with String#unpack afterwards
40 41 42 43 44 45 |
# File 'lib/i2c/backends/i2c-dev.rb', line 40 def read(address, size, *params) ret = "" write(address, *params) ret = @device.sysread(size) return ret end |
#read_byte(address) ⇒ Object
Read a byte from the current address. Return a one char String which can be treated with String#unpack
49 50 51 52 53 54 |
# File 'lib/i2c/backends/i2c-dev.rb', line 49 def read_byte(address) ret="" @device.ioctl(I2C_SLAVE,address) ret=@device.sysread(1) return ret end |
#write(address, *params) ⇒ Object
sends every param, begining with params[0]
If the current param is a Fixnum, it is treated as one byte. If the param is a String, this string will be send byte by byte. You can use Array#pack to create a string from an array For Fixnum there is a convinient function to_short which transforms the number to a string this way: 12345.to_short == [12345].pack(āsā)
27 28 29 30 31 32 33 34 35 |
# File 'lib/i2c/backends/i2c-dev.rb', line 27 def write(address, *params) data = String.new data.force_encoding("US-ASCII") params.each do |value| data << value end @device.ioctl(I2C_SLAVE, address) @device.syswrite(data) end |