|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2021 Hugo Dahl for Adafruit Industries |
| 4 | +# SPDX-FileCopyrightText: 2021 Jose David M. |
| 5 | +# SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +# Before you can run this example, you will need to install the |
| 8 | +# required libraries identifies in the `requirements.txt` file. |
| 9 | +# You can do so automatically by using the "pip" utility. |
| 10 | + |
| 11 | +""" |
| 12 | +Shows the ability of the progress bar to change color on the fly. |
| 13 | +This example is and adaptation from the progressbar_displayio_blinka test |
| 14 | +""" |
| 15 | + |
| 16 | +import time |
| 17 | +import adafruit_fancyled.adafruit_fancyled as fancy |
| 18 | +import displayio |
| 19 | +from blinka_displayio_pygamedisplay import PyGameDisplay |
| 20 | +from adafruit_progressbar.horizontalprogressbar import ( |
| 21 | + HorizontalProgressBar, |
| 22 | + HorizontalFillDirection, |
| 23 | +) |
| 24 | + |
| 25 | +display = PyGameDisplay(width=320, height=240, auto_refresh=False) |
| 26 | +splash = displayio.Group(max_size=10) |
| 27 | +display.show(splash) |
| 28 | + |
| 29 | +# Setting up the grayscale values, You could use a different scale, and add more entries |
| 30 | +# to have detailed transitions |
| 31 | +# see learning guide regarding the FancyLed library |
| 32 | +# https://learn.adafruit.com/fancyled-library-for-circuitpython/palettes |
| 33 | +grad = [ |
| 34 | + (0.0, 0x000000), |
| 35 | + (0.20, 0x333333), |
| 36 | + (0.40, 0x666666), |
| 37 | + (0.60, 0x999999), |
| 38 | + (0.80, 0xCCCCCC), |
| 39 | + (1.0, 0xEEEEEE), |
| 40 | +] |
| 41 | + |
| 42 | +# Creating the grayscale Palette using the FancyLed Library |
| 43 | +palette = fancy.expand_gradient(grad, 50) |
| 44 | + |
| 45 | +colors = list() |
| 46 | + |
| 47 | +# We create an equal space palette. This is done for convenience and clarity as we use |
| 48 | +# a value from 0 to 100 in our ProgressBar |
| 49 | +for i in range(99): |
| 50 | + color = fancy.palette_lookup(palette, i / 100) |
| 51 | + colors.append(color.pack()) |
| 52 | + |
| 53 | +# Background creation |
| 54 | +color_bitmap = displayio.Bitmap(display.width, display.height, 1) |
| 55 | +color_palette = displayio.Palette(1) |
| 56 | +color_palette[0] = 0x2266AA # Teal-ish-kinda |
| 57 | + |
| 58 | +bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) |
| 59 | +splash.append(bg_sprite) |
| 60 | + |
| 61 | + |
| 62 | +horizontal_bar = HorizontalProgressBar( |
| 63 | + (10, 80), |
| 64 | + (180, 40), |
| 65 | + fill_color=0x990099, |
| 66 | + outline_color=0x0000FF, |
| 67 | + bar_color=0x00FF00, |
| 68 | + direction=HorizontalFillDirection.LEFT_TO_RIGHT, |
| 69 | +) |
| 70 | +splash.append(horizontal_bar) |
| 71 | + |
| 72 | +# List of step values for the progress bar |
| 73 | +test_value_range_1 = [i for i in range(99)] |
| 74 | + |
| 75 | +# Must check display.running in the main loop! |
| 76 | +while display.running: |
| 77 | + print("\nDemonstration of values between 0 and 100 - Horizontal") |
| 78 | + for val in test_value_range_1: |
| 79 | + horizontal_bar.value = val |
| 80 | + horizontal_bar.bar_color = colors[val] |
| 81 | + display.refresh() |
| 82 | + time.sleep(0.1) |
0 commit comments