|
| 1 | +# SPDX-FileCopyrightText: 2022 Mark Komus |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import random |
| 6 | +import time |
| 7 | +import board |
| 8 | +import busio |
| 9 | +import digitalio |
| 10 | +import displayio |
| 11 | +import framebufferio |
| 12 | +import is31fl3741 |
| 13 | +from adafruit_is31fl3741.is31fl3741_PixelBuf import IS31FL3741_PixelBuf |
| 14 | +from adafruit_is31fl3741.led_glasses_map import ( |
| 15 | + glassesmatrix_ledmap_no_ring, |
| 16 | + left_ring_map_no_inner, |
| 17 | + right_ring_map_no_inner, |
| 18 | +) |
| 19 | +from adafruit_display_text import label |
| 20 | +from adafruit_bitmap_font import bitmap_font |
| 21 | +from adafruit_led_animation.animation.chase import Chase |
| 22 | +from adafruit_debouncer import Debouncer |
| 23 | + |
| 24 | +# List of possible messages to display. Randomly chosen |
| 25 | +MESSAGES = ( |
| 26 | + "GO TEAM GO", |
| 27 | + "WE ARE NUMBER 1", |
| 28 | + "I LIKE THE HALFTIME SHOW", |
| 29 | +) |
| 30 | + |
| 31 | +# Colors used for the text and ring lights |
| 32 | +BLUE_TEXT = (0, 20, 255) |
| 33 | +BLUE_RING = (0, 10, 120) |
| 34 | +YELLOW_TEXT = (220, 210, 0) |
| 35 | +YELLOW_RING = (150, 140, 0) |
| 36 | + |
| 37 | + |
| 38 | +def ScrollMessage(text, color, repeat): |
| 39 | + """Scroll a message across the eyeglasses a set number of times""" |
| 40 | + text_area.text = text |
| 41 | + text_area.color = color |
| 42 | + |
| 43 | + # Start the message just off the side of the glasses |
| 44 | + x = display.width |
| 45 | + text_area.x = x |
| 46 | + |
| 47 | + # Determine the width of the message to scroll |
| 48 | + width = text_area.bounding_box[2] |
| 49 | + |
| 50 | + for _ in range(repeat): |
| 51 | + while x != -width: |
| 52 | + x = x - 1 |
| 53 | + text_area.x = x |
| 54 | + |
| 55 | + # Update the switch and if it has been pressed abort scrolling this message |
| 56 | + switch.update() |
| 57 | + if switch.value is False: |
| 58 | + return |
| 59 | + |
| 60 | + time.sleep(0.025) # adjust to change scrolling speed |
| 61 | + x = display.width |
| 62 | + |
| 63 | + |
| 64 | +def Score(text, color, ring_color, repeat): |
| 65 | + """Show a scrolling text message and animated effects on the eye rings. |
| 66 | + The messages scrolls left to right, then right to left while the eye rings |
| 67 | + are animated using the adafruit_led_animation library.""" |
| 68 | + |
| 69 | + # Set up a led animation chase sequence for both eyelights |
| 70 | + chase_left = Chase(left_eye, speed=0.11, color=ring_color, size=8, spacing=4) |
| 71 | + chase_right = Chase(right_eye, speed=0.07, color=ring_color, size=8, spacing=4) |
| 72 | + |
| 73 | + text_area.text = text |
| 74 | + text_area.color = color |
| 75 | + |
| 76 | + x = display.width |
| 77 | + text_area.x = x |
| 78 | + |
| 79 | + width = text_area.bounding_box[2] |
| 80 | + |
| 81 | + for _ in range(repeat): |
| 82 | + # Scroll the text left and animate the eyes |
| 83 | + while x != -width: |
| 84 | + x = x - 1 |
| 85 | + text_area.x = x |
| 86 | + chase_left.animate() |
| 87 | + chase_right.animate() |
| 88 | + time.sleep(0.008) # adjust to change scrolling speed |
| 89 | + # Scroll the text right and animate the eyes |
| 90 | + while x != display.width: |
| 91 | + x = x + 1 |
| 92 | + text_area.x = x |
| 93 | + chase_left.animate() |
| 94 | + chase_right.animate() |
| 95 | + time.sleep(0.008) # adjust to change scrolling speed |
| 96 | + |
| 97 | + |
| 98 | +# Remove any existing displays |
| 99 | +displayio.release_displays() |
| 100 | + |
| 101 | +# Set up the top button used to trigger a special message when pressed |
| 102 | +switch_pin = digitalio.DigitalInOut(board.SWITCH) |
| 103 | +switch_pin.direction = digitalio.Direction.INPUT |
| 104 | +switch_pin.pull = digitalio.Pull.UP |
| 105 | +switch = Debouncer(switch_pin) |
| 106 | + |
| 107 | +# Initialize the LED Glasses |
| 108 | +# |
| 109 | +# In this example scale is set to True. When True the logical display is |
| 110 | +# three times the physical display size and scaled down to allow text to |
| 111 | +# look more natural for small display sizes. Hence the display is created |
| 112 | +# as 54x15 when the physical display is 18x5. |
| 113 | +# |
| 114 | +i2c = busio.I2C(board.SCL, board.SDA, frequency=1000000) |
| 115 | +is31 = is31fl3741.IS31FL3741(i2c=i2c) |
| 116 | +is31_framebuffer = is31fl3741.IS31FL3741_FrameBuffer( |
| 117 | + is31, 54, 15, glassesmatrix_ledmap_no_ring, scale=True, gamma=True |
| 118 | +) |
| 119 | +display = framebufferio.FramebufferDisplay(is31_framebuffer, auto_refresh=True) |
| 120 | + |
| 121 | +# Set up the left and right eyelight rings |
| 122 | +# init is set to False as the IS31FL3741_FrameBuffer has already initialized the IS31FL3741 driver |
| 123 | +left_eye = IS31FL3741_PixelBuf( |
| 124 | + is31, left_ring_map_no_inner, init=False, auto_write=False |
| 125 | +) |
| 126 | +right_eye = IS31FL3741_PixelBuf( |
| 127 | + is31, right_ring_map_no_inner, init=False, auto_write=False |
| 128 | +) |
| 129 | + |
| 130 | +# Dim the display. Full brightness is BRIGHT |
| 131 | +is31_framebuffer.brightness = 0.2 |
| 132 | + |
| 133 | +# Load the font to be used - scrolly only has upper case letters |
| 134 | +font = bitmap_font.load_font("scrolly.bdf") |
| 135 | + |
| 136 | +# Set up the display elements |
| 137 | +text_area = label.Label(font, text="", color=(0, 0, 0)) |
| 138 | +text_area.y = 8 |
| 139 | +group = displayio.Group() |
| 140 | +group.append(text_area) |
| 141 | +display.show(group) |
| 142 | + |
| 143 | +while True: |
| 144 | + # Run the debouncer code to get the updated switch value |
| 145 | + switch.update() |
| 146 | + |
| 147 | + # If the switch has been pressed interrupt start a special message |
| 148 | + if switch.value is False: # False means the switch has been pressed |
| 149 | + Score("SCORE!", YELLOW_TEXT, BLUE_RING, 2) |
| 150 | + |
| 151 | + # If the switch is not pressed pick a random message and scroll it |
| 152 | + left_eye.fill(BLUE_RING) |
| 153 | + right_eye.fill(BLUE_RING) |
| 154 | + left_eye.show() |
| 155 | + right_eye.show() |
| 156 | + ScrollMessage(random.choice(MESSAGES), YELLOW_TEXT, 2) |
0 commit comments