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