Simplified pattern handling

This commit is contained in:
Maximilian Giller 2024-03-03 16:17:38 +01:00
parent 6e42689a91
commit d10359f8d1

View file

@ -69,31 +69,15 @@ class MatrixDisplay:
draw.rectangle((0, 0, 31, 7), outline="white", fill="white") draw.rectangle((0, 0, 31, 7), outline="white", fill="white")
async def pattern(self, pattern: str = "0,1", off_ms: int = 500, on_ms: int = 500): async def pattern(self, pattern: str = "0,1", off_ms: int = 500, on_ms: int = 500):
# Preprocess pattern # Parse
pattern_steps = [step.strip() == "1" for step in pattern.split(",")] pattern_steps = [step.strip() == "1" for step in pattern.split(",")]
# Execute
class SimplifiedStep:
state: bool
size: int
def __init__(self, state: bool) -> None:
self.state = state
self.size = 1
pattern_simplified: list[SimplifiedStep] = []
for step in pattern_steps: for step in pattern_steps:
if len(pattern_simplified) == 0 or step != pattern_simplified[-1].state: if step:
pattern_simplified.append(SimplifiedStep(step, off_ms, on_ms))
else:
pattern_simplified[-1].size += 1
# Execute pattern
for step in pattern_simplified:
if step.state:
self.turn_full() self.turn_full()
else: else:
self.turn_off() self.turn_off()
await asyncio.sleep(step.size * (on_ms if step.state else off_ms) / 1000) await asyncio.sleep((on_ms if step else off_ms) / 1000)
def show_current_time(self): def show_current_time(self):
self.device.contrast(self.contrast) self.device.contrast(self.contrast)