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