Class: PiDriver::Pin
- Inherits:
-
Object
show all
- Defined in:
- lib/pi_driver/pin.rb,
lib/pi_driver/pin/board.rb,
lib/pi_driver/pin/direction.rb,
lib/pi_driver/pin/file_helper.rb,
lib/pi_driver/pin/directory_helper.rb
Defined Under Namespace
Classes: Board, Direction, DirectoryHelper, FileHelper
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(gpio_number, options = {}) ⇒ Pin
Returns a new instance of Pin.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/pi_driver/pin.rb', line 9
def initialize(gpio_number, options = {})
@argument_helper = Utils::ArgumentHelper.new prefix: 'PiDriver::Pin'
@gpio_number = gpio_number
@argument_helper.check(:gpio_number, @gpio_number, Board::VALID_NUMBERS)
@argument_helper.prefix = "PiDriver::Pin ##{gpio_number}"
@direction = options[:direction] || Direction::INPUT
@argument_helper.check(:direction, @direction, Direction::VALID_DIRECTIONS)
@state = options[:state] || Utils::State::LOW
@argument_helper.check(:state, @state, Utils::State::VALID_STATES)
@file_helper = FileHelper.new @gpio_number
@file_helper.write_export
@file_helper.write_direction(@direction)
input? ? @file_helper.read_value : @file_helper.write_value(@state)
end
|
Instance Attribute Details
#gpio_number ⇒ Object
Returns the value of attribute gpio_number.
7
8
9
|
# File 'lib/pi_driver/pin.rb', line 7
def gpio_number
@gpio_number
end
|
Instance Method Details
#clear ⇒ Object
Also known as:
off
50
51
52
53
54
55
|
# File 'lib/pi_driver/pin.rb', line 50
def clear
return unless output?
@state = Utils::State::LOW
@file_helper.write_value(@state)
@state
end
|
#clear? ⇒ Boolean
Also known as:
off?
59
60
61
|
# File 'lib/pi_driver/pin.rb', line 59
def clear?
state == Utils::State::LOW
end
|
#clear_interrupt ⇒ Object
91
92
93
|
# File 'lib/pi_driver/pin.rb', line 91
def clear_interrupt
@interrupt&.clear
end
|
29
30
31
32
|
# File 'lib/pi_driver/pin.rb', line 29
def input
@direction = Direction::INPUT
@file_helper.write_direction(@direction)
end
|
34
35
36
|
# File 'lib/pi_driver/pin.rb', line 34
def input?
@direction == Direction::INPUT
end
|
#interrupt(edge = Utils::Edge::RISING) ⇒ Object
84
85
86
87
88
89
|
# File 'lib/pi_driver/pin.rb', line 84
def interrupt(edge = Utils::Edge::RISING)
@argument_helper.check(:edge, edge, Utils::Edge::VALID_EDGES)
@edge = edge
@interrupt = Utils::Interrupt.new(@edge) { @file_helper.read_value }
@interrupt.start { yield }
end
|
#output(state = Utils::State::LOW) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/pi_driver/pin.rb', line 38
def output(state = Utils::State::LOW)
@argument_helper.check(:state, state, Utils::State::VALID_STATES)
@state = state
@direction = Direction::OUTPUT
@file_helper.write_direction(@direction)
@file_helper.write_value(@state)
end
|
#output? ⇒ Boolean
46
47
48
|
# File 'lib/pi_driver/pin.rb', line 46
def output?
@direction == Direction::OUTPUT
end
|
#set ⇒ Object
Also known as:
on
65
66
67
68
69
70
|
# File 'lib/pi_driver/pin.rb', line 65
def set
return unless output?
@state = Utils::State::HIGH
@file_helper.write_value(@state)
@state
end
|
#set? ⇒ Boolean
Also known as:
on?
74
75
76
|
# File 'lib/pi_driver/pin.rb', line 74
def set?
state == Utils::State::HIGH
end
|
#state ⇒ Object
80
81
82
|
# File 'lib/pi_driver/pin.rb', line 80
def state
input? ? @file_helper.read_value : @state
end
|