Module: Denko::Behaviors::MultiPin
- Includes:
- Component
- Included in:
- DigitalIO::RotaryEncoder, Display::HD44780, LED::RGB, LED::SevenSegment, Motor::L298, Motor::Stepper, SPI::BitBang, Sensor::HCSR04, UART::BitBang
- Defined in:
- lib/denko/behaviors/multi_pin.rb
Instance Attribute Summary collapse
-
#pin ⇒ Object
readonly
Returns the value of attribute pin.
-
#pins ⇒ Object
readonly
Returns the value of attribute pins.
-
#proxies ⇒ Object
readonly
Returns the value of attribute proxies.
Attributes included from Component
Instance Method Summary collapse
- #before_initialize(options = {}) ⇒ Object
- #convert_pins(options = {}) ⇒ Object
-
#proxy_pin(name, klass, pin_options = {}) ⇒ Object
Proxy a pin to a single-pin component.
-
#proxy_states ⇒ Object
Return a hash with the state of each proxy component.
-
#require_pin(name) ⇒ Object
Require a single pin that may or may not be proxied.
- #require_pins(*array) ⇒ Object
Methods included from Component
Methods included from State
Instance Attribute Details
#pin ⇒ Object (readonly)
Returns the value of attribute pin.
9 10 11 |
# File 'lib/denko/behaviors/multi_pin.rb', line 9 def pin @pin end |
#pins ⇒ Object
Returns the value of attribute pins.
9 10 11 |
# File 'lib/denko/behaviors/multi_pin.rb', line 9 def pins @pins end |
#proxies ⇒ Object
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(={}) # Get given pins early. Avoids giving them again to require or proxy. self.pins = [:pins] self.proxies = {} super() end |
#convert_pins(options = {}) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/denko/behaviors/multi_pin.rb', line 27 def convert_pins(={}) super() 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, ={}) # Proxied pins are required by default. require_pin(name) unless [:optional] # Make the proxy, passing through options, and store it. if self.pins[name] proxy = klass.new .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_states ⇒ Object
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.
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 |