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 (also: #off)
- #clear? ⇒ Boolean (also: #off?)
- #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 (also: #on)
- #set? ⇒ Boolean (also: #on?)
- #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 |
# File 'lib/pi_driver/pin.rb', line 12 def initialize(gpio_number, = {}) @argument_helper = ArgumentHelper.new gpio_number @gpio_number = gpio_number @argument_helper.check(:gpio_number, @gpio_number, Board::VALID_NUMBERS) @direction = [:direction] || Direction::INPUT @argument_helper.check(:direction, @direction, Direction::VALID_DIRECTIONS) @value = [:value] || Value::LOW @argument_helper.check(:value, @value, Value::VALID_VALUES) @file_helper = FileHelper.new @gpio_number @file_helper.write_export @file_helper.write_direction(@direction) 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 Also known as: off
51 52 53 54 55 56 |
# File 'lib/pi_driver/pin.rb', line 51 def clear return unless output? @value = Value::LOW @file_helper.write_value(@value) @value end |
#clear? ⇒ Boolean Also known as: off?
60 61 62 |
# File 'lib/pi_driver/pin.rb', line 60 def clear? value == Value::LOW end |
#clear_interrupt ⇒ Object
99 100 101 |
# File 'lib/pi_driver/pin.rb', line 99 def clear_interrupt @interrupt_thread.kill if @interrupt_thread end |
#input ⇒ Object
30 31 32 33 |
# File 'lib/pi_driver/pin.rb', line 30 def input @direction = Direction::INPUT @file_helper.write_direction(@direction) end |
#input? ⇒ Boolean
35 36 37 |
# File 'lib/pi_driver/pin.rb', line 35 def input? @direction == Direction::INPUT end |
#interrupt(edge = Edge::RISING) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/pi_driver/pin.rb', line 85 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
39 40 41 42 43 44 45 |
# File 'lib/pi_driver/pin.rb', line 39 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
47 48 49 |
# File 'lib/pi_driver/pin.rb', line 47 def output? @direction == Direction::OUTPUT end |
#set ⇒ Object Also known as: on
66 67 68 69 70 71 |
# File 'lib/pi_driver/pin.rb', line 66 def set return unless output? @value = Value::HIGH @file_helper.write_value(@value) @value end |
#set? ⇒ Boolean Also known as: on?
75 76 77 |
# File 'lib/pi_driver/pin.rb', line 75 def set? value == Value::HIGH end |
#value ⇒ Object
81 82 83 |
# File 'lib/pi_driver/pin.rb', line 81 def value input? ? @file_helper.read_value : @value end |