Onion GPIO Python Module

The onionGpio Python module provides a Python object, OnionGpio that allows programs to control the Omega’s GPIOs. The module uses the sysfs GPIO interface that is part of the Linux operating system.

Programming Flow

Once the OnionGpio object is initialized, the class methods can be used to:

  • Set the GPIO to the input or output direction
  • Read the value of the GPIO (for both the input and output directions)
  • Set the value of the GPIO (only in the output direction)

Installing the Module

To install the Python module, run the following commands:

opkg update
opkg install python-light pyOnionGpio

This will install the module to /usr/lib/python2.7/

This only needs to be done once

To make use of the Onion GPIO Module, include the following in your code:

Example Code

Several examples of how the OnionGpio object is used can be found in the examples in the onion-gpio-sysfs repo. This directory contains all of the code seen below as well as some additional examples.

The main example is the gpio-test.py script, it sets GPIO14 to be an input and reads the value, it then changes the GPIO direction to output, reads the current value, sets the value to 1, and reads the value again.

Functions

Function Prototype
Constructor onionGpio.OnionGpio(gpioNumber)
Reading the Current Direction getDirection()
Setting Pin to Input setInputDirection()
Setting Pin to Output setOutputDirection(defaultValue)
Reading the Value getValue()
Setting the Value setValue(value)

Constructor - onionGpio.OnionGpio()

The object needs to be initialized before it can be used, hence, the constructor:

After this call, the object can be used freely.

Arguments

The gpioNumber argument is an integer that indicates the GPIO that is to be controlled by this object.

Example

To initialize an OnionGpio object to control GPIO14:

GPIO Direction

The GPIOs on the Omega can be set to the input or output direction. When in the input direction, external signals can be connected to the GPIO and the digital value can be read. When in the output direction, the digital value the GPIO is driving can be programmed.

Reading the Current Direction - getDirection()

In some instances, it will be useful to find out the current direction of the GPIO:

Return Value

The function will return the following: * in if the Input direction is selected * out if the Output direction is selected

Setting the Direction

Before using a pin, it’s best to make sure it is set to operate in the correct direction:

Set Pin to Input- setInputDirection()

The direction of the GPIO can be set to input, to read values from the pin:

Return Value

The function will return the following: * 0 if the operation is successful * -1 if the operation is NOT successful

Examples

Set your GPIO to input:

Set Pin to Output - setOutputDirection()

Or output:

Return Value

The function will return the following: * 0 if the operation is successful * -1 if the operation is NOT successful

Arguments

The setOutputDirection() function has an optional integer argument that, when defined, will set the initial value of the GPIO to ensure glitch-free operation.

If the optional argument is not set, the GPIO will just be set to output and the initial value will most likely be LOW. However, glitch-free operation cannot be guaranteed.

Examples

Set the GPIO to output:

Set the GPIO to output with LOW (0) as the initial value:

Set the GPIO to output with HIGH (1) as the initial value:

GPIO Value

The good part, finally! Now we will be reading and setting a GPIO’s value.

Reading the Value - getValue()

Reading the current value of the GPIO:

Note that the value of the GPIO can be read in both Input and Output mode.

The difference is that in input mode, the GPIO can be driven high or low based on external signals, and that is the value that will be read. In output mode, the value that will be read is what the GPIO is currently outputting.

Return Value

The function will return the current value of the GPIO: either 0 or 1

Setting the Value - setValue()

And what we’ve all been waiting for, setting the value of a GPIO:

Note that this will only work the the GPIO is programmed to the Output direction!

Arguments

The value argument is the value to set the GPIO. Set it to 0 to make the GPIO output a digital 0 (LOW), or set to 1 to output a digital 1 (HIGH).

Return Value

The function will return the following: * 0 if the operation is successful * -1 if the operation is NOT successful