23
23
* Adafruit CircuitPython firmware for the supported boards:
24
24
https://github.com/adafruit/circuitpython/releases
25
25
26
+ * Adafruit's PortalBase library: https://github.com/adafruit/Adafruit_CircuitPython_PortalBase
27
+
26
28
"""
27
29
28
30
import gc
29
31
from time import sleep
30
32
import board
31
- import displayio
33
+ from adafruit_portalbase . graphics import GraphicsBase
32
34
33
35
__version__ = "0.0.0-auto.0"
34
36
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git"
35
37
36
38
37
- class Graphics :
39
+ class Graphics ( GraphicsBase ) :
38
40
"""Graphics Helper Class for the MagTag Library
39
41
40
42
:param default_bg: The path to your default background image file or a hex color.
@@ -50,53 +52,13 @@ class Graphics:
50
52
def __init__ (
51
53
self , * , default_bg = 0xFFFFFF , auto_refresh = True , rotation = 270 , debug = False
52
54
):
53
-
54
55
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
82
58
self .auto_refresh = auto_refresh
83
-
84
- if self ._debug :
85
- print ("Init display" )
86
- self .splash = displayio .Group (max_size = 15 )
87
59
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 )
98
60
99
- gc . collect ( )
61
+ super (). __init__ ( board . DISPLAY , default_bg = default_bg , debug = debug )
100
62
101
63
def set_background (self , file_or_color , position = None ):
102
64
"""The background image to a bitmap file.
@@ -105,39 +67,7 @@ def set_background(self, file_or_color, position=None):
105
67
:param tuple position: Optional x and y coordinates to place the background at.
106
68
107
69
"""
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 )
141
71
if self .auto_refresh :
142
72
self .display .refresh ()
143
73
gc .collect ()
@@ -153,52 +83,7 @@ def qrcode(
153
83
:param y: The y position of upper left corner of the QR code on the display.
154
84
155
85
"""
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 )
202
87
if self .auto_refresh :
203
88
self .display .refresh ()
204
89
sleep (5 )
0 commit comments