RPi + Unicorn: Python 8x8 Matrix Summary of YOUR Codes


#1

Hi Guys,

I received my RPi with the Unicorn Hat (8x8) and my anidees silver/clear case. It’s working all amazing … and i’m kinda blown away from the code examples inside the unicorn hat folders.

But with my limited knowledge about LED’s and python coding I am desperately looking for other examples/finished codes which work with the raspberry pi and the unicorn hat. (without editing to fit to rpi or unicorn)

Have you guys build your own codes for the unicorn hat? I think it would be a great idea to collect codes from each other and post them here or on github.

I only changed the examples a little bit but not with huge success.

Looking forward to your ideas


#2

The fundamentals of the unicorn hat python library are covered here.

https://learn.pimoroni.com/tutorial/sandyj/getting-started-with-unicorn-phat

Although you will need to change the line from.

uh.set_layout(uh.PHAT)

To

uh.set_layout(uh.HAT)

So that way you can program it yourself


#3

thx. Maybe this will help me to understand the codes a bit more. What do you think about creating a collection of codes?


#4

Do you mean a collection of code examples? If so, (provided the author allows it), it would be good to get them committed back into the examples folder of the UnicornHAT repo (makes sense for them to live there).

For what it’s worth, I have spent some time playing with the UnicornHAT, but all my examples are in Ruby, so I’m not sure how much use they’ll be to you:

I was mostly playing with the idea of animating figures. Here are some videos of what I produced, sorry for the potato quality:

(RIP vine)


#5

They’re all dead for me :(

Here’s an audio visualiser I made:


#6

Oh, that sucks. As I say, rip vine.

What are you using as a diffuser?


#7

3D printed grid with some adhesive vinyl stuck over the top.


#8

With the links you guys provided me, I made a Zelda heart which gets lower every second for a future boot script for retropie. (don’t know how to upload on github)

For Kodi I would like to create this scene from Independence Day for the Unicorn Hat

Will let you guys know how it worked out when I know how to do it. :)


#9

Hi Guys,

I guess it’s not even possible. Can I change the brightness of each single LED or is it always set for the complete hat?

I am still searching for a way to get the independence day effect and tried some random codes to get the board to pulse the LEDs:

import time
import unicornhat as unicorn
import random

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)

while True:
br = random.uniform(0.2,1)
y = random.randint(0,7)
x = random.randint(0,7)
red = 255
green = random.randint(0,153)
blue = random.randint(0,153)
unicorn.set_pixel(x, y, red, green, blue)
unicorn.brightness(br)
unicorn.show()
time.sleep(0.125)


#10

So let’s say I wan to light up (0,0) as white.

I could do this by

unicornhat.set_pixel(0,0,255,255,255)

However if I would like this pixel to be a dimmer white I could do it like this

unicornhat.set_pixel(0,0,100,100,100)

Hope this helps!


#11

Using colorsys.hsv_to_rgb() is a good way to do this. In HSV colour, the V sets the brightness, so you can pick a particular hue on the colour wheel and then control the brightness separately with the V.

For instance, to get a pixel to red at 50% brightness, you do:

r, g, b = [int(c * 255.0) for c in colorsys.hsv_to_rgb(1.0, 1.0, 0.5)]

Hope that helps!


#12

Thanks for the help guys. But it’s not working so far.

#!/usr/bin/env python

import time
import random
import colorsys
import unicornhat

unicornhat.brightness(1.0)
unicornhat.set_layout(unicornhat.AUTO)
unicornhat.rotation(0)

l = colorsys.hsv_to_rgb(24,1,1)
l = [int(c * 255.0) for c in colorsys.hsv_to_rgb(24, 1.0, 1.0)]

y1 = random.uniform(0.4,0.75)
y2 = random.uniform(0.125,0.4)
y3 = random.uniform(0.0,0.125)

e1 = colorsys.hsv_to_rgb(24,1,0)
e1 = [int(x*255) for x in e1]
e2 = colorsys.hsv_to_rgb(24,1,y1)
e2 = [int(x*255) for x in e2]
e3 = colorsys.hsv_to_rgb(24,1,y2)
e3 = [int(x*255) for x in e3]
e4 = colorsys.hsv_to_rgb(24,1,y3)
e4 = [int(x*255) for x in e4]


pixels = [  [l, e1, e1, e1, e1, e1, e1, l],
            [l, e1, e1, e1, e1, e1, e1, l],
            [l, e1, e1, e1, e1, e1, e1, l],
            [l, e1, e1, e1, e1, e1, e1, l],
            [l, e1, e1, e1, e1, e1, e1, l],
            [l, e1, e1, e1, e1, e1, e1, l],
            [l, e1, e1, e1, e1, e1, e1, l],
            [l, e1, e1, e1, e1, e1, e1, l]]

unicornhat.set_pixels(pixels)
unicornhat.show()
time.sleep(0.4)
unicornhat.clear

pixels = [  [e2, l, e1, e1, e1, e1, l, e2],
            [e2, l, e1, e1, e1, e1, l, e2],
            [e2, l, e1, e1, e1, e1, l, e2],
            [e2, l, e1, e1, e1, e1, l, e2],
            [e2, l, e1, e1, e1, e1, l, e2],
            [e2, l, e1, e1, e1, e1, l, e2],
            [e2, l, e1, e1, e1, e1, l, e2],
            [e2, l, e1, e1, e1, e1, l, e2]]

unicornhat.set_pixels(pixels)
unicornhat.show()
time.sleep(0.4)
unicornhat.clear

pixels = [  [e3, e2, l, e1, e1, l, e2, e3],
            [e3, e2, l, e1, e1, l, e2, e3],
            [e3, e2, l, e1, e1, l, e2, e3],
            [e3, e2, l, e1, e1, l, e2, e3],
            [e3, e2, l, e1, e1, l, e2, e3],
            [e3, e2, l, e1, e1, l, e2, e3],
            [e3, e2, l, e1, e1, l, e2, e3],
            [e3, e2, l, e1, e1, l, e2, e3]]

unicornhat.set_pixels(pixels)
unicornhat.show()
time.sleep(0.4)
unicornhat.clear

pixels = [  [e4, e3, e2, l, l, e2, e3, e4],
            [e4, e3, e2, l, l, e2, e3, e4],
            [e4, e3, e2, l, l, e2, e3, e4],
            [e4, e3, e2, l, l, e2, e3, e4],
            [e4, e3, e2, l, l, e2, e3, e4],
            [e4, e3, e2, l, l, e2, e3, e4],
            [e4, e3, e2, l, l, e2, e3, e4],
            [e4, e3, e2, l, l, e2, e3, e4]]

unicornhat.set_pixels(pixels)
unicornhat.show()
time.sleep(0.4)
unicornhat.clear

pixels = [  [e1, e4, e3, e2, e2, e3, e4, e1],
            [e1, e4, e3, e2, e2, e3, e4, e1],
            [e1, e4, e3, e2, e2, e3, e4, e1],
            [e1, e4, e3, e2, e2, e3, e4, e1],
            [e1, e4, e3, e2, e2, e3, e4, e1],
            [e1, e4, e3, e2, e2, e3, e4, e1],
            [e1, e4, e3, e2, e2, e3, e4, e1],
            [e1, e4, e3, e2, e2, e3, e4, e1]]

unicornhat.set_pixels(pixels)
unicornhat.show()
time.sleep(0.4)
unicornhat.clear

pixels = [  [e1, e1, e4, e3, e3, e4, e1, e1],
            [e1, e1, e4, e3, e3, e4, e1, e1],
            [e1, e1, e4, e3, e3, e4, e1, e1],
            [e1, e1, e4, e3, e3, e4, e1, e1],
            [e1, e1, e4, e3, e3, e4, e1, e1],
            [e1, e1, e4, e3, e3, e4, e1, e1],
            [e1, e1, e4, e3, e3, e4, e1, e1],
            [e1, e1, e4, e3, e3, e4, e1, e1]]

unicornhat.set_pixels(pixels)
unicornhat.show()
time.sleep(0.4)
unicornhat.clear

pixels = [  [e1, e1, e1, e4, e4, e1, e1, e1],
            [e1, e1, e1, e4, e4, e1, e1, e1],
            [e1, e1, e1, e4, e4, e1, e1, e1],
            [e1, e1, e1, e4, e4, e1, e1, e1],
            [e1, e1, e1, e4, e4, e1, e1, e1],
            [e1, e1, e1, e4, e4, e1, e1, e1],
            [e1, e1, e1, e4, e4, e1, e1, e1],
            [e1, e1, e1, e4, e4, e1, e1, e1]]

unicornhat.set_pixels(pixels)
unicornhat.show()
time.sleep(0.4)
unicornhat.clear

The color is always red and e2, e3, e4, e5 and e6 don’t get their random V. And it’s not smooth.


#13

any ideas guys? I can’t find a enough tutorials for the unicornhat.

The above code is still not optimal and there are a couple of things I still can’t manage to find out:

  • light up all LED’s up in the same color
  • light up a row of LED’s
  • light up a column of LED’s
  • get a breathing effect on the LED’s

#14

have you looked at the examples in the repo… your first 3 items are covered in simple.py.

Breathing effect it depends what you want and there is undoutedly more ways to do it than one, but you’ll basically want to update the brightness the way @sandyjmacdonald indicated.


#15

all leds

for x in range(8):
    for y in range(8):
        uh.set_pixel(x, y, 0, 255, 255)
uh.show()

Single row

for x in range(8):
    uh.set_pixel(x,0,255,0,0)
uh.show()

Single column

for y in range(8):
    uh.set_pixel(0,y,255,0,0)
uh.show()

#16

I assume by breathing you mean a pulsing effect.

There are probably more efficient ways to do it but this way it is easy to understand the code

from unicornhat import *
import time
off()
def set_all(r,g,b):
    for i in range(8):
        for y in range(8):
            set_pixel(i,y,r,g,b)
    show()

while True:
    for r in range(255):
    
        set_all(r,0,0)
        time.sleep(0.001)
    for r in range(255,0,-1):
    
        set_all(r,0,0)
        time.sleep(0.001)

    


#17

thanks guys. Your posts will let me understand python a little bit more. Thanks a lot!!!


#18

note that unicornhat.set_all(r, g, b) already exist in the library, so for that application you wouldn’t have to write your own custom method (unless you want it to do something differently).


#19

where can I find all existend library files?

pi2003 is importing [unicorn import *] which is unclear to me. Is there a guide for the library? Thx guys


#20

The unicorn import * is for importing everything so therefore you do not have to right
unicornhat. or whatever at the beginning of every unicorn hat function. As I linked earlier the learning tutorial article is a great place for the library info in a tutorial and http://docs.pimoroni.com/unicornhat/ is where you can find docs for all the functions.

all the library files can be found at https://github.com/pimoroni/unicorn-hat