|
| 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) |
0 commit comments