|
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: MIT
|
4 | 4 | # LCARS MatrixPortal Display
|
| 5 | +# LED brigthness code by Jan Goolsbey |
5 | 6 |
|
6 | 7 | import time
|
7 | 8 | import os
|
8 | 9 | import board
|
9 | 10 | import displayio
|
10 | 11 | from digitalio import DigitalInOut, Pull
|
| 12 | +from simpleio import map_range # For color brightness calculation |
11 | 13 | from adafruit_matrixportal.matrix import Matrix
|
12 | 14 | from adafruit_debouncer import Debouncer
|
13 | 15 |
|
| 16 | +import supervisor |
| 17 | +supervisor.runtime.autoreload = True |
| 18 | + |
14 | 19 | SPRITESHEET_FOLDER = "/bmps"
|
15 | 20 | DEFAULT_FRAME_DURATION = 0.7 # 100ms
|
16 | 21 | AUTO_ADVANCE_LOOPS = 3
|
| 22 | +bitmap = "" |
| 23 | +brightness = 15 # ### Integer value from 0 to 15 |
17 | 24 |
|
18 | 25 | # --- Display setup ---
|
19 |
| -matrix = Matrix(bit_depth=1, width=128, height=64) |
| 26 | +matrix = Matrix(bit_depth=4, width=128, height=64) |
20 | 27 | sprite_group = displayio.Group()
|
21 | 28 | matrix.display.show(sprite_group)
|
22 | 29 |
|
|
47 | 54 | frame_count = 0
|
48 | 55 | frame_duration = DEFAULT_FRAME_DURATION
|
49 | 56 |
|
| 57 | +def image_brightness(new_bright=0): |
| 58 | + """Calculate the white color brightness. |
| 59 | + Returns a white RBG888 color value proportional to `new_bright`.""" |
| 60 | + # Scale brightness value |
| 61 | + bright = int(map_range(new_bright, 0, 15, 0x00, 0xFF)) |
| 62 | + # Recombine and return a composite RGB888 value |
| 63 | + return (bright << 16) + (bright << 8) + bright |
50 | 64 |
|
51 | 65 | def load_image():
|
52 | 66 | """
|
53 | 67 | Load an image as a sprite
|
54 | 68 | """
|
55 | 69 | # pylint: disable=global-statement
|
56 |
| - global current_frame, current_loop, frame_count, frame_duration |
| 70 | + global current_frame, current_loop, frame_count, frame_duration, bitmap |
57 | 71 | while sprite_group:
|
58 | 72 | sprite_group.pop()
|
59 | 73 |
|
60 | 74 | filename = SPRITESHEET_FOLDER + "/" + file_list[current_image]
|
61 | 75 |
|
62 | 76 | bitmap = displayio.OnDiskBitmap(filename)
|
| 77 | + ### Change the palette value proportional to BRIGHTNESS |
| 78 | + bitmap.pixel_shader[1] = image_brightness(brightness) |
63 | 79 | sprite = displayio.TileGrid(
|
64 | 80 | bitmap,
|
65 | 81 | pixel_shader=bitmap.pixel_shader,
|
@@ -102,14 +118,19 @@ def advance_frame():
|
102 | 118 |
|
103 | 119 | advance_image()
|
104 | 120 |
|
| 121 | +last_time = time.monotonic() |
| 122 | + |
| 123 | + |
105 | 124 | while True:
|
106 | 125 | button_down.update()
|
107 | 126 | button_up.update()
|
108 | 127 | if button_up.fell:
|
109 |
| - print("up fell") |
110 |
| - auto_advance = not auto_advance |
111 |
| - if button_down.fell: |
112 |
| - print("down fell") |
113 | 128 | advance_image()
|
114 |
| - advance_frame() |
115 |
| - time.sleep(frame_duration) |
| 129 | + if button_down.fell: |
| 130 | + brightness = (brightness + 2) % 16 |
| 131 | + print(brightness) |
| 132 | + bitmap.pixel_shader[1] = image_brightness(brightness) # ### Change the brightness |
| 133 | + |
| 134 | + if time.monotonic() - last_time > frame_duration: |
| 135 | + advance_frame() |
| 136 | + last_time = time.monotonic() |
0 commit comments