Sha256: a42b577d683b0ba50c861a19419875411c8e565c53af6bdee62e55e8346e9ac3

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module RubyPins

  class Pin
    attr_accessor :pin, :name, :state, :pinset, :host

    def initialize args
      args.each {|k, v| self.send "#{k}=", v}
      self.state= :off unless self.state
      self.host ||= :local
    end

    def state= state
      self.send state
    end

    def pin= pin
      unexport if self.pin
      @pin = pin
    end

    def on
      @state = :on
      run export, set_out, turn_on
    end

    def off
      @state = :off
      run(unexport) if exported?
    end

    def run *commands
      std_out = ''
      if host == :local
        commands.each {|cmd| std_out << %x(#{cmd})}
      else
        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
      end
      std_out
    end

    def turn_on
      set_value 1
    end

    def set_out
      set_direction 'out'
    end

    def send_in
      set_direction 'in'
    end

    def set_direction dir
      "echo #{dir} > /sys/class/gpio/gpio#{self.pin}/direction"
    end

    def set_value val
      "echo #{val} > /sys/class/gpio/gpio#{self.pin}/value"
    end

    def export
      "echo #{self.pin} > /sys/class/gpio/export"
    end

    def unexport
      "echo #{self.pin} > /sys/class/gpio/unexport"
    end

    def exported?
      run("ls /sys/class/gpio/").include? "#{self.pin}"
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby_pins-0.1.2 lib/ruby_pins/pin.rb