Class: PiDriver::Device::MCP23017

Inherits:
Object
  • Object
show all
Defined in:
lib/pi_driver/device/mcp23017.rb,
lib/pi_driver/device/mcp23017/port.rb,
lib/pi_driver/device/mcp23017/register.rb,
lib/pi_driver/device/mcp23017/register/gpio.rb,
lib/pi_driver/device/mcp23017/register/gppu.rb,
lib/pi_driver/device/mcp23017/register/intf.rb,
lib/pi_driver/device/mcp23017/register/ipol.rb,
lib/pi_driver/device/mcp23017/register/olat.rb,
lib/pi_driver/device/mcp23017/register/iocon.rb,
lib/pi_driver/device/mcp23017/register/iodir.rb,
lib/pi_driver/device/mcp23017/register/defval.rb,
lib/pi_driver/device/mcp23017/register/intcap.rb,
lib/pi_driver/device/mcp23017/register/intcon.rb,
lib/pi_driver/device/mcp23017/hardware_address.rb,
lib/pi_driver/device/mcp23017/register/gpinten.rb,
lib/pi_driver/device/mcp23017/register/register_helper.rb

Defined Under Namespace

Classes: Defval, Gpinten, Gpio, Gppu, HardwareAddress, Intcap, Intcon, Intf, Iocon, Iodir, Ipol, Olat, Port, Register

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MCP23017

Returns a new instance of MCP23017.



21
22
23
24
25
26
# File 'lib/pi_driver/device/mcp23017.rb', line 21

def initialize(options = {})
  @argument_helper = Utils::ArgumentHelper.new prefix: 'MCP23017'
  @i2c_master = options[:i2c_master]
  @hardware_address = HardwareAddress.new observer: self
  update_opcodes
end

Instance Attribute Details

#hardware_addressObject (readonly)

Returns the value of attribute hardware_address.



19
20
21
# File 'lib/pi_driver/device/mcp23017.rb', line 19

def hardware_address
  @hardware_address
end

Instance Method Details

#read(*register_array) ⇒ Object

TODO: read and write look similar. consider consolidation



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pi_driver/device/mcp23017.rb', line 43

def read(*register_array)
  check_registers register_array

  @i2c_master.start

  register_array.each_with_index do |register, sequence_index|
    @i2c_master.restart unless sequence_index.zero?
    read_register register
  end

  @i2c_master.stop
end

#update_opcodesObject



35
36
37
38
39
40
# File 'lib/pi_driver/device/mcp23017.rb', line 35

def update_opcodes
  bits = [0, 1, 0, 0, @hardware_address.a2, @hardware_address.a1, @hardware_address.a0]
  base = Utils::Byte.bits_to_byte(bits)
  @opcode_for_write = PiDriver::I2CMaster.prepare_address_for_write base
  @opcode_for_read = PiDriver::I2CMaster.prepare_address_for_read base
end

#update_registersObject



28
29
30
31
32
33
# File 'lib/pi_driver/device/mcp23017.rb', line 28

def update_registers
  bank = registers[:iocon].bank
  registers.each_value do |register|
    register.update_address bank
  end
end

#write(*register_array) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pi_driver/device/mcp23017.rb', line 56

def write(*register_array)
  check_registers register_array

  @i2c_master.start

  register_array.each_with_index do |register, sequence_index|
    @i2c_master.restart unless sequence_index.zero?
    write_register register
  end

  @i2c_master.stop
end