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
Public Instance Methods
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
Set the pin into :low state @return [void]
# File lib/BBB/pins/digital_pin.rb, line 60 def off! write(:low) end
Check if the pin state is low @return [Boolean]
# File lib/BBB/pins/digital_pin.rb, line 74 def off? !on? end
Set the pin into :high state @return [void]
# File lib/BBB/pins/digital_pin.rb, line 53 def on! write(:high) end
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 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
# File lib/BBB/pins/digital_pin.rb, line 80 def default_io IO::GPIO.new(direction, position) end