1from os.path import join 2from glob import glob 3 4 5def 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 16 17GPIO_BASE = find_gpio_base() 18 19 20def convertGpio(name): 21 offset = int(''.join(list(filter(str.isdigit, name)))) 22 port = list(filter(str.isalpha, name.upper())) 23 a = ord(port[-1]) - ord('A') 24 if len(port) > 1: 25 a += 26 26 base = a * 8 + GPIO_BASE 27 return base + offset 28