|
| 1 | +# PyGamer NeoPixel Strip Control with CusorControl |
| 2 | +# Adapted from PyPortal_NeoPixel_Color_Picker.py by Kattni Rembor |
| 3 | +import time |
| 4 | +import board |
| 5 | +from adafruit_button import Button |
| 6 | +import displayio |
| 7 | +import neopixel |
| 8 | +from adafruit_cursorcontrol.cursorcontrol import Cursor |
| 9 | +from adafruit_cursorcontrol.cursorcontrol_cursormanager import DebouncedCursorManager |
| 10 | + |
| 11 | +# Set the NeoPixel brightness |
| 12 | +NEO_BRIGHTNESS = 0.3 |
| 13 | + |
| 14 | +# Set up the NeoPixel strip |
| 15 | +strip = neopixel.NeoPixel(board.D3, 30, brightness=NEO_BRIGHTNESS) |
| 16 | + |
| 17 | +# Turn off NeoPixels to start |
| 18 | +strip.fill(0) |
| 19 | + |
| 20 | +# Create the display |
| 21 | +display = board.DISPLAY |
| 22 | + |
| 23 | +# Create the display context |
| 24 | +splash = displayio.Group(max_size=15) |
| 25 | + |
| 26 | +# Button colors |
| 27 | +RED = (255, 0, 0) |
| 28 | +ORANGE = (255, 34, 0) |
| 29 | +YELLOW = (255, 170, 0) |
| 30 | +GREEN = (0, 255, 0) |
| 31 | +CYAN = (0, 255, 255) |
| 32 | +BLUE = (0, 0, 255) |
| 33 | +VIOLET = (153, 0, 255) |
| 34 | +MAGENTA = (255, 0, 51) |
| 35 | +PINK = (255, 51, 119) |
| 36 | +AQUA = (85, 125, 255) |
| 37 | +WHITE = (255, 255, 255) |
| 38 | +OFF = (0, 0, 0) |
| 39 | + |
| 40 | +# Button properties |
| 41 | +btn_x = 40 |
| 42 | +btn_y = 40 |
| 43 | + |
| 44 | +spots = [ |
| 45 | + {"label": "1", "pos": (0, 5), "size": (btn_x, btn_y), "color": RED}, |
| 46 | + {"label": "2", "pos": (40, 5), "size": (btn_x, btn_y), "color": ORANGE}, |
| 47 | + {"label": "3", "pos": (80, 5), "size": (btn_x, btn_y), "color": YELLOW}, |
| 48 | + {"label": "4", "pos": (120, 5), "size": (btn_x, btn_y), "color": GREEN}, |
| 49 | + {"label": "5", "pos": (0, 45), "size": (btn_x, btn_y), "color": CYAN}, |
| 50 | + {"label": "6", "pos": (40, 45), "size": (btn_x, btn_y), "color": BLUE}, |
| 51 | + {"label": "7", "pos": (80, 45), "size": (btn_x, btn_y), "color": VIOLET}, |
| 52 | + {"label": "8", "pos": (120, 45), "size": (btn_x, btn_y), "color": MAGENTA}, |
| 53 | + {"label": "9", "pos": (0, 85), "size": (btn_x, btn_y), "color": PINK}, |
| 54 | + {"label": "10", "pos": (40, 85), "size": (btn_x, btn_y), "color": AQUA}, |
| 55 | + {"label": "11", "pos": (80, 85), "size": (btn_x, btn_y), "color": WHITE}, |
| 56 | + {"label": "12", "pos": (120, 85), "size": (btn_x, btn_y), "color": OFF}, |
| 57 | +] |
| 58 | + |
| 59 | +buttons = [] |
| 60 | +for spot in spots: |
| 61 | + button = Button( |
| 62 | + x=spot["pos"][0], |
| 63 | + y=spot["pos"][1], |
| 64 | + width=spot["size"][0], |
| 65 | + height=spot["size"][1], |
| 66 | + style=Button.SHADOWROUNDRECT, |
| 67 | + fill_color=spot["color"], |
| 68 | + outline_color=0x222222, |
| 69 | + name=spot["label"], |
| 70 | + ) |
| 71 | + splash.append(button.group) |
| 72 | + buttons.append(button) |
| 73 | + |
| 74 | +# initialize the mouse cursor object |
| 75 | +mouse_cursor = Cursor(display, display_group=splash) |
| 76 | + |
| 77 | +# initialize the cursormanager |
| 78 | +cursor = DebouncedCursorManager(mouse_cursor) |
| 79 | + |
| 80 | +# Show splash group |
| 81 | +display.show(splash) |
| 82 | + |
| 83 | +prev_btn = None |
| 84 | +while True: |
| 85 | + cursor.update() |
| 86 | + if cursor.is_clicked is True: |
| 87 | + for i, b in enumerate(buttons): |
| 88 | + if b.contains((mouse_cursor.x, mouse_cursor.y)): |
| 89 | + b.selected = True |
| 90 | + print("Button %d clicked" % i) |
| 91 | + strip.fill(b.fill_color) |
| 92 | + prev_btn = b |
| 93 | + elif prev_btn is not None: |
| 94 | + prev_btn.selected = False |
| 95 | + time.sleep(0.1) |
0 commit comments