|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 Erin St. Blaine for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +import time |
| 5 | +from random import randint |
| 6 | +import board |
| 7 | +import neopixel |
| 8 | +import fontio |
| 9 | +from adafruit_display_text.bitmap_label import Label |
| 10 | +from adafruit_bitmap_font import bitmap_font |
| 11 | +from displayio import Bitmap |
| 12 | +from rainbowio import colorwheel |
| 13 | + |
| 14 | +tom_thumb = bitmap_font.load_font("tom-thumb.pcf", Bitmap) |
| 15 | + |
| 16 | +_glyph_keys = ['bitmap', 'tile_index', 'width', 'height', 'dx', 'dy', 'shift_x', 'shift_y'] |
| 17 | +def patch_glyph(base, **kw): |
| 18 | + d = {} |
| 19 | + for k in _glyph_keys: |
| 20 | + d[k] = kw.get(k, getattr(base, k)) |
| 21 | + return fontio.Glyph(**d) |
| 22 | + |
| 23 | +class PatchedFont: |
| 24 | + def __init__(self, base_font, patches): |
| 25 | + self.base_font = base_font |
| 26 | + self.patches = patches |
| 27 | + |
| 28 | + def get_glyph(self, glyph): |
| 29 | + g = self.base_font.get_glyph(glyph) |
| 30 | + patch = self.patches.get(glyph) |
| 31 | + if patch is not None: |
| 32 | + # print("patching", repr(chr(glyph)), g) |
| 33 | + g = patch_glyph(g, **patch) |
| 34 | + # print("patched", g) |
| 35 | + return g |
| 36 | + |
| 37 | + def get_bounding_box(self): |
| 38 | + return self.base_font.get_bounding_box() |
| 39 | + |
| 40 | +font = PatchedFont(tom_thumb, |
| 41 | + {32: {'shift_x': 1, 'dx': 0}, |
| 42 | + 105: {'dx': 0, 'shift_x': 2}, |
| 43 | + 33: {'dx': 0, 'shift_x': 2}, |
| 44 | + }) |
| 45 | + |
| 46 | +# List of text strings |
| 47 | +text_strings = [" love bun ", " dear me ", " my bear ", |
| 48 | + " my my ", " you are babe "] |
| 49 | +# Create a label object |
| 50 | +label = Label(text="text", font=font) |
| 51 | +bitmap = label.bitmap |
| 52 | +heart_bitmap = [ |
| 53 | + 0, 1, 1, 0, 0, |
| 54 | + 1, 1, 1, 1, 0, |
| 55 | + 0, 1, 1, 1, 1, |
| 56 | + 1, 1, 1, 1, 0, |
| 57 | + 0, 1, 1, 0, 0 |
| 58 | +] |
| 59 | +pixels = neopixel.NeoPixel(board.A1, 5*5, brightness=.08, auto_write=False) |
| 60 | +while True: |
| 61 | + for hue in range(0, 255, 3): |
| 62 | + color = colorwheel(hue) |
| 63 | + pixels[:] = [pixel * color for pixel in heart_bitmap] |
| 64 | + pixels.show() |
| 65 | + time.sleep(.05) |
| 66 | + hue = 0 |
| 67 | + string_index = randint(0, 4) |
| 68 | + label.text = text_strings[string_index] |
| 69 | + bitmap = label.bitmap |
| 70 | + print(string_index) |
| 71 | + for i in range(bitmap.width): |
| 72 | + # Use a rainbow of colors, shifting each column of pixels |
| 73 | + hue = hue + 7 |
| 74 | + if hue >= 256: |
| 75 | + hue = hue - 256 |
| 76 | + color = colorwheel(hue) |
| 77 | + # Scoot the old text left by 1 pixel |
| 78 | + pixels[:20] = pixels[5:] |
| 79 | + # Draw in the next line of text |
| 80 | + for y in range(5): |
| 81 | + # Select black or color depending on the bitmap pixel |
| 82 | + pixels[20+y] = color * bitmap[i,y] |
| 83 | + pixels.show() |
| 84 | + time.sleep(.2) |
0 commit comments