Skip to content

Commit 6f15b5d

Browse files
initial repo load
1 parent 3b22a4a commit 6f15b5d

1 file changed

Lines changed: 172 additions & 3 deletions

File tree

examples/palettefader_simpletest.py

100644100755
Lines changed: 172 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,173 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2-
# SPDX-FileCopyrightText: Copyright (c) 2022 JG for Cedar Grove Maker Studios
1+
# SPDX-FileCopyrightText: 2022-09-06 JG for Cedar Grove Maker Studios
2+
# SPDX-License-Identifier: MIT
33
#
4-
# SPDX-License-Identifier: Unlicense
4+
# palettefader_simpletest.py
5+
6+
"""
7+
This is a PaletteFader class example for an Adafruit MatrixPortal with a
8+
32x64 RGB LED display panel. Two text labels, a displayio shape, a vectorio
9+
shape, and an icon spritesheet tile are placed on the display in a layer over a
10+
background image. The Matrix Portal's analog voltage input on pin A0 controls
11+
the foreground layer brightness over the fixed-brightness background image.
12+
"""
13+
14+
import time
15+
import random
16+
import board
17+
from analogio import AnalogIn
18+
import displayio
19+
import vectorio
20+
import terminalio
21+
from simpleio import map_range
22+
from adafruit_matrixportal.matrix import Matrix
23+
from adafruit_display_text.label import Label
24+
from adafruit_display_shapes.rect import Rect
25+
26+
# from adafruit_display_shapes.circle import Circle
27+
import adafruit_imageload
28+
from cedargrove_palettefader import PaletteFader
29+
30+
# fmt: off
31+
# Define a few colors
32+
YELLOW = 0xFFFF00 # temperature
33+
AQUA = 0x00FFFF # humidity
34+
FUCHSIA = 0xFF00FF # watchdog
35+
36+
# Define icon graphic object parameters
37+
ICON_SPRITESHEET_FILE = "weather-icons.bmp"
38+
ICON_SPRITE_WIDTH = 16
39+
ICON_SPRITE_HEIGHT = 16
40+
ICON_SPRITE = 0 # Select a tile from the spritesheet
41+
42+
# Define background graphic object parameters
43+
BKG_BRIGHTNESS = 0.2 # Initial brightness level
44+
BKG_GAMMA = 0.65 # Works nicely for brightness = 0.2
45+
BKG_IMAGE_FILE = "background.bmp"
46+
47+
# Instantiate fader potentiometer
48+
ANALOG_FADER = True # True to enable; False to disable
49+
fader = AnalogIn(board.A0)
50+
51+
# Instantiate RGB LED matrix display panel
52+
DISPLAY_BRIGHTNESS = 0.3 # Initial brightness level
53+
DISPLAY_BIT_DEPTH = 6 # Default is 2-bits; maximum of 6-bits
54+
display = Matrix(bit_depth=DISPLAY_BIT_DEPTH).display
55+
display.rotation = 270 # Portrait orientation; MatrixPortal board on bottom
56+
DISPLAY_CENTER = (display.width // 2, display.height // 2)
57+
# fmt: on
58+
59+
# Define the primary and foreground display groups
60+
primary_group = displayio.Group()
61+
fg_group = displayio.Group()
62+
63+
# Load the background image and source color palette
64+
bkg_bitmap, bkg_palette_source = adafruit_imageload.load(
65+
BKG_IMAGE_FILE, bitmap=displayio.Bitmap, palette=displayio.Palette
66+
)
67+
# Instantiate background PaletteFader object and display on-screen
68+
bkg_faded = PaletteFader(bkg_palette_source, BKG_BRIGHTNESS, BKG_GAMMA, normalize=True)
69+
bkg_tile = displayio.TileGrid(bkg_bitmap, pixel_shader=bkg_faded.palette)
70+
primary_group.append(bkg_tile)
71+
72+
display.show(primary_group)
73+
74+
# Load the icon spritesheet and source palette
75+
icon_spritesheet, icon_palette_source = adafruit_imageload.load(
76+
ICON_SPRITESHEET_FILE, bitmap=displayio.Bitmap, palette=displayio.Palette
77+
)
78+
# Set transparency for icon source palette index 0
79+
icon_palette_source.make_transparent(0)
80+
81+
# Instantiate the icon TileGrid and place it on the display
82+
icon_tile = displayio.TileGrid(
83+
icon_spritesheet,
84+
pixel_shader=icon_palette_source,
85+
tile_width=ICON_SPRITE_WIDTH,
86+
tile_height=ICON_SPRITE_HEIGHT,
87+
)
88+
icon_tile.x = DISPLAY_CENTER[0] - 8
89+
icon_tile.y = 22
90+
fg_group.append(icon_tile)
91+
92+
# Place a displayio display_shapes rectangle in the upper left corner
93+
watchdog = Rect(0, 0, 5, 5, fill=FUCHSIA, outline=AQUA, stroke=1)
94+
fg_group.append(watchdog)
95+
96+
# Define the text labels. Add an attribute for the group items palette.
97+
temperature = Label(terminalio.FONT)
98+
temperature.anchor_point = (0.5, 0.5)
99+
temperature.anchored_position = (DISPLAY_CENTER[0], 14)
100+
temperature.color = YELLOW
101+
fg_group.append(temperature)
102+
103+
humidity = Label(terminalio.FONT)
104+
humidity.anchor_point = (0.5, 0.5)
105+
humidity.anchored_position = (DISPLAY_CENTER[0], 45)
106+
humidity.color = AQUA
107+
fg_group.append(humidity)
108+
109+
# Place a vectorio circle in the upper right corner
110+
sun_palette = displayio.Palette(1)
111+
sun_palette[0] = YELLOW
112+
sun = vectorio.Circle(pixel_shader=sun_palette, radius=8, x=30, y=0)
113+
fg_group.append(sun)
114+
115+
# pylint: disable=protected-access
116+
# Create foreground group color list from ._palette and .pixel_shader contents
117+
fg_colors_source = []
118+
for i, group_object in enumerate(fg_group):
119+
if hasattr(group_object, "_palette"):
120+
# It's a displayio display_shapes or label object
121+
for j, color in enumerate(group_object._palette):
122+
fg_colors_source.append(color)
123+
elif hasattr(group_object, "pixel_shader"):
124+
# It's a bitmap or vectorio object
125+
for j, color in enumerate(group_object.pixel_shader):
126+
fg_colors_source.append(color)
127+
128+
primary_group.append(fg_group)
129+
130+
# Instantiate foreground color list PaletteFader object
131+
fg_colors = PaletteFader(fg_colors_source, DISPLAY_BRIGHTNESS, normalize=False)
132+
133+
# ### Primary code loop starts here ###
134+
135+
# Reset the information and fader refresh timers; None for initial run
136+
info_refresh_timer = None
137+
fader_refresh_timer = None
138+
139+
while True:
140+
# Update the on-screen information every 10 seconds and on initial run
141+
if (not info_refresh_timer) or (time.monotonic() - info_refresh_timer) > 10:
142+
# Get a random temperature and humidity value
143+
current_temperature = random.randrange(500, 1000) / 10
144+
temperature.text = f"{current_temperature:.0f}°"
145+
current_humidity = random.randrange(200, 900) / 10
146+
humidity.text = f"{current_humidity:.0f}%"
147+
icon_tile[0] = random.randrange(0, 17)
148+
149+
info_refresh_timer = time.monotonic()
150+
151+
# Update brightness fader values every 0.1 second and on initial run
152+
if (not fader_refresh_timer) or (time.monotonic() - fader_refresh_timer) > 0.1:
153+
154+
if ANALOG_FADER:
155+
# Read the potentiometer and calculate a new brightness value
156+
DISPLAY_BRIGHTNESS = int(map_range(fader.value, 300, 54000, 5, 100)) / 100
157+
158+
# Update foreground group ._palette and .pixel_shader contents
159+
fg_colors.brightness = DISPLAY_BRIGHTNESS
160+
fg_colors_index = 0
161+
for i, _ in enumerate(fg_group):
162+
if hasattr(fg_group[i], "_palette"):
163+
# For a displayio display_shapes or label object
164+
for j, _ in enumerate(fg_group[i]._palette):
165+
fg_group[i]._palette[j] = fg_colors.palette[fg_colors_index]
166+
fg_colors_index += 1
167+
elif hasattr(fg_group[i], "pixel_shader"):
168+
# For a bitmap or vectorio object
169+
for j, _ in enumerate(fg_group[i].pixel_shader):
170+
fg_group[i].pixel_shader[j] = fg_colors.palette[fg_colors_index]
171+
fg_colors_index += 1
172+
173+
fader_refresh_timer = time.monotonic()

0 commit comments

Comments
 (0)