class BBB::Pins::DigitalPin

A Digital Pin on the board. The digital pins uses GPIO on the filesystem to communicate. This class assumes its the only actor on the pin. Therfore it can cache the status in memory instead of reading it from the filesystem every time.

It is advised to use the DigitalInputPin and DigitalOutputPin classes for clarity, buy, nothings stops you from using a DigitalPin with the :input or :output direction.

Attributes

opts[R]
status[R]

Public Instance Methods

direction() click to toggle source

Gets the direction of the pin from the options and memoizes it in the @direction attribute.

@return [Symbol] either :input or :output

# File lib/BBB/pins/digital_pin.rb, line 23
def direction
  @direction ||= @opts.fetch(:direction)
end
off!() click to toggle source

Set the pin into :low state @return [void]

# File lib/BBB/pins/digital_pin.rb, line 60
def off!
  write(:low)
end
off?() click to toggle source

Check if the pin state is low @return [Boolean]

# File lib/BBB/pins/digital_pin.rb, line 74
def off?
  !on?
end
on!() click to toggle source

Set the pin into :high state @return [void]

# File lib/BBB/pins/digital_pin.rb, line 53
def on!
  write(:high)
end
on?() click to toggle source

Check if the pin state is high @return [Boolean]

# File lib/BBB/pins/digital_pin.rb, line 67
def on?
  status == :high
end
write(value) click to toggle source

Write value to the specified pin Digitally. This might fail hard if you try to write to an input pin. However, for performance reasons we do not want to check the direction of the pin every write.

# File lib/BBB/pins/digital_pin.rb, line 32
def write(value)
  @status = value
  io.write(value)
end

Private Instance Methods

default_io() click to toggle source
# File lib/BBB/pins/digital_pin.rb, line 80
def default_io
  IO::GPIO.new(direction, position)
end