Skip to content

Commit 1d8a064

Browse files
committed
examples: Added pins.py example script to list pin config/af.
Script is due to Dave Hylands.
1 parent 2c4e67e commit 1d8a064

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

examples/pins.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Print a nice list of pins, their current settings, and available afs.
2+
# Requires pins_af.py from stmhal/build-PYBV10/ directory.
3+
4+
import pyb
5+
import pins_af
6+
7+
def af():
8+
max_name_width = 0
9+
max_af_width = 0
10+
for pin_entry in pins_af.PINS_AF:
11+
max_name_width = max(max_name_width, len(pin_entry[0]))
12+
for af_entry in pin_entry[1:]:
13+
max_af_width = max(max_af_width, len(af_entry[1]))
14+
for pin_entry in pins_af.PINS_AF:
15+
pin_name = pin_entry[0]
16+
print('%-*s ' % (max_name_width, pin_name), end='')
17+
for af_entry in pin_entry[1:]:
18+
print('%2d: %-*s ' % (af_entry[0], max_af_width, af_entry[1]), end='')
19+
print('')
20+
21+
def pins():
22+
mode_str = { pyb.Pin.IN : 'IN',
23+
pyb.Pin.OUT_PP : 'OUT_PP',
24+
pyb.Pin.OUT_OD : 'OUT_OD',
25+
pyb.Pin.AF_PP : 'AF_PP',
26+
pyb.Pin.AF_OD : 'AF_OD',
27+
pyb.Pin.ANALOG : 'ANALOG' }
28+
pull_str = { pyb.Pin.PULL_NONE : '',
29+
pyb.Pin.PULL_UP : 'PULL_UP',
30+
pyb.Pin.PULL_DOWN : 'PULL_DOWN' }
31+
width = [0, 0, 0, 0]
32+
rows = []
33+
for pin_entry in pins_af.PINS_AF:
34+
row = []
35+
pin_name = pin_entry[0]
36+
pin = pyb.Pin(pin_name)
37+
pin_mode = pin.mode()
38+
row.append(pin_name)
39+
row.append(mode_str[pin_mode])
40+
row.append(pull_str[pin.pull()])
41+
if pin_mode == pyb.Pin.AF_PP or pin_mode == pyb.Pin.AF_OD:
42+
pin_af = pin.af()
43+
for af_entry in pin_entry[1:]:
44+
if pin_af == af_entry[0]:
45+
af_str = '%d: %s' % (pin_af, af_entry[1])
46+
break
47+
else:
48+
af_str = '%d' % pin_af
49+
else:
50+
af_str = ''
51+
row.append(af_str)
52+
for col in range(len(width)):
53+
width[col] = max(width[col], len(row[col]))
54+
rows.append(row)
55+
for row in rows:
56+
for col in range(len(width)):
57+
print('%-*s ' % (width[col], row[col]), end='')
58+
print('')

0 commit comments

Comments
 (0)