|
| 1 | +""" |
| 2 | +Basic progressbar example script |
| 3 | +adapted for use on MagTag. |
| 4 | +""" |
| 5 | +import time |
| 6 | +import board |
| 7 | +import displayio |
| 8 | +import digitalio |
| 9 | +from adafruit_progressbar import ProgressBar |
| 10 | + |
| 11 | +# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.) |
| 12 | +# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.) |
| 13 | +# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus |
| 14 | +display = board.DISPLAY |
| 15 | +time.sleep(display.time_to_refresh) |
| 16 | + |
| 17 | +# a button will be used to advance the progress |
| 18 | +a_btn = digitalio.DigitalInOut(board.BUTTON_A) |
| 19 | +a_btn.direction = digitalio.Direction.INPUT |
| 20 | +a_btn.pull = digitalio.Pull.UP |
| 21 | + |
| 22 | +# Make the display context |
| 23 | +splash = displayio.Group(max_size=10) |
| 24 | +display.show(splash) |
| 25 | + |
| 26 | +# set progress bar width and height relative to board's display |
| 27 | +BAR_WIDTH = display.width - 40 |
| 28 | +BAR_HEIGHT = 30 |
| 29 | + |
| 30 | +x = display.width // 2 - BAR_WIDTH // 2 |
| 31 | +y = display.height // 3 |
| 32 | + |
| 33 | +# Create a new progress_bar object at (x, y) |
| 34 | +progress_bar = ProgressBar( |
| 35 | + x, y, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x666666, outline_color=0xFFFFFF |
| 36 | +) |
| 37 | + |
| 38 | +# Append progress_bar to the splash group |
| 39 | +splash.append(progress_bar) |
| 40 | + |
| 41 | +current_progress = (time.monotonic() % 101) / 100.0 |
| 42 | +print(current_progress) |
| 43 | +progress_bar.progress = current_progress |
| 44 | + |
| 45 | +# refresh the display |
| 46 | +display.refresh() |
| 47 | + |
| 48 | +prev_a = a_btn.value |
| 49 | +while True: |
| 50 | + cur_a = a_btn.value |
| 51 | + # if a_btn was just pressed down |
| 52 | + if not cur_a and prev_a: |
| 53 | + current_progress += 0.20 |
| 54 | + if current_progress > 1.0: |
| 55 | + current_progress = 0.0 |
| 56 | + print(current_progress) |
| 57 | + progress_bar.progress = current_progress |
| 58 | + |
| 59 | + time.sleep(display.time_to_refresh) |
| 60 | + display.refresh() |
| 61 | + time.sleep(display.time_to_refresh) |
| 62 | + |
| 63 | + prev_a = cur_a |
0 commit comments