Module: GPIO

Defined in:
lib/ruby-gpio.rb

Defined Under Namespace

Classes: Pin

Class Method Summary collapse

Class Method Details

.access(hash, &block) ⇒ Object

main entry for the DSL



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ruby-gpio.rb', line 74

def access(hash, &block)
  @pins = {} # a hash of available pins
  # set up pins
  hash.each do |k, v|
    @pins[k] = Pin.new(k.to_s, v)
    export v
  end
  begin
    instance_eval &block        
  ensure        
    # make sure all pins are unexported before we end the program
    hash.each do |k, v|
      unexport v
    end
  end
end

.export(gpio_num) ⇒ Object



98
99
100
# File 'lib/ruby-gpio.rb', line 98

def export(gpio_num)
  write "export", gpio_num
end

.method_missing(name, *args, &block) ⇒ Object

method missing used to return the pin named earlier in the hash parameter to access



92
93
94
95
96
# File 'lib/ruby-gpio.rb', line 92

def method_missing(name, *args, &block)
  if @pins.keys.include?(name)
    return @pins[name]
  end
end

.read(gpio_num) ⇒ Object



106
107
108
# File 'lib/ruby-gpio.rb', line 106

def read(gpio_num)
  File.read("/sys/class/gpio/gpio#{gpio_num}/value").to_i
end

.unexport(gpio_num) ⇒ Object



102
103
104
# File 'lib/ruby-gpio.rb', line 102

def unexport(gpio_num)
  write "unexport", gpio_num
end

.write(command, value) ⇒ Object



110
111
112
113
114
# File 'lib/ruby-gpio.rb', line 110

def write(command, value)
  File.open("/sys/class/gpio/#{command}", "w") do |f|
    f.write value
  end
end