Class: PiPiper::Pin

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

Overview

Represents a GPIO pin on the Raspberry Pi

Constant Summary

Constants included from PinValues

PiPiper::PinValues::GPIO_FSEL_ALT0, PiPiper::PinValues::GPIO_FSEL_ALT1, PiPiper::PinValues::GPIO_FSEL_ALT2, PiPiper::PinValues::GPIO_FSEL_ALT3, PiPiper::PinValues::GPIO_FSEL_ALT4, PiPiper::PinValues::GPIO_FSEL_ALT5, PiPiper::PinValues::GPIO_FSEL_INPT, PiPiper::PinValues::GPIO_FSEL_MASK, PiPiper::PinValues::GPIO_FSEL_OUTP, PiPiper::PinValues::GPIO_HIGH, PiPiper::PinValues::GPIO_LOW, PiPiper::PinValues::GPIO_PUD_DOWN, PiPiper::PinValues::GPIO_PUD_OFF, PiPiper::PinValues::GPIO_PUD_UP, PiPiper::PinValues::PWM_MODE, PiPiper::PinValues::PWM_PIN

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 :offing. 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)

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pi_piper/pin.rb', line 29

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

  @pin       = options[:pin]
  @direction = options[:direction]
  @invert    = options[:invert]
  @trigger   = options[:trigger]
  @pull      = options[:pull]

  raise ArgumentError, 'Pin # required' unless @pin
  unless valid_pull?
    raise PiPiper::PinError, 'Invalid pull mode. Options are :up, :down or :float (default)'
  end
  unless valid_direction?
    raise PiPiper::PinError, 'Invalid direction. Options are :in or :out'
  end
  if @direction != :in && [:up, :down].include?(@pull)
    raise PiPiper::PinError, 'Unable to use pull-ups : pin direction must be :in for this'
  end
  unless valid_trigger?
    raise PiPiper::PinError, 'Invalid trigger. Options are :rising, :falling, or :both'
  end

  if @direction == :out
    Platform.driver.pin_output(@pin)
  else
    Platform.driver.pin_input(@pin)
  end
  pull!(@pull)

  read
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


157
158
159
# File 'lib/pi_piper/pin.rb', line 157

def method_missing(method, *args, &block)
    Platform.driver.send(method, @pin, *args, &block)
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction


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

def direction
  @direction
end

#invertObject (readonly)

Returns the value of attribute invert


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

def invert
  @invert
end

#last_valueObject (readonly)

Returns the value of attribute last_value


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

def last_value
  @last_value
end

#pinObject (readonly)

Returns the value of attribute pin


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

def pin
  @pin
end

Instance Method Details

#changed?Boolean

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

Returns:

  • (Boolean)

135
136
137
# File 'lib/pi_piper/pin.rb', line 135

def changed?
  last_value != value
end

#offObject

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


78
79
80
# File 'lib/pi_piper/pin.rb', line 78

def off
  Platform.driver.pin_set(pin, GPIO_LOW) if direction == :out
end

#off?Boolean

Tests if the logic level is low.

Returns:

  • (Boolean)

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

def off?
  value == GPIO_LOW
end

#onObject

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


67
68
69
# File 'lib/pi_piper/pin.rb', line 67

def on
  Platform.driver.pin_set(pin, GPIO_HIGH) if direction == :out
end

#on?Boolean

Tests if the logic level is high.

Returns:

  • (Boolean)

72
73
74
# File 'lib/pi_piper/pin.rb', line 72

def on?
  not off?
end

#pull!(state) ⇒ Object

When the pin has been initialized in input mode, internal resistors can be pulled up or down (respectively with :up and :down).

Pulling an input pin will prevent noise from triggering it when the input is floating.

For instance when nothing is plugged in, pulling the pin-up will make subsequent value readings to return 'on' (or high, or 1…). pin direction is set to :in. Either :up, :down or :offing. Defaults to :off.

Parameters:

  • state (Symbol)

    Indicates if and how pull mode must be set when

Raises:


109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pi_piper/pin.rb', line 109

def pull!(state)
 raise PiPiper::PinError, "Unable to use pull-ups : pin direction must be ':in' for this" if 
    @direction != :in and [:up, :down].include?(state)
  @pull = case state
          when :up then GPIO_PUD_UP
          when :down then GPIO_PUD_DOWN
          # :float and :off are just aliases
          when :float, :off then GPIO_PUD_OFF
          else nil
          end

  Platform.driver.pin_set_pud(@pin, @pull) if @pull
  @pull
end

#pull?Boolean

If the pin direction is input, it will return the current state of pull-up/pull-down resistor, either :up, :down or :off.

Returns:

  • (Boolean)

126
127
128
129
130
131
132
# File 'lib/pi_piper/pin.rb', line 126

def pull?
  case @pull
  when GPIO_PUD_UP then :up
  when GPIO_PUD_DOWN then :down
  else :off
  end
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.


150
151
152
153
154
# File 'lib/pi_piper/pin.rb', line 150

def read
  val = Platform.driver.pin_read(@pin)
  @last_value = @value
  @value = 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.


94
95
96
# File 'lib/pi_piper/pin.rb', line 94

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

#valueObject


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

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.


141
142
143
# File 'lib/pi_piper/pin.rb', line 141

def wait_for_change
  Platform.driver.pin_wait_for(@pin, @trigger)
end