blob: f7ea9610af2c2239902788e23891fc2c2dd4e5b7 [file] [log] [blame]
Andrew Jefferyed9ff702017-02-06 15:00:16 +10301from glob import glob
Patrick Williamsd8c6f5a2022-12-08 06:18:18 -06002from os.path import join
Andrew Jefferyed9ff702017-02-06 15:00:16 +10303
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):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050021 offset = int("".join(list(filter(str.isdigit, name))))
CamVan Nguyend65b2d52018-02-27 15:14:41 -060022 port = list(filter(str.isalpha, name.upper()))
Patrick Williams75fe8cc2022-07-22 16:12:12 -050023 a = ord(port[-1]) - ord("A")
Xo Wangf35a7dd2017-01-09 16:21:08 -080024 if len(port) > 1:
25 a += 26
26 base = a * 8 + GPIO_BASE
27 return base + offset