-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvertGpioInfo2Json.py
executable file
·58 lines (45 loc) · 1.3 KB
/
convertGpioInfo2Json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/env python3
import subprocess
import sys
import json
filename = "gpioinfo.json"
if len(sys.argv) > 1:
filename = sys.argv[1]
print("Will write results to: %s" % filename)
print("Gathering infos with gpioinfo")
proc = subprocess.run("gpioinfo", shell=True, capture_output=True, encoding="UTF-8")
if proc.returncode > 0:
print("Failed to execute gpioinfo. Aborting")
sys.exit(1)
print("Processing the output of gpioinfo")
info = proc.stdout.splitlines(True)
chips = {}
gpios = {}
curChipName = None
curChipNmbr = -1
curChip = {}
for line in info:
line = line.strip()
#print (line)
if line.startswith("gpiochip"):
if curChipName != None:
chips[curChipName] = curChip
curChipName = line.split()[0]
curChipNmbr = int(curChipName.replace("gpiochip",""))
curChip = {}
elif line.startswith("line"):
lineArray = line.split()
lineNmbr = int(lineArray[1].replace(":",""))
gpioName = lineArray[2].replace('"',"")
curChip[gpioName] = lineNmbr
gpios[gpioName] = [curChipNmbr, lineNmbr]
if curChipName != None:
chips[curChipName] = curChip
result = {
"gpios":gpios,
"chips":chips
}
print("Writing result file")
out_file = open(filename, "w")
json.dump(result, out_file, indent = 4)
out_file.close()