refactor: remove obsolete sentences tool

This commit is contained in:
2026-11-09 21:15:09 +01:00
parent ee1de505d3
commit bf9ec8664f
4 changed files with 9 additions and 98 deletions
+2 -4
View File
@@ -6,7 +6,7 @@ from .display import get_display
from .forge_data import ForgeData
from .i2c import I2C
from .input import Inputs
from .tools import debug_img, gen_img, sentences
from .tools import debug_img, gen_img
def main_loop() -> None:
@@ -29,7 +29,7 @@ def main() -> None:
parser.add_argument(
"--mode",
default="default",
choices=["default", "gen-img", "debug-img", "sentences"],
choices=["default", "gen-img", "debug-img"],
)
args = parser.parse_args()
@@ -37,7 +37,5 @@ def main() -> None:
gen_img()
elif args.mode == "debug-img":
debug_img()
elif args.mode == "sentences":
sentences()
else:
main_loop()
+1 -2
View File
@@ -1,5 +1,4 @@
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", "sentences"]
__all__ = ["debug_img", "gen_img"]
+6 -6
View File
@@ -1,6 +1,6 @@
import logging
from PIL import Image, ImageDraw
from PIL import Image
from ..display import get_display
from ..forge_data import ForgeData
@@ -11,12 +11,12 @@ log = logging.getLogger(__name__)
def debug_image(im: Image.Image) -> None:
im = im.resize((1280, 640)).convert("RGB")
draw2 = ImageDraw.Draw(im)
# draw2 = ImageDraw.Draw(im)
for x in range(1, 128):
draw2.line((x * 10, 0, x * 10, 640), fill=(50, 50, 50))
for y in range(1, 64):
draw2.line((0, y * 10, 1280, y * 10), fill=(50, 50, 50))
# for x in range(1, 128):
# draw2.line((x * 10, 0, x * 10, 640), fill=(50, 50, 50))
# for y in range(1, 64):
# draw2.line((0, y * 10, 1280, y * 10), fill=(50, 50, 50))
im.resize((1280, 640)).save("debug.png")
log.info("Debug image saved as debug.png")
-86
View File
@@ -1,86 +0,0 @@
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"
FILE_CONTENT = """
#ifndef INC_SENTENCES
#define INC_SENTENCES
#define SENTENCE_COUNT {0}
#define SENTENCE_MAX_LENGTH {1}
const int sentences[] = int[SENTENCE_COUNT * SENTENCE_MAX_LENGTH](
{2}
);
const int lengths[] = int[SENTENCE_COUNT](
{3}
);
#endif
"""
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)
TARGET_FILE.write_text(
FILE_CONTENT.format(
len(sentences),
max_len,
"".join(
" "
+ convert(sentence, mapping, max_len)
+ ("," if i < len(sentences) - 1 else " ")
+ f" // {sentence}\n"
for i, sentence in enumerate(sentences)
),
" " + ", ".join(str(len(sentence)) for sentence in sentences) + "\n",
)
)
log.info(f"Wrote {len(sentences)} sentences to {TARGET_FILE}")
if __name__ == "__main__":
main()