|
| 1 | +# SPDX-FileCopyrightText: 2021 Tod Kurt @todbot and John Park for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# QT Py encoder based on https://github.com/todbot/qtpy-knob |
| 4 | +# Retroreflective chromakey light ring |
| 5 | +# Mount a rotary encoder directly to an Adafruit QT Py, |
| 6 | +# add some neopixels to get a color/brightness controller |
| 7 | +# |
| 8 | +import time |
| 9 | +import board |
| 10 | +from digitalio import DigitalInOut, Direction, Pull |
| 11 | +import neopixel |
| 12 | +import rotaryio |
| 13 | + |
| 14 | + |
| 15 | +dim_val = 0.2 |
| 16 | + |
| 17 | +NUM_PIX = 24 |
| 18 | + |
| 19 | +PIX_TYPE = "RGB" # RGB or RGBW |
| 20 | + |
| 21 | +if PIX_TYPE == "RGB": |
| 22 | + ORDER = (1, 0, 2) |
| 23 | + GREEN = (0, 255, 0) |
| 24 | + BLUE = (0, 0, 255) |
| 25 | + WHITE = (255, 255, 255) |
| 26 | + BLACK = (0, 0, 0) |
| 27 | +else: |
| 28 | + ORDER = (1, 0, 2, 3) |
| 29 | + GREEN = (0, 255, 0, 0) |
| 30 | + BLUE = (0, 0, 255, 0) |
| 31 | + WHITE = (0, 0, 0, 255) |
| 32 | + BLACK = (0, 0, 0, 0) |
| 33 | + |
| 34 | + |
| 35 | +colors = [GREEN, BLUE, WHITE, BLACK] |
| 36 | +current_color = 0 |
| 37 | + |
| 38 | + |
| 39 | +ring = neopixel.NeoPixel( |
| 40 | + board.MISO, NUM_PIX, brightness=0.2, auto_write=False, pixel_order=ORDER |
| 41 | +) |
| 42 | +ring.fill(colors[current_color]) |
| 43 | +ring.show() |
| 44 | + |
| 45 | +# button of rotary encoder |
| 46 | +button = DigitalInOut(board.MOSI) |
| 47 | +button.pull = Pull.UP |
| 48 | + |
| 49 | +# Use pin A2 as a fake ground for the rotary encoder |
| 50 | +fakegnd = DigitalInOut(board.A2) |
| 51 | +fakegnd.direction = Direction.OUTPUT |
| 52 | +fakegnd.value = False |
| 53 | + |
| 54 | +encoder = rotaryio.IncrementalEncoder(board.A3, board.A1) |
| 55 | + |
| 56 | +print("---Chromakey Light Ring---") |
| 57 | + |
| 58 | +last_encoder_val = encoder.position |
| 59 | +ring_pos = 0 |
| 60 | +rainbow_pos = 0 |
| 61 | +last_time = time.monotonic() |
| 62 | +ring_on = True |
| 63 | + |
| 64 | +while True: |
| 65 | + encoder_diff = last_encoder_val - encoder.position # encoder clicks since last read |
| 66 | + last_encoder_val = encoder.position |
| 67 | + |
| 68 | + if button.value is False: # button pressed |
| 69 | + current_color = (current_color + 1) % len(colors) |
| 70 | + ring.fill(colors[current_color]) |
| 71 | + ring.show() |
| 72 | + time.sleep(0.5) # debounce |
| 73 | + |
| 74 | + else: |
| 75 | + if encoder_diff > 0: |
| 76 | + if dim_val >= 0.01: |
| 77 | + dim_val = (dim_val - 0.01) % 1.0 |
| 78 | + ring.brightness = dim_val |
| 79 | + ring.show() |
| 80 | + elif encoder_diff < 0: |
| 81 | + if dim_val <= 0.99: |
| 82 | + dim_val = (dim_val + 0.01) % 1.0 |
| 83 | + ring.brightness = dim_val |
| 84 | + ring.show() |
| 85 | + |
| 86 | + time.sleep(0.01) |
0 commit comments