From 3c9e414c8ac1005672421da378e3fec9a4c568f8 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Sat, 8 Aug 2020 09:56:14 -0500 Subject: [PATCH 1/7] Updated label.py to correct position of initial blank text --- adafruit_display_text/label.py | 43 +++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index aa868cd..a7f55c4 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -95,10 +95,7 @@ def __init__( self.width = max_glyphs self._font = font self._text = None - if anchor_point is None: - self._anchor_point = (0, 0) - else: - self._anchor_point = anchor_point + self._anchor_point = anchor_point self.x = x self.y = y @@ -126,7 +123,7 @@ def __init__( if text is not None: self._update_text(str(text)) - if anchored_position is not None: + if (anchored_position is not None) and (anchor_point is not None): self.anchored_position = anchored_position def _create_background_box(self, lines, y_offset): @@ -374,14 +371,19 @@ def anchor_point(self): @anchor_point.setter def anchor_point(self, new_anchor_point): - current_anchored_position = self.anchored_position - self._anchor_point = new_anchor_point - self.anchored_position = current_anchored_position + if self._anchor_point is not None: + current_anchored_position = self.anchored_position + self._anchor_point = new_anchor_point + self.anchored_position = current_anchored_position + else: + self._anchor_point = new_anchor_point @property def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" + if self._anchor_point is None: + return None return ( int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale)), int( @@ -393,14 +395,17 @@ def anchored_position(self): @anchored_position.setter def anchored_position(self, new_position): - new_x = int( - new_position[0] - - self._anchor_point[0] * (self._boundingbox[2] * self._scale) - ) - new_y = int( - new_position[1] - - (self._anchor_point[1] * self._boundingbox[3] * self._scale) - + round((self._boundingbox[3] * self._scale) / 2.0) - ) - self.x = new_x - self.y = new_y + if (self._anchor_point is None) or (new_position is None): + pass # Note: anchor_point must be set before setting anchored_position + else: + new_x = int( + new_position[0] + - self._anchor_point[0] * (self._boundingbox[2] * self._scale) + ) + new_y = int( + new_position[1] + - (self._anchor_point[1] * self._boundingbox[3] * self._scale) + + round((self._boundingbox[3] * self._scale) / 2.0) + ) + self.x = new_x + self.y = new_y From 4cfc2a467534c150eb2943e9aaa76cd58590d0f7 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Sat, 8 Aug 2020 10:15:38 -0500 Subject: [PATCH 2/7] ran black --- adafruit_display_text/label.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index a7f55c4..1a3e65a 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -395,8 +395,8 @@ def anchored_position(self): @anchored_position.setter def anchored_position(self, new_position): - if (self._anchor_point is None) or (new_position is None): - pass # Note: anchor_point must be set before setting anchored_position + if (self._anchor_point is None) or (new_position is None): + pass # Note: anchor_point must be set before setting anchored_position else: new_x = int( new_position[0] From e89c457f19032d2e6e4d831d925ed96eef43c933 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Mon, 10 Aug 2020 18:23:01 -0500 Subject: [PATCH 3/7] Changed pass to return per requested change --- adafruit_display_text/label.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 1a3e65a..6b52b7e 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -396,7 +396,7 @@ def anchored_position(self): @anchored_position.setter def anchored_position(self, new_position): if (self._anchor_point is None) or (new_position is None): - pass # Note: anchor_point must be set before setting anchored_position + return # Note: anchor_point must be set before setting anchored_position else: new_x = int( new_position[0] From b66efa13235c8d93907f6f02b6f77de2cd316780 Mon Sep 17 00:00:00 2001 From: Margaret Matocha Date: Mon, 10 Aug 2020 18:32:47 -0500 Subject: [PATCH 4/7] Deleted else and corrected tabbing --- adafruit_display_text/label.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 6b52b7e..9f65598 100755 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -397,15 +397,14 @@ def anchored_position(self): def anchored_position(self, new_position): if (self._anchor_point is None) or (new_position is None): return # Note: anchor_point must be set before setting anchored_position - else: - new_x = int( - new_position[0] - - self._anchor_point[0] * (self._boundingbox[2] * self._scale) - ) - new_y = int( - new_position[1] - - (self._anchor_point[1] * self._boundingbox[3] * self._scale) - + round((self._boundingbox[3] * self._scale) / 2.0) - ) - self.x = new_x - self.y = new_y + new_x = int( + new_position[0] + - self._anchor_point[0] * (self._boundingbox[2] * self._scale) + ) + new_y = int( + new_position[1] + - (self._anchor_point[1] * self._boundingbox[3] * self._scale) + + round((self._boundingbox[3] * self._scale) / 2.0) + ) + self.x = new_x + self.y = new_y From e08643cdf801b8a4f4c7e3c347bbf1b1673a87fd Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Tue, 11 Aug 2020 18:42:34 -0500 Subject: [PATCH 5/7] changing example script to simpletest --- .../display_text_bitmap_label_simpletest.py | 12 ++ ...y_text_label_vs_bitmap_label_simpletest.py | 180 ------------------ 2 files changed, 12 insertions(+), 180 deletions(-) create mode 100644 examples/display_text_bitmap_label_simpletest.py delete mode 100755 examples/display_text_label_vs_bitmap_label_simpletest.py diff --git a/examples/display_text_bitmap_label_simpletest.py b/examples/display_text_bitmap_label_simpletest.py new file mode 100644 index 0000000..acb2ca0 --- /dev/null +++ b/examples/display_text_bitmap_label_simpletest.py @@ -0,0 +1,12 @@ +import board +import terminalio +from adafruit_display_text import bitmap_label + + +text = "Hello world" +text_area = bitmap_label.Label(terminalio.FONT, text=text) +text_area.x = 10 +text_area.y = 10 +board.DISPLAY.show(text_area) +while True: + pass diff --git a/examples/display_text_label_vs_bitmap_label_simpletest.py b/examples/display_text_label_vs_bitmap_label_simpletest.py deleted file mode 100755 index 5565a26..0000000 --- a/examples/display_text_label_vs_bitmap_label_simpletest.py +++ /dev/null @@ -1,180 +0,0 @@ -# Sample for comparing label and bitmap_label memory usage with Builtin or loaded BDF fonts - -import gc -import time -import board -import displayio -import terminalio - - -# pylint: disable=no-member - -########## -# Use these Boolean variables to select the text display library and which font style to use -########## -use_bitmaplabel = True # Set True if to use 'bitmap_label.py' -# Set False to use 'label.py' library -########## -use_builtinfont = True # Set True to use the terminalio.FONT BuiltinFont, -# Set False to use a BDF loaded font, see "fontFiles" below -########## - - -my_scale = 1 - -if use_bitmaplabel: # use bitmap_label.py library (Bitmap) - from adafruit_display_text import bitmap_label as label - - version = "bitmap_label.py" - -else: # use label.py library (TileGrid) - from adafruit_display_text import label - - version = "label.py" - -# Setup the SPI display - -if "DISPLAY" not in dir(board): - # Setup the LCD display with driver - # You may need to change this to match the display driver for the chipset - # used on your display - from adafruit_ili9341 import ILI9341 - - displayio.release_displays() - - # setup the SPI bus - spi = board.SPI() - tft_cs = board.D9 # arbitrary, pin not used - tft_dc = board.D10 - tft_backlight = board.D12 - tft_reset = board.D11 - - while not spi.try_lock(): - spi.configure(baudrate=32000000) - spi.unlock() - - display_bus = displayio.FourWire( - spi, - command=tft_dc, - chip_select=tft_cs, - reset=tft_reset, - baudrate=32000000, - polarity=1, - phase=1, - ) - - # Number of pixels in the display - DISPLAY_WIDTH = 320 - DISPLAY_HEIGHT = 240 - - # create the display - display = ILI9341( - display_bus, - width=DISPLAY_WIDTH, - height=DISPLAY_HEIGHT, - rotation=180, # The rotation can be adjusted to match your configuration. - auto_refresh=True, - native_frames_per_second=90, - ) - - # reset the display to show nothing. - display.show(None) -else: - # built-in display - display = board.DISPLAY - -print("Display is started") - -# load all the fonts -print("loading fonts...") - - -# Setup file locations for BDF font files -font_files = [ - "fonts/Helvetica-Bold-16.bdf", - # 'fonts/BitstreamVeraSans-Roman-24.bdf', - # 'fonts/BitstreamVeraSans-Roman-16.bdf', - # 'fonts/Fayette-HandwrittenScript-64.bdf', -] - -font_list = [] - -for i, font_file in enumerate(font_files): - - if use_builtinfont: - this_font = ( - terminalio.FONT - ) # comment this out to switch back to BDF loaded fonts - else: - from adafruit_bitmap_font import bitmap_font - - this_font = bitmap_font.load_font(font_file) - - font_list.append(this_font) - - -preload_the_glyphs = ( - True # set this to True if you want to preload the font glyphs into memory -) -# preloading the glyphs will help speed up the rendering of text but will use more RAM - -if preload_the_glyphs: - - # identify the glyphs to load into memory -> increases rendering speed - glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! " - - print("loading glyphs...") - for font in font_list: - if font is not terminalio.FONT: - font.load_glyphs(glyphs) - - print("Glyphs are loaded.") - -print("Fonts completed loading.") - -# create group - -gc.collect() -print("After creating Group, Memory free: {}".format(gc.mem_free())) - - -my_string = "Welcome to using displayio on CircuitPython!" - -gc.collect() -label_start_memory = gc.mem_free() -start_time = time.monotonic() - -bmap_label = label.Label( - font=font_list[0], - text=my_string, - color=0xFFFFFF, - max_glyphs=len(my_string), - background_color=0xFF0000, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=False, - x=10, - y=30, - line_spacing=1.25, - scale=my_scale, -) -end_time = time.monotonic() - -gc.collect() -label_end_memory = gc.mem_free() - -bmap_group = displayio.Group(max_size=1) # Create a group for displaying -bmap_group.append(bmap_label) - - -print("***") -print("{} memory used: {}".format(version, label_start_memory - label_end_memory)) -print("{} time to process: {} seconds".format(version, end_time - start_time)) -display.auto_refresh = True - -display.show(bmap_group) - -while True: - pass From b6662efe48c6e30bed1cd43fd17b59500c7de0b9 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Tue, 11 Aug 2020 19:04:15 -0500 Subject: [PATCH 6/7] working on bitmap_label vs. label comparison example --- ..._text_label_vs_bitmap_label_alternating.py | 239 ++++-------------- 1 file changed, 45 insertions(+), 194 deletions(-) diff --git a/examples/display_text_label_vs_bitmap_label_alternating.py b/examples/display_text_label_vs_bitmap_label_alternating.py index 41b0d22..54728c8 100755 --- a/examples/display_text_label_vs_bitmap_label_alternating.py +++ b/examples/display_text_label_vs_bitmap_label_alternating.py @@ -10,24 +10,33 @@ from adafruit_bitmap_font import bitmap_font from adafruit_display_text import bitmap_label - from adafruit_display_text import label # pylint: disable=no-member -# Setup the SPI display + ########## # Use this Boolean variables to select which font style to use ########## -use_builtinfont = True # Set True to use the terminalio.FONT BuiltinFont, +use_builtinfont = False # Set True to use the terminalio.FONT BuiltinFont, +fontToUse = terminalio.FONT # Set False to use a BDF loaded font, see "fontFiles" below ########## +if not use_builtinfont: + # load the fonts + print("loading font...") + + fontList = [] + + # Load some proportional fonts + fontFile = "fonts/Helvetica-Bold-16.bdf" + fontToUse = bitmap_font.load_font(fontFile) + # Set scaling factor for display text my_scale = 1 # Setup the SPI display - if "DISPLAY" not in dir(board): # Setup the LCD display with driver # You may need to change this to match the display driver for the chipset @@ -80,58 +89,28 @@ print("Display is started") -# load all the fonts -print("loading fonts...") - -fontList = [] - -# Load some proportional fonts -fontFiles = [ - "fonts/Helvetica-Bold-16.bdf", - # 'fonts/BitstreamVeraSans-Roman-24.bdf', # Header2 - # 'fonts/BitstreamVeraSans-Roman-16.bdf', # mainText -] - - -for i, fontFile in enumerate(fontFiles): - - if use_builtinfont: - thisFont = ( - terminalio.FONT - ) # comment this out to switch back to BDF loaded fonts - else: - thisFont = bitmap_font.load_font(fontFile) - - fontList.append(thisFont) - preload_glyphs = ( True # set this to True if you want to preload the font glyphs into memory ) # preloading the glyphs will help speed up the rendering of text but will use more RAM -if preload_glyphs: +if preload_glyphs and not use_builtinfont: # identify the glyphs to load into memory -> increases rendering speed glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! " print("loading glyphs...") - for font in fontList: - if font is not terminalio.FONT: - font.load_glyphs(glyphs) + fontToUse.load_glyphs(glyphs) print("Glyphs are loaded.") - print("Fonts completed loading.") # create group -my_string1 = "This is a label.py and\nbitmap_label.py comparison." -my_string23 = "none" -my_string_bitmap_label = "bitmap_label" -my_string_label = "label bitmap_label" - +long_string = "The purple snake\nbrings python fun\nto everyone." +short_string = "Hello World" ##### # Create the "bitmap_label.py" versions of the text labels. @@ -140,107 +119,45 @@ bitmap_label_start = gc.mem_free() bmap_label1 = bitmap_label.Label( - font=fontList[0], - text=my_string1, + font=fontToUse, + text=short_string, color=0xFFFFFF, - max_glyphs=len(my_string1), + max_glyphs=len(short_string), background_color=0xFF0000, padding_bottom=0, padding_left=0, padding_right=0, padding_top=0, background_tight=True, - x=10, - y=60, line_spacing=1.25, scale=my_scale, - anchor_point=(0.5, 0), - anchored_position=(160, 50), + anchor_point=(0.0, 0), + anchored_position=(10, 60), ) label2_padding = 10 bmap_label2 = bitmap_label.Label( - font=fontList[0], - text=my_string23, + font=fontToUse, + text=long_string, color=0x000000, - max_glyphs=len(my_string23), + max_glyphs=len(long_string), background_color=0xFFFF00, padding_bottom=label2_padding, padding_left=0, padding_right=0, padding_top=label2_padding, background_tight=False, - x=10, - y=100, line_spacing=1.25, scale=my_scale, - anchor_point=(0, 0), - anchored_position=(200, 150), + anchor_point=(0.0, 0), + anchored_position=(10, 120), ) -bmap_label3 = bitmap_label.Label( - font=fontList[0], - text=my_string23, - color=0x000000, - max_glyphs=len(my_string23), - background_color=0xFFFF00, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=150, - line_spacing=1.25, - scale=my_scale, -) - -bmap_label4 = bitmap_label.Label( - font=fontList[0], - text=my_string_label, - color=0x000000, - max_glyphs=len(my_string_label), - background_color=0xFFFF00, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=False, - x=10, - y=200, - line_spacing=1.25, - scale=my_scale, -) - -my_string5 = "bitmap_label -->" -bmap_label5 = bitmap_label.Label( - font=fontList[0], - text=my_string5, - color=0xFFFFFF, - max_glyphs=len(my_string5), - background_color=0x000000, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=200, - line_spacing=1.25, - anchor_point=(1, 0.5), - anchored_position=(200, 200), - scale=my_scale, -) - - gc.collect() bitmap_label_end = gc.mem_free() -bmap_group = displayio.Group(max_size=5) # Create a group for displaying +bmap_group = displayio.Group(max_size=4) # Create a group for displaying bmap_group.append(bmap_label1) bmap_group.append(bmap_label2) -bmap_group.append(bmap_label3) -bmap_group.append(bmap_label4) -bmap_group.append(bmap_label5) ##### @@ -250,121 +167,55 @@ label_start = gc.mem_free() label1 = label.Label( - font=fontList[0], - text=my_string1, + font=fontToUse, + text=short_string, color=0xFFFFFF, - max_glyphs=len(my_string1), + max_glyphs=len(short_string), background_color=0xFF0000, padding_bottom=0, padding_left=0, padding_right=0, padding_top=0, background_tight=True, - x=10, - y=60, line_spacing=1.25, scale=my_scale, - anchor_point=(0.5, 0), - anchored_position=(160, 50), + anchor_point=(1.0, 0), + anchored_position=(display.width-10, 60), ) label2 = label.Label( - font=fontList[0], - text=my_string23, + font=fontToUse, + text=long_string, color=0x000000, - max_glyphs=len(my_string23), + max_glyphs=len(long_string), background_color=0xFFFF00, padding_bottom=label2_padding, padding_left=0, padding_right=0, padding_top=label2_padding, background_tight=False, - x=10, - y=100, - line_spacing=1.25, - scale=my_scale, - anchor_point=(0, 0), - anchored_position=(200, 150), -) - -label3 = label.Label( - font=fontList[0], - text=my_string23, - color=0x000000, - max_glyphs=len(my_string23), - background_color=0xFFFF00, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=150, - line_spacing=1.25, - scale=my_scale, -) - -label4 = label.Label( - font=fontList[0], - text=my_string_label, - color=0x000000, - max_glyphs=len(my_string_label), - background_color=0xFFFF00, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=True, - x=10, - y=200, - line_spacing=1.25, - scale=my_scale, -) - - -my_string5 = "<-- label" -label5 = label.Label( - font=fontList[0], - text=my_string5, - color=0xFFFFFF, - max_glyphs=len(my_string5), - background_color=0x000000, - padding_bottom=0, - padding_left=0, - padding_right=0, - padding_top=0, - background_tight=False, - x=10, - y=200, line_spacing=1.25, - anchor_point=(0, 0.5), - anchored_position=(50, 200), scale=my_scale, + anchor_point=(1.0, 0), + anchored_position=(display.width-10, 120), ) gc.collect() label_end = gc.mem_free() -label_group = displayio.Group(max_size=5) # Create a group for displaying +label_group = displayio.Group(max_size=4) # Create a group for displaying label_group.append(label1) label_group.append(label2) -label_group.append(label3) -label_group.append(label4) -label_group.append(label5) print("***") +main_group = displayio.Group() +main_group.append(label_group) +main_group.append(bmap_group) + display.auto_refresh = True +display.show(main_group) while True: - print("bitmap_label") - time.sleep(0.1) - display.show(bmap_group) - - time.sleep(2) - - print("label") - time.sleep(0.1) - display.show(label_group) - time.sleep(2) + pass \ No newline at end of file From 8c98dacd815e804ec86d129c94857884a5c352c6 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Tue, 11 Aug 2020 20:09:22 -0500 Subject: [PATCH 7/7] working on comparison example --- ...> display_text_label_vs_bitmap_label_comparison.py} | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) rename examples/{display_text_label_vs_bitmap_label_alternating.py => display_text_label_vs_bitmap_label_comparison.py} (96%) mode change 100755 => 100644 diff --git a/examples/display_text_label_vs_bitmap_label_alternating.py b/examples/display_text_label_vs_bitmap_label_comparison.py old mode 100755 new mode 100644 similarity index 96% rename from examples/display_text_label_vs_bitmap_label_alternating.py rename to examples/display_text_label_vs_bitmap_label_comparison.py index 54728c8..a9bde6c --- a/examples/display_text_label_vs_bitmap_label_alternating.py +++ b/examples/display_text_label_vs_bitmap_label_comparison.py @@ -110,7 +110,6 @@ # create group long_string = "The purple snake\nbrings python fun\nto everyone." -short_string = "Hello World" ##### # Create the "bitmap_label.py" versions of the text labels. @@ -120,9 +119,8 @@ bmap_label1 = bitmap_label.Label( font=fontToUse, - text=short_string, + text="bitmap_label", color=0xFFFFFF, - max_glyphs=len(short_string), background_color=0xFF0000, padding_bottom=0, padding_left=0, @@ -155,6 +153,8 @@ gc.collect() bitmap_label_end = gc.mem_free() +print("bitmap_label used: {} memory".format(bitmap_label_start - bitmap_label_end)) + bmap_group = displayio.Group(max_size=4) # Create a group for displaying bmap_group.append(bmap_label1) bmap_group.append(bmap_label2) @@ -168,9 +168,8 @@ label1 = label.Label( font=fontToUse, - text=short_string, + text="label", color=0xFFFFFF, - max_glyphs=len(short_string), background_color=0xFF0000, padding_bottom=0, padding_left=0, @@ -203,6 +202,7 @@ gc.collect() label_end = gc.mem_free() +print("label used: {} memory".format(label_start - label_end)) label_group = displayio.Group(max_size=4) # Create a group for displaying label_group.append(label1) label_group.append(label2)