Module: Denko::Behaviors::MultiPin

Instance Attribute Summary collapse

Attributes included from Component

#board

Instance Method Summary collapse

Methods included from Component

#initialize, #micro_delay

Methods included from State

#initialize, #state

Instance Attribute Details

#pinObject (readonly)

Returns the value of attribute pin.



9
10
11
# File 'lib/denko/behaviors/multi_pin.rb', line 9

def pin
  @pin
end

#pinsObject

Returns the value of attribute pins.



9
10
11
# File 'lib/denko/behaviors/multi_pin.rb', line 9

def pins
  @pins
end

#proxiesObject

Returns the value of attribute proxies.



9
10
11
# File 'lib/denko/behaviors/multi_pin.rb', line 9

def proxies
  @proxies
end

Instance Method Details

#before_initialize(options = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/denko/behaviors/multi_pin.rb', line 20

def before_initialize(options={})
  # Get given pins early. Avoids giving them again to require or proxy.
  self.pins = options[:pins]
  self.proxies = {}
  super(options)
end

#convert_pins(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
# File 'lib/denko/behaviors/multi_pin.rb', line 27

def convert_pins(options={})
  super(options)
  self.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

#proxy_pin(name, klass, pin_options = {}) ⇒ Object

Proxy a pin to a single-pin component. Set this up in the including component’s #initialize_pins method. Additional options for each proxy (eg. pullup/pulldown) can be injected there.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/denko/behaviors/multi_pin.rb', line 39

def proxy_pin(name, klass, pin_options={})
  # Proxied pins are required by default.
  require_pin(name) unless pin_options[:optional]
    
  # Make the proxy, passing through options, and store it.
  if self.pins[name]
    proxy = klass.new pin_options.merge(board: self.board, pin: self.pins[name])          
    self.proxies[name] = proxy
    instance_variable_set("@#{name}", proxy)
  end

  # Accessor for the proxy's instance var, or nil, if not given.
  singleton_class.class_eval { attr_reader name }
end

#proxy_statesObject

Return a hash with the state of each proxy component.



12
13
14
15
16
17
18
# File 'lib/denko/behaviors/multi_pin.rb', line 12

def proxy_states
  hash = {}
  proxies.each_key do |key|
    hash[key] = proxies[key].state if self.proxies[key]
  end
  hash
end

#require_pin(name) ⇒ Object

Require a single pin that may or may not be proxied. This is useful for components using libraries running on the board, where we need to specify the pin, but not do anything with it.

Raises:

  • (ArgumentError)


59
60
61
# File 'lib/denko/behaviors/multi_pin.rb', line 59

def require_pin(name)
  raise ArgumentError, "missing #{name.inspect} pin" unless self.pins[name]
end

#require_pins(*array) ⇒ Object



63
64
65
# File 'lib/denko/behaviors/multi_pin.rb', line 63

def require_pins(*array)
  [array].flatten.each { |name| require_pin(name) }
end