Sha256: dbcd2bc7dc8d1a8706ade381358dbec04236fe42097cda41d96ef53a4da50374

Contents?: true

Size: 1.19 KB

Versions: 5

Compression:

Stored size: 1.19 KB

Contents

#
# Example that shows the default I2C bus pins, and addresses of any
# devices connected to the bus.
#
require 'bundler/setup'
require 'denko'

# Method to let the user set I2C pins.
def enter_pins
  puts  "Please manually specify I2C pins..."
  print "I2C SDA pin: "; sda = gets
  print "I2C SCL pin: "; scl = gets
  puts
  [sda.to_i, scl.to_i]
end

board = Denko::Board.new(Denko::Connection::Serial.new)

# If no board map, ask user to set pins manually.
unless board.map
  puts "Error: Pin map not available for this board"
  sda, scl = enter_pins

# Else get defaults from map.
else
  sda = board.map[:SDA] || board.map[:SDA0]
  scl = board.map[:SCL] || board.map[:SCL0]

  # If not in map, ask user to set manually.
  unless sda && scl
    puts "Error: I2C pins not found in this board's pin map"
    sda, scl = enter_pins
  end
end

puts "Using I2C interface on pins #{sda} (SDA) and #{scl} (SCL)"
puts

bus = Denko::I2C::Bus.new(board: board, pin: sda)
bus.search

if bus.found_devices.empty?
  puts "No devices found on I2C bus"
else
  puts "I2C device addresses found:"
  bus.found_devices.each do |address|
    # Print as hexadecimal.
    puts "0x#{address.to_s(16).upcase}"
  end
end

puts
board.finish_write

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
denko-0.13.4 examples/i2c/search.rb
denko-0.13.3 examples/i2c/search.rb
denko-0.13.2 examples/i2c/search.rb
denko-0.13.1 examples/i2c/search.rb
denko-0.13.0 examples/i2c/search.rb