Skip to content

Instantly share code, notes, and snippets.

@deckerego
Last active December 15, 2019 03:34
Show Gist options
  • Save deckerego/a55c94418a9464cbcd8e09ec9b13076d to your computer and use it in GitHub Desktop.
Save deckerego/a55c94418a9464cbcd8e09ec9b13076d to your computer and use it in GitHub Desktop.
Helper lamp with varying color temperature and brightness
import analogio
import simpleio
import board
import time
import neopixel
import touchio
from digitalio import DigitalInOut, Direction, Pull
# NeoPixels
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=False)
color = (255, 255, 255)
# Light indicator
light = analogio.AnalogIn(board.LIGHT)
brightness = simpleio.map_range(light.value, 800, 60000, 0, 100) / 50.0
# Toggle Switch
switch = DigitalInOut(board.SLIDE_SWITCH)
switch.direction = Direction.INPUT
switch.pull = Pull.UP
power = False
# Button A
button_a = DigitalInOut(board.BUTTON_A)
button_a.direction = Direction.INPUT
button_a.pull = Pull.DOWN
# Button B
button_b = DigitalInOut(board.BUTTON_B)
button_b.direction = Direction.INPUT
button_b.pull = Pull.DOWN
# Touchpads
touchpad_1 = touchio.TouchIn(board.A1)
touchpad_2 = touchio.TouchIn(board.A2)
touchpad_3 = touchio.TouchIn(board.A3)
touchpad_4 = touchio.TouchIn(board.A4)
touchpad_5 = touchio.TouchIn(board.A5)
touchpad_6 = touchio.TouchIn(board.A6)
touchpad_7 = touchio.TouchIn(board.A7)
# Color Palette by Receive Code
palette = {
1: (255, 147, 41), # Touchpad 1
2: (255, 197, 143), # Touchpad 2
3: (255, 214, 170), # Touchpad 3
4: (255, 241, 224), # Touchpad 4
5: (255, 250, 244), # Touchpad 5
6: (255, 255, 251), # Touchpad 6
7: (201, 226, 255), # Touchpad 7
}
def get_touchpad():
if touchpad_1.value: return 1
elif touchpad_2.value: return 2
elif touchpad_3.value: return 3
elif touchpad_4.value: return 4
elif touchpad_5.value: return 5
elif touchpad_6.value: return 6
elif touchpad_7.value: return 7
else : return False
def show_pixels():
for index in range(10):
pixels[index] = color if power else (0, 0, 0)
pixels.brightness = brightness
pixels.show()
while True:
if switch.value:
if not power:
brightness = simpleio.map_range(light.value, 800, 60000, 5, 100) / 100.0
power = True
sleep = 0.5
if button_a.value:
brightness = brightness - 0.05 if brightness > 0.0 else 1.0
if button_b.value:
brightness = brightness + 0.05 if brightness < 1.0 else 1.0
touchpad = get_touchpad()
if touchpad:
color = palette[touchpad]
show_pixels()
time.sleep(0.5)
else:
if power:
brightness = 0
power = False
show_pixels()
time.sleep(1.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment