Class: Denko::I2C::Bus

Inherits:
Object
  • Object
show all
Includes:
Behaviors::BusControllerAddressed, Behaviors::Reader, Behaviors::SinglePin
Defined in:
lib/denko/i2c/bus.rb

Instance Attribute Summary collapse

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::SinglePin

#mode, #pin

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Reader

#read, #read_using, #wait_for_read

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #initialize, #pre_callback_filter, #remove_callback, #update

Methods included from Behaviors::State

#initialize, #state

Methods included from Behaviors::BusControllerAddressed

#add_component

Methods included from Behaviors::BusController

#mutex

Methods included from Behaviors::Subcomponents

#add_component, #components, #remove_component, #single_pin_components

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Attribute Details

#found_devicesObject (readonly)

Returns the value of attribute found_devices.



8
9
10
# File 'lib/denko/i2c/bus.rb', line 8

def found_devices
  @found_devices
end

Instance Method Details

#_read(address, register, num_bytes, frequency = 100000, repeated_start = false) ⇒ Object



25
26
27
# File 'lib/denko/i2c/bus.rb', line 25

def _read(address, register, num_bytes, frequency=100000, repeated_start=false)
  board.i2c_read(address, register, num_bytes, frequency, repeated_start)
end

#after_initialize(options = {}) ⇒ Object



10
11
12
13
14
# File 'lib/denko/i2c/bus.rb', line 10

def after_initialize(options={})
  super(options)
  @found_devices = []
  bubble_callbacks
end

#bubble_callbacksObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/denko/i2c/bus.rb', line 29

def bubble_callbacks
  add_callback(:bus_controller) do |str|
    if str && str.match(/\A\d+-/)
      address, data = str.split("-", 2)
      address = address.to_i

      data = data.split(",").map(&:to_i)
      data = nil if data.empty?

      update_component(address, data)
    end
  end
end

#searchObject



16
17
18
19
# File 'lib/denko/i2c/bus.rb', line 16

def search
  addresses = read_using -> { board.i2c_search }
  @found_devices = addresses.split(":").map(&:to_i) if addresses
end

#update_component(address, data) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/denko/i2c/bus.rb', line 43

def update_component(address, data)
  components.each do |component|
    if component.address == address
      component.update(data)
    end
  end
end

#write(address, bytes, frequency = 100000, repeated_start = false) ⇒ Object



21
22
23
# File 'lib/denko/i2c/bus.rb', line 21

def write(address, bytes, frequency=100000, repeated_start=false)
  board.i2c_write(address, bytes, frequency, repeated_start)
end