module BBB::Pins::Pinnable

Module which is included in all pins. The module provides the basic interface for a BBB::Pin and also takes care of selecting the right IO class to work with.

It defines a pinnable? method to test if the module is properly included in a pin.

Attributes

opts[R]
position[R]

Public Class Methods

new(position, opts={}) click to toggle source

Initializes a Pin

@param position [Symbol] The position of the pin @option opts [Boolean] :mock whether or not to use MockIO

# File lib/BBB/pins/pinnable.rb, line 20
def initialize(position, opts={})
  @position     = position
  @opts         = opts
end

Public Instance Methods

io() click to toggle source

Returns an instance of the IO class for the pin and memoizes this.

@return [IO]

# File lib/BBB/pins/pinnable.rb, line 30
def io
  @io ||= mock? ? mock_io : default_io
end
mock?() click to toggle source

Returns if this is a mock pin.

@return [Boolean]

# File lib/BBB/pins/pinnable.rb, line 39
def mock?
  @mock ||= @opts.fetch(:mock, BBB.configuration.test_mode)
end
pinnable?() click to toggle source

Always returns true, method is used to test if module is included.

@return [Boolean] true

# File lib/BBB/pins/pinnable.rb, line 48
def pinnable?
  true
end

Private Instance Methods

default_io() click to toggle source

If a class includes the Pinnable module, it should overwrite the default_io method. If this is not done, the module raises an error to point the developer into the right direction.

The default_io method should return an instantiated object that makes the interface between the filesystem, drivers etc, and the ruby code. Probably the object will behave like an IO object, relying on the linux kernel and drivers to get things done on the board.

# File lib/BBB/pins/pinnable.rb, line 64
def default_io
  fail NotImplementedError
end
mock_io() click to toggle source

The IO instance used for mock pins. By default this is StringIO.new, however, each class should determine if it defines its own MockIO class. An example of which can be found at the PWMPin.

# File lib/BBB/pins/pinnable.rb, line 73
def mock_io
  StringIO.new
end