Module: BBB::Components::Pinnable

Included in:
AnalogComponent, Button, ESC, Led, Nunchuck, Servo, WiiMotionPlus
Defined in:
lib/BBB/components/pinnable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pinsObject (readonly)

Returns the value of attribute pins.



4
5
6
# File 'lib/BBB/components/pinnable.rb', line 4

def pins
  @pins
end

#positionsObject (readonly)

Returns the value of attribute positions.



5
6
7
# File 'lib/BBB/components/pinnable.rb', line 5

def positions
  @positions
end

Class Method Details

.included(base) ⇒ Object



46
47
48
# File 'lib/BBB/components/pinnable.rb', line 46

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#after_connect_callbacksObject



108
109
110
111
112
113
114
115
116
# File 'lib/BBB/components/pinnable.rb', line 108

def after_connect_callbacks
  self.class.after_connect_callbacks.each do |callback|
    if callback.responds_to?(:call)
      callback.call
    else
      self.send(callback)
    end
  end
end

#connect(*positions) ⇒ Object Also known as: pin_initialization

Initialize the pin classes with their positions and options. Turning them from their Classes into working pin instances.

The argument list of the methods is a bit odd. Since it’s not possible to have both a splat and an option array as arguments, the method signature only shows a splat of positions. The options are then taken out of the positions, if the last position is actually a Hash as opposed to a Symbol. If the last element of the positions list is not a Hash, the options are set to an empty hash.

Parameters:

  • positions (Array<Symbol>)

    positions the pins should be initialized with.

  • opts (Hash)

    Options to pass along to the pins during initialization.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/BBB/components/pinnable.rb', line 68

def connect(*positions)
  positions = self.positions if positions.empty?

  positions.flatten!
  opts = positions.last.kind_of?(Hash) ? positions.pop : {}

  verify_pin_position_count(positions)

  @pins = []
  self.class.pin_classes.each_with_index do |pin, index|
    @pins << pin.new(positions[index], opts)
  end
  after_connect_callbacks
  return self # so that we can chain it with the initializer
end

#initialize(options = {}) ⇒ Object

Provide a default initializer



140
141
142
# File 'lib/BBB/components/pinnable.rb', line 140

def initialize(options={})
  set_options(options)
end

#pinObject

Convenience method to grab the first pin in the pins array

Returns:

  • BBB::Pins::AnalogPin



133
134
135
# File 'lib/BBB/components/pinnable.rb', line 133

def pin
  pins.first
end

#pinnable?Boolean

Method that is used in the test suite to test if the pinnable module is included in an object

Returns:

  • (Boolean)

    true



124
125
126
# File 'lib/BBB/components/pinnable.rb', line 124

def pinnable?
  true
end

#set_options(options) ⇒ Object

Provide a function to handle the options

Parameters:

  • options (Hash)


149
150
151
152
153
154
# File 'lib/BBB/components/pinnable.rb', line 149

def set_options(options)
  @positions = [options[:pin],
                options[:pins],
                options[:position],
                options[:path]].flatten.compact
end

#verify_pin_position_count(positions) ⇒ Object

Verifies if the number of pins registered in the @pins array match with the number of pins provided as an argument to the method. This function normally gets called as part of the initialize pins method to verify if the positions given to the connect method matches the number of registered pins.

Parameters:

  • positions (Array<Symbol>)

    The array of positions

Returns:

  • Void

Raises:

  • (PinsDoNotMatchException)

    If the pin counts between the positions provided as an argument and the pins in the pins array do not match, this exception gets raised, to prevent hard to debug situations later on in the lifecycle of an application.



101
102
103
104
105
106
# File 'lib/BBB/components/pinnable.rb', line 101

def verify_pin_position_count(positions)
  if self.class.pin_classes.count != positions.count
    fail PinsDoNotMatchException,
      "#{self.class.to_s} requires #{self.class.pin_classes.count} but received #{positions.count} pin position."
  end
end