|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | +""" |
| 5 | +Rainbow LED grid layout demo for MacroPad. Displays the key pressed in a grid matching the key |
| 6 | +layout on the built-in display, and animates a rainbow the first time you press a key and turns it |
| 7 | +off on the next press. |
| 8 | +""" |
| 9 | +import displayio |
| 10 | +import terminalio |
| 11 | +from adafruit_display_text import bitmap_label as label |
| 12 | +from adafruit_displayio_layout.layouts.grid_layout import GridLayout |
| 13 | +from adafruit_macropad import MacroPad |
| 14 | +from rainbowio import colorwheel |
| 15 | + |
| 16 | +macropad = MacroPad() |
| 17 | + |
| 18 | +main_group = displayio.Group() |
| 19 | +macropad.display.show(main_group) |
| 20 | +title = label.Label( |
| 21 | + y=4, |
| 22 | + font=terminalio.FONT, |
| 23 | + color=0x0, |
| 24 | + text=" KEYPRESSES ", |
| 25 | + background_color=0xFFFFFF, |
| 26 | +) |
| 27 | +layout = GridLayout(x=0, y=10, width=128, height=54, grid_size=(3, 4), cell_padding=5) |
| 28 | +labels = [] |
| 29 | +for _ in range(12): |
| 30 | + labels.append(label.Label(terminalio.FONT, text="")) |
| 31 | + |
| 32 | +for index in range(12): |
| 33 | + x = index % 3 |
| 34 | + y = index // 3 |
| 35 | + layout.add_content(labels[index], grid_position=(x, y), cell_size=(1, 1)) |
| 36 | + |
| 37 | +main_group.append(title) |
| 38 | +main_group.append(layout) |
| 39 | + |
| 40 | +lit_keys = [False] * 12 |
| 41 | +wheel_offset = 0 |
| 42 | +while True: |
| 43 | + key_event = macropad.keys.events.get() |
| 44 | + if key_event: |
| 45 | + if key_event.pressed: |
| 46 | + labels[key_event.key_number].text = "KEY{}".format(key_event.key_number) |
| 47 | + # Turn the LED on with the first press, and off with the second press. |
| 48 | + lit_keys[key_event.key_number] = not lit_keys[key_event.key_number] |
| 49 | + else: |
| 50 | + labels[key_event.key_number].text = "" |
| 51 | + |
| 52 | + wheel_offset += 1 # Glow thru the colorwheel. |
| 53 | + for pixel in range(12): |
| 54 | + if lit_keys[pixel]: # Animate the specific LED. |
| 55 | + macropad.pixels[pixel] = colorwheel((pixel / 12 * 256) + wheel_offset) |
| 56 | + else: |
| 57 | + macropad.pixels[pixel] = 0 # Otherwise, turn the LED off. |
0 commit comments