Manual browser: gpio.attach(3lua)

Section:
Page:
GPIO(3lua) 3lua GPIO(3lua)

NAME

gpioaccess gpio(4) pins from Lua

SYNOPSIS

local gpio = require 'gpio'

gpiodev = gpio.open(path)
pins = gpio.info(gpiodev)
gpio.close(gpiodev)
gpio.set(gpiodev, pin, flags)
gpio.unset(gpiodev, pin)
state = gpio.read(gpiodev, pin)
oldstate = gpio.write(gpiodev, pin, state)
gpio.toggle(gpiodev, pin)
gpio.attach(gpiodev, driver, offset, mask [, flags])

DESCRIPTION

The gpio Lua binding provides access to a gpio(4) device using the ioctl(2) interface.

gpiodev = gpio.open(path)
Open the gpio device and return an object to access its pins.

pins = gpio.info(gpiodev)
Returns the number of pins. As with all remaining functions, this can also be called using the : notation, i.e. as gpiodev:info().

gpio.close(gpiodev)
Close the gpio device.

gpio.set(gpiodev, pin, flags)
Set gpio pin flags. Note that the pin number in this and all remaining functions is zero based and not one based, this to avoid confusion with tools like gpioctl(8) which also number pins starting at zero. The following flags are defined:

gpio.PIN_INPUT
Pin is an input.

gpio.PIN_OUTPUT
Pin is an output.

gpio.PIN_INOUT
Pin is birectional.

gpio.PIN_OPENDRAIN
Pin is an open-drain output.

gpio.PIN_PUSHPULL
Pin is a push-pull output.

gpio.PIN_TRISTATE
Pin is tri-state (output disabled).

gpio.PIN_PULLUP
Pin has an internal pull-up enabled.

gpio.PIN_PULLDOWN
Pin has an internal pull-down enabled.

gpio.PIN_INVIN
Invert input.

gpio.PIN_INVOUT
Invert output.

gpio.PIN_USER
Pin accessible by users.

gpio.PIN_PULSATE
Pulsate pin at a hardware set frequency.

gpio.PIN_SET
Pin is set.

gpio.unset(gpiodev, pin)
Unset gpio pin.

stat = gpio.read(gpiodev, pin)
Read the current pin state.

oldstate = gpio.write(gpiodev, pin, state)
Write the pin state returning the old state. The following states are defined:

gpio.PIN_LOW
Pin is in the low state.

gpio.PIN_HIGH
Pin is in the high state.

gpio.toggle(gpiodev, pin)
Toggle pin state.

gpio.attach(gpiodev, driver, offset, mask [, flags])
Attach a device driver with offset, mask, and optional flags at a pin.

EXAMPLES

The following example code opens /dev/gpio0 and prints all pin values:

local gpio = require 'gpio' 
 
gpiodev = gpio.open('/dev/gpio0') 
 
local npins = gpiodev:info() 
 
for n = 1, npins do 
	print('pin ' .. n .. ': ' .. gpiodev:read(n - 1)) 
end

HISTORY

A gpio manual appeared in NetBSD 7.0.

AUTHORS

The gpio Lua binding was written by Marc Balmer <mbalmer@NetBSD.org>.
Januar 7, 2014 NetBSD 7.0