Skip to content

Refactored to use PortalBase #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions adafruit_magtag/fakerequests.py

This file was deleted.

133 changes: 9 additions & 124 deletions adafruit_magtag/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases

* Adafruit's PortalBase library: https://github.com/adafruit/Adafruit_CircuitPython_PortalBase

"""

import gc
from time import sleep
import board
import displayio
from adafruit_portalbase.graphics import GraphicsBase

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


class Graphics:
class Graphics(GraphicsBase):
"""Graphics Helper Class for the MagTag Library

:param default_bg: The path to your default background image file or a hex color.
Expand All @@ -50,53 +52,13 @@ class Graphics:
def __init__(
self, *, default_bg=0xFFFFFF, auto_refresh=True, rotation=270, debug=False
):

self._debug = debug
if not hasattr(board, "DISPLAY"):
import adafruit_il0373 # pylint: disable=import-outside-toplevel

displayio.release_displays()
display_bus = displayio.FourWire(
board.SPI(),
command=board.EPD_DC,
chip_select=board.EPD_CS,
reset=board.EPD_RESET,
baudrate=1000000,
)

self.display = adafruit_il0373.IL0373(
display_bus,
width=296,
height=128,
rotation=rotation,
black_bits_inverted=False,
color_bits_inverted=False,
grayscale=True,
refresh_time=1,
seconds_per_frame=1,
)
else:
self.display = board.DISPLAY
self.display.rotation = rotation

self.display = board.DISPLAY
self.display.rotation = rotation
self.auto_refresh = auto_refresh

if self._debug:
print("Init display")
self.splash = displayio.Group(max_size=15)
self._qr_group = None
if self._debug:
print("Init background")
self._bg_group = displayio.Group(max_size=1)
self._bg_file = None
self.splash.append(self._bg_group)
self.display.show(self.splash)

# set the default background
if default_bg is not None:
self.set_background(default_bg)

gc.collect()
super().__init__(board.DISPLAY, default_bg=default_bg, debug=debug)

def set_background(self, file_or_color, position=None):
"""The background image to a bitmap file.
Expand All @@ -105,39 +67,7 @@ def set_background(self, file_or_color, position=None):
:param tuple position: Optional x and y coordinates to place the background at.

"""
while self._bg_group:
self._bg_group.pop()

if not position:
position = (0, 0) # default in top corner

if not file_or_color:
return # we're done, no background desired
if self._bg_file:
self._bg_file.close()
if isinstance(file_or_color, str): # its a filenme:
self._bg_file = open(file_or_color, "rb")
background = displayio.OnDiskBitmap(self._bg_file)
self._bg_sprite = displayio.TileGrid(
background,
pixel_shader=displayio.ColorConverter(),
x=position[0],
y=position[1],
)
elif isinstance(file_or_color, int):
# Make a background color fill
color_bitmap = displayio.Bitmap(self.display.width, self.display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = file_or_color
self._bg_sprite = displayio.TileGrid(
color_bitmap,
pixel_shader=color_palette,
x=position[0],
y=position[1],
)
else:
raise RuntimeError("Unknown type of background")
self._bg_group.append(self._bg_sprite)
super().set_background(file_or_color, position)
if self.auto_refresh:
self.display.refresh()
gc.collect()
Expand All @@ -153,52 +83,7 @@ def qrcode(
:param y: The y position of upper left corner of the QR code on the display.

"""
import adafruit_miniqr # pylint: disable=import-outside-toplevel

# generate the QR code
for qrtype in range(1, 5):
try:
qrcode = adafruit_miniqr.QRCode(qr_type=qrtype)
qrcode.add_data(qr_data)
qrcode.make()
break
except RuntimeError:
pass
# print("Trying with larger code")
else:
raise RuntimeError("Could not make QR code")
# monochrome (2 color) palette
palette = displayio.Palette(2)
palette[0] = 0xFFFFFF
palette[1] = qr_color

# pylint: disable=invalid-name
# bitmap the size of the matrix, plus border, monochrome (2 colors)
qr_bitmap = displayio.Bitmap(
qrcode.matrix.width + 2, qrcode.matrix.height + 2, 2
)
for i in range(qr_bitmap.width * qr_bitmap.height):
qr_bitmap[i] = 0

# transcribe QR code into bitmap
for xx in range(qrcode.matrix.width):
for yy in range(qrcode.matrix.height):
qr_bitmap[xx + 1, yy + 1] = 1 if qrcode.matrix[xx, yy] else 0

# display the QR code
qr_sprite = displayio.TileGrid(qr_bitmap, pixel_shader=palette)
if self._qr_group:
try:
self._qr_group.pop()
except IndexError: # later test if empty
pass
else:
self._qr_group = displayio.Group()
self.splash.append(self._qr_group)
self._qr_group.scale = qr_size
self._qr_group.x = x
self._qr_group.y = y
self._qr_group.append(qr_sprite)
super().qrcode(qr_data, qr_size=qr_size, x=x, y=y, qr_color=qr_color)
if self.auto_refresh:
self.display.refresh()
sleep(5)
Loading