Andrew Jeffery | ed9ff70 | 2017-02-06 15:00:16 +1030 | [diff] [blame] | 1 | from os.path import join |
| 2 | from glob import glob |
| 3 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 4 | |
Andrew Jeffery | ed9ff70 | 2017-02-06 15:00:16 +1030 | [diff] [blame] | 5 | def 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 Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 16 | |
Andrew Jeffery | ed9ff70 | 2017-02-06 15:00:16 +1030 | [diff] [blame] | 17 | GPIO_BASE = find_gpio_base() |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 18 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 19 | |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 20 | def convertGpio(name): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 21 | offset = int(''.join(list(filter(str.isdigit, name)))) |
| 22 | port = list(filter(str.isalpha, name.upper())) |
Xo Wang | f35a7dd | 2017-01-09 16:21:08 -0800 | [diff] [blame] | 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 |