Class: PiDriver::Pin
- Inherits:
-
Object
- Object
- PiDriver::Pin
- Defined in:
- lib/pi_driver/pin.rb
Instance Attribute Summary collapse
-
#gpio_number ⇒ Object
readonly
Returns the value of attribute gpio_number.
Instance Method Summary collapse
- #clear ⇒ Object
- #clear? ⇒ Boolean
- #clear_interrupt ⇒ Object
-
#initialize(gpio_number, options = {}) ⇒ Pin
constructor
A new instance of Pin.
- #input ⇒ Object
- #input? ⇒ Boolean
- #interrupt(edge = Edge::RISING) ⇒ Object
- #output(value = Value::LOW) ⇒ Object
- #output? ⇒ Boolean
- #set ⇒ Object
- #set? ⇒ Boolean
- #value ⇒ Object
Constructor Details
#initialize(gpio_number, options = {}) ⇒ Pin
Returns a new instance of Pin.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pi_driver/pin.rb', line 12 def initialize(gpio_number, = {}) @gpio_number = gpio_number @argument_helper = ArgumentHelper.new @gpio_number @argument_helper.check(:gpio_number, gpio_number, Board::VALID_NUMBERS) @direction = [:direction] || Direction::INPUT @value = [:value] || Value::LOW @file_helper = FileHelper.new @gpio_number @file_helper.write_export @argument_helper.check(:direction, @direction, Direction::VALID_DIRECTIONS) @file_helper.write_direction(@direction) @argument_helper.check(:value, @value, Value::VALID_VALUES) input? ? @file_helper.read_value : @file_helper.write_value(@value) end |
Instance Attribute Details
#gpio_number ⇒ Object (readonly)
Returns the value of attribute gpio_number.
10 11 12 |
# File 'lib/pi_driver/pin.rb', line 10 def gpio_number @gpio_number end |
Instance Method Details
#clear ⇒ Object
53 54 55 56 57 58 |
# File 'lib/pi_driver/pin.rb', line 53 def clear return unless output? @value = Value::LOW @file_helper.write_value(@value) @value end |
#clear? ⇒ Boolean
60 61 62 |
# File 'lib/pi_driver/pin.rb', line 60 def clear? value == Value::LOW end |
#clear_interrupt ⇒ Object
93 94 95 |
# File 'lib/pi_driver/pin.rb', line 93 def clear_interrupt @interrupt_thread.kill if @interrupt_thread end |
#input ⇒ Object
32 33 34 35 |
# File 'lib/pi_driver/pin.rb', line 32 def input @direction = Direction::INPUT @file_helper.write_direction(@direction) end |
#input? ⇒ Boolean
37 38 39 |
# File 'lib/pi_driver/pin.rb', line 37 def input? @direction == Direction::INPUT end |
#interrupt(edge = Edge::RISING) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/pi_driver/pin.rb', line 79 def interrupt(edge = Edge::RISING) @argument_helper.check(:edge, edge, Edge::VALID_EDGES) @edge = edge @interrupt_thread = Thread.new do last_value = @file_helper.read_value loop do new_value = @file_helper.read_value yield if block_given? && interrupted?(new_value, last_value) last_value = new_value end end end |
#output(value = Value::LOW) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/pi_driver/pin.rb', line 41 def output(value = Value::LOW) @argument_helper.check(:value, value, Value::VALID_VALUES) @value = value @direction = Direction::OUTPUT @file_helper.write_direction(@direction) @file_helper.write_value(@value) end |
#output? ⇒ Boolean
49 50 51 |
# File 'lib/pi_driver/pin.rb', line 49 def output? @direction == Direction::OUTPUT end |
#set ⇒ Object
64 65 66 67 68 69 |
# File 'lib/pi_driver/pin.rb', line 64 def set return unless output? @value = Value::HIGH @file_helper.write_value(@value) @value end |
#set? ⇒ Boolean
71 72 73 |
# File 'lib/pi_driver/pin.rb', line 71 def set? value == Value::HIGH end |
#value ⇒ Object
75 76 77 |
# File 'lib/pi_driver/pin.rb', line 75 def value input? ? @file_helper.read_value : @value end |