Skip to content

Commit 7b617d3

Browse files
Merge pull request #29 from jposada202020/adding_color_scale_example
adding new example showing the progress bar feature to change color programmatically
2 parents c48372c + a6c291c commit 7b617d3

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

docs/examples.rst

+10
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/progressbar_simpletest.py
77
:caption: examples/progressbar_simpletest.py
88
:linenos:
9+
10+
11+
Color Scale
12+
------------
13+
14+
Example showing how to change the progressbar color while updating the values
15+
16+
.. literalinclude:: ../examples/progressbar_displayio_blinka_color_scale.py
17+
:caption: examples/progressbar_displayio_blinka_color_scale.py
18+
:linenos:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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)

examples/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
Adafruit-Blinka
1010
adafruit-blinka-displayio
1111
blinka-displayio-pygamedisplay
12+
adafruit-circuitpython-fancyled

0 commit comments

Comments
 (0)