Skip to content

Instantly share code, notes, and snippets.

@ali1234
Created February 26, 2018 00:11
unicorn hat hd demos
import time, math, colorsys, random
from datetime import datetime, timedelta
from picamera import PiCamera
import numpy as np
import unicornhathd
unicornhathd.rotation(270)
class DisplayOutput():
def write(self, buf):
img = np.frombuffer(buf, dtype=np.uint8).reshape(16, 4, 16, 4, 3)
img = np.average(img, axis=-2)
# note: this mirrors the picture
unicornhathd._buf[::-1,:,:] = np.average(img, axis=-3)
unicornhathd.show()
def picamera(runtime=30):
with PiCamera() as camera:
camera.resolution = (64, 64)
camera.contrast = 50
camera.start_preview()
output = DisplayOutput()
camera.start_recording(output, 'rgb')
try:
time.sleep(runtime)
finally:
camera.stop_recording()
def rainbow(runtime=30):
buf = np.empty((16,16,3), dtype=np.uint8)
start = datetime.now()
step = random.randint(0, 0xffffffff)
while datetime.now() < (start + timedelta(seconds=runtime)):
step += 1
dx = (math.sin(step / 20.0) * 15.0) + 7.0
dy = (math.cos(step / 15.0) * 15.0) + 7.0
sc = (math.cos(step / 10.0) * 10.0) + 16.0
xx = np.fromfunction(lambda x, y: x-dx, (16, 16), dtype=np.float)
yy = np.fromfunction(lambda x, y: y-dy, (16, 16), dtype=np.float)
h = np.sqrt((xx*xx) + (yy*yy)) / sc
unicornhathd._buf[:,:,0] = np.clip((0.333 - np.abs(np.mod(h, 1)-0.5))*6, 0, 1) * 255
unicornhathd._buf[:,:,1] = np.clip((0.333 - np.abs(np.mod(h+0.333, 1)-0.5))*6, 0, 1) * 255
unicornhathd._buf[:,:,2] = np.clip((0.333 - np.abs(np.mod(h+0.666, 1)-0.5))*6, 0, 1) * 255
unicornhathd.show()
def matrix(runtime=30):
wrd_rgb = [[154, 173, 154], [0, 255, 0], [0, 235, 0], [0, 220, 0], [0, 185, 0],
[0, 165, 0], [0, 128, 0], [0, 0, 0,], [154, 173, 154], [0, 145, 0],
[0, 125, 0], [0, 100, 0], [0, 80, 0], [0, 60, 0], [0, 40, 0], [0, 0, 0,]]
start = datetime.now()
clock = 0
blue_pilled_population = [[random.randint(0,15), 15]]
while datetime.now() < (start + timedelta(seconds=runtime)):
for person in blue_pilled_population:
y = person[1]
for rgb in wrd_rgb:
if (y <= 15) and (y >= 0):
unicornhathd.set_pixel(person[0], y, rgb[0], rgb[1], rgb[2])
y += 1
person[1] -= 1
unicornhathd.show()
time.sleep(0.1)
clock += 1
if clock % 5 == 0:
blue_pilled_population.append([random.randint(0,15), 15])
if clock % 7 == 0:
blue_pilled_population.append([random.randint(0,15), 15])
while len(blue_pilled_population) > 100:
blue_pilled_population.pop(0)
if __name__ == '__main__':
while(True):
picamera(60*5)
rainbow(30)
picamera(60*5)
matrix(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment