Skip to content

Instantly share code, notes, and snippets.

Created June 18, 2016 00:40
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save howientc/606545e0ff47e2cda61f14fca5c46eee to your computer and use it in GitHub Desktop.
Gets the base of XIOs on C.H.I.P. computers. Currently on 4.3, it's 408, and on 4.4 it's 1016. This calculates it dynamically
import os
GPIO_PATH = "/sys/class/gpio" # The root of the GPIO directories
EXPANDER = "pcf8574a" # This is the expander that is used on CHIP for the XIOs
def get_xio_base():
Determines the base of the XIOs on the system by iterating through the /sys/class/gpio
directory and looking for the expander that is used. It then looks for the
"base" file and returns its contents as an integer
names = os.listdir(GPIO_PATH)
for name in names: # loop through child directories
prefix = GPIO_PATH + "/" + name + "/"
file_name = prefix + "label"
if os.path.isfile(file_name): # is there a label file in the directory?
with open(file_name) as label:
contents =
if contents.startswith(EXPANDER): # does label contain our expander?
file_name = prefix + "base"
with open(file_name) as base: # read the sibling file named base
contents =
return int(contents) # convert result to an int
if __name__ == '__main__':
print get_xio_base()
Copy link

Couldn't it be simplified for those who do not want to use python, by just:

# grep -l pcf8574a $(find /sys/class/gpio/gpio*/ -maxdepth 1 -type f -name label) | grep -o "[0-9]*"

This single grep command seems to show me that the expander pcf8574a is under xio_base 1013..

Copy link

Or better:

grep -l pcf8574a /sys/class/gpio/gpiochip*/label  | grep -o "[0-9]*"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment