Class: Artoo::Adaptors::IO::I2c

Inherits:
Object
  • Object
show all
Defined in:
lib/artoo/adaptors/io/i2c.rb

Constant Summary collapse

I2C_SLAVE =
0x0703

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i2c_location, address) ⇒ I2c

Returns a new instance of I2c.



9
10
11
12
# File 'lib/artoo/adaptors/io/i2c.rb', line 9

def initialize(i2c_location, address)
  @i2c_location = i2c_location
  start(address)
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



5
6
7
# File 'lib/artoo/adaptors/io/i2c.rb', line 5

def address
  @address
end

#handleObject (readonly)

Returns the value of attribute handle.



5
6
7
# File 'lib/artoo/adaptors/io/i2c.rb', line 5

def handle
  @handle
end

#i2c_locationObject (readonly)

Returns the value of attribute i2c_location.



5
6
7
# File 'lib/artoo/adaptors/io/i2c.rb', line 5

def i2c_location
  @i2c_location
end

Instance Method Details

#read(len) ⇒ Object



32
33
34
35
36
37
38
# 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) ⇒ Object



14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
# 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