Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Clue Terminal and Context Fixes #290

Merged
merged 22 commits into from
Apr 2, 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
Binary file not shown.
6 changes: 6 additions & 0 deletions src/base_circuitpython/base_cp_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
IMG_DIR_NAME = "img"
SCREEN_HEIGHT_WIDTH = 240

BLINKA_BMP = "blinka.bmp"
CLUE_TERMINAL_LINE_HEIGHT = 16
CLUE_TERMINAL_LINE_NUM_MAX = 15
CLUE_TERMINAL_X_OFFSET = 15
CLUE_TERMINAL_Y_OFFSET = 5
CLUE_TERMINAL_LINE_BREAK_AMT = 37
BMP_IMG = "BMP"

BMP_IMG_ENDING = ".bmp"
Expand Down
21 changes: 18 additions & 3 deletions src/base_circuitpython/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@
# https://learn.adafruit.com/arduino-to-circuitpython/the-board-module


import terminal_handler


class Display:
def __init__(self):
pass
self.active_group = None
self.terminal = terminal_handler.Terminal()

def show(self, group=None):
if group != self.active_group:
self.active_group = group

if group == None:
self.terminal.draw()
return

def show(self, group):
group.draw()
# if the group has no attribute called
# "draw", then it is liable for updating itself
# when it calls show
if hasattr(group, "draw"):
group.draw()


DISPLAY = Display()
Expand Down
2 changes: 1 addition & 1 deletion src/base_circuitpython/displayio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
from .palette import Palette

# references to img and bmp_img are for testing purposes
from .tile_grid import TileGrid, img, bmp_img
from .tile_grid import TileGrid
75 changes: 58 additions & 17 deletions src/base_circuitpython/displayio/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from PIL import Image
import adafruit_display_text

from .tile_grid import TileGrid, bmp_img, img
from .tile_grid import TileGrid
from . import constants as CONSTANTS

import common
import board

# Group implementation loosely based on the
# displayio.Group class in Adafruit CircuitPython
Expand All @@ -16,12 +17,17 @@


class Group:
def __init__(self, max_size, scale=1, auto_write=True):
def __init__(self, max_size, scale=1, check_active_group_ref=True, auto_write=True):
self.__check_active_group_ref = check_active_group_ref
self.__auto_write = auto_write
self.__contents = []
self.max_size = max_size
self.scale = scale
self.auto_write = auto_write
self.in_group = False
self.parent = None

@property
def in_group(self):
return self.parent != None

def append(self, item):
if len(self.__contents) == self.max_size:
Expand All @@ -32,19 +38,47 @@ def append(self, item):
raise ValueError(CONSTANTS.LAYER_ALREADY_IN_GROUP)

self.__contents.append(item)
item.in_group = True
if self.auto_write:
self.draw(show=True)
item.parent = self

self.__elem_changed()

def __elem_changed(self):
# Ensure that this group is what the board is currently showing.
# Otherwise, don't bother to draw it.
if (
self.__auto_write
and self.__check_active_group_ref
and board.DISPLAY.active_group == self
):
self.draw()

elif self.in_group:

# If a sub-group is modified, propagate to top level to
# see if one of the parents are the current active group.
self.parent.__elem_changed()

def __getitem__(self, index):
return self.__contents[index]

def __setitem__(self, index, val):
old_val = self.__contents[index]

self.__contents[index] = val

def draw(self, x=0, y=0, scale=None, show=False):
if old_val != val:
self.__elem_changed()

def draw(self, img=None, x=0, y=0, scale=None, show=True):
# this function is not a part of the orignal implementation
# it is what prints itself and its children to the frontend
# it is what draws itself and its children and potentially shows it to the
# frontend
if img == None:
img = Image.new(
"RGBA",
(CONSTANTS.SCREEN_HEIGHT_WIDTH, CONSTANTS.SCREEN_HEIGHT_WIDTH),
(0, 0, 0, 0),
)
if scale is None:
scale = self.scale
else:
Expand All @@ -55,11 +89,11 @@ def draw(self, x=0, y=0, scale=None, show=False):
# adafruit_display_text has some positioning considerations
# that need to be handled.

# found manually, display must be positioned upwards
# This was found manually, display must be positioned upwards
# 1 unit (1 unit * scale = scale)
y -= scale

# group is positioned against anchored_position (default (0,0)),
# Group is positioned against anchored_position (default (0,0)),
# which is positioned against anchor_point

x += self._anchor_point[0]
Expand All @@ -72,15 +106,19 @@ def draw(self, x=0, y=0, scale=None, show=False):

for elem in self.__contents:
if isinstance(elem, Group):
elem.draw(x, y, scale, False)
img = elem.draw(img=img, x=x, y=y, scale=scale, show=False,)
else:
elem.draw(x, y, scale)
img = elem.draw(img=img, x=x, y=y, scale=scale)

# show should only be true to the highest parent group
if show:
self.show()
self.show(img)

# return value only used if this is within another group
return img

def show(self):
# sends current bmp_img to the frontend
def show(self, img):
# sends current img to the frontend
buffered = BytesIO()
img.save(buffered, format="BMP")
byte_base64 = base64.b64encode(buffered.getvalue())
Expand All @@ -96,4 +134,7 @@ def __len__(self):
return len(self.__contents)

def pop(self, i=-1):
return self.__contents.pop(i)
item = self.__contents.pop(i)
item.parent = None
self.__elem_changed()
return item
10 changes: 6 additions & 4 deletions src/base_circuitpython/displayio/test/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from common import utils

from ..tile_grid import TileGrid, img, bmp_img
from ..tile_grid import TileGrid
from ..group import Group
from ..palette import Palette
from ..bitmap import Bitmap
Expand Down Expand Up @@ -149,20 +149,22 @@ def test_draw_group(
tg = TileGrid(bitmap=bmp_1, pixel_shader=palette, position=(0, 0))
tg2 = TileGrid(bitmap=bmp_2, pixel_shader=palette, position=(50, 50))

group_main = Group(max_size=10, scale=scale_main)
group_main = Group(max_size=10, scale=scale_main, check_active_group_ref=False)
group_sub = Group(max_size=10, scale=scale_sub)

group_sub.append(tg)
group_main.append(group_sub)
group_main.append(tg2)
# img = Image.new("RGBA", (240, 240))
img = group_main.draw()

group_main.draw(0, 0)
img.putalpha(255)
expected = Image.open(
os.path.join(self.abs_path, "img", "group_test_result.bmp")
)
expected.putalpha(255)
bmp_img_expected = expected.load()

bmp_img = img.load()
for i in range(CONSTANTS.SCREEN_HEIGHT_WIDTH):
for j in range(CONSTANTS.SCREEN_HEIGHT_WIDTH):
assert bmp_img_expected[j, i] == bmp_img[j, i]
16 changes: 12 additions & 4 deletions src/base_circuitpython/displayio/test/test_tile_grid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from ..tile_grid import TileGrid, img, bmp_img
from PIL import Image
from ..tile_grid import TileGrid
from ..palette import Palette
from ..bitmap import Bitmap
from .. import constants as CONSTANTS
Expand Down Expand Up @@ -135,9 +136,12 @@ def test_draw(

tg = TileGrid(bitmap=bmp, pixel_shader=palette, position=(0, 0))
tg2 = TileGrid(bitmap=bmp, pixel_shader=palette, position=(0, 0))

img = Image.new(
"RGBA", (CONSTANTS.SCREEN_HEIGHT_WIDTH, CONSTANTS.SCREEN_HEIGHT_WIDTH)
)
# without scaling, test output
tg.draw(x_offset, y_offset, 1)
img = tg.draw(img, x_offset, y_offset, 1)
bmp_img = img.load()
for i in range(CONSTANTS.SCREEN_HEIGHT_WIDTH):
for j in range(CONSTANTS.SCREEN_HEIGHT_WIDTH):
if (i in range(y_offset + y, y_offset + y + draw_h)) and (
Expand All @@ -149,8 +153,12 @@ def test_draw(
):
assert bmp_img[j, i] == bg_color

img = Image.new(
"RGBA", (CONSTANTS.SCREEN_HEIGHT_WIDTH, CONSTANTS.SCREEN_HEIGHT_WIDTH)
)
# with scaling, test output
tg.draw(x_offset, y_offset, scale)
img = tg.draw(img, x_offset, y_offset, scale)
bmp_img = img.load()
for i in range(CONSTANTS.SCREEN_HEIGHT_WIDTH):
for j in range(CONSTANTS.SCREEN_HEIGHT_WIDTH):
if (
Expand Down
21 changes: 7 additions & 14 deletions src/base_circuitpython/displayio/tile_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@
# https://circuitpython.readthedocs.io/en/5.0.x/shared-bindings/displayio/TileGrid.html


# Create a new black (default) image
img = Image.new(
"RGBA", (CONSTANTS.SCREEN_HEIGHT_WIDTH, CONSTANTS.SCREEN_HEIGHT_WIDTH), (0, 0, 0, 0)
)

# Create the pixel map
# All displayio classes can access this
# instance to read and write to the output image.
bmp_img = img.load()


class TileGrid:
def __init__(
self,
Expand Down Expand Up @@ -56,7 +45,11 @@ def __init__(
self.bitmap = bitmap
self.pixel_shader = pixel_shader
self.default_tile = default_tile
self.in_group = False
self.parent = None

@property
def in_group(self):
return self.parent != None

# setitem for an index simply gets the index of the bitmap
# rather than the tile index
Expand All @@ -78,8 +71,7 @@ def __getitem__(self, index):

# methods that are not in the origin class:

def draw(self, x, y, scale):

def draw(self, img, x, y, scale):
# draw the current bitmap with
# appropriate scale on the global bmp_img
x = self.x * scale + x
Expand All @@ -90,6 +82,7 @@ def draw(self, x, y, scale):
)

img.paste(new_shape, (x, y), new_shape)
return img

def draw_group(self, x, y, y_start, y_end, x_start, x_end, scale):
height = y_end - y_start
Expand Down
Binary file added src/base_circuitpython/img/blinka.bmp
Binary file not shown.
Loading