Class: RubyPins::Pin

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_pins/pin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Pin

Returns a new instance of Pin.



6
7
8
9
10
# 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
  self.host ||= :local
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/ruby_pins/pin.rb', line 4

def host
  @host
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/ruby_pins/pin.rb', line 4

def name
  @name
end

#pinObject

Returns the value of attribute pin.



4
5
6
# File 'lib/ruby_pins/pin.rb', line 4

def pin
  @pin
end

#pinsetObject

Returns the value of attribute pinset.



4
5
6
# File 'lib/ruby_pins/pin.rb', line 4

def pinset
  @pinset
end

#stateObject

Returns the value of attribute state.



4
5
6
# File 'lib/ruby_pins/pin.rb', line 4

def state
  @state
end

Instance Method Details

#exportObject



65
66
67
# File 'lib/ruby_pins/pin.rb', line 65

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

#exported?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/ruby_pins/pin.rb', line 73

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

#offObject



26
27
28
29
# File 'lib/ruby_pins/pin.rb', line 26

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

#onObject



21
22
23
24
# File 'lib/ruby_pins/pin.rb', line 21

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

#run(*commands) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_pins/pin.rb', line 31

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

#send_inObject



53
54
55
# File 'lib/ruby_pins/pin.rb', line 53

def send_in
  set_direction 'in'
end

#set_direction(dir) ⇒ Object



57
58
59
# File 'lib/ruby_pins/pin.rb', line 57

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

#set_outObject



49
50
51
# File 'lib/ruby_pins/pin.rb', line 49

def set_out
  set_direction 'out'
end

#set_value(val) ⇒ Object



61
62
63
# File 'lib/ruby_pins/pin.rb', line 61

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

#turn_onObject



45
46
47
# File 'lib/ruby_pins/pin.rb', line 45

def turn_on
  set_value 1
end

#unexportObject



69
70
71
# File 'lib/ruby_pins/pin.rb', line 69

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