Compare commits

...
3 Commits
Author SHA1 Message Date
klemek e6e59665e6 refactor: better sentence generator 2026-11-05 22:28:07 +01:00
klemek d2d2095081 refactor: basic logging 2026-11-05 22:09:19 +01:00
klemek 82ccc1d2e8 refactor: separate tools from main code 2026-11-05 22:00:24 +01:00
11 changed files with 241 additions and 119 deletions
+2
View File
@@ -5,4 +5,6 @@ build
debug.png
error.glsl
forge_default_save.*
project/inc_sentences.glsl
sentences.txt
tmp*
+15 -2
View File
@@ -43,7 +43,7 @@ print-%:
# ACTIONS
.PHONY: init
init: uv-sync git-submodule-init git-submodule-update ## install project
init: uv-sync git-submodule-init git-submodule-update sentences ## install project
.PHONY: update
update: uv-upgrade ## update project dependencies
@@ -54,6 +54,15 @@ format: ruff-fix ruff-format ## format project
.PHONY: lint
lint: ruff ruff-format-check ty ## lint project
.PHONY: gen-img
gen-img: uv-run-invar-mode-gen-img ## generate resources/*.png
.PHONY: debug-img
debug-img: uv-run-invar-mode-debug-img ## debug i2c screen with mock data
.PHONY: sentences
sentences: uv-run-invar-mode-sentences ## generate project/inc_sentences.glsl
.PHONY: rasp-init
rasp-init: rasp-chmod-opt rasp-rsync rasp-unmount ## init raspberry
@@ -62,7 +71,7 @@ rasp-install: rasp-install-pal ## setup raspberry
.PHONY: rasp-install-%
rasp-install-%: ## setup raspberry (PAL/NTSC/HDMI)
$(MAKE) git-submodule-update rasp-rsync rasp-setup-config-$* rasp-setup-cmdline-$* rasp-setup-start-$* rasp-unmount
$(MAKE) git-submodule-update sentences rasp-rsync rasp-setup-config-$* rasp-setup-cmdline-$* rasp-setup-start-$* rasp-unmount
.PHONY: rasp-reset
rasp-reset: rasp-rsync rasp-setup-config-hdmi rasp-reset-cmdline rasp-reset-start rasp-unmount ## reset raspberry to console mode
@@ -77,6 +86,10 @@ uv-sync: ## uv sync
uv-upgrade: ## uv sync upgrade
$(UV) sync --active --upgrade
.PHONY: uv-run-invar-mode-%
uv-run-invar-mode-%: .venv ## uv run forge-invar --mode=(tool)
$(UV) run --active forge-invar --mode=$*
.PHONY: ruff
ruff: .venv ## ruff check
$(RUFF) check
-21
View File
@@ -1,21 +0,0 @@
#ifndef INC_SENTENCES
#define INC_SENTENCES
#define SENTENCE_COUNT 8
#define SENTENCE_MAX_LENGTH 20
const int sentences[] = int[SENTENCE_COUNT * SENTENCE_MAX_LENGTH](
0x46, 0x6F, 0x6C, 0x6C, 0x65, 0x20, 0x4E, 0x75, 0x69, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x44, 0x75, 0x20, 0x51, 0x75, 0x61, 0x72, 0x61, 0x6E, 0x74, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x4C, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x46, 0x6F, 0x6C, 0x6C, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x4E, 0x75, 0x69, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x44, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x51, 0x75, 0x61, 0x72, 0x61, 0x6E, 0x74, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x35, 0x33, 0xF8, 0x20, 0x4D, 0x69, 0x65, 0x74, 0x74, 0x65, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
);
const int lengths[] = int[SENTENCE_COUNT](
10, 11, 2, 5, 4, 2, 8, 11
);
#endif
+17
View File
@@ -0,0 +1,17 @@
forge
FORGE
F.O.R.G.E.
Fusion
Of
Real-time
Generative
Effects
Everything you see
Pixel by pixel
Live
Generated
Controlled
π
Kleπek
Code as art
Art as code
+23 -23
View File
@@ -1,33 +1,14 @@
import argparse
import logging
import time
from .display import (
debug_image,
generate_background_image,
generate_loading_image,
get_display,
)
from .display import get_display
from .forge_data import ForgeData
from .i2c import I2C
from .tools import debug_img, gen_img, sentences
def host_generate() -> None:
data = ForgeData().mock()
generate_loading_image()
generate_background_image()
image = get_display(data, 0)
debug_image(image)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--generate", action="store_true")
args = parser.parse_args()
if args.generate:
host_generate()
return
def main_loop() -> None:
t0 = time.time()
with ForgeData() as data, I2C() as device:
device.update_display(get_display(data, t0))
@@ -38,3 +19,22 @@ def main() -> None:
continue
device.update_display(get_display(data, t0))
def main() -> None:
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument(
"--mode", default="default", choices=["default", "gen-img", "debug-img", "sentences"]
)
args = parser.parse_args()
if args.mode == "gen-img":
gen_img()
elif args.mode == "debug-img":
debug_img()
elif args.mode == "sentences":
sentences()
else:
main_loop()
-69
View File
@@ -157,72 +157,3 @@ def get_display(data: ForgeData, t0: float) -> Image.Image:
if data.state == ForgeState.RUNNING:
return __get_running_display(data)
return __get_loading_display(data, t0)
def generate_loading_image() -> None:
global LOADING_IMG
im = BLANK_IMG.copy()
draw = ImageDraw.Draw(im)
draw.text((26, 24), "F.O.R.G.E.", fill="white", font=FONT_2)
draw.rectangle((24, 35, 104, 40), outline="white", fill="black")
im.save(LOADING_IMG_PATH)
LOADING_IMG = im
def generate_background_image() -> None:
global BACKGROUND_IMG
im = BLANK_IMG.copy()
draw = ImageDraw.Draw(im)
# tempo
draw.text((7, -2), "TEMPO:", fill="white", font=FONT_1)
draw.rectangle((6, 6, 60, 11), outline="white", fill="black")
# time
draw.text((68, -2), "TIME:", fill="white", font=FONT_1)
draw.rectangle((67, 6, 121, 11), outline="white", fill="black")
# pipes
draw.rectangle((26, 22, 78, 24), fill="white")
draw.rectangle((26, 46, 78, 48), fill="white")
draw.rectangle((78, 34, 105, 36), fill="white")
draw.rectangle((76, 23, 78, 46), fill="white")
# squares
draw.rectangle((12, 16, 26, 30), outline="white", fill="black")
draw.rectangle((41, 16, 55, 30), outline="white", fill="black")
draw.rectangle((12, 40, 26, 54), outline="white", fill="black")
draw.rectangle((41, 40, 55, 54), outline="white", fill="black")
draw.rectangle((70, 28, 84, 42), outline="white", fill="black")
draw.rectangle((99, 28, 113, 42), outline="white", fill="black")
# page
draw.circle((55, 61), 2, outline="white", fill="black")
draw.circle((63, 61), 2, outline="white", fill="black")
draw.circle((71, 61), 2, outline="white", fill="black")
im.save(BACKGROUND_IMG_PATH)
BACKGROUND_IMG = im
def debug_image(im: Image.Image) -> None:
im = im.resize((1280, 640)).convert("RGB")
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))
im.resize((1280, 640)).save("debug.png")
+9 -4
View File
@@ -1,3 +1,4 @@
import logging
import sys
import typing
@@ -6,6 +7,8 @@ from luma.core.interface.serial import i2c
from luma.oled.device import device, sh1106
from PIL import Image
log = logging.getLogger(__name__)
class I2C:
def __init__(self) -> None:
@@ -28,15 +31,17 @@ class I2C:
# Initialize the device. If using SH1106, change ssd1306 to sh1106
self.__device = sh1106(serial_interface, width=128, height=64, rotate=0)
except DeviceNotFoundError:
print("Fatal: OLED not found at 0x3C. Check wiring and run i2cdetect -y 1")
log.critical(
"Fatal: OLED not found at 0x3C. Check wiring and run i2cdetect -y 1"
)
sys.exit(1)
except OSError as e:
if e.errno == 121:
print(
log.critical(
"Fatal: Remote I/O error. I2C bus is locked or physically disconnected."
)
else:
print(f"Fatal OS Error: {e}")
log.critical(f"Fatal OS Error: {e}")
sys.exit(1)
def update_display(self, image: Image.Image) -> None:
@@ -45,5 +50,5 @@ class I2C:
self.__device.display(image)
except OSError as e:
if e.errno == 121:
print("Runtime Error: I2C bus dropped. Check physical connections.")
log.error("Runtime Error: I2C bus dropped. Check physical connections.")
raise
+5
View File
@@ -0,0 +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", "sentences"]
+31
View File
@@ -0,0 +1,31 @@
import logging
from PIL import Image, ImageDraw
from ..display import get_display
from ..forge_data import ForgeData
log = logging.getLogger(__name__)
def debug_image(im: Image.Image) -> None:
im = im.resize((1280, 640)).convert("RGB")
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))
im.resize((1280, 640)).save("debug.png")
log.info("Debug image saved as debug.png")
def main():
data = ForgeData().mock()
image = get_display(data, 0)
debug_image(image)
if __name__ == "__main__":
main()
+69
View File
@@ -0,0 +1,69 @@
import logging
from PIL import ImageDraw
from ..constants import (
BACKGROUND_IMG_PATH,
LOADING_IMG_PATH,
)
from ..display import BLANK_IMG, FONT_1, FONT_2
log = logging.getLogger(__name__)
def generate_loading_image() -> None:
im = BLANK_IMG.copy()
draw = ImageDraw.Draw(im)
draw.text((26, 24), "F.O.R.G.E.", fill="white", font=FONT_2)
draw.rectangle((24, 35, 104, 40), outline="white", fill="black")
im.save(LOADING_IMG_PATH)
log.info(f"Generated {LOADING_IMG_PATH}")
def generate_background_image() -> None:
im = BLANK_IMG.copy()
draw = ImageDraw.Draw(im)
# tempo
draw.text((7, -2), "TEMPO:", fill="white", font=FONT_1)
draw.rectangle((6, 6, 60, 11), outline="white", fill="black")
# time
draw.text((68, -2), "TIME:", fill="white", font=FONT_1)
draw.rectangle((67, 6, 121, 11), outline="white", fill="black")
# pipes
draw.rectangle((26, 22, 78, 24), fill="white")
draw.rectangle((26, 46, 78, 48), fill="white")
draw.rectangle((78, 34, 105, 36), fill="white")
draw.rectangle((76, 23, 78, 46), fill="white")
# squares
draw.rectangle((12, 16, 26, 30), outline="white", fill="black")
draw.rectangle((41, 16, 55, 30), outline="white", fill="black")
draw.rectangle((12, 40, 26, 54), outline="white", fill="black")
draw.rectangle((41, 40, 55, 54), outline="white", fill="black")
draw.rectangle((70, 28, 84, 42), outline="white", fill="black")
draw.rectangle((99, 28, 113, 42), outline="white", fill="black")
# page
draw.circle((55, 61), 2, outline="white", fill="black")
draw.circle((63, 61), 2, outline="white", fill="black")
draw.circle((71, 61), 2, outline="white", fill="black")
im.save(BACKGROUND_IMG_PATH)
log.info(f"Generated {BACKGROUND_IMG_PATH}")
def main():
generate_loading_image()
generate_background_image()
if __name__ == "__main__":
main()
+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()