denko-rb / lgpio

Ruby C extension for Linux GPIO
MIT License
7 stars 0 forks source link

lgpio

Ruby gem with bindings for the lgpio (lg) C library. This is for single-board-computers (SBCs) running Linux, such as Orange Pi, Raspberry Pi, etc. It provides low-level access to the GPIO, I2C, SPI, and PWM subsytems.

Standard LGPIO Features

Extra Features (Built on LGPIO)

Hardware PWM Features

These use the sysfs PWM interface, not lgpio C, but are a good fit for this gem.

Note: Once a pin is bound to hardware PWM in the device tree, it shouldn't be used as regular GPIO. Behavior is inconsistent across different hardware.

Installation

On Debian-based Linuxes (RaspberryPi OS, Armbian, DietPi etc.):

# Requirements to install lgpio C
sudo apt install swig python3-dev python3-setuptools gcc make

# Temporary fork of: wget https://github.com/joan2937/lg/archive/master.zip
wget https://github.com/vickash/lg/archive/refs/heads/master.zip

# Install lgpio C
unzip master.zip
cd lg-master
make
sudo make install

# The latest Ruby 3 + YJIT is recommended, but you can use the system Ruby from apt too.
# sudo apt install ruby ruby-dev

gem install lgpio

Enabling Hardware & Permissions

Depending on your SBC and Linux distro/version, you may need to manually enable hardware I2C, SPI, and PWM. You should use the config tool that came with your distro for that, if possible.

Even when these are enabled, you may not have permission to access them. To run without sudo, you need read+write permission to some or all of the following:

/dev/gpiochip*          (For GPIO, example: /dev/gpiochip0)
/dev/i2c-*              (For I2C,  example: /dev/i2c-1)
/dev/spidev*            (For SPI,  example: /dev/spidev0.1)
/sys/class/pwm/pwmchip* (For PWM,  example: /sys/class/pwm/pwmchip0)

Documentation