From 1c9447f07b8d520889135cb4632003ed4bd8268f Mon Sep 17 00:00:00 2001 From: John Park Date: Mon, 8 Mar 2021 12:29:03 -0800 Subject: [PATCH] first commit chroma light ring code --- Chromakey_Light_Ring/chromakey_light_ring.py | 86 ++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Chromakey_Light_Ring/chromakey_light_ring.py diff --git a/Chromakey_Light_Ring/chromakey_light_ring.py b/Chromakey_Light_Ring/chromakey_light_ring.py new file mode 100644 index 000000000..9949609db --- /dev/null +++ b/Chromakey_Light_Ring/chromakey_light_ring.py @@ -0,0 +1,86 @@ +# SPDX-FileCopyrightText: 2021 Tod Kurt @todbot and John Park for Adafruit Industries +# SPDX-License-Identifier: MIT +# QT Py encoder based on https://github.com/todbot/qtpy-knob +# Retroreflective chromakey light ring +# Mount a rotary encoder directly to an Adafruit QT Py, +# add some neopixels to get a color/brightness controller +# +import time +import board +from digitalio import DigitalInOut, Direction, Pull +import neopixel +import rotaryio + + +dim_val = 0.2 + +NUM_PIX = 24 + +PIX_TYPE = "RGB" # RGB or RGBW + +if PIX_TYPE == "RGB": + ORDER = (1, 0, 2) + GREEN = (0, 255, 0) + BLUE = (0, 0, 255) + WHITE = (255, 255, 255) + BLACK = (0, 0, 0) +else: + ORDER = (1, 0, 2, 3) + GREEN = (0, 255, 0, 0) + BLUE = (0, 0, 255, 0) + WHITE = (0, 0, 0, 255) + BLACK = (0, 0, 0, 0) + + +colors = [GREEN, BLUE, WHITE, BLACK] +current_color = 0 + + +ring = neopixel.NeoPixel( + board.MISO, NUM_PIX, brightness=0.2, auto_write=False, pixel_order=ORDER +) +ring.fill(colors[current_color]) +ring.show() + +# button of rotary encoder +button = DigitalInOut(board.MOSI) +button.pull = Pull.UP + +# Use pin A2 as a fake ground for the rotary encoder +fakegnd = DigitalInOut(board.A2) +fakegnd.direction = Direction.OUTPUT +fakegnd.value = False + +encoder = rotaryio.IncrementalEncoder(board.A3, board.A1) + +print("---Chromakey Light Ring---") + +last_encoder_val = encoder.position +ring_pos = 0 +rainbow_pos = 0 +last_time = time.monotonic() +ring_on = True + +while True: + encoder_diff = last_encoder_val - encoder.position # encoder clicks since last read + last_encoder_val = encoder.position + + if button.value is False: # button pressed + current_color = (current_color + 1) % len(colors) + ring.fill(colors[current_color]) + ring.show() + time.sleep(0.5) # debounce + + else: + if encoder_diff > 0: + if dim_val >= 0.01: + dim_val = (dim_val - 0.01) % 1.0 + ring.brightness = dim_val + ring.show() + elif encoder_diff < 0: + if dim_val <= 0.99: + dim_val = (dim_val + 0.01) % 1.0 + ring.brightness = dim_val + ring.show() + + time.sleep(0.01)