Class: MockI2CDevice

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMockI2CDevice

Returns a new instance of MockI2CDevice.



8
9
10
11
12
13
14
# File 'lib/i2c/mocki2cdevice.rb', line 8

def initialize
  @temp = Tempfile.new("i2c")
  @ioctl = []
  @memory = [ 0 ]
  @address = nil
  @state = nil
end

Instance Attribute Details

#ioctl(cmd, arg) ⇒ Object (readonly)

Returns the value of attribute ioctl.



5
6
7
# File 'lib/i2c/mocki2cdevice.rb', line 5

def ioctl
  @ioctl
end

#memoryObject (readonly)

Returns the value of attribute memory.



4
5
6
# File 'lib/i2c/mocki2cdevice.rb', line 4

def memory
  @memory
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/i2c/mocki2cdevice.rb', line 6

def state
  @state
end

Instance Method Details

#closeObject



31
32
33
34
35
# File 'lib/i2c/mocki2cdevice.rb', line 31

def close
  @address = nil
  @state = nil
  self
end

#openObject



25
26
27
28
29
# File 'lib/i2c/mocki2cdevice.rb', line 25

def open
  @address = nil
  @state = :init
  self
end

#pathObject



16
17
18
# File 'lib/i2c/mocki2cdevice.rb', line 16

def path
  @temp.path
end

#sysread(size) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/i2c/mocki2cdevice.rb', line 52

def sysread(size)
  ret = []
  case @state
  when :init
    raise "Invalid State"
  when :wait
    size.times do
      ret << @memory[@address]
      @address += 1
    end
  end
  ret.pack("C*")
end

#syswrite(buf) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/i2c/mocki2cdevice.rb', line 37

def syswrite(buf)
  buf.unpack("C*").each do |c|
    case @state
    when :init
      # p "@address = 0x%02x" % c
      @address = c
      @state = :wait
    when :wait
      # p "@memory[0x%02x] = 0b%08b" % [@address, c]
      @memory[@address] = c
      @address += 1
    end
  end
end