-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinNameToGPIO.py
executable file
·53 lines (35 loc) · 1.2 KB
/
pinNameToGPIO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python3
DOC = """
Problem. Given a pin name e.g. "P9_14" you would like to know its
associated GPIO file in sysfs.
Solution.
$> pinNameToGPIO.py P9_14
/sys/class/gpio/gpio50
CAVEAT.
. The pin PX_Y must be already defined to be a GPIO.
. If no correspondence is found in sysfs then nothing is returned.
"""
import sys
import os
# . where to look fro gpio files
BASE_DIR = '/sys/class/gpio';
# . the first argument is needed, abort if absent
if len(sys.argv) < 2:
print("Error, first argument is needed, a pin name, e.g. P9.11 or P9_11.")
sys.exit(1)
# . the pin name is the first argument, allow some variabiliy in input: P9_14 = P9.14 = p9.14
PIN_NAME = sys.argv[1]
# . dot is admitted instead of underscore
PIN_NAME = PIN_NAME.replace('.', '_', 1).strip()
# . 'P' case is irrelevant
PIN_NAME = PIN_NAME.replace('p', 'P', 1).strip()
# all files in like /sys/class/gpio/gpio*
allFiles = os.listdir(BASE_DIR)
allFiles = [x for x in allFiles if 'gpio' in x]
for f in allFiles:
fullPath = BASE_DIR + '/' + f + '/label'
text = open(fullPath, 'r').read().strip()
# print(text)
if PIN_NAME in text :
print(fullPath.replace('/label','',1))
break