Skip to content

Instantly share code, notes, and snippets.

@qistoph
Created November 27, 2021 16:25
Show Gist options
  • Save qistoph/a5269b9a70722311a3cb8f81e80280cf to your computer and use it in GitHub Desktop.
Save qistoph/a5269b9a70722311a3cb8f81e80280cf to your computer and use it in GitHub Desktop.
Example 1:
>>> print(pins2ascii({
1: 'Rst',
2: 'RX',
4: 'Gnd', #'⏚',
5: 'SDA',
6: 'SCL',
8: '5V', #'↑'
}))
┌─╮_╭─┐
Rst┥1 8┝5V
RX┥2 7┝
┥3 6┝SCL
Gnd┥4 5┝SDA
└─────┘
Example 2:
>>> print(pins2ascii(['Rst', 'RX', '', 'Gnd', 'SDA', 'SCL' , '', '5V']))
┌─╮_╭─┐
Rst┥1 8┝5V
RX┥2 7┝
┥3 6┝SCL
Gnd┥4 5┝SDA
└─────┘
Example 3:
>>> print(pins2ascii({
1: ('PB5','Rst'),
2: ('PB3','RX'),
3: ('PB4',''),
4: ('GND','⏚'),
5: ('PB0','SDA'),
6: ('PB1','SCL'),
7: ('PB2', ''),
8: ('VCC','↑'),
}))
┌───╮_╭───┐
Rst┥PB5 VCC┝↑
RX┥PB3 PB2┝
┥PB4 PB1┝SCL
⏚┥GND PB0┝SDA
└─────────┘
Example 4:
>>> print(pins2ascii([
('RST ',''),
('A0', ''),
('D0', ''),
('D5', 'SD CLK'),
('D6', 'SD MISO'),
('D7', 'SD MOSI'),
('D8', 'SD CS'),
('3V3',''),
('TX', 'Serial TX'),
('RX', 'NeoPixel (DMA)'),
('D1', 'SCL'),
('D2', 'SDA'),
('D3', 'OLED Reset'),
('D4', 'Internal LED'),
('GND', ''),
('5V', '')
], markTop=False, markBottom=True))
┌─────────────┐
┥RST 5V┝
┥A0 GND┝
┥D0 D4┝Internal LED
SD CLK┥D5 D3┝OLED Reset
SD MISO┥D6 D2┝SDA
SD MOSI┥D7 D1┝SCL
SD CS┥D8 RX┝NeoPixel (DMA)
┥3V3 TX┝Serial TX
└─────┘‾└─────┘
def pins2ascii(pins, markTop = True, markBottom = False):
''' Create a string with 'asciiart' representing a chip. Actually uses unicode characters though. '''
NL = "\n"
if type(pins) is dict:
max_pin = int((max(pins.keys()) + 1) / 2) * 2
pins = [pins[k] if k in pins else '' for k in range(1, max_pin+1)]
else:
max_pin = int((len(pins) + 1) / 2) * 2
values = [
pin[1] if type(pin) is tuple else pin
for pin in pins
]
labels = [
pin[0] if type(pin) is tuple else str(n+1)
for n, pin in enumerate(pins)
]
left_width = max([len(s) for s in values[:len(values)//2]])
right_width = max([len(s) for s in values[len(values)//2:]])
label_width = max([len(s) for s in labels])
if markTop:
out = " " * left_width + "┌" + "─" * label_width + "╮_╭" + "─" * label_width + "┐" + NL
else:
out = " " * left_width + "┌" + "─" * (2 * label_width + 3) + "┐" + NL
for v in range(max_pin//2):
li = v
ri = max_pin - v - 1
out += "{0:>{1}s}".format(values[li], left_width)
out += "┥{0:<{1}s} {2:>{3}s}┝".format(labels[li], label_width, labels[ri], label_width)
out += "{0:<{1}s}".format(values[ri], right_width)
out += NL
if markBottom:
out += " " * left_width + "└" + "─" * label_width + "┘‾└" + "─" * label_width + "┘"
else:
out += " " * left_width + "└" + "─" * (2 * label_width + 3) + "┘"
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment