class RubyPins::Pin

Attributes

host[RW]
name[RW]
pin[RW]
pinset[RW]
state[RW]

Public Class Methods

new(args) click to toggle source
# 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

Public Instance Methods

export() click to toggle source
# File lib/ruby_pins/pin.rb, line 64
def export
  "echo #{self.pin} > /sys/class/gpio/export"
end
exported?() click to toggle source
# File lib/ruby_pins/pin.rb, line 72
def exported?
  run("ls /sys/class/gpio/").include? "#{self.pin}"
end
off() click to toggle source
# File lib/ruby_pins/pin.rb, line 25
def off
  @state = :off
  run(unexport) if exported?
end
on() click to toggle source
# File lib/ruby_pins/pin.rb, line 20
def on
  @state = :on
  run export, set_out, turn_on
end
pin=(pin) click to toggle source
# File lib/ruby_pins/pin.rb, line 15
def pin= pin
  unexport if self.pin
  @pin = pin
end
run(*commands) click to toggle source
# 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() click to toggle source
# File lib/ruby_pins/pin.rb, line 52
def send_in
  set_direction 'in'
end
set_direction(dir) click to toggle source
# File lib/ruby_pins/pin.rb, line 56
def set_direction dir
  "echo #{dir} > /sys/class/gpio/gpio#{self.pin}/direction"
end
set_out() click to toggle source
# File lib/ruby_pins/pin.rb, line 48
def set_out
  set_direction 'out'
end
set_value(val) click to toggle source
# File lib/ruby_pins/pin.rb, line 60
def set_value val
  "echo #{val} > /sys/class/gpio/gpio#{self.pin}/value"
end
state=(state) click to toggle source
# File lib/ruby_pins/pin.rb, line 11
def state= state
  self.send state
end
turn_on() click to toggle source
# File lib/ruby_pins/pin.rb, line 44
def turn_on
  set_value 1
end
unexport() click to toggle source
# File lib/ruby_pins/pin.rb, line 68
def unexport
  "echo #{self.pin} > /sys/class/gpio/unexport"
end