refactor: better sentence generator

This commit is contained in:
2026-11-05 22:28:07 +01:00
parent d2d2095081
commit e6e59665e6
7 changed files with 102 additions and 28 deletions
+4 -2
View File
@@ -5,7 +5,7 @@ import time
from .display import get_display
from .forge_data import ForgeData
from .i2c import I2C
from .tools import debug_img, gen_img
from .tools import debug_img, gen_img, sentences
def main_loop() -> None:
@@ -26,7 +26,7 @@ def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--mode", default="default", choices=["default", "gen-img", "debug-img"]
"--mode", default="default", choices=["default", "gen-img", "debug-img", "sentences"]
)
args = parser.parse_args()
@@ -34,5 +34,7 @@ def main() -> None:
gen_img()
elif args.mode == "debug-img":
debug_img()
elif args.mode == "sentences":
sentences()
else:
main_loop()
+2 -1
View File
@@ -1,4 +1,5 @@
from .debug_img import main as debug_img
from .gen_img import main as gen_img
from .sentences import main as sentences
__all__ = ["debug_img", "gen_img"]
__all__ = ["debug_img", "gen_img", "sentences"]
+70
View File
@@ -0,0 +1,70 @@
import logging
import re
from ..constants import PROJECT_PATH
TARGET_FILE = PROJECT_PATH / "project" / "inc_sentences.glsl"
CP437_FILE = PROJECT_PATH / "project" / "inc_cp437.glsl"
SENTENCES_FILE = PROJECT_PATH / "sentences.txt"
BACKUP_SENTENCES_FILE = PROJECT_PATH / "sentences.example.txt"
log = logging.getLogger(__name__)
def read_mapping() -> dict[str, str]:
mapping = {' ': '0x20'}
with CP437_FILE.open() as cp437:
for line in cp437:
if (match := re.match(r'.*// (0x\w\w), (.)$', line)):
mapping[match.group(2)] = match.group(1)
return mapping
def read_sentences() -> list[str]:
if SENTENCES_FILE.exists():
return SENTENCES_FILE.read_text().splitlines()
log.warning("sentences.txt not found, using backup")
return BACKUP_SENTENCES_FILE.read_text().splitlines()
def convert(sentence: str, mapping: dict[str, str], max_len: int) -> str:
out = []
for i in range(max_len):
if i >= len(sentence):
out += ["0x00"]
elif sentence[i] not in mapping:
log.warning(f"Char '{sentence[i]}' not in CP437")
out += ["0x20"]
else:
out += [mapping[sentence[i]]]
return ", ".join(out)
def main():
mapping = read_mapping()
sentences = read_sentences()
max_len = max(len(sentence) for sentence in sentences)
with TARGET_FILE.open("w") as file:
file.write("#ifndef INC_SENTENCES\n")
file.write("#define INC_SENTENCES\n\n")
file.write(f"#define SENTENCE_COUNT {len(sentences)}\n")
file.write(f"#define SENTENCE_MAX_LENGTH {max_len}\n\n")
file.write(
"const int sentences[] = int[SENTENCE_COUNT * SENTENCE_MAX_LENGTH](\n"
)
file.writelines(
" " + convert(sentence, mapping, max_len) + ("," if i < len(sentences) - 1 else " ") + f" // {sentence}\n"
for i, sentence in enumerate(sentences)
)
file.write(");\n\n")
file.write("const int lengths[] = int[SENTENCE_COUNT](\n")
file.write(
" " + ", ".join(str(len(sentence)) for sentence in sentences) + "\n"
)
file.write(");\n")
file.write("#endif")
if __name__ == "__main__":
main()