Class: PiPiper::Pin

Inherits:
Object
  • Object
show all
Defined in:
lib/pi_piper/pin.rb

Overview

Represents a GPIO pin on the Raspberry Pi

Constant Summary collapse

GPIO_HIGH =
1
GPIO_LOW =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Pin

Initializes a new GPIO pin.

either :in or :out. Defaults to :in.

physical pin should be inverted. Defaults to false.

method will detect a change, either :rising, :falling, or :both edge triggers. Defaults to :both.

set when pin direction is set to :in. Either :up, :down or :off. Defaults to :off.

Parameters:

  • options (Hash)

    A hash of options

Options Hash (options):

  • :pin (Fixnum)

    The pin number to initialize. Required.

  • :direction (Symbol)

    The direction of communication,

  • :invert (Boolean)

    Indicates if the value read from the

  • :trigger (Symbol)

    Indicates when the wait_for_change

  • :pull (Symbol)

    Indicates if and how pull mode must be

Raises:

  • (ArgumentError)

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pi_piper/pin.rb', line 28

def initialize(options)
  @options = {:direction => :in,
              :invert => false,
              :trigger => :none,
              :pull => :off,
            }.merge(options)

  raise ArgumentError, 'Pin # required' unless @options[:pin]

  PiPiper.driver.pin_direction(@options[:pin], @options[:direction])
  PiPiper.driver.pin_set_trigger(@options[:pin], @options[:trigger])
  if @options[:direction] == :out && @options[:pull] != :off
    raise ArgumentError, 'Unable to use pull-ups : pin direction must be :in for this'
  else
    PiPiper.driver.pin_set_pud(@options[:pin], @options[:pull])
  end
  read
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)


104
105
106
# File 'lib/pi_piper/pin.rb', line 104

def method_missing(method, *args, &block)
  PiPiper.driver.send(method, @options[:pin], *args, &block)
end

Instance Attribute Details

#last_valueObject (readonly)

Returns the value of attribute last_value


7
8
9
# File 'lib/pi_piper/pin.rb', line 7

def last_value
  @last_value
end

#optionsObject (readonly)

Returns the value of attribute options


7
8
9
# File 'lib/pi_piper/pin.rb', line 7

def options
  @options
end

#pinObject (readonly)

Returns the value of attribute pin


7
8
9
# File 'lib/pi_piper/pin.rb', line 7

def pin
  @pin
end

Instance Method Details

#changed?Boolean

Tests if the logic level has changed since the pin was last read.

Returns:

  • (Boolean)

82
83
84
# File 'lib/pi_piper/pin.rb', line 82

def changed?
  last_value != value
end

#offObject

If the pin has been initialized for output this method will set the logic level low.


60
61
62
# File 'lib/pi_piper/pin.rb', line 60

def off
  PiPiper.driver.pin_write(@options[:pin], GPIO_LOW) if @options[:direction] == :out
end

#off?Boolean

Tests if the logic level is low.

Returns:

  • (Boolean)

65
66
67
# File 'lib/pi_piper/pin.rb', line 65

def off?
  value == GPIO_LOW
end

#onObject

If the pin has been initialized for output this method will set the logic level high.


49
50
51
# File 'lib/pi_piper/pin.rb', line 49

def on
  PiPiper.driver.pin_write(@options[:pin], GPIO_HIGH) if @options[:direction] == :out
end

#on?Boolean

Tests if the logic level is high.

Returns:

  • (Boolean)

54
55
56
# File 'lib/pi_piper/pin.rb', line 54

def on?
  not off?
end

#readObject

Reads the current value from the pin. Without calling this method first, `value`, `last_value` and `changed?` will not be updated.

In short, you must call this method if you are curious about the current state of the pin.


97
98
99
100
101
# File 'lib/pi_piper/pin.rb', line 97

def read
  val = PiPiper.driver.pin_read(@options[:pin])
  @last_value = @value
  @value = @options[:invert] ? (val ^ 1) : val
end

#update_value(new_value) ⇒ Object Also known as: value=

If the pin has been initialized for output this method will either raise or lower the logic level depending on `new_value`.

Parameters:

  • new_value (Object)

    If false or 0 the pin will be set to off, otherwise on.


76
77
78
# File 'lib/pi_piper/pin.rb', line 76

def update_value(new_value)
  !new_value || new_value == GPIO_LOW ? off : on
end

#valueObject


69
70
71
# File 'lib/pi_piper/pin.rb', line 69

def value
  @value ||= read
end

#wait_for_changeObject

Blocks until a logic level change occurs. The initializer option `:trigger` modifies what edge this method will release on.


88
89
90
# File 'lib/pi_piper/pin.rb', line 88

def wait_for_change
  PiPiper.driver.pin_wait_for(@options[:pin])
end