Skip to content

Commit bfacf82

Browse files
mattytrentinidpgeorge
authored andcommitted
rp2/boards/WEACTSTUDIO_RP2350B_CORE: Add board.pinout.
This adds an ANSI-rendered pinout for the WeAct Studio RP2350B Core board. Signed-off-by: Matt Trentini <[email protected]>
1 parent 7357fc5 commit bfacf82

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
include("$(PORT_DIR)/boards/manifest.py")
2+
freeze("modules")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import deflate
2+
import io
3+
4+
_compressed_pinout = b'(\x91\xcd\x92\xbdn\xdb0\x10\xc7w\xbdB\x96\x1b\x93M$E\xc9\x82\xa7:Q\x83"A\x1c\xd8\x89;d*\x1a\x0f\x1d\x04\x03A:t\x0b\xbc6F:HA\x91%C\xbb\x14h\x9d<A\x1f\x86OR\x8a\x92]K"O\x96\x98\x02=\x10\x12\xc1\xfb \x7f\xff;\x07r\xdb\xb9\xf0\xbc>\x89amo\xa7\xaf\xde_\xc3\xf8\xfa\xe3\xe5\x87\x19\x8cN)\xe3\xee\x00\xf6gWS\x18\xcc\xde]]\xfe\r\xdc\xb9pc\xc7\x01\x83\x89\xf4\x97Hn\xe4\x82Q4\x8e\xceVqG\xd3O\xb0{(\xab\xeeA\xee\xceV\xba\x94\xf1\xc9|3i0\x1c\x9eUk\xaf\xbd\xffp\xa5KG\x82\xb1^\x9f\xf7\t\x8f\xa58\xd9\x8ey1P?\'\x06\x83\x9b\xaf\xdc\x12\xa4\x88\xa0\xd4\x8d\xc5\xedou\xac9*p\xd2e}\xb3\x81[\xbb\x8d\x12?\x86\xfd\xe3\xa3\xe2:\xd0\x95\xae\x1f)y\xf5/\xf7P0\xa0\xach\xb5!\xbd\x87\xeb\x12\xb4\xd7%\x99\x8b\x9f\xdf\xb3om\x93\xeb"\xff\x986\x07o\x86\xe5\x17uV\x866(CPe\x98\x8b+\x13v\x98\x98g5\x1fO\xf5M\xed\x8aL\tq\xf7\xa0\xae\xc8&J\xa9fV\x8e\xc8\xf7N\x98\x8a\xb6U\r\xc7\x06\x12\xe2\xaa\xe1\xa23\xd2Z5i\xc7\xd1\xc1\xba\x1c\rcq\xffc\xe5\xd9\xae\xc4\xd6\x17Y\xcf\x1c\xe95\xa8\x17\xe0\xeay\xb8z\xac\x83z\xbb\x87\xa7\x94\xef\xc9\x8dx\xfc,\x1e\x17\xb0a\xb6\xb0~\x03,\xc7a\xf1t\xc6\xbb\x8cJn\x125\xff-^\x0e\x16o\r\x10\x86\xc3\xe2\x83\xc1\x82\xae\xb0\x05j\x05\xd7\x16\x966\xc0\x12#,\xed\xc5\xe0\xb98l\xd8\x1dv\xa1\xe1\xb6\x85\xc5_\x0b\x10\xe2\xb0f\xad\x94\x9b\xd8\x8c\xf1\xa2\x82j\r\x0b\xf8\x1c\x02\x048\xac\x87\xc3\xb2\xee\xb0\n\xb7\x84j\x0f\xeb7\xc0r\x1c\xd6\x9c\xae\xdc\xdc\x06\xb6l\xb6\xa0\xe6\xb6\xe4\xa0l\x05\n\xa3\xf3\x130\x01\x05\xff\x0f\x10m\x00"\xc6\xce\x91\x18\xd8\x84\x99\xb2K\xde\xd6\x98"\xb9\x15\xc9Mm}y1l\x17\x9d\xb8\xc9(zm\xc4\xf63\xa0\xbb\x87j\x01\x7f]\x80\xca\x0b\xa2\x13C\x8b\xef\xbf5\xb3\xcf+\x9b\n\xaf\xa6D~\xa4\xe75=X\x13\xd0\x96\xd9\x18\xd0\x0c]\x1cm\r\xdd\xb2\xc9r\xfc\xf8\xc4\xfc\\\xd9\xe2\xc1\xf9\xb8\xc0-\x9bH\x9fu\xa3W_\x89\xee\xf0\xebV\xb9\xe9\x93\xe3\xfc\x01\x83\'\xd6K'
5+
6+
7+
def pinout():
8+
print(str(deflate.DeflateIO(io.BytesIO(_compressed_pinout), deflate.ZLIB).read(), "utf-8"))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Pinout diagram
2+
3+
Pinout diagrams can be a helpful tool to display the pin layout for a board.
4+
5+
Rendering them using ANSI and building them into a board's firmware means they
6+
are always conveniently available, even over a serial connection.
7+
8+
![WeAct RP2350B Core pinout diagram](weact_pinout.png)
9+
10+
## Overview
11+
12+
`pinout.py` generates a unicode pinout diagram that uses ANSI escape characters
13+
to add colour. It currently generates the pinout diagram for the WeActStudio
14+
RP2350B board but is an example that could be extended for any board.
15+
16+
Display the output by executing the script:
17+
18+
```bash
19+
python pinout.py
20+
```
21+
22+
23+
## Compression
24+
25+
`compress.py` uses zlib to _compress input_ and output a _byte string_.
26+
27+
The output from `pinout.py` can be large but compresses efficiently, so the
28+
intent is that the byte string output from `compress.py` can then be copied to
29+
`../modules/board.py` so that the _compressed_ pinout will be included in the
30+
firmware.
31+
32+
To execute:
33+
34+
```bash
35+
python pinout.py | python compress.py
36+
```
37+
38+
## Reference
39+
40+
[Build your own Command Line with ANSI escape
41+
codes](https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html)
42+
provides a good reference of how to use ANSI codes, including helpful colour
43+
lookups.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
import zlib
3+
4+
data_str = sys.stdin.read()
5+
compressed = zlib.compress(bytes(data_str, "utf-8"), wbits=10)
6+
print(
7+
f"{len(data_str) / len(compressed):.1f}x smaller: uncompressed={len(data_str)}, compressed={len(compressed)}",
8+
file=sys.stderr,
9+
)
10+
print(compressed)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import re
2+
3+
pinout_str = """
4+
\u001b[44;1m WeAct Studio RP2350B Core Board \u001b[0m
5+
6+
╭── RESET
7+
Key (GP23) ───╮ │ ╭── BOOT
8+
╭────────────────────────────────╮
9+
[26 ] [25 ] │⌾ ⌾ ╭─╮╭─╮╭─╮ ╭─[CLK] ⌾ ⌾│ [24 ] [ 23 ]
10+
[28 ] [27 ] │⌾ ⌾ │⬤││⬤││⬤│ │ ╭─[DIO] ⌾ ⌾│ [22 ] [ 21 ]
11+
[30 ] [29 ] │⌾ ⌾ ╰─╯╰─╯╰─╯[⏚]╮ │ │ ╭─[3V3]⌾ ⌾│ [20 ] [ 19 ]
12+
[32 ] [31 ] │⌾ ⌾ LED ▩ ⌾ ⌾ ⌾ ⌾ ⌾ ⌾│ [18 ] [ 17 ]
13+
[34 ] [33 ] │⌾ ⌾ (GP25) ⟋⟍ ⌾ ⌾│ [16 ] [ 15 ]
14+
[36 ] [35 ] │⌾ ⌾ ⟋ ⟍ ⌾ ⌾│ [14 ] [ 13 ]
15+
[38 ] [37 ] │⌾ ⌾ ⟋ ⟍ ⌾ ⌾│ [12 ] [ 11 ]
16+
[40 ] [39 ] │⌾ ⌾ ⟍ ⟋ ⌾ ⌾│ [10 ] [ 9 ]
17+
[42 ] [41 ] │⌾ ⌾ ⟍ ⟋ ⌾ ⌾│ [ 8 ] [ 7 ]
18+
[44 ] [43 ] │⌾ ⌾ ⟍⟋ ⌾ ⌾│ [ 6 ] [ 5 ]
19+
[46 ] [45 ] │⌾ ⌾ ⌾ ⌾│ [ 4 ] [ 3 ]
20+
[RUN] [47 ] │⌾ ⌾ ⌾ ⌾│ [ 2 ] [ 1 ]
21+
[3V3] [3V3] │⌾ ⌾ ┌──────┐ ⌾ ⌾│ [ 0 ] [VREF]
22+
[ ⏚ ] [EN ] │▣ ⌾ │ │ ▣ ▣│ [ ⏚ ] [ ⏚ ]
23+
[ ⏚ ] [ ⏚ ] │▣ ▣ │ │ ⌾ ⌾│ [5V ] [VBUS]
24+
╰────────────└──────┘────────────╯
25+
"""
26+
27+
28+
def ansi_colour(wrap_str: str, colours: tuple[int | None, int | None]) -> str:
29+
"""Wrap a string in the ANSI foreground and background colour escape sequences."""
30+
wrapped_str = ""
31+
32+
wrapped_str += "\u001b[38;5;" + str(colours[0]) + "m" if colours[0] else ""
33+
wrapped_str += "\u001b[48;5;" + str(colours[1]) + "m" if colours[1] else ""
34+
wrapped_str += wrap_str
35+
wrapped_str += "\u001b[0m" if colours[0] or colours[1] else ""
36+
37+
return wrapped_str
38+
39+
40+
def add_colour(pinout_str):
41+
symbol_colours = {
42+
"⌾": (220, None), # Pin (Yellow)
43+
"▣": (220, None), # Ground pin (Yellow)
44+
"↺": (15, None), # Reset (White)
45+
"▩": (129, None), # LED (Purple)
46+
}
47+
48+
for symbol, colours in symbol_colours.items():
49+
pinout_str = pinout_str.replace(symbol, ansi_colour(symbol, colours))
50+
return pinout_str
51+
52+
53+
def colour_tags(matchobj: re.Match) -> str:
54+
white_on_red = (15, 1)
55+
white_on_peach = (15, 216)
56+
white_on_dark_green = (15, 28)
57+
white_on_black = (15, 16)
58+
black_on_pink = (16, 224)
59+
60+
tag_colours = {
61+
"5V": white_on_red,
62+
"VBUS": white_on_red,
63+
"VREF": white_on_dark_green,
64+
"3V3": white_on_red,
65+
"⏚": white_on_black,
66+
"EN": black_on_pink,
67+
"CLK": white_on_peach,
68+
"DIO": white_on_peach,
69+
}
70+
71+
if matchobj.group(2) not in tag_colours.keys():
72+
return matchobj.group(0)
73+
74+
pin_colours = tag_colours[matchobj.group(2)]
75+
76+
return ansi_colour(matchobj.group(1), pin_colours)
77+
78+
79+
def replace_tags(pinout_str):
80+
return re.sub(r"(\[\s*(\S+)\s*\])", colour_tags, pinout_str)
81+
82+
83+
def colour_pins(matchobj: re.Match) -> str:
84+
# Regular GPIO is green, ADC pins (40-47) are darker greeen
85+
gpio_colours = (15, 34) # White on green
86+
adc_colours = (15, 28) # White on dark green
87+
pin_number = int(matchobj.group(2))
88+
89+
pin_colours = gpio_colours if pin_number < 40 else adc_colours
90+
91+
return ansi_colour(matchobj.group(1), pin_colours)
92+
93+
94+
def replace_gpio(pinout_str) -> str:
95+
return re.sub(r"(\[\s*(\d+)\s*\])", colour_pins, pinout_str)
96+
97+
98+
pinout_str = replace_gpio(replace_tags(add_colour(pinout_str)))
99+
# Colours include a square bracket; temporarily change them!
100+
pinout_str = pinout_str.replace("\u001b[", "~")
101+
pinout_str = pinout_str.replace("[", " ").replace("]", " ")
102+
pinout_str = pinout_str.replace("~", "\u001b[") # Put them back
103+
104+
print(pinout_str)
21.8 KB
Loading

0 commit comments

Comments
 (0)