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
# 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

#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



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

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

#exported?Boolean

Returns:

  • (Boolean)


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

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

#offObject



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

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

#onObject



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_inObject



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_outObject



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_onObject



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

def turn_on
  set_value 1
end

#unexportObject



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

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