Skip to content

Commit 1549dc0

Browse files
committed
utils: move Profile Wizard code to new file
In preparation for the addition of the AD9528 parser, move Profile Wizard code to it's own file. Signed-off-by: Liam Beguin <liambeguin@gmail.com>
1 parent ace2bb1 commit 1549dc0

File tree

2 files changed

+94
-92
lines changed

2 files changed

+94
-92
lines changed

adidt/parts/adrv9009.py

Lines changed: 9 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import xmltodict
22
from typing import Dict
33
from adidt.dt import dt
4+
from adidt.utils import profilewiz
45
import fdt
56
import math
67

@@ -11,92 +12,8 @@ def handle_ints(val):
1112
return int(hex((val + (1 << 32)) % (1 << 32)), 16)
1213

1314

14-
def coefs_to_long_string(coefs):
15-
"""Convert coefficient array to string.
16-
17-
Args:
18-
coefs (list): Coefficients.
19-
20-
Returns:
21-
str: Coefficients as a string.
22-
"""
23-
result = ""
24-
for coef in coefs.split("\n"):
25-
coef = coef.replace(" ", "")
26-
result += f"({coef}) "
27-
return result[:-1]
28-
29-
30-
def profile_to_xml(filename):
31-
# Update profile to non-shitty xml
32-
with open(filename) as sxmlfile:
33-
sxmldata = sxmlfile.read()
34-
# Correct each header
35-
outfile = []
36-
for line in sxmldata.split("\n"):
37-
if "<" in line and "</" not in line:
38-
within = line[line.find("<") + 1 : line.find(">")]
39-
# Fix all prop=val
40-
if "," in within:
41-
oline = []
42-
# print(line)
43-
for index, sline in enumerate(line.split(" ")):
44-
if index == 3:
45-
sline = f"Bandwidth={sline}"
46-
if index in [4, 6]:
47-
sline = sline + "="
48-
oline += [sline]
49-
line = " ".join(oline)
50-
line = line.replace(",", "")
51-
line = line.replace("= ", "=")
52-
if " " in within:
53-
# print(line)
54-
oline = []
55-
spaces = 0
56-
for sline in line.split(" "):
57-
if len(sline) > 0:
58-
starter = sline + " "
59-
break
60-
spaces += 1
61-
for sline in line.split(" "):
62-
if (
63-
len(oline) == 0
64-
and len(sline)
65-
and "<" not in sline
66-
and ">" not in sline
67-
and "=" not in sline
68-
):
69-
oline += [f'type="{sline}"']
70-
if "=" in sline:
71-
sline = sline.replace(">", "")
72-
sline = sline.replace("<", "")
73-
p = sline.split("=")[0]
74-
v = sline.split("=")[1]
75-
# print(f"{p}={v}")
76-
oline += [f'{p}="{v}"']
77-
78-
# print(" "*spaces+starter+" ".join(oline)+">")
79-
outfile += [" " * spaces + starter + " ".join(oline) + ">"]
80-
else:
81-
# print(line)
82-
if "=" in line:
83-
s1 = line.find("<")
84-
s2 = line.find(">")
85-
within = line[s1 + 1 : s2]
86-
o = within.split("=")
87-
ss = f"{o[0]}>{o[1]}</{o[0]}"
88-
line = line.replace(within, ss)
89-
# print(line)
90-
91-
outfile += [line]
92-
else:
93-
outfile += [line]
94-
95-
return "\n".join(outfile)
96-
97-
9815
def parse_profile(filename):
99-
nsxml = profile_to_xml(filename)
16+
nsxml = profilewiz.profile_to_xml(filename)
10017
profile = xmltodict.parse(nsxml)["profile"]
10118

10219
rx = profile['rx']
@@ -160,22 +77,22 @@ def parse_profile(filename):
16077

16178
tx["filter"]["@gain_dB"] = f"({tx['filter']['@gain_dB']})"
16279

163-
rx["rxAdcProfile"]["coefs"] = coefs_to_long_string(rx["rxAdcProfile"]["#text"])
80+
rx["rxAdcProfile"]["coefs"] = profilewiz.coefs_to_long_string(rx["rxAdcProfile"]["#text"])
16481
del(rx["rxAdcProfile"]["#text"])
165-
rx["filter"]["coefs"] = coefs_to_long_string(rx["filter"]["#text"])
82+
rx["filter"]["coefs"] = profilewiz.coefs_to_long_string(rx["filter"]["#text"])
16683
del(rx["filter"]["#text"])
16784

168-
orx["filter"]["coefs"] = coefs_to_long_string(orx["filter"]["#text"])
85+
orx["filter"]["coefs"] = profilewiz.coefs_to_long_string(orx["filter"]["#text"])
16986
del(orx["filter"]["#text"])
170-
orx["orxBandPassAdcProfile"]["coefs"] = coefs_to_long_string(orx["orxBandPassAdcProfile"]["#text"])
87+
orx["orxBandPassAdcProfile"]["coefs"] = profilewiz.coefs_to_long_string(orx["orxBandPassAdcProfile"]["#text"])
17188
del(orx["orxBandPassAdcProfile"]["#text"])
172-
orx["orxLowPassAdcProfile"]["coefs"] = coefs_to_long_string(orx["orxLowPassAdcProfile"]["#text"])
89+
orx["orxLowPassAdcProfile"]["coefs"] = profilewiz.coefs_to_long_string(orx["orxLowPassAdcProfile"]["#text"])
17390
del(orx["orxLowPassAdcProfile"]["#text"])
17491

175-
tx["filter"]["coefs"] = coefs_to_long_string(tx["filter"]["#text"])
92+
tx["filter"]["coefs"] = profilewiz.coefs_to_long_string(tx["filter"]["#text"])
17693
del(tx["filter"]["#text"])
17794

178-
lpbk["lpbkAdcProfile"]["coefs"] = coefs_to_long_string(lpbk["lpbkAdcProfile"]["#text"])
95+
lpbk["lpbkAdcProfile"]["coefs"] = profilewiz.coefs_to_long_string(lpbk["lpbkAdcProfile"]["#text"])
17996
del(lpbk["lpbkAdcProfile"]["#text"])
18097

18198
return {"rx": rx, "tx": tx, "orx": orx, "lpbk": lpbk, "clocks": clocks}

adidt/utils/profilewiz.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import xmltodict
2+
3+
4+
def coefs_to_long_string(coefs):
5+
"""Convert coefficient array to string.
6+
7+
Args:
8+
coefs (list): Coefficients.
9+
10+
Returns:
11+
str: Coefficients as a string.
12+
"""
13+
result = ""
14+
for coef in coefs.split("\n"):
15+
coef = coef.replace(" ", "")
16+
result += f"({coef}) "
17+
return result[:-1]
18+
19+
20+
def profile_to_xml(filename):
21+
# Update profile to non-shitty xml
22+
with open(filename) as sxmlfile:
23+
sxmldata = sxmlfile.read()
24+
# Correct each header
25+
outfile = []
26+
for line in sxmldata.split("\n"):
27+
if "<" in line and "</" not in line:
28+
within = line[line.find("<") + 1 : line.find(">")]
29+
# Fix all prop=val
30+
if "," in within:
31+
oline = []
32+
# print(line)
33+
for index, sline in enumerate(line.split(" ")):
34+
if index == 3:
35+
sline = f"Bandwidth={sline}"
36+
if index in [4, 6]:
37+
sline = sline + "="
38+
oline += [sline]
39+
line = " ".join(oline)
40+
line = line.replace(",", "")
41+
line = line.replace("= ", "=")
42+
if " " in within:
43+
# print(line)
44+
oline = []
45+
spaces = 0
46+
for sline in line.split(" "):
47+
if len(sline) > 0:
48+
starter = sline + " "
49+
break
50+
spaces += 1
51+
for sline in line.split(" "):
52+
if (
53+
len(oline) == 0
54+
and len(sline)
55+
and "<" not in sline
56+
and ">" not in sline
57+
and "=" not in sline
58+
):
59+
oline += [f'type="{sline}"']
60+
if "=" in sline:
61+
sline = sline.replace(">", "")
62+
sline = sline.replace("<", "")
63+
p = sline.split("=")[0]
64+
v = sline.split("=")[1]
65+
# print(f"{p}={v}")
66+
oline += [f'{p}="{v}"']
67+
68+
# print(" "*spaces+starter+" ".join(oline)+">")
69+
outfile += [" " * spaces + starter + " ".join(oline) + ">"]
70+
else:
71+
# print(line)
72+
if "=" in line:
73+
s1 = line.find("<")
74+
s2 = line.find(">")
75+
within = line[s1 + 1 : s2]
76+
o = within.split("=")
77+
ss = f"{o[0]}>{o[1]}</{o[0]}"
78+
line = line.replace(within, ss)
79+
# print(line)
80+
81+
outfile += [line]
82+
else:
83+
outfile += [line]
84+
85+
return "\n".join(outfile)

0 commit comments

Comments
 (0)