class Artoo::Adaptors::IO::I2c

Constants

I2C_SLAVE

Attributes

address[R]
handle[R]
i2c_location[R]

Public Class Methods

new(i2c_location, address) click to toggle source
# File lib/artoo/adaptors/io/i2c.rb, line 9
def initialize(i2c_location, address)
  @i2c_location = i2c_location
  start(address)
end

Public Instance Methods

read(len) click to toggle source
# File lib/artoo/adaptors/io/i2c.rb, line 32
def read(len)
  begin
    @handle.read_nonblock(len).unpack("C#{len}")
  rescue Exception => e
    start(@address)
  end
end
start(address) click to toggle source
# File lib/artoo/adaptors/io/i2c.rb, line 14
def start(address)
  @address = address
  @handle = File.open(@i2c_location, 'r+')
  @handle.ioctl(I2C_SLAVE, @address)

  write 0
end
write(*data) click to toggle source
# File lib/artoo/adaptors/io/i2c.rb, line 22
def write(*data)
  ret = ""
  ret.force_encoding("US-ASCII")
  data.each do |n|
    ret << [n].pack("v")[0]
    ret << [n].pack("v")[1]
  end
  @handle.write(ret)
end