class Omega2Gpio::Gpio

Attributes

direction[R]
gpio_number[R]
value[R]

Public Class Methods

is_mock?() click to toggle source
# File lib/omega2_gpio/gpio.rb, line 24
def self.is_mock?
  Omega2Gpio.configuration.mock
end
new(gpio_number, direction) click to toggle source
# File lib/omega2_gpio/gpio.rb, line 8
def initialize(gpio_number, direction)
  @gpio_number = gpio_number
  @value = 0
  @direction = direction

  command = "fast-gpio set-#{direction} #{self.gpio_number}"
  if Omega2Gpio.configuration.mock && gpio_number.between?(0, Omega2Gpio.configuration.highest_gpio_number)
    Omega2Gpio.messenger.debug "Init Fake-GPIO#{@gpio_number} and mock all interactions as valid"
    mock_fast_gpio_command command
  else
    execute_fast_gpio_command command
  end

  self
end

Public Instance Methods

get() click to toggle source
# File lib/omega2_gpio/gpio.rb, line 40
def get
  read
end
high?() click to toggle source
# File lib/omega2_gpio/gpio.rb, line 48
def high?
  is_high?
end
is_high?() click to toggle source
# File lib/omega2_gpio/gpio.rb, line 44
def is_high?
  read == 1
end
is_low?() click to toggle source
# File lib/omega2_gpio/gpio.rb, line 52
def is_low?
  read == 0
end
low?() click to toggle source
# File lib/omega2_gpio/gpio.rb, line 56
def low?
  is_low?
end
read() click to toggle source
# File lib/omega2_gpio/gpio.rb, line 28
def read
  command = "fast-gpio read #{@gpio_number}"
  if Omega2Gpio.configuration.mock
    mock_fast_gpio_command command
  else
    stdout = execute_fast_gpio_command command
    @value = stdout.gets.to_s.split.last.to_i
  end

  @value
end

Private Instance Methods

execute_fast_gpio_command(command) click to toggle source
# File lib/omega2_gpio/gpio.rb, line 62
def execute_fast_gpio_command(command)
  stdin, stdout, stderr = Open3.popen3(command)
  if stderr.gets
    Omega2Gpio.raise_error(stderr.gets)
  end
  Omega2Gpio.messenger.debug "System command: #{command}"
  Omega2Gpio.messenger.debug "  returns: #{stdout}"
  stdout
end
mock_fast_gpio_command(command) click to toggle source
# File lib/omega2_gpio/gpio.rb, line 72
def mock_fast_gpio_command(command)
  Omega2Gpio.messenger.debug "Mock command: #{command}"
end