class Taopaipai::IO

Constants

MAX_WRITE_ATTEMPTS

Public Class Methods

new(base_path) click to toggle source
# File lib/taopaipai/io.rb, line 5
def initialize(base_path)
  @base_path = base_path
end

Public Instance Methods

direction(pin, direction) click to toggle source
# File lib/taopaipai/io.rb, line 17
def direction(pin, direction)
  write_to_file("gpio#{pin}/direction", direction)
end
export(pin) click to toggle source
# File lib/taopaipai/io.rb, line 9
def export(pin)
  write_to_file('export', pin.to_s)
end
read(pin) click to toggle source
# File lib/taopaipai/io.rb, line 21
def read(pin)
  File.open(relative("gpio#{pin}/value"), 'r'){|f| f.read }
end
unexport(pin) click to toggle source
# File lib/taopaipai/io.rb, line 13
def unexport(pin)
  write_to_file('unexport', pin.to_s)
end
write(pin, value) click to toggle source
# File lib/taopaipai/io.rb, line 25
def write(pin, value)
  write_to_file("gpio#{pin}/value", value)
end

Private Instance Methods

relative(path) click to toggle source
# File lib/taopaipai/io.rb, line 44
def relative(path)
  "#{@base_path}/#{path}"
end
write_to_file(path, content, attempts = 1) click to toggle source
# File lib/taopaipai/io.rb, line 30
def write_to_file(path, content, attempts = 1)
  begin
    File.open(relative(path), 'w'){|f| f.write(content) }
  rescue => e
    if attempts <= MAX_WRITE_ATTEMPTS
      # Wait 100 ms before retry
      sleep 0.1
      write_to_file(path, content, attempts + 1)
    else
      raise e
    end
  end
end