|
| 1 | +import time |
| 2 | +import board |
| 3 | +from adafruit_pyportal import PyPortal |
| 4 | +from adafruit_button import Button |
| 5 | +import neopixel |
| 6 | +import analogio |
| 7 | + |
| 8 | +# Set the background color |
| 9 | +BACKGROUND_COLOR = 0x443355 |
| 10 | + |
| 11 | +light_sensor = analogio.AnalogIn(board.LIGHT) |
| 12 | + |
| 13 | +strip_1 = neopixel.NeoPixel(board.D4, 30, brightness=0.3) |
| 14 | +strip_2 = neopixel.NeoPixel(board.D3, 30, brightness=0.3) |
| 15 | + |
| 16 | +# Turn off NeoPixels to start |
| 17 | +strip_1.fill(0) |
| 18 | +strip_2.fill(0) |
| 19 | + |
| 20 | +# Setup PyPortal without networking |
| 21 | +pyportal = PyPortal(default_bg=BACKGROUND_COLOR) |
| 22 | + |
| 23 | +# Button colors |
| 24 | +RED = (255, 0, 0) |
| 25 | +ORANGE = (255, 34, 0) |
| 26 | +YELLOW = (255, 170, 0) |
| 27 | +GREEN = (0, 255, 0) |
| 28 | +CYAN = (0, 255, 255) |
| 29 | +BLUE = (0, 0, 255) |
| 30 | +VIOLET = (153, 0, 255) |
| 31 | +MAGENTA = (255, 0, 51) |
| 32 | +PINK = (255, 51, 119) |
| 33 | +AQUA = (85, 125, 255) |
| 34 | +WHITE = (255, 255, 255) |
| 35 | +OFF = (0, 0, 0) |
| 36 | + |
| 37 | +spots = [ |
| 38 | + {'label': "1", 'pos': (10, 10), 'size': (60, 60), 'color': RED}, |
| 39 | + {'label': "2", 'pos': (90, 10), 'size': (60, 60), 'color': ORANGE}, |
| 40 | + {'label': "3", 'pos': (170, 10), 'size': (60, 60), 'color': YELLOW}, |
| 41 | + {'label': "4", 'pos': (250, 10), 'size': (60, 60), 'color': GREEN}, |
| 42 | + {'label': "5", 'pos': (10, 90), 'size': (60, 60), 'color': CYAN}, |
| 43 | + {'label': "6", 'pos': (90, 90), 'size': (60, 60), 'color': BLUE}, |
| 44 | + {'label': "7", 'pos': (170, 90), 'size': (60, 60), 'color': VIOLET}, |
| 45 | + {'label': "8", 'pos': (250, 90), 'size': (60, 60), 'color': MAGENTA}, |
| 46 | + {'label': "9", 'pos': (10, 170), 'size': (60, 60), 'color': PINK}, |
| 47 | + {'label': "10", 'pos': (90, 170), 'size': (60, 60), 'color': AQUA}, |
| 48 | + {'label': "11", 'pos': (170, 170), 'size': (60, 60), 'color': WHITE}, |
| 49 | + {'label': "12", 'pos': (250, 170), 'size': (60, 60), 'color': OFF} |
| 50 | + ] |
| 51 | + |
| 52 | +buttons = [] |
| 53 | +for spot in spots: |
| 54 | + button = Button(x=spot['pos'][0], y=spot['pos'][1], |
| 55 | + width=spot['size'][0], height=spot['size'][1], |
| 56 | + style=Button.SHADOWROUNDRECT, |
| 57 | + fill_color=spot['color'], outline_color=0x222222, |
| 58 | + name=spot['label']) |
| 59 | + pyportal.splash.append(button.group) |
| 60 | + buttons.append(button) |
| 61 | + |
| 62 | +mode = 0 |
| 63 | +mode_change = None |
| 64 | + |
| 65 | +# Calibrate light sensor on start to deal with different lighting situations |
| 66 | +# If the mode change isn't responding properly, reset your PyPortal to recalibrate |
| 67 | +initial_light_value = light_sensor.value |
| 68 | +while True: |
| 69 | + if light_sensor.value < (initial_light_value * 0.3) and mode_change is None: |
| 70 | + mode_change = "mode_change" |
| 71 | + if light_sensor.value > (initial_light_value * 0.5) and mode_change == "mode_change": |
| 72 | + mode += 1 |
| 73 | + mode_change = None |
| 74 | + if mode > 2: |
| 75 | + mode = 0 |
| 76 | + print(mode) |
| 77 | + touch = pyportal.touchscreen.touch_point |
| 78 | + if touch: |
| 79 | + for button in buttons: |
| 80 | + if button.contains(touch): |
| 81 | + print("Touched", button.name) |
| 82 | + if mode == 0: |
| 83 | + strip_1.fill(button.fill_color) |
| 84 | + elif mode == 1: |
| 85 | + strip_2.fill(button.fill_color) |
| 86 | + elif mode == 2: |
| 87 | + strip_1.fill(button.fill_color) |
| 88 | + strip_2.fill(button.fill_color) |
| 89 | + break |
| 90 | + time.sleep(0.05) |
0 commit comments