Skip to content
Jennifer Programming Language

gpio API reference

Raspberry-Pi (and any Linux SBC) GPIO over sysfs. /sys/class/gpio is plain files, so fs is the whole backend: export a pin, set its direction, read / write its value, unexport it. "Blink an LED from a five-line script." It is stateless and pin-keyed (sysfs derives every path from the pin number, so no handle is needed). The sysfs root defaults to /sys/class/gpio; set the JENNIFER_GPIO_BASE environment variable to point elsewhere (a differently mounted sysfs, or a mock tree under test). This is the sysfs backend, which is deprecated in favour of the /dev/gpiochip character device but stays available on the hobbyist Pi kernels this targets; the API is kept stable so the backend could later be swapped for a chardev system library with no change to scripts. GPIO is a Linux-platform feature: off a GPIO-capable host every call reports a clear "base directory not found" error rather than crashing.

Import with import "gpio.j" as gpio;. See the gpio guide for prose and examples.

Functions

gpio.read(pin as int)

Return a pin's current value (0 or 1).

Parameters

  • pin {int} - the GPIO pin number

Returns {int} - the pin's current value, 0 or 1

Throws

  • {Error} - when the sysfs GPIO tree is absent

gpio.release(pin as int)

Unexport a pin.

Parameters

  • pin {int} - the GPIO pin number

Throws

  • {Error} - when the sysfs GPIO tree is absent

gpio.setup(pin as int, direction as string)

Export a pin and set its direction (gpio.IN or gpio.OUT).

Parameters

  • pin {int} - the GPIO pin number
  • direction {string} - the pin direction, gpio.IN or gpio.OUT (the "in" / "out" strings)

Throws

  • {Error} - when direction is not gpio.IN/gpio.OUT, or the sysfs GPIO tree is absent

gpio.write(pin as int, value as int)

Set an output pin's value (0 or 1).

Parameters

  • pin {int} - the GPIO pin number
  • value {int} - the value to write, 0 or 1

Throws

  • {Error} - when value is not 0/1, or the sysfs GPIO tree is absent