blob: ff6c53724657caf242c84519bcb0998a0aa7e393 [file] [log] [blame]
Andrew Jefferyed9ff702017-02-06 15:00:16 +10301from os.path import join
2from glob import glob
3
4def find_gpio_base(path="/sys/class/gpio/"):
5 pattern = "gpiochip*"
6 for gc in glob(join(path, pattern)):
7 with open(join(gc, "label")) as f:
8 label = f.readline().strip()
9 if label == "1e780000.gpio":
10 with open(join(gc, "base")) as f:
11 return int(f.readline().strip())
12 # trigger a file not found exception
13 open(join(path, "gpiochip"))
14
15GPIO_BASE = find_gpio_base()
Brad Bishopa7ac8052016-09-21 09:17:05 -040016
17def convertGpio(name):
Xo Wangf35a7dd2017-01-09 16:21:08 -080018 offset = int(filter(str.isdigit, name))
19 port = filter(str.isalpha, name.upper())
20 a = ord(port[-1]) - ord('A')
21 if len(port) > 1:
22 a += 26
23 base = a * 8 + GPIO_BASE
24 return base + offset