Class: PiPiper::Pin
- Inherits:
-
Object
- Object
- PiPiper::Pin
- 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
-
#last_value ⇒ Object
readonly
Returns the value of attribute last_value.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#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.
-
#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 :off. Defaults to :off.
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 = {:direction => :in, :invert => false, :trigger => :none, :pull => :off, }.merge() 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
Instance Attribute Details
#last_value ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/pi_piper/pin.rb', line 7 def @options end |
#pin ⇒ Object (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.
82 83 84 |
# File 'lib/pi_piper/pin.rb', line 82 def changed? last_value != value end |
#off ⇒ Object
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.
65 66 67 |
# File 'lib/pi_piper/pin.rb', line 65 def off? value == GPIO_LOW end |
#on ⇒ Object
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.
54 55 56 |
# File 'lib/pi_piper/pin.rb', line 54 def on? not off? 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.
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`.
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 |
#value ⇒ Object
69 70 71 |
# File 'lib/pi_piper/pin.rb', line 69 def value @value ||= read end |