Skip to content

Commit

Permalink
Refactor Subcomponents behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Sep 26, 2024
1 parent 2b6d497 commit a468f25
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
46 changes: 31 additions & 15 deletions lib/denko/behaviors/subcomponents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ def single_pin_components
@single_pin_components ||= {}
end

def hw_i2c_devs
@hw_i2c_devs ||= {}
end

def add_component(component)
# Prevent multiple I2C::Bus instances using one physical device.
if component.respond_to?(:i2c_index)
components.each do |other_component|
if other_component.respond_to?(:i2c_index)
if component.i2c_index == other_component.i2c_index
raise StandardError, "I2C device index #{component.i2c_index} already in use by: #{other_component}"
end
end
end
end
add_single_pin(component)
add_hw_i2c(component)
components << component
end

def remove_component(component)
remove_single_pin(component)
remove_hw_i2c(component)
deleted = components.delete(component)
component.stop if deleted && component.respond_to?(:stop)
end

def add_single_pin(component)
if component.respond_to?(:pin) && component.pin.class == Integer
unless single_pin_components[component.pin]
single_pin_components[component.pin] = component
Expand All @@ -29,17 +35,27 @@ def add_component(component)
"already in use by: #{single_pin_components[component.pin]}"
end
end
end

components << component
def add_hw_i2c(component)
if component.respond_to?(:i2c_index)
unless hw_i2c_devs[component.i2c_index]
hw_i2c_devs[component.pin] = component
else
raise StandardError, "Error adding #{component} to #{self}. I2C dev: #{component.i2c_index} " \
"already in use by: #{hw_i2c_devs[component.pin]}"
end
end
end

def remove_component(component)
def remove_single_pin(component)
if component.respond_to?(:pin) && component.pin.class == Integer
single_pin_components[component.pin] = nil
single_pin_components.delete(component.pin)
end
end

deleted = components.delete(component)
component.stop if deleted && component.respond_to?(:stop)
def remove_hw_i2c(component)
hw_i2c_devs.delete(component.i2c_index) if component.respond_to?(:i2c_index)
end
end
end
Expand Down
14 changes: 12 additions & 2 deletions test/behaviors/subcomponents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,21 @@ def test_calls_stop_on_remove
mock.verify
end

def test_stops_i2c_duplication
def test_disallows_duplicate_hw_i2c_buses
i2c0 = Denko::I2C::Bus.new(board: board, pin: 0)
assert_raises { Denko::I2C::Bus.new(board: board, pin: 1) }
end

i2c1 = Denko::I2C::Bus.new(board: board, pin: 2, i2c_index: 1)
def test_hw_i2c_buses_add_and_remove_properly
i2c0 = Denko::I2C::Bus.new(board: board, pin: 0, i2c_index: 0)
i2c1 = Denko::I2C::Bus.new(board: board, pin: 1, i2c_index: 1)
assert_equal [i2c0, i2c1], board.components

hash = { 0 => i2c0, 1 => i2c1 }
assert_equal hash, board.hw_i2c_devs

board.remove_component(i2c0)
hash = { 1 => i2c1 }
assert_equal hash, board.hw_i2c_devs
end
end

0 comments on commit a468f25

Please sign in to comment.