制作简易墨水屏阅读器

2021-07-14  本文已影响0人  Lupino

这是我的第一个尝试,用 pipico 制作一个电子墨水屏屏阅读器。
我们第一步是要制作字体,我们将所有字读出来。
然后将我们的文章变成ID串,pipico 根据 ID 输出文字。
然后计算出整屏显示多少个字,将文字切成每页几个字的形式。
制作两个按钮,上一页,下一页的功能,最后生成我们每页的ID码。
通过脚本,我们很快就制作出我们的内容库,和字体库,代码如下:

with open('chapter.01.md', 'r') as f:
    data = f.read()


codes = [' ']
docs = []
doc = []
for c in data:
    if c == "\n":
        if len(doc) > 0:
            docs.append(doc)
        doc = []
        continue
    if c not in codes:
        codes.append(c)
    idx = codes.index(c)
    doc.append(str(idx))

    if len(doc) >= 30:
        docs.append(doc)
        doc = []

if len(doc) > 0:
    docs.append(doc)

with open('chinese.txt', 'w') as f:
    f.write(''.join(codes))

with open('pages.c', 'w') as f:
    f.write('#include <inttypes.h>\n')
    offsets = [];
    pages = []
    for idx, page in enumerate(docs):
        offsets.append(str(len(pages)))
        pages += page


    maxPage = len(offsets)
    pages = ', '.join(pages)
    offsets = ', '.join(offsets)
    f.write(f'const uint32_t pages[] = {{{pages}}};\n')
    f.write(f'const int pageOffsets[] = {{{offsets}}};\n')
    f.write(f'const int maxPage = {maxPage};\n')
上一篇 下一篇

猜你喜欢

热点阅读