Skip to content

Commit

Permalink
Fix Behaviors::MultiPin so SPI version of SSD1306 will work
Browse files Browse the repository at this point in the history
Should always do params manipulation in a before_initialize block
  • Loading branch information
vickash committed Sep 29, 2024
1 parent f8c3ce7 commit 9bf8dc8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
14 changes: 6 additions & 8 deletions lib/denko/behaviors/multi_pin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module MultiPin

attr_reader :pin, :pins, :proxies

def proxies
@proxies ||= {}
end

# Return a hash with the state of each proxy component.
def proxy_states
hash = {}
Expand All @@ -19,15 +23,9 @@ def proxy_states
hash
end

before_initialize do
# Get given pins early. Avoids giving them again to require or proxy.
self.pins = params[:pins]
self.proxies = {}
end

def convert_pins(options={})
super(options)
self.pins.each { |key,pin| self.pins[key] = board.convert_pin(pin) }
self.pins = {}
params[:pins].each { |key,pin| self.pins[key] = board.convert_pin(pin) }
pin_array = pins.values
raise ArgumentError, "duplicate pins in: #{pins.inspect}" unless pin_array == pin_array.uniq
end
Expand Down
5 changes: 4 additions & 1 deletion lib/denko/digital_io/rotary_encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class RotaryEncoder
include Behaviors::Callbacks
include Behaviors::Lifecycle

def initialize_pins(params={})
before_initialize do
# Allow pins to be given as printed on common parts.
unless params[:pins][:a]
params[:pins][:a] = params[:pins][:clk] if params[:pins][:clk]
Expand All @@ -18,6 +18,9 @@ def initialize_pins(params={})

# But always refer to them as a and b internally.
[:clk, :clock, :dt, :data].each { |key| params[:pins].delete(key) }
end

def initialize_pins(params={})
proxy_pin :a, DigitalIO::Input
proxy_pin :b, DigitalIO::Input
end
Expand Down
6 changes: 4 additions & 2 deletions lib/denko/spi/bit_bang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class BitBang
include Behaviors::MultiPin
include Behaviors::BusController
include Behaviors::Reader
include Behaviors::Lifecycle

def initialize_pins(options={})
before_initialize do
# Allow pin aliases.
pins[:input] = pins[:input] || pins[:poci] || pins[:miso]
pins[:output] = pins[:output] || pins[:pico] || pins[:mosi]
Expand All @@ -17,8 +18,9 @@ def initialize_pins(options={})
# Validate pins.
raise ArgumentError, "either output or input pin required" unless pins[:input] || pins[:output]
raise ArgumentError, "clock pin required" unless pins[:clock]
end

# Create proxies.
def initialize_pins(options={})
proxy_pin :clock, DigitalIO::CBitBang
proxy_pin :output, DigitalIO::CBitBang if pins[:output]
proxy_pin :input, DigitalIO::CBitBang if pins[:input]
Expand Down

0 comments on commit 9bf8dc8

Please sign in to comment.