Skip to content

Instantly share code, notes, and snippets.

@dglaude
Created November 2, 2017 21:41
Show Gist options
  • Select an option

  • Save dglaude/b39da6f2a02cd9a1b2a8cc4c5db1e11e to your computer and use it in GitHub Desktop.

Select an option

Save dglaude/b39da6f2a02cd9a1b2a8cc4c5db1e11e to your computer and use it in GitHub Desktop.
DEMO: Scroll pHAT + Scroll pHAT HD + Unicorn HAT + Blinkt
#!/usr/bin/env python
import time
import math
import colorsys
from sys import exit
has_scroll = True
has_scrollhd = True
has_unicorn = True
has_blinkt = True
if has_scroll :
import scrollphat
j = 0
buf = [0] * 11
scrollphat.set_brightness(20)
if has_scrollhd :
import scrollphathd
from scrollphathd.fonts import font5x5
DISPLAY_BAR = False
BRIGHTNESS = 0.3
if has_unicorn :
import unicornhat as unicorn
try:
import numpy
except ImportError:
exit("This script requires the numpy module\nInstall with: sudo pip install numpy")
if has_blinkt :
import blinkt
blinkt.set_clear_on_exit()
REDS = [0, 0, 0, 0, 0, 16, 64, 255, 64, 16, 0, 0, 0, 0, 0, 0]
start_time = time.time()
if has_unicorn :
unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(0.5)
width,height=unicorn.get_shape()
while True:
if has_blinkt :
delta = (time.time() - start_time) * 16
offset = int(abs((delta % len(REDS)) - blinkt.NUM_PIXELS))
for i in range(blinkt.NUM_PIXELS):
blinkt.set_pixel(i , REDS[offset + i], 0, 0)
blinkt.show()
if has_unicorn :
rand_mat = numpy.random.rand(width,height)
for y in range(height):
for x in range(width):
h = 0.1 * rand_mat[x, y]
s = 0.8
v = rand_mat[x, y]
rgb = colorsys.hsv_to_rgb(h, s, v)
r = int(rgb[0]*255.0)
g = int(rgb[1]*255.0)
b = int(rgb[2]*255.0)
unicorn.set_pixel(x, y, r, g, b)
unicorn.show()
if has_scroll :
for x in range(0, 11):
y = (math.sin((j + (x * 10)) / 10.0) + 1) # Produces range from 0 to 2
y *= 2.5 # Scale to 0 to 5
buf[x] = 1 << int(y)
scrollphat.set_buffer(buf)
scrollphat.update()
j += 10
if has_scrollhd :
scrollphathd.clear()
float_sec = (time.time() % 60) / 59.0
seconds_progress = float_sec * 15
if DISPLAY_BAR:
for y in range(15):
current_pixel = min(seconds_progress, 1)
scrollphathd.set_pixel(y + 1, 6, current_pixel * BRIGHTNESS)
seconds_progress -= 1
if seconds_progress <= 0:
break
else:
scrollphathd.set_pixel(int(seconds_progress), 6, BRIGHTNESS)
# Display the time (HH:MM) in a 5x5 pixel font
scrollphathd.write_string(
time.strftime("%H:%M"),
x=0, # Align to the left of the buffer
y=0, # Align to the top of the buffer
font=font5x5, # Use the font5x5 font we imported above
brightness=BRIGHTNESS # Use our global brightness value
)
if int(time.time()) % 2 == 0:
scrollphathd.clear_rect(8, 0, 1, 5)
scrollphathd.show()
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment