Digital Inputs/Outputs

[source code]

import sys
sys.path.append('/svn/engineering/software/pimotion/src')
import pilib


pm = pilib.Pidev('192.168.1.13') # make a connection to the PiMotion

pm.gpio_set_direction(0xff, 0b01111111) # gpio 0 is output, all others input

pm.gpio_set_value(0xff, 0b10000000) # gpio 0 output set high

print bin(pm.gpio_get_value(0b11111111)) # read and print values from all gpios

Output:

0b10000000

Basic functions of the GPIO

Set direction

Set direction configures the pins to be inputs or outputs. This function takes two arguments separated by a comma: Mask and direction. Mask values of 1 allow that pin to be changed by the function. If the mask value is 0 for a pin it will remain in the last configuration that was set. Direction is set for each pin by a 1 for input or a 0 for output.

Note

For a any pin not covered by a mask a GPIO set direction value of 0 sets that pin to an output. A value of 1 sets the pin to an input.

Set value

Set value sets a pin configured as an output high (3.3V) or low (0V). Set value takes two arguments: Mask and value. In the example above the mask is set to all 1s so the second argument will affect all pins. The second argument sets pin 0 high and all others low.

Get value

Get value reads the value of all pins requested. This function can read pins configured as inputs or outputs. It only takes one argument: Mask. Again, only pins with a mask value of 1 will be read and return a value. Pins not read will return a value of 0 regardless of state.

Mask argument

The mask is used to allow or prevent changes to the state of a pin. To allow your values to be set to all the pins use a mask of all ones or 0xff. A mask of zero leaves the pin in its present state regardless of the value set for it. Here is an example.

current value = 0b10000000
mask          = 0b11111111
set value     = 0b00001111
                ----------
current value = 0b00001111
mask          = 0b00001111
set value     = 0b11110000
                ----------
current value = 0b00000000