Class: Beaglebone::I2CDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/beaglebone/i2c.rb

Overview

Object Oriented I2C Implementation. This treats the I2C device as an object.

Instance Method Summary collapse

Constructor Details

#initialize(i2c) ⇒ I2CDevice

Initialize an I2C device. Returns an I2CDevice object

Examples:

i2c = I2CDevice.new(:I2C2)

Parameters:

  • i2c

    should be a symbol representing the I2C device



216
217
218
219
# File 'lib/beaglebone/i2c.rb', line 216

def initialize(i2c)
  @i2c = i2c
  I2C::setup(@i2c)
end

Instance Method Details

#disableObject

Note:

device trees cannot be unloaded at this time without kernel panic.

Disable the specified I2C device.



251
252
253
# File 'lib/beaglebone/i2c.rb', line 251

def disable
  I2C::disable(@i2c)
end

#fileObject

Return the file descriptor to the open I2C device



256
257
258
# File 'lib/beaglebone/i2c.rb', line 256

def file
  I2C::file(@i2c)
end

#read(address, bytes = 1, register = nil) ⇒ Object

Read data from an I2C device

Examples:

# read 3 big endian signed shorts starting at register 0x03
data = i2c.read(0x1e, 6, [0x03].pack("C*"))
  x,z,y = raw.unpack("s>*")

Parameters:

  • address

    the address of the slave device

  • bytes (defaults to: 1)

    bytes to read

  • register (defaults to: nil)

    optional register to read from



244
245
246
# File 'lib/beaglebone/i2c.rb', line 244

def read(address, bytes=1, register=nil)
  I2C::read(@i2c, address, bytes, register)
end

#write(address, data) ⇒ Object

Write data to an I2C device

Examples:

i2c.write(0x1e, [0x00, 0b10010000].pack("C*") )

Parameters:

  • address

    the address of the slave device

  • data

    the data to write

Returns:

  • Integer the number of bytes written



230
231
232
# File 'lib/beaglebone/i2c.rb', line 230

def write(address, data)
  I2C::write(@i2c, address, data)
end