92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
# Standalone MicroPython Voice Animation: Battery Low
|
|
# Exposes run(text="", frames=...) and runs automatically if executed directly.
|
|
import time
|
|
|
|
try:
|
|
import board_config
|
|
display = getattr(board_config, 'display_instance', None)
|
|
except Exception:
|
|
board_config = None
|
|
display = None
|
|
|
|
def draw_center_text(d, msg, y, w, scale=1, c=1):
|
|
char_w = 8 * scale
|
|
x = max(0, (w - len(msg) * char_w) // 2)
|
|
if hasattr(d, 'text_large') and scale > 1:
|
|
try:
|
|
d.text_large(msg, x, y, scale, c)
|
|
return
|
|
except Exception:
|
|
pass
|
|
d.text(msg, x, y, c)
|
|
|
|
def run(text="", frames=100):
|
|
if not display:
|
|
print("board_config.display_instance is not available. Skipping animation.")
|
|
return
|
|
|
|
width = getattr(display, 'width', 400)
|
|
height = getattr(display, 'height', 300)
|
|
cx, cy = width // 2, height // 2
|
|
|
|
label = text if text else "LOW BATTERY!"
|
|
frame = 0
|
|
|
|
try:
|
|
while True:
|
|
if frames is not None and frames > 0 and frame >= frames:
|
|
break
|
|
|
|
display.clear(0)
|
|
|
|
# Center coordinates adjusted for warning triangle on the left side
|
|
# Battery offset to the right
|
|
bat_x = cx + 20
|
|
bat_y = cy - 10
|
|
|
|
# Draw battery container outline
|
|
display.rect(bat_x - 65, bat_y - 30, 115, 60, 1)
|
|
display.rect(bat_x - 64, bat_y - 29, 113, 58, 1) # thicker
|
|
# Battery cap tip
|
|
display.fill_rect(bat_x + 50, bat_y - 12, 10, 24, 1)
|
|
|
|
# Low charge blinking segment on the left
|
|
blink_on = (frame // 4) % 2 == 0
|
|
if blink_on:
|
|
# Draw small low charge bar
|
|
display.fill_rect(bat_x - 58, bat_y - 23, 25, 46, 1)
|
|
|
|
# Draw a warning triangle on the left
|
|
warn_x = cx - 110
|
|
warn_y = cy - 10
|
|
|
|
# Warning triangle outline
|
|
display.line(warn_x, warn_y - 25, warn_x - 25, warn_y + 20, 1)
|
|
display.line(warn_x, warn_y - 25, warn_x + 25, warn_y + 20, 1)
|
|
display.line(warn_x - 25, warn_y + 20, warn_x + 25, warn_y + 20, 1)
|
|
|
|
# Warning exclamation mark
|
|
display.fill_rect(warn_x - 2, warn_y - 8, 4, 16, 1)
|
|
display.fill_rect(warn_x - 2, warn_y + 11, 4, 4, 1)
|
|
|
|
# Draw diagonal warning grid lines in battery background if empty to look stylish
|
|
if not blink_on:
|
|
for diag in range(-20, 40, 10):
|
|
display.line(bat_x - 58, bat_y - 23 + diag, bat_x - 38, bat_y + 23 + diag, 1)
|
|
|
|
# Draw centered text label at the bottom
|
|
draw_center_text(display, label, height - 30, width, scale=1, c=1)
|
|
|
|
display.show()
|
|
frame += 1
|
|
|
|
try:
|
|
time.sleep_ms(60)
|
|
except AttributeError:
|
|
time.sleep(0.06)
|
|
except KeyboardInterrupt:
|
|
print("Animation interrupted by user.")
|
|
|
|
if __name__ in ('__main__', '__name__'):
|
|
run()
|