class I2CDevice

Generic abstract class for I2C manipulation.

Constants

VERSION

Attributes

address[RW]

Slave address

Public Class Methods

new(args={}) click to toggle source
args[:address]
Integer

7-bit slave address without r/w bit. MSB is always 0.

args[:driver]
I2CDevice::Driver::I2CDev

Instance of driver class

# File lib/templates/grove_pi/i2c/i2c.rb, line 15
def initialize(args={})
        if args[:driver].nil?
                require "i2c/driver/i2c-dev"
                args[:driver] = I2CDevice::Driver::I2CDev.new
        end

        @driver  = args[:driver]
        @address = args[:address] or raise I2CException, "args[:address] required"
end

Public Instance Methods

i2cget(param, length=1) click to toggle source

This method read data from slave with following process:

  1. Write `param` to slave

  2. re-start

  3. Read data until NACK or `length`

param
Integer

First writing byte. Typically, this is slave memory address.

length=1
Integer

Read bytes length

Returns
String

Bytes

# File lib/templates/grove_pi/i2c/i2c.rb, line 34
def i2cget(param, length=1)
        @driver.i2cget(@address, param, length)
end
i2cset(*data) click to toggle source

Write data to slave.

data
Array

Writing bytes array.

Returns
String

Wrote bytes

# File lib/templates/grove_pi/i2c/i2c.rb, line 41
def i2cset(*data)
        @driver.i2cset(@address, *data)
end