-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-char.py
69 lines (57 loc) · 2.03 KB
/
get-char.py
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
#!/usr/bin/env python3
import itertools
from typing import *
def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
"Collect data into non-overlapping fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
# grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
# grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
args = [iter(iterable)] * n
if incomplete == 'fill':
return itertools.zip_longest(*args, fillvalue=fillvalue)
if incomplete == 'strict':
return zip(*args, strict=True)
if incomplete == 'ignore':
return zip(*args)
else:
raise ValueError('Expected fill, strict, or ignore')
def charpoint(data : str) -> Tuple[int, List[str]]:
if not data.startswith('STARTCHAR'):
return 0, []
bitmap = data.split('BITMAP\n', 1)[1].split('\n')[:-1]
for line in data.split('\n'):
if line.startswith('ENCODING'):
return int(line.split(' ', 1)[1]), bitmap
handler_name = 'writeChar'
with open('vendor/12x12.bdf') as f:
bitmaps = dict(charpoint(c) for c in f.read().split('\n\n') )
while True:
try:
text = input('> ')
except EOFError:
break
if not text:
break
chars : List[str] = []
success = True
for c in text:
if ord(c) not in bitmaps:
print('Unknown character:', c)
success = False
break
if c not in chars:
chars.append(c)
if not success:
continue
for c in text:
print(f'.int _{handler_name}_{c} {handler_name} # {hex(ord(c))}')
# print('.int waitChars')
print('end\n\n')
for c in chars:
print(f'.label _{handler_name}_{c}')
bitmap = [row[:3].rjust(3, '0') for row in bitmaps[ord(c)]]
if len(bitmap) < 12:
bitmap = ['000'] * (12 - len(bitmap)) + bitmap
for i, d in enumerate(grouper(bitmap, 4, fillvalue='000')):
print('dataPixel', i + 1, ' = 0x', *d, sep='')
print(f'jump {handler_name}\n')