Skip to content

Instantly share code, notes, and snippets.

@kwalseth
Created May 29, 2020 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwalseth/a765cf3950bcffc5d66621a48189157e to your computer and use it in GitHub Desktop.
Save kwalseth/a765cf3950bcffc5d66621a48189157e to your computer and use it in GitHub Desktop.
emblem
import time
import board
from adafruit_pyportal import PyPortal
from adafruit_button import Button
import neopixel
import analogio
# Set the background color
BACKGROUND_COLOR = 0x443355
# Set the NeoPixel brightness
BRIGHTNESS = 0.3
light_sensor = analogio.AnalogIn(board.LIGHT)
pixels = neopixel.NeoPixel(board.D4, 74, brightness=BRIGHTNESS)
# Turn off NeoPixels to start
pixels.fill(0)
# Setup PyPortal without networking
pyportal = PyPortal(default_bg=BACKGROUND_COLOR)
def fireeffect():
pixels[0:28] = [0xFF0000] * 28
pixels[67:74] = [0xFF0000] * 7
pixels[28:67] = [0x850000] * 39
pixels.show()
time.sleep(.2)
pixels[28:67] = [0xFF0000] * 39
pixels[0:28] = [0x850000] * 28
pixels[67:74] = [0x850000] * 7
pixels.show()
time.sleep(.2)
pixels[0:28] = [0xFF0000] * 28
pixels[67:74] = [0xFF0000] * 7
pixels[28:67] = [0x850000] * 39
pixels.show()
time.sleep(.2)
pixels[28:67] = [0xFF0000] * 39
pixels[0:28] = [0x850000] * 28
pixels[67:74] = [0x850000] * 7
pixels.show()
time.sleep(.2)
pixels[0:28] = [0xFF0000] * 28
pixels[67:74] = [0xFF0000] * 7
pixels[28:67] = [0x850000] * 39
pixels.show()
time.sleep(.2)
pixels[28:67] = [0xFF0000] * 39
pixels[0:28] = [0x850000] * 28
pixels[67:74] = [0x850000] * 7
pixels.show()
time.sleep(.2)
pixels.fill(RED)
pixels.show()
time.sleep(.1)
pixels.fill(ORANGE)
pixels.show()
time.sleep(.1)
pixels.fill(RED)
pixels.show()
time.sleep(.1)
pixels.fill(ORANGE)
pixels.show()
time.sleep(.1)
pixels.fill(RED)
pixels.show()
time.sleep(.1)
pixels.fill(ORANGE)
pixels.show()
time.sleep(.1)
# Button colors
RED = (255, 0, 0)
ORANGE = (255, 34, 0)
YELLOW = (255, 170, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
VIOLET = (153, 0, 255)
MAGENTA = (255, 0, 51)
PINK = (255, 51, 119)
AQUA = (85, 125, 255)
WHITE = (255, 255, 255)
Fire = (0, 0, 0)
spots = [
{'label': "1", 'pos': (10, 10), 'size': (60, 60), 'color': RED},
{'label': "2", 'pos': (90, 10), 'size': (60, 60), 'color': ORANGE},
{'label': "3", 'pos': (170, 10), 'size': (60, 60), 'color': YELLOW},
{'label': "4", 'pos': (250, 10), 'size': (60, 60), 'color': GREEN},
{'label': "5", 'pos': (10, 90), 'size': (60, 60), 'color': CYAN},
{'label': "6", 'pos': (90, 90), 'size': (60, 60), 'color': BLUE},
{'label': "7", 'pos': (170, 90), 'size': (60, 60), 'color': VIOLET},
{'label': "8", 'pos': (250, 90), 'size': (60, 60), 'color': MAGENTA},
{'label': "9", 'pos': (10, 170), 'size': (60, 60), 'color': PINK},
{'label': "10", 'pos': (90, 170), 'size': (60, 60), 'color': AQUA},
{'label': "11", 'pos': (170, 170), 'size': (60, 60), 'color': WHITE},
{'label': "12", 'pos': (250, 170), 'size': (60, 60), 'color': Fire}
]
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.SHADOWROUNDRECT,
fill_color=spot['color'], outline_color=0x222222,
name=spot['label'])
pyportal.splash.append(button.group)
buttons.append(button)
mode = 0
mode_change = None
# Calibrate light sensor on start to deal with different lighting situations
# If the mode change isn't responding properly, reset your PyPortal to recalibrate
initial_light_value = light_sensor.value
while True:
if light_sensor.value < (initial_light_value * 0.3) and mode_change is None:
mode_change = "mode_change"
if light_sensor.value > (initial_light_value * 0.5) and mode_change == "mode_change":
mode += 1
mode_change = None
if mode > 1:
mode = 0
touch = pyportal.touchscreen.touch_point
if touch:
for button in buttons:
if button.contains(touch):
print("Touched", button.name)
pixels.fill(button.fill_color)
if button.name == 12:
fireeffect()
time.sleep(0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment