Module: Denko::Behaviors::MultiPin
- Included in:
- AnalogIO::Joystick, DigitalIO::RotaryEncoder, Display::HD44780, I2C::BitBang, LED::RGB, LED::SevenSegment, Motor::A3967, Motor::L298, SPI::BitBang, SPI::Peripheral, Sensor::HCSR04, UART::BitBang
- Defined in:
- lib/denko/behaviors/multi_pin.rb
Constant Summary
Constants included from Lifecycle
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
Attributes included from State
Instance Method Summary collapse
- #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 Lifecycle
Methods included from Component
Methods included from State
Instance Attribute Details
#pin ⇒ Object (readonly)
Returns the value of attribute pin.
11 12 13 |
# File 'lib/denko/behaviors/multi_pin.rb', line 11 def pin @pin end |
#pins ⇒ Object (readonly)
Returns the value of attribute pins.
11 12 13 |
# File 'lib/denko/behaviors/multi_pin.rb', line 11 def pins @pins end |
#proxies ⇒ Object (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
26 27 28 29 30 31 32 33 |
# File 'lib/denko/behaviors/multi_pin.rb', line 26 def convert_pins(={}) @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, ={}) # Proxied pins are required by default. require_pin(name) unless [:optional] # Make the proxy, passing through options, and store it. if self.pins[name] # Allow pin_options to override board or pin number. = [:board] ||= self.board [:pin] ||= self.pins[name] proxy = klass.new() 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.
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.
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 |