Skip to content

Instantly share code, notes, and snippets.

@jameseleach
Last active December 25, 2020 01:02
Show Gist options
  • Save jameseleach/d874080d58eda14de7d40c7040c9cee5 to your computer and use it in GitHub Desktop.
Save jameseleach/d874080d58eda14de7d40c7040c9cee5 to your computer and use it in GitHub Desktop.
NeoPixel Lights
import time
import board
import neopixel
from adafruit_button import Button
from adafruit_pyportal import PyPortal
from adafruit_bitmap_font import bitmap_font
# Define colors
colors = [
{'name': "1900K", 'color': (255, 147, 41)}, # Candle
{'name': "2600K", 'color': (255, 197, 143)}, # 40W Tungsten
{'name': "2850K", 'color': (255, 214, 170)}, # 100W Tungsten
{'name': "3200K", 'color': (255, 241, 224)}, # Halogen
{'name': "5200K", 'color': (255, 250, 244)}, # Carbon Arc
{'name': "5400K", 'color': (255, 255, 251)}, # High Noon Sun
{'name': "6000K", 'color': (255, 255, 255)}, # Direct Sunlight
{'name': "7000K", 'color': (201, 226, 255)} # Overcast Sky
]
# Setup NeoPixel Strip
strip = neopixel.NeoPixel(board.D3, 30)
current_brightness = 2
current_color = 5
strip.brightness = current_brightness / 10
strip.fill(colors[current_color]['color'])
# Setup PyPortal without networking
pyportal = PyPortal()
# Load the font for the buttons
font = bitmap_font.load_font("/fonts/Arial-14.bdf")
# glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: '
glyphs = b'0123456789KainxFMNO↑↓'
font.load_glyphs(glyphs)
# Setup buttons
spots = [
{'label': "ON", 'name': "ON", 'pos': (20, 15), 'size': (130, 60)},
{'label': "OFF", 'name': "OFF", 'pos': (170, 15), 'size': (130, 60)},
{'label': "↓", 'name': "BRIGHT_DN", 'pos': (20, 90), 'size': (60, 60)},
{'label': str(current_brightness), 'name': "Brightness", 'pos': (100, 90), 'size': (120, 60)},
{'label': "↑", 'name': "BRIGHT_UP", 'pos': (240, 90), 'size': (60, 60)},
{'label': "↓", 'name': "TEMP_DN", 'pos': (20, 165), 'size': (60, 60)},
{'label': colors[current_color]['name'], 'name': "Color", 'pos': (100, 165), 'size': (120, 60)},
{'label': "↑", 'name': "TEMP_UP", 'pos': (240, 165), 'size': (60, 60)}
]
# Populate the buttons
buttons = []
for spot in spots:
button = Button(
x=spot['pos'][0],
y=spot['pos'][1],
width=spot['size'][0],
height=spot['size'][1],
style=Button.ROUNDRECT,
label_font=font,
fill_color=0x000000,
outline_color=0xFFFFFF,
label_color=0xFFFFFF,
name=spot['name'],
label=spot['label']
)
pyportal.splash.append(button)
buttons.append(button)
# Set friendly names for the two 'label' buttons
BRIGHTNESS_BUTTON = buttons[3]
COLORTEMP_BUTTON = buttons[6]
def on():
print('Turning Neopixel Strip on.')
strip.fill(colors[current_color]['color'])
def off():
print('Turning NeoPixel Strip off.')
strip.fill((0,0,0))
def inc_brightness():
global current_brightness
if current_brightness < 10:
current_brightness += 1
print('Setting NeoPixel Strip brightness to {0}'.format(current_brightness / 10))
strip.brightness = current_brightness / 10
if current_brightness == 10:
BRIGHTNESS_BUTTON.label = "Max"
else:
BRIGHTNESS_BUTTON.label = str(current_brightness)
else:
print('NeoPixel Strip brightness already at max.')
def dec_brightness():
global current_brightness
if current_brightness > 1:
current_brightness -= 1
print('Setting NeoPixel Strip brightness to {0}'.format(current_brightness / 10))
strip.brightness = current_brightness / 10
if current_brightness == 1:
BRIGHTNESS_BUTTON.label = "Min"
else:
BRIGHTNESS_BUTTON.label = str(current_brightness)
else:
print('NeoPixel Strip brightness already at minimum.')
def inc_temp():
global current_color
current_color
if current_color < (len(colors) -1):
current_color += 1
print('Setting color temperature to {0} with index {1}'.format(colors[current_color]['name'], current_color))
strip.fill(colors[current_color]['color'])
if current_color == (len(colors) -1):
COLORTEMP_BUTTON.label = "Max"
else:
COLORTEMP_BUTTON.label = colors[current_color]['name']
else:
print('Color temp already at max.')
def dec_temp():
global current_color
if current_color > 0:
current_color -= 1
print('Setting color temperature to {0} with index {1}'.format(colors[current_color]['name'], current_color))
strip.fill(colors[current_color]['color'])
if current_color == 0:
COLORTEMP_BUTTON.label = "Min"
else:
COLORTEMP_BUTTON.label = colors[current_color]['name']
else:
print('Color temp already at minimum.')
func_dict = {
'ON': (on),
'OFF': (off),
'BRIGHT_DN': (dec_brightness),
'BRIGHT_UP': (inc_brightness),
'TEMP_DN': (dec_temp),
'TEMP_UP': (inc_temp)
}
while True:
touch = pyportal.touchscreen.touch_point
if touch:
for button in buttons:
if button.contains(touch):
button.selected = True
print("Touched", button.name)
func_dict.get(button.name)()
time.sleep(0.1)
button.selected = False
while pyportal.touchscreen.touch_point:
pass
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment