forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard_summaries.py
More file actions
198 lines (176 loc) · 6.45 KB
/
board_summaries.py
File metadata and controls
198 lines (176 loc) · 6.45 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# The MIT License (MIT)
#
# SPDX-FileCopyrightText: Copyright (c) 2024 Tim Chinowsky
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import re
import sys
sys.path.append("../docs")
from shared_bindings_matrix import support_matrix_by_board
def module_incidence_matrix_csvs(support_matrix, rows=1000, present="1", absent=""):
all_modules = set()
for info in support_matrix.values():
all_modules.update(info["modules"])
all_modules = sorted(all_modules)
csvs = []
row = 0
for board, info in support_matrix.items():
if (row % rows) == 0:
csv = ["board,branded_name,mcu,flash,port,pins," + ",".join(all_modules) + "\n"]
chip_pin_set = set([chip_pin for _, chip_pin in info["pins"]])
n_chip_pins = len(chip_pin_set)
module_incidence = [present if m in info["modules"] else absent for m in all_modules]
line = (
f"{board},{info.get('branded_name')},{info.get('mcu')},"
+ f"{info.get('flash')},{info.get('port')},{n_chip_pins},"
+ ",".join(module_incidence)
+ "\n"
)
csv.append(line)
row += 1
if (row % rows) == 0:
csvs.append(csv)
if (row % rows) != 0:
csvs.append(csv)
return csvs
def frozen_incidence_matrix_csvs(support_matrix, rows=1000, present="1", absent=""):
all_frozen = set()
for info in support_matrix.values():
frozen = info["frozen_libraries"]
frozen = [f[0] if type(f) == tuple else f for f in frozen]
all_frozen.update(frozen)
all_frozen = sorted(all_frozen)
csvs = []
row = 0
for board, info in support_matrix.items():
if (row % rows) == 0:
csv = ["board,branded_name,mcu,flash,port,pins," + ",".join(all_frozen) + "\n"]
# remove urls if present
frozen = info["frozen_libraries"]
chip_pin_set = set([chip_pin for _, chip_pin in info["pins"]])
n_chip_pins = len(chip_pin_set)
frozen = [f[0] if type(f) == tuple else f for f in frozen]
frozen_incidence = [present if f in frozen else absent for f in all_frozen]
line = (
f"{board},{info.get('branded_name')},{info.get('mcu')},"
+ f"{info.get('flash')},{info.get('port')},{n_chip_pins},"
+ ",".join(frozen_incidence)
+ "\n"
)
csv.append(line)
row += 1
if (row % rows) == 0:
csvs.append(csv)
if (row % rows) != 0:
csvs.append(csv)
return csvs
def summarize_pins(pins):
"""Given a list of pin names, generate a compact string
like "BUTTON, LED1:4, PA0:10;13:14, PB5:7, SCL, SDA"
summarizing the names in the list"""
pin_prefixes = {}
for p in pins:
match = re.match(r"^(.*?)(\d*)$", p)
if match:
prefix = match.group(1)
n_str = match.group(2)
else:
raise ValueError("Cannot parse pin name")
if prefix in pin_prefixes:
pin_prefixes[prefix].add(n_str)
else:
pin_prefixes[prefix] = {n_str}
return ", ".join(
[f"{prefix}{span_string(pin_prefixes[prefix])}" for prefix in sorted(pin_prefixes.keys())]
)
def int_or_zero(s):
try:
return int(s)
except ValueError:
return 0
def span_string(number_strs):
int_strs = sorted([(int_or_zero(s), s) for s in number_strs])
last_i = None
last_s = None
run = False
for i, s in int_strs:
if last_i is None:
out = s
elif i != last_i + 1:
if run:
out += f":{last_s};{s}"
else:
out += f";{s}"
run = False
else:
run = True
last_i = i
last_s = s
if run:
out += f":{s}"
return out
def board_pins_matrix_csvs(support_matrix, rows=1000):
csvs = []
row = 0
for board, info in support_matrix.items():
if (row % rows) == 0:
csv = [
"board,branded_name,mcu,flash,port,n_board_pins,"
"board_pins,n_chip_pins,chip_pins\n"
]
board_pins = [board_pin for board_pin, _ in info["pins"]]
chip_pins = [chip_pin for _, chip_pin in info["pins"]]
line = (
f"{board},{info.get('branded_name')},{info.get('mcu')},"
+ f"{info.get('flash')},{info.get('port')},"
+ str(len(set(board_pins)))
+ ',"'
+ summarize_pins(board_pins)
+ '",'
+ str(len(set(chip_pins)))
+ ',"'
+ summarize_pins(chip_pins)
+ '"\n'
)
csv.append(line)
row += 1
if (row % rows) == 0:
csvs.append(csv)
if (row % rows) != 0:
csvs.append(csv)
return csvs
def write_csvs(rows=1000, present="1", absent="0"):
print("generating csvs...")
s = support_matrix_by_board(
use_branded_name=False, add_port=True, add_chips=True, add_pins=True, add_branded_name=True
)
csvs = {
"modules": module_incidence_matrix_csvs(s, rows=rows, present=present, absent=absent),
"frozen": frozen_incidence_matrix_csvs(s, rows=rows, present=present, absent=absent),
"pins": board_pins_matrix_csvs(s, rows=rows),
}
for key in csvs:
for i in range(len(csvs[key])):
filename = f"{key}_{i}.csv"
print(f"writing {filename}")
with open(filename, "w") as f:
f.writelines(csvs[key][i])
if __name__ == "__main__":
write_csvs()