Last active
February 9, 2020 00:44
-
-
Save jsatt/9678fa2718f0244b855379e6ba9b00c1 to your computer and use it in GitHub Desktop.
CircuitPy neopixel color dial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import board | |
import neopixel | |
import analogio | |
import time | |
from simpleio import map_range | |
NEOPIXEL_PIN = board.D5 | |
NEOPIXEL_COUNT = 20 | |
NEOPIXEL_ORDER = neopixel.GRB | |
BRIGHTNESS_PIN = board.A2 | |
HUE_PIN = board.A1 | |
brightness_pin = analogio.AnalogIn(BRIGHTNESS_PIN) | |
brightness_range = (128, 65520) | |
hue_pin = analogio.AnalogIn(HUE_PIN) | |
hue_range = (128, 65520) | |
def hsv_to_rgb(hue, saturation, value): | |
i = math.floor(hue * 6) | |
f = hue * 6 - i | |
p = value * (1 - saturation) | |
q = value * (1 - f * saturation) | |
t = value * (1 - (1 - f) * saturation) | |
red, green, blue = [ | |
(value, t, p), | |
(q, value, p), | |
(p, value, t), | |
(p, q, value), | |
(t, p, value), | |
(value, p, q), | |
][int(i % 6)] | |
return red, green, blue | |
with neopixel.NeoPixel( | |
NEOPIXEL_PIN, | |
NEOPIXEL_COUNT, | |
pixel_order=NEOPIXEL_ORDER, | |
auto_write=False) as pixels: | |
while True: | |
hue = map_range(hue_pin.value, hue_range[0], hue_range[1], 0, 1) | |
rgb = [round(v * 255) for v in hsv_to_rgb(hue, 1, 1)] | |
# print(hue, rgb) | |
pixels[::] = [rgb] * 20 | |
pixels.brightness = (map_range(brightness_pin.value, brightness_range[0], brightness_range[1], 0, 1)) | |
pixels.show() | |
time.sleep(.125) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment