Andrew Jeffery | ed9ff70 | 2017-02-06 15:00:16 +1030 | [diff] [blame^] | 1 | from os.path import join |
| 2 | from glob import glob |
| 3 | |
| 4 | def 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 | |
| 15 | GPIO_BASE = find_gpio_base() |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 16 | |
| 17 | def convertGpio(name): |
Xo Wang | f35a7dd | 2017-01-09 16:21:08 -0800 | [diff] [blame] | 18 | 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 |