Class: RubyPins::Pin
- Inherits:
-
Object
- Object
- RubyPins::Pin
- Defined in:
- lib/ruby_pins/pin.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pin ⇒ Object
Returns the value of attribute pin.
-
#pinset ⇒ Object
Returns the value of attribute pinset.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #export ⇒ Object
- #exported? ⇒ Boolean
-
#initialize(args) ⇒ Pin
constructor
A new instance of Pin.
- #off ⇒ Object
- #on ⇒ Object
- #run(*commands) ⇒ Object
- #send_in ⇒ Object
- #set_direction(dir) ⇒ Object
- #set_out ⇒ Object
- #set_value(val) ⇒ Object
- #turn_on ⇒ Object
- #unexport ⇒ Object
Constructor Details
#initialize(args) ⇒ Pin
Returns a new instance of Pin.
6 7 8 9 |
# File 'lib/ruby_pins/pin.rb', line 6 def initialize args args.each {|k, v| self.send "#{k}=", v} self.state= :off unless self.state end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
4 5 6 |
# File 'lib/ruby_pins/pin.rb', line 4 def host @host end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/ruby_pins/pin.rb', line 4 def name @name end |
#pin ⇒ Object
Returns the value of attribute pin.
4 5 6 |
# File 'lib/ruby_pins/pin.rb', line 4 def pin @pin end |
#pinset ⇒ Object
Returns the value of attribute pinset.
4 5 6 |
# File 'lib/ruby_pins/pin.rb', line 4 def pinset @pinset end |
#state ⇒ Object
Returns the value of attribute state.
4 5 6 |
# File 'lib/ruby_pins/pin.rb', line 4 def state @state end |
Instance Method Details
#export ⇒ Object
64 65 66 |
# File 'lib/ruby_pins/pin.rb', line 64 def export "echo #{self.pin} > /sys/class/gpio/export" end |
#exported? ⇒ Boolean
72 73 74 |
# File 'lib/ruby_pins/pin.rb', line 72 def exported? run("ls /sys/class/gpio/").include? "#{self.pin}" end |
#off ⇒ Object
25 26 27 28 |
# File 'lib/ruby_pins/pin.rb', line 25 def off @state = :off run(unexport) if exported? end |
#on ⇒ Object
20 21 22 23 |
# File 'lib/ruby_pins/pin.rb', line 20 def on @state = :on run export, set_out, turn_on end |
#run(*commands) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ruby_pins/pin.rb', line 30 def run *commands std_out = '' if self.host commands.each do |cmd| Net::SSH.start(self.host.address, self.host.user, password: self.host.password) do |ssh| std_out << ssh.exec!(cmd) end end else commands.each {|cmd| std_out << %x(#{cmd})} end std_out end |
#send_in ⇒ Object
52 53 54 |
# File 'lib/ruby_pins/pin.rb', line 52 def send_in set_direction 'in' end |
#set_direction(dir) ⇒ Object
56 57 58 |
# File 'lib/ruby_pins/pin.rb', line 56 def set_direction dir "echo #{dir} > /sys/class/gpio/gpio#{self.pin}/direction" end |
#set_out ⇒ Object
48 49 50 |
# File 'lib/ruby_pins/pin.rb', line 48 def set_out set_direction 'out' end |
#set_value(val) ⇒ Object
60 61 62 |
# File 'lib/ruby_pins/pin.rb', line 60 def set_value val "echo #{val} > /sys/class/gpio/gpio#{self.pin}/value" end |
#turn_on ⇒ Object
44 45 46 |
# File 'lib/ruby_pins/pin.rb', line 44 def turn_on set_value 1 end |
#unexport ⇒ Object
68 69 70 |
# File 'lib/ruby_pins/pin.rb', line 68 def unexport "echo #{self.pin} > /sys/class/gpio/unexport" end |