Class: PiFacer::FIO
- Inherits:
-
Object
- Object
- PiFacer::FIO
- Defined in:
- lib/pi_facer/io.rb
Overview
Represents a Piface io on the Raspberry Pi
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#invert ⇒ Object
readonly
Returns the value of attribute invert.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#last_value ⇒ Object
readonly
Returns the value of attribute last_value.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#changed? ⇒ Boolean
Tests if the logic level has changed since the io was last read.
-
#initialize(direction: :in, invert: true, trigger: :both, **options) ⇒ FIO
constructor
Initializes a new Piface io.
-
#off ⇒ Object
If the io 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 io has been initialized for output this method will set the logic level high.
-
#on? ⇒ Boolean
Tests if the logic level is high.
- #read ⇒ Object
-
#update_value(new_value) ⇒ Object
If the io has been initialized for output this method will either raise or lower the logic level depending on ‘new_value`.
-
#wait_for_change ⇒ Object
blocks until a logic level change occurs.
Constructor Details
#initialize(direction: :in, invert: true, trigger: :both, **options) ⇒ FIO
Initializes a new Piface io.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pi_facer/io.rb', line 13 def initialize(direction: :in, invert: true, trigger: :both, **) raise "Invalid direction. Options are :in or :out" unless [:in, :out].include? direction raise "Invalid IO, Piface IO numbers go from 1 to 8" unless (1..8).include? [:io] raise 'Invalid trigger' unless %I[rising falling both].include? trigger @io = [:io] @direction = direction @invert = invert @trigger = trigger read end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
4 5 6 |
# File 'lib/pi_facer/io.rb', line 4 def direction @direction end |
#invert ⇒ Object (readonly)
Returns the value of attribute invert.
4 5 6 |
# File 'lib/pi_facer/io.rb', line 4 def invert @invert end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
4 5 6 |
# File 'lib/pi_facer/io.rb', line 4 def io @io end |
#last_value ⇒ Object (readonly)
Returns the value of attribute last_value.
4 5 6 |
# File 'lib/pi_facer/io.rb', line 4 def last_value @last_value end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
4 5 6 |
# File 'lib/pi_facer/io.rb', line 4 def value @value end |
Instance Method Details
#changed? ⇒ Boolean
Tests if the logic level has changed since the io was last read.
53 54 55 56 |
# File 'lib/pi_facer/io.rb', line 53 def changed? read last_value != value end |
#off ⇒ Object
If the io has been initialized for output this method will set the logic level low.
37 38 39 |
# File 'lib/pi_facer/io.rb', line 37 def off Piface.write( @io, 0 ) if direction == :out end |
#off? ⇒ Boolean
Tests if the logic level is low.
42 43 44 |
# File 'lib/pi_facer/io.rb', line 42 def off? value == 0 end |
#on ⇒ Object
If the io has been initialized for output this method will set the logic level high.
27 28 29 |
# File 'lib/pi_facer/io.rb', line 27 def on Piface.write( @io, 1 ) if direction == :out end |
#on? ⇒ Boolean
Tests if the logic level is high.
32 33 34 |
# File 'lib/pi_facer/io.rb', line 32 def on? not off? end |
#read ⇒ Object
69 70 71 72 73 |
# File 'lib/pi_facer/io.rb', line 69 def read @last_value = @value val = Piface.read @io @value = invert ? (val ^ 1) : val end |
#update_value(new_value) ⇒ Object
If the io has been initialized for output this method will either raise or lower the logic level depending on ‘new_value`.
48 49 50 |
# File 'lib/pi_facer/io.rb', line 48 def update_value(new_value) !new_value || new_value == 0 ? off : on end |
#wait_for_change ⇒ Object
blocks until a logic level change occurs. The initializer option ‘:trigger` modifies what edge this method will release on.
59 60 61 62 63 64 65 66 67 |
# File 'lib/pi_facer/io.rb', line 59 def wait_for_change loop do if changed? next if @trigger == :rising and @value == 0 next if @trigger == :falling and @value == 1 break end end end |