-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeathereminDisplay2.py
More file actions
executable file
·201 lines (154 loc) · 7.11 KB
/
Copy pathfeathereminDisplay2.py
File metadata and controls
executable file
·201 lines (154 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
Class to wrap the display hardware in standard methods.
Adafruit 2.2" TFT Display - 2.2" 18-bit color TFT LCD display
AdafruitProduct ID: 1480
and using the Adafruit CircuitPython DisplayIO ILI9341 or compatible module.
Uses the SPI ("four wire") interface.
Version 2, using a BMP file for a background.
"""
import board
import terminalio
import displayio
import gc
import random
import time
import sys
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
import adafruit_ili9341
import adafruit_imageload
from digitalio import DigitalInOut, Direction
from adafruit_vl53l0x import VL53L0X
'''
The display object.
The GPIO pins to use are passed in on object creation.
'''
class FeathereminDisplay:
MESSAGE_TEXT_COLOR = 0xFF0000
STATUS_TEXT_COLOR = 0X000000
FONT_TO_LOAD = "fonts/SFDigitalReadout-Medium-24.bdf"
BACKGROUND_BITMAP = "images/background.bmp"
SPRITE_BITMAP = "images/led_sprite_sheet.bmp"
def __init__(self, p_rotation, boardPinCS, boardPinDC, boardPinReset, loadBackground) -> None:
gc.collect()
start_mem = gc.mem_free()
print(f"FeathereminDisplay: start free mem: {start_mem}")
# set this to false to get it to run, for debugging
show_background = loadBackground
self.init_OK = False
self.text_area_1_ = None
self.text_area_2_ = None
self.text_area_3_ = None
self.text_area_l_ = None
self.text_area_r_ = None
try:
displayio.release_displays()
spi = board.SPI()
display_bus = displayio.FourWire(spi, command=boardPinDC, chip_select=boardPinCS, reset=boardPinReset)
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240, rotation=p_rotation)
except:
print("**** FeathereminDisplay: No ILI9341 display found?")
# TODO: what to do if construction fails?
return
bitmap, palette = None, None
if show_background:
try:
bitmap, palette = adafruit_imageload.load(
FeathereminDisplay.BACKGROUND_BITMAP, bitmap=displayio.Bitmap, palette=displayio.Palette)
# FIXME: why do I have to set this here? isn't this global, so to speak?
# self.MESSAGE_TEXT_COLOR = 0xFF0000 # red
except:
print("**** FeathereminDisplay: Can't load background bitmap?")
# TODO: what to do if construction fails?
return
else:
bitmap = displayio.Bitmap(320, 240, 1)
palette = displayio.Palette(1)
palette[0] = 0x0000FF # blue
self.MESSAGE_TEXT_COLOR = 0x000000 # black
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette) # Create a TileGrid to hold the bitmap
background_group = displayio.Group() # Create a Group to hold the TileGrid
background_group.append(tile_grid) # Add the TileGrid to the Group
display.show(background_group) # Add the Group to the Display
font = None
fontScale = 2
try:
font = bitmap_font.load_font(FeathereminDisplay.FONT_TO_LOAD)
fontScale = 1
except:
print("FeathereminDisplay:Can't load custom font!")
font = terminalio.FONT
# Labels
# Because this is scaled by 2, the coordinates are half what you'd expect.
# (The display area is effectively 160 x 120)
#
text_group = displayio.Group(scale=fontScale, x=0, y=72)
self.text_area_1_ = label.Label(font, text="1", color=FeathereminDisplay.MESSAGE_TEXT_COLOR, x=20, y=4)
text_group.append(self.text_area_1_) # Subgroup for text scaling
self.text_area_2_ = label.Label(font, text="2", color=FeathereminDisplay.MESSAGE_TEXT_COLOR, x=20, y=50)
text_group.append(self.text_area_2_)
self.text_area_3_ = label.Label(font, text="3", color=FeathereminDisplay.MESSAGE_TEXT_COLOR, x=20, y=100)
text_group.append(self.text_area_3_)
self.text_area_l_ = label.Label(font, text="4", color=FeathereminDisplay.STATUS_TEXT_COLOR, x=6, y=666)
text_group.append(self.text_area_l_)
self.text_area_r_ = label.Label(font, text="5", color=FeathereminDisplay.STATUS_TEXT_COLOR, x=66, y=666)
text_group.append(self.text_area_r_)
background_group.append(text_group)
# Load the sprite sheet bitmap
sprite_sheet, led_palette = adafruit_imageload.load(FeathereminDisplay.SPRITE_BITMAP,
bitmap=displayio.Bitmap,
palette=displayio.Palette)
# The color we want to be transparent seems to be the last one in the palette.
# Is this always true?
led_palette.make_transparent(len(led_palette)-1)
# Create a sprite (tilegrid)
self.led_sprite = displayio.TileGrid(sprite_sheet, pixel_shader=led_palette,
width = 1, height = 1,
tile_width = 16, tile_height = 16)
# Create a Group to hold the sprite
led_group = displayio.Group(scale=1)
led_group.append(self.led_sprite)
# Set sprite location
led_group.x = 25
led_group.y = 203
background_group.append(led_group)
self.init_OK = True
gc.collect()
now_mem = gc.mem_free()
used_mem = start_mem - now_mem
print("--------------------------------")
print(f"FeathereminDisplay: Now free mem: {now_mem:8}")
print(f"FeathereminDisplay: Used mem: {used_mem:8}")
# end __init__
# Make the LED red? the lit, red led is first in the sprite sheet, so index 0
def setLEDStatus(self, status: bool):
self.led_sprite[0] = 0 if status else 1
# "setters" for the text areas
#
def setTextArea1(self, pText):
self.text_area_1_._update_text(pText)
def setTextArea2(self, pText):
self.text_area_2_._update_text(pText)
def setTextArea3(self, pText):
self.text_area_3_._update_text(pText)
def setTextAreaL(self, pText):
self.text_area_l_._update_text(pText)
def setTextAreaR(self, pText):
self.text_area_r_._update_text(pText)
'''
This does not return!
'''
def test(self) -> NoReturn:
self.setTextArea2("You are a")
self.setTextArea3("hideous orangutan!")
self.setTextAreaL("")
self.setTextAreaR("Testing")
i = 0
while True:
self.setLEDStatus(random.choice([True, False]))
self.setTextArea1(f"Tick {i}...")
i += 1
time.sleep(.5)
print("Display test waiting, so display doesn't get erased.")
while True:
pass