Skip to content

Commit

Permalink
Use "comps" over "devs" for clarity managing I2C & SPI components
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Sep 29, 2024
1 parent 81c99ae commit 2892fe8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
73 changes: 53 additions & 20 deletions lib/denko/behaviors/subcomponents.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
module Denko
module Behaviors
module Subcomponents
def components
@components ||= []
end

def single_pin_components
@single_pin_components ||= {}
end

def hw_i2c_devs
@hw_i2c_devs ||= {}
end

#
# Main Methods
#
def add_component(component)
add_single_pin(component)
add_hw_i2c(component)
add_hw_spi(component)
components << component
end

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

def components
@components ||= []
end

#
# Single Pin
#
def add_single_pin(component)
if component.respond_to?(:pin) && component.pin.class == Integer
unless single_pin_components[component.pin]
Expand All @@ -37,25 +37,58 @@ def add_single_pin(component)
end
end

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

def single_pin_components
@single_pin_components ||= {}
end

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

def remove_single_pin(component)
if component.respond_to?(:pin) && component.pin.class == Integer
single_pin_components.delete(component.pin)
def remove_hw_i2c(component)
hw_i2c_comps.delete(component.i2c_index) if component.respond_to?(:i2c_index)
end

def hw_i2c_comps
@hw_i2c_comps ||= {}
end

#
# SPI
#
def add_hw_spi(component)
if component.respond_to?(:spi_index)
unless hw_spi_comps[component.spi_index]
hw_spi_comps[component.spi_index] = component
else
raise StandardError, "Error adding #{component} to #{self}. SPI dev: #{component.spi_index} " \
"already in use by: #{hw_spi_comps[component.spi_index]}"
end
end
end

def remove_hw_i2c(component)
hw_i2c_devs.delete(component.i2c_index) if component.respond_to?(:i2c_index)
def remove_hw_spi(component)
hw_spi_comps.delete(component.spi_index) if component.respond_to?(:spi_index)
end

def hw_spi_comps
@hw_spi_comps ||= {}
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/denko/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def update(line)
match = pin.match /\AI2C(\d*)/
if match
dev_index = match[1].to_i
dev = hw_i2c_devs[dev_index]
dev = hw_i2c_comps[dev_index]
dev.update(message) if dev
return
end
Expand Down
6 changes: 3 additions & 3 deletions test/behaviors/subcomponents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def test_hw_i2c_buses_add_and_remove_properly
assert_equal [i2c0, i2c1], board.components

# Have the right hash keys.
assert_equal i2c0, board.hw_i2c_devs[0]
assert_equal i2c1, board.hw_i2c_devs[1]
assert_equal i2c0, board.hw_i2c_comps[0]
assert_equal i2c1, board.hw_i2c_comps[1]

# Get removed correctly.
board.remove_component(i2c0)
hash = { 1 => i2c1 }
assert_equal hash, board.hw_i2c_devs
assert_equal hash, board.hw_i2c_comps
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def single_pin_component_exists(pin)
end

def hw_i2c_exists(index)
return hw_i2c_devs[index]
return hw_i2c_comps[index]
false
end

Expand Down

0 comments on commit 2892fe8

Please sign in to comment.