class MiniGPIO

Constants

GPCLR0
GPCLR1
GPLEV0
GPSET0
GPSET1

Attributes

ptr[R]

Public Class Methods

new() click to toggle source
# File lib/mini_gpio.rb, line 38
def initialize
  @ptr = File.open("/dev/gpiomem", File::Constants::RDWR | File::Constants::SYNC) do |f|
    Mmap.mmap(nil, 4*1024, Mmap::PROT_READ|Mmap::PROT_WRITE, Mmap::MAP_SHARED, f.to_i, 0)
  end
end

Public Instance Methods

mode(pin) click to toggle source

Get the pin mode

# File lib/mini_gpio.rb, line 45
def mode pin
  reg = pin / 10
  shift = (pin % 10) * 3
  (get_int_at(reg) >> shift) & 7
end
read(pin) click to toggle source

Read the pin

# File lib/mini_gpio.rb, line 60
def read pin
  0 != get_int_at(GPLEV0 + PI_BANK(pin)) & PI_BIT(pin) ? 1 : 0
end
set_mode(pin, mode) click to toggle source

Set the pin mode

# File lib/mini_gpio.rb, line 52
def set_mode pin, mode
  reg = pin / 10
  shift = (pin % 10) * 3
  new_value = (get_int_at(reg) & ~(7 << shift)) | (mode << shift)
  set_int_at(reg, new_value)
end
write(pin, value) click to toggle source

Write to the pin

# File lib/mini_gpio.rb, line 65
def write pin, value
  if value == 0
    set_int_at(GPCLR0 + PI_BANK(pin), PI_BIT(pin))
  else
    set_int_at(GPSET0 + PI_BANK(pin), PI_BIT(pin))
  end
end

Private Instance Methods

PI_BANK(gpio ;) click to toggle source
# File lib/mini_gpio.rb, line 75
def PI_BANK gpio ; gpio >> 5; end
PI_BIT(gpio ;) click to toggle source
# File lib/mini_gpio.rb, line 76
def PI_BIT  gpio ; (1 << (gpio&0x1F)); end
get_int_at(offset) click to toggle source
# File lib/mini_gpio.rb, line 78
def get_int_at offset
  @ptr[offset * Fiddle::SIZEOF_INT, Fiddle::SIZEOF_INT].unpack1("L")
end
set_int_at(offset, value) click to toggle source
# File lib/mini_gpio.rb, line 82
def set_int_at offset, value
  @ptr[offset * Fiddle::SIZEOF_INT, Fiddle::SIZEOF_INT] = [value].pack("L")
end