Module: Denko::Behaviors::MultiPin

Constant Summary

Constants included from Lifecycle

Lifecycle::CALLBACK_METHODS

Instance Attribute Summary collapse

Attributes included from Component

#board, #params

Attributes included from State

#state

Instance Method Summary collapse

Methods included from Lifecycle

included

Methods included from Component

#initialize, #micro_delay

Methods included from State

#update_state

Instance Attribute Details

#pinObject (readonly)

Returns the value of attribute pin.



11
12
13
# File 'lib/denko/behaviors/multi_pin.rb', line 11

def pin
  @pin
end

#pinsObject (readonly)

Returns the value of attribute pins.



11
12
13
# File 'lib/denko/behaviors/multi_pin.rb', line 11

def pins
  @pins
end

#proxiesObject (readonly)

Returns the value of attribute proxies.



11
12
13
# File 'lib/denko/behaviors/multi_pin.rb', line 11

def proxies
  @proxies
end

Instance Method Details

#convert_pins(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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

def convert_pins(options={})
  @pins = {}
  params[:pins].each do |key,pin|
    self.pins[key] = pin ? board.convert_pin(pin) : nil
  end
  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. mode: :input_pullup) can be injected there.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/denko/behaviors/multi_pin.rb', line 40

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]
    # Allow pin_options to override board or pin number.
    proxy_options = pin_options
    proxy_options[:board] ||= self.board
    proxy_options[:pin]   ||= self.pins[name]

    proxy = klass.new(proxy_options)
    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.



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

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)


65
66
67
# File 'lib/denko/behaviors/multi_pin.rb', line 65

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

#require_pins(*array) ⇒ Object



69
70
71
# File 'lib/denko/behaviors/multi_pin.rb', line 69

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