Class: Neotrellis::Seesaw

Inherits:
Object
  • Object
show all
Defined in:
lib/neotrellis/seesaw.rb

Overview

Driver for Seesaw i2c generic conversion chip. See www.adafruit.com/product/3657 for example board.

Examples:

Display Seesaw’s device version

seesaw = Neotrellis::Seesaw.new(device: "/dev/i2c-1", addr: 0x2E)
puts seesaw.version

Constant Summary collapse

DEFAULT_I2C_ADDR =

Default SeeSaw I2C address

0x49

Instance Method Summary collapse

Constructor Details

#initialize(device: '/dev/i2c-0', addr: DEFAULT_I2C_ADDR, debug: false) ⇒ Seesaw

Initialize a Seesaw chip on the i2c bus. It use the i2c kernel driver to communicate with the chip.



60
61
62
63
64
65
66
67
68
69
# File 'lib/neotrellis/seesaw.rb', line 60

def initialize(device: '/dev/i2c-0', addr: DEFAULT_I2C_ADDR, debug: false)
  @i2c = I2C.create(device)
  @addr = addr
  @debug = debug

  sw_reset
rescue I2C::AckError
  STDERR.puts "I2C initialization error, check your wiring and I2C addresses."
  raise
end

Instance Method Details

#read_byte(base_reg, function_reg) ⇒ Byte

Read a byte from a Seesaw register

Raises:

  • (ReadError)

    If no data is returned form the underlying I2C device



100
101
102
# File 'lib/neotrellis/seesaw.rb', line 100

def read_byte(base_reg, function_reg)
  read_raw(1, base_reg, function_reg).ord
end

#read_bytes(size, base_reg, function_reg) ⇒ Array

Read bytes from a Seesaw register

Raises:

  • (ReadError)

    If no data is returned form the underlying I2C device



112
113
114
# File 'lib/neotrellis/seesaw.rb', line 112

def read_bytes(size, base_reg, function_reg)
  read_raw(size, base_reg, function_reg).unpack("C#{size}")
end

#sw_resetObject

Trigger a software reset of the SeeSaw chip



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/neotrellis/seesaw.rb', line 72

def sw_reset()
  write(STATUS_BASE, STATUS_SWRST, 0xFF)

  # Give some time to the device to reset (but not when testing)
  sleep(0.5) unless testing?

  chip_id = read_byte(STATUS_BASE, STATUS_HW_ID)

  if chip_id != HW_ID_CODE
    raise "Seesaw hardware ID returned #{chip_id.to_shex} is not correct! Expected #{HW_ID_CODE.to_shex}. Please check your wiring."
  end
end

#versionInteger

Get the version of the Seesaw chip



88
89
90
91
# File 'lib/neotrellis/seesaw.rb', line 88

def version()
  # 4 bytes for Unsigned Int Big Endian
  @i2c.read(@addr, 4, STATUS_BASE, STATUS_VERSION).unpack('I>').first
end

#write(base_reg, function_reg, *data) ⇒ Object

Write data to the given register



121
122
123
124
# File 'lib/neotrellis/seesaw.rb', line 121

def write(base_reg, function_reg, *data)
  puts "DEBUG: I2C WRITE: %02X %02X %s" % [base_reg, function_reg, data.map{|i| "%02X" % [i]}.join(' ')] if @debug
  @i2c.write(@addr, base_reg, function_reg, *data)
end