class SunxiGPIO::Pin

Attributes

direction[R]
invert[R]
last_value[R]
pin[R]

Public Class Methods

close() click to toggle source

Cleanup GPIO interface to make it available for other programs

# File lib/sunxi_gpio/pin.rb, line 13
def self.close
  ::Gpio_lib.sunxi_gpio_cleanup
end
new(options) click to toggle source
# File lib/sunxi_gpio/pin.rb, line 23
def initialize(options)
  options = {
    direction: :in,
    invert: false,
    pull: :off
  }.merge(options)

  @pin = symbol_to_pin(options[:pin])
  @direction = options[:direction]
  @invert = options[:invert]
  @pull = options[:pull]
  
  raise "Invalid pull mode. Options are :up, :down or :float (default)" unless [:up, :down, :float, :off].include? @pull
  raise "Unable to use pull-ups : pin direction must be ':in' for this" if @direction != :in && [:up, :down].include?(@pull)
  raise "Invalid direction. Options are :in or :out" unless [:in, :out].include? @direction

  if @direction == :out
    ::Gpio_lib.sunxi_gpio_set_cfgpin(@pin, GPIO_DIRECTION_OUTPUT)
  else
    ::Gpio_lib.sunxi_gpio_set_cfgpin(@pin, GPIO_DIRECTION_INPUT)
  end
  
  pull!(@pull)
  read
end
open() click to toggle source
# File lib/sunxi_gpio/pin.rb, line 18
def self.open
  ::Gpio_lib.sunxi_gpio_init
end

Public Instance Methods

changed?() click to toggle source

Tests if the logic level has changed since the pin was last read.

# File lib/sunxi_gpio/pin.rb, line 107
def changed?
  last_value != value
end
off() click to toggle source

If the pin has been initialized for output this method will set the logic level low

# File lib/sunxi_gpio/pin.rb, line 58
def off
  ::Gpio_lib.sunxi_gpio_output(@pin, GPIO_LOW) if direction == :out
end
on() click to toggle source

If the pin has been initialized for output this method will set the logic level high.

# File lib/sunxi_gpio/pin.rb, line 52
def on
  ::Gpio_lib.sunxi_gpio_output(@pin, GPIO_HIGH) if direction == :out
end
pull!(state) click to toggle source
# File lib/sunxi_gpio/pin.rb, line 70
def pull!(state)
  return nil if @direction != :in
  
  @pull = case state
            when :up then
              GPIO_PUD_UP
            when :down then
              GPIO_PUD_DOWN
            # :float and :off are just aliases
            when :float, :off then
              GPIO_PUD_OFF
            else
              nil
          end
  
  ::Gpio_lib.sunxi_gpio_set_pull(@pin, @pull) if @pull
  @pull
end
pull?() click to toggle source

If the pin direction is input, it will return the current state of pull-up/pull-down resistor, either :up, :down or :off.

# File lib/sunxi_gpio/pin.rb, line 93
def pull?
  case @pull
    when GPIO_PUD_UP then
      :up
    when GPIO_PUD_DOWN then
      :down
    else
      :off
  end
end
read() click to toggle source
# File lib/sunxi_gpio/pin.rb, line 63
def read
  @last_value = @value
  val = ::Gpio_lib.sunxi_gpio_input(@pin)
  @value = invert ? (val ^ 1) : val
end
watch(watch_value, &block) click to toggle source

Watch the pin to change to the watch_value (ON or OFF) - it only triggered when switching from invert value to new value

# File lib/sunxi_gpio/pin.rb, line 115
def watch(watch_value, &block)
  new_thread = Thread.new do
    prev_value = (self.read == GPIO_HIGH ? GPIO_LOW : GPIO_HIGH)

    loop do
      current_value = self.read
      flip = (prev_value != current_value)

      if current_value == watch_value && flip
        self.instance_exec &block
      end

      prev_value = current_value

      sleep WATCH_POLLING_SEC
    end
  end

  new_thread.abort_on_exception = true
  new_thread
end

Private Instance Methods

symbol_to_pin(symbol) click to toggle source
# File lib/sunxi_gpio/pin.rb, line 140
def symbol_to_pin(symbol)
  PINS[symbol]
end