Class: PiPiper::Pin
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
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#invert ⇒ Object
readonly
Returns the value of attribute invert.
-
#last_value ⇒ Object
readonly
Returns the value of attribute last_value.
-
#pin ⇒ Object
readonly
Returns the value of attribute pin.
Instance Method Summary collapse
-
#changed? ⇒ Boolean
Tests if the logic level has changed since the pin was last read.
-
#initialize(options) ⇒ Pin
constructor
Initializes a new GPIO pin.
-
#off ⇒ Object
If the pin has been initialized for output this method will set the logic level low.
-
#off? ⇒ Boolean
Tests if the logic level is low.
-
#on ⇒ Object
If the pin has been initialized for output this method will set the logic level high.
-
#on? ⇒ Boolean
Tests if the logic level is high.
-
#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).
-
#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.
-
#read ⇒ Object
Reads the current value from the pin.
-
#update_value(new_value) ⇒ Object
(also: #value=)
If the pin has been initialized for output this method will either raise or lower the logic level depending on
new_value. - #value ⇒ Object
-
#wait_for_change ⇒ Object
Blocks until a logic level change occurs.
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.
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() = { :direction => :in, :invert => false, :trigger => :both, :pull => :off }.merge() @pin = [:pin] @direction = [:direction] @invert = [:invert] @trigger = [:trigger] @pull = [: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
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
8 9 10 |
# File 'lib/pi_piper/pin.rb', line 8 def direction @direction end |
#invert ⇒ Object (readonly)
Returns the value of attribute invert.
8 9 10 |
# File 'lib/pi_piper/pin.rb', line 8 def invert @invert end |
#last_value ⇒ Object (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 |
#pin ⇒ Object (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.
135 136 137 |
# File 'lib/pi_piper/pin.rb', line 135 def changed? last_value != value end |
#off ⇒ Object
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.
83 84 85 |
# File 'lib/pi_piper/pin.rb', line 83 def off? value == GPIO_LOW end |
#on ⇒ Object
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.
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.
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.
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 |
#read ⇒ Object
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.
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 |
#value ⇒ Object
87 88 89 |
# File 'lib/pi_piper/pin.rb', line 87 def value @value ||= read end |