buba/buba/BubaAnimator.py

76 lines
2.2 KiB
Python

import logging
from datetime import datetime
from threading import Thread
from time import sleep
from buba.BubaCmd import BubaCmd
class BubaAnimation:
def __init__(self, buba: BubaCmd):
self.buba = buba
pass
def run(self):
pass
def __repr__(self):
return f"<{type(self).__name__}>"
class BubaTime(BubaAnimation):
def __init__(self, buba: BubaCmd):
super().__init__(buba)
def run(self):
self.buba.simple_text(page=0, row=0, col=0, text="Buba")
self.buba.simple_text(page=0, row=1, col=0, text="Choas Computer Club")
self.buba.simple_text(page=0, row=2, col=0, text="Hansestadt Hamburg")
self.buba.simple_text(page=0, row=3, col=0, text="Hello, world!")
self.buba.set_page(0)
for i in range(5):
self.buba.simple_text(page=0, row=0, col=93, text=datetime.now().strftime("%H:%M:%S"))
sleep(1)
class BubaCharset(BubaAnimation):
def __init__(self, buba: BubaCmd, single=None):
super().__init__(buba)
self.charset = bytes(range(256)).decode("CP437")
self.single = single
def run(self):
if self.single is not None:
while True:
self.render(self.single)
sleep(1)
else:
for page in range(16):
self.render(page)
sleep(5)
def render(self, page):
for row in range(4):
start = ((page + row) * 16) % 256
self.buba.simple_text(page=0, row=row, col=0, text=f"${start:02x}: {self.charset[start:start + 16]}")
class BubaAnimator:
def __init__(self, buba: BubaCmd):
self.log = logging.getLogger(__name__)
self.buba = buba
self.animations = []
Thread(target=self.run, daemon=True).start()
def run(self):
while True:
if len(self.animations) == 0:
a = BubaTime(self.buba)
a.run()
else:
for a in self.animations:
self.log.debug(f"Starting animation: {a}")
a.run()
def add(self, animation, *args, **kwargs):
self.animations.append(animation(self.buba, *args, **kwargs))