Class: BBB::Pins::DigitalPin

Inherits:
Object
  • Object
show all
Includes:
Pinnable
Defined in:
lib/BBB/pins/digital_pin.rb

Overview

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.

Direct Known Subclasses

DigitalInputPin, DigitalOutputPin

Instance Attribute Summary collapse

Attributes included from Pinnable

#position

Instance Method Summary collapse

Methods included from Pinnable

#initialize, #io, #mock?, #pinnable?

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



16
17
18
# File 'lib/BBB/pins/digital_pin.rb', line 16

def opts
  @opts
end

#statusSymbol (readonly)

Read value from the pin for input pins, or from memory for output pins.

Returns:

  • (Symbol)

    :high or :low



42
43
44
# File 'lib/BBB/pins/digital_pin.rb', line 42

def status
  @status
end

Instance Method Details

#directionSymbol

Gets the direction of the pin from the options and memoizes it in the

Returns:

  • (Symbol)

    either :input or :output



23
24
25
# File 'lib/BBB/pins/digital_pin.rb', line 23

def direction
  @direction ||= @opts.fetch(:direction)
end

#off!void

This method returns an undefined value.

Set the pin into :low state



60
61
62
# File 'lib/BBB/pins/digital_pin.rb', line 60

def off!
  write(:low)
end

#off?Boolean

Check if the pin state is low

Returns:

  • (Boolean)


74
75
76
# File 'lib/BBB/pins/digital_pin.rb', line 74

def off?
  !on?
end

#on!void

This method returns an undefined value.

Set the pin into :high state



53
54
55
# File 'lib/BBB/pins/digital_pin.rb', line 53

def on!
  write(:high)
end

#on?Boolean

Check if the pin state is high

Returns:

  • (Boolean)


67
68
69
# File 'lib/BBB/pins/digital_pin.rb', line 67

def on?
  status == :high
end

#write(value) ⇒ Object

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.



32
33
34
35
# File 'lib/BBB/pins/digital_pin.rb', line 32

def write(value)
  @status = value
  io.write(value)
end