refactor: Use asyncio for non-blocking display animations

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-03-28 17:41:33 -06:00
parent eb003853e6
commit 03730ec60f
2 changed files with 27 additions and 26 deletions

View File

@@ -6,6 +6,7 @@ from smbus2 import SMBus
import time
import math
import random
import asyncio
"""
Each segment of the 14segment display is represented by a single bit
@@ -249,7 +250,7 @@ class StarburstHT16K33:
self.show()
# Animated scrolling text with easing curves
def scroll_text(self, text, scroll_speed=8, pulse=False, loop=False, easing="linear"):
async def scroll_text(self, text, scroll_speed=8, pulse=False, loop=False, easing="linear"):
"""
Demonstrates:
• nested functions
@@ -295,7 +296,7 @@ class StarburstHT16K33:
b = 1 + int(wave * 14)
self.set_brightness(b)
time.sleep(delay)
await asyncio.sleep(delay)
if not loop:
break
@@ -303,13 +304,13 @@ class StarburstHT16K33:
self.set_brightness(base_brightness)
# Marquee wrapper
def marquee(self, text, scroll_speed=8, pulse=False, cycles=1, easing="linear"):
async def marquee(self, text, scroll_speed=8, pulse=False, cycles=1, easing="linear"):
"""Just calls scroll_text() multiple times."""
for _ in range(cycles):
self.scroll_text(text, scroll_speed, pulse, False, easing)
await self.scroll_text(text, scroll_speed, pulse, False, easing)
# Per-segment chase effect
def chase(self, speed=8, cycles=2):
async def chase(self, speed=8, cycles=2):
"""Lights one segment at a time across all digits."""
delay = 0.15 * (0.85 ** speed)
for _ in range(cycles):
@@ -317,10 +318,10 @@ class StarburstHT16K33:
for pos in range(8):
self.buffer[pos] = segmask
self.show()
time.sleep(delay)
await asyncio.sleep(delay)
# Rainbow brightness sweep
def rainbow(self, cycles=2, speed=8):
async def rainbow(self, cycles=2, speed=8):
"""Brightness follows a sine wave."""
steps = 60
delay = 0.05 * (0.85 ** speed)
@@ -331,10 +332,10 @@ class StarburstHT16K33:
b = 1 + int(14 * 0.5 * (1 - math.cos(2 * math.pi * phase)))
self.set_brightness(b)
self.show()
time.sleep(delay)
await asyncio.sleep(delay)
# Sparkle effect
def sparkle(self, duration=2.0, density=0.15, speed=8):
async def sparkle(self, duration=2.0, density=0.15, speed=8):
"""Random segments flicker like stars."""
delay = 0.05 * (0.85 ** speed)
end = time.time() + duration
@@ -345,9 +346,9 @@ class StarburstHT16K33:
if random.random() < density:
self.buffer[pos] = random.choice(ALL_SEGMENTS)
self.show()
time.sleep(delay)
await asyncio.sleep(delay)
def main():
async def main():
"""
Demonstrates:
• object creation
@@ -357,23 +358,23 @@ def main():
disp = StarburstHT16K33(brightness=10)
disp.write_text("HELLO")
time.sleep(1)
await asyncio.sleep(1)
disp.scroll_text("Learning Python!", scroll_speed=1, easing="inout", pulse=True)
disp.marquee("HP FONT DEMO ", scroll_speed=8, cycles=2)
disp.marquee("Learning Python! ", scroll_speed=1, cycles=5)
await disp.scroll_text("Learning Python!", scroll_speed=1, easing="inout", pulse=True)
await disp.marquee("HP FONT DEMO ", scroll_speed=8, cycles=2)
await disp.marquee("Learning Python! ", scroll_speed=1, cycles=5)
disp.chase(speed=8)
await disp.chase(speed=8)
disp.write_text("RAINBOW")
disp.rainbow()
await disp.rainbow()
disp.write_text("SPARKLE")
disp.sparkle(duration=2.0)
await disp.sparkle(duration=2.0)
disp.write_text("DONE")
time.sleep(1)
await asyncio.sleep(1)
disp.clear()
disp.show()
if __name__ == "__main__":
main()
asyncio.run(main())

12
main.py
View File

@@ -88,19 +88,19 @@ async def manage_display(disp):
while True:
await asyncio.sleep(2)
disp.scroll_text('WELCOME TO PROTOSPACE!', scroll_speed=2, easing='inout', pulse=True)
await disp.scroll_text('WELCOME TO PROTOSPACE!', scroll_speed=2, easing='inout', pulse=True)
disp.set_brightness(15)
disp.marquee('PRESENTING...', scroll_speed=8, cycles=1)
disp.marquee('THE BASH REGISTER', scroll_speed=2, cycles=1)
await disp.marquee('PRESENTING...', scroll_speed=8, cycles=1)
await disp.marquee('THE BASH REGISTER', scroll_speed=2, cycles=1)
disp.set_brightness(15)
disp.chase(speed=8)
await disp.chase(speed=8)
disp.write_text('SEND TO')
disp.rainbow()
await disp.rainbow()
disp.set_brightness(15)
disp.marquee('PROTOSPACE.CA/SIGN', scroll_speed=2, cycles=3)
await disp.marquee('PROTOSPACE.CA/SIGN', scroll_speed=2, cycles=3)