Skip to content

Commit edc9830

Browse files
authored
Merge pull request #35 from makermelissa/refactor
Refactored to use PortalBase
2 parents c298bc3 + 789cee8 commit edc9830

File tree

8 files changed

+72
-1063
lines changed

8 files changed

+72
-1063
lines changed

adafruit_magtag/fakerequests.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

adafruit_magtag/graphics.py

Lines changed: 9 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@
2323
* Adafruit CircuitPython firmware for the supported boards:
2424
https://github.com/adafruit/circuitpython/releases
2525
26+
* Adafruit's PortalBase library: https://github.com/adafruit/Adafruit_CircuitPython_PortalBase
27+
2628
"""
2729

2830
import gc
2931
from time import sleep
3032
import board
31-
import displayio
33+
from adafruit_portalbase.graphics import GraphicsBase
3234

3335
__version__ = "0.0.0-auto.0"
3436
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git"
3537

3638

37-
class Graphics:
39+
class Graphics(GraphicsBase):
3840
"""Graphics Helper Class for the MagTag Library
3941
4042
:param default_bg: The path to your default background image file or a hex color.
@@ -50,53 +52,13 @@ class Graphics:
5052
def __init__(
5153
self, *, default_bg=0xFFFFFF, auto_refresh=True, rotation=270, debug=False
5254
):
53-
5455
self._debug = debug
55-
if not hasattr(board, "DISPLAY"):
56-
import adafruit_il0373 # pylint: disable=import-outside-toplevel
57-
58-
displayio.release_displays()
59-
display_bus = displayio.FourWire(
60-
board.SPI(),
61-
command=board.EPD_DC,
62-
chip_select=board.EPD_CS,
63-
reset=board.EPD_RESET,
64-
baudrate=1000000,
65-
)
66-
67-
self.display = adafruit_il0373.IL0373(
68-
display_bus,
69-
width=296,
70-
height=128,
71-
rotation=rotation,
72-
black_bits_inverted=False,
73-
color_bits_inverted=False,
74-
grayscale=True,
75-
refresh_time=1,
76-
seconds_per_frame=1,
77-
)
78-
else:
79-
self.display = board.DISPLAY
80-
self.display.rotation = rotation
81-
56+
self.display = board.DISPLAY
57+
self.display.rotation = rotation
8258
self.auto_refresh = auto_refresh
83-
84-
if self._debug:
85-
print("Init display")
86-
self.splash = displayio.Group(max_size=15)
8759
self._qr_group = None
88-
if self._debug:
89-
print("Init background")
90-
self._bg_group = displayio.Group(max_size=1)
91-
self._bg_file = None
92-
self.splash.append(self._bg_group)
93-
self.display.show(self.splash)
94-
95-
# set the default background
96-
if default_bg is not None:
97-
self.set_background(default_bg)
9860

99-
gc.collect()
61+
super().__init__(board.DISPLAY, default_bg=default_bg, debug=debug)
10062

10163
def set_background(self, file_or_color, position=None):
10264
"""The background image to a bitmap file.
@@ -105,39 +67,7 @@ def set_background(self, file_or_color, position=None):
10567
:param tuple position: Optional x and y coordinates to place the background at.
10668
10769
"""
108-
while self._bg_group:
109-
self._bg_group.pop()
110-
111-
if not position:
112-
position = (0, 0) # default in top corner
113-
114-
if not file_or_color:
115-
return # we're done, no background desired
116-
if self._bg_file:
117-
self._bg_file.close()
118-
if isinstance(file_or_color, str): # its a filenme:
119-
self._bg_file = open(file_or_color, "rb")
120-
background = displayio.OnDiskBitmap(self._bg_file)
121-
self._bg_sprite = displayio.TileGrid(
122-
background,
123-
pixel_shader=displayio.ColorConverter(),
124-
x=position[0],
125-
y=position[1],
126-
)
127-
elif isinstance(file_or_color, int):
128-
# Make a background color fill
129-
color_bitmap = displayio.Bitmap(self.display.width, self.display.height, 1)
130-
color_palette = displayio.Palette(1)
131-
color_palette[0] = file_or_color
132-
self._bg_sprite = displayio.TileGrid(
133-
color_bitmap,
134-
pixel_shader=color_palette,
135-
x=position[0],
136-
y=position[1],
137-
)
138-
else:
139-
raise RuntimeError("Unknown type of background")
140-
self._bg_group.append(self._bg_sprite)
70+
super().set_background(file_or_color, position)
14171
if self.auto_refresh:
14272
self.display.refresh()
14373
gc.collect()
@@ -153,52 +83,7 @@ def qrcode(
15383
:param y: The y position of upper left corner of the QR code on the display.
15484
15585
"""
156-
import adafruit_miniqr # pylint: disable=import-outside-toplevel
157-
158-
# generate the QR code
159-
for qrtype in range(1, 5):
160-
try:
161-
qrcode = adafruit_miniqr.QRCode(qr_type=qrtype)
162-
qrcode.add_data(qr_data)
163-
qrcode.make()
164-
break
165-
except RuntimeError:
166-
pass
167-
# print("Trying with larger code")
168-
else:
169-
raise RuntimeError("Could not make QR code")
170-
# monochrome (2 color) palette
171-
palette = displayio.Palette(2)
172-
palette[0] = 0xFFFFFF
173-
palette[1] = qr_color
174-
175-
# pylint: disable=invalid-name
176-
# bitmap the size of the matrix, plus border, monochrome (2 colors)
177-
qr_bitmap = displayio.Bitmap(
178-
qrcode.matrix.width + 2, qrcode.matrix.height + 2, 2
179-
)
180-
for i in range(qr_bitmap.width * qr_bitmap.height):
181-
qr_bitmap[i] = 0
182-
183-
# transcribe QR code into bitmap
184-
for xx in range(qrcode.matrix.width):
185-
for yy in range(qrcode.matrix.height):
186-
qr_bitmap[xx + 1, yy + 1] = 1 if qrcode.matrix[xx, yy] else 0
187-
188-
# display the QR code
189-
qr_sprite = displayio.TileGrid(qr_bitmap, pixel_shader=palette)
190-
if self._qr_group:
191-
try:
192-
self._qr_group.pop()
193-
except IndexError: # later test if empty
194-
pass
195-
else:
196-
self._qr_group = displayio.Group()
197-
self.splash.append(self._qr_group)
198-
self._qr_group.scale = qr_size
199-
self._qr_group.x = x
200-
self._qr_group.y = y
201-
self._qr_group.append(qr_sprite)
86+
super().qrcode(qr_data, qr_size=qr_size, x=x, y=y, qr_color=qr_color)
20287
if self.auto_refresh:
20388
self.display.refresh()
20489
sleep(5)

0 commit comments

Comments
 (0)