blob: 7bcea9ef541013d49dbce567ac4c255e953e5f75 [file] [log] [blame]
Andrew Jefferyed9ff702017-02-06 15:00:16 +10301from os.path import join
2from glob import glob
3
Adriana Kobylak24341f92018-01-26 15:07:23 -06004
Andrew Jefferyed9ff702017-02-06 15:00:16 +10305def find_gpio_base(path="/sys/class/gpio/"):
6 pattern = "gpiochip*"
7 for gc in glob(join(path, pattern)):
8 with open(join(gc, "label")) as f:
9 label = f.readline().strip()
10 if label == "1e780000.gpio":
11 with open(join(gc, "base")) as f:
12 return int(f.readline().strip())
13 # trigger a file not found exception
14 open(join(path, "gpiochip"))
15
Adriana Kobylak24341f92018-01-26 15:07:23 -060016
Andrew Jefferyed9ff702017-02-06 15:00:16 +103017GPIO_BASE = find_gpio_base()
Brad Bishopa7ac8052016-09-21 09:17:05 -040018
Adriana Kobylak24341f92018-01-26 15:07:23 -060019
Brad Bishopa7ac8052016-09-21 09:17:05 -040020def convertGpio(name):
CamVan Nguyend65b2d52018-02-27 15:14:41 -060021 offset = int(''.join(list(filter(str.isdigit, name))))
22 port = list(filter(str.isalpha, name.upper()))
Xo Wangf35a7dd2017-01-09 16:21:08 -080023 a = ord(port[-1]) - ord('A')
24 if len(port) > 1:
25 a += 26
26 base = a * 8 + GPIO_BASE
27 return base + offset