def __init__(self):
     super().__init__()
     unicorn.set_layout(unicorn.HAT)
     unicorn.rotation(180)
     unicorn.brightness(0.5)
     self.width, self.height = unicorn.get_shape()
Exemple #2
0
def reset_leds():
    global width, height
    unicorn.set_layout(unicorn.AUTO)
    unicorn.rotation(0)
    unicorn.brightness(1.0)
    width, height = unicorn.get_shape()
Exemple #3
0
from random import randint

import unicornhat as unicorn

print("""Drop: Four Orientation

The same as Drop, but showing the virtual bucket in 4 different orientations.

If you're using a Unicorn HAT and only half the screen lights up, 
edit this example and  change 'unicorn.AUTO' to 'unicorn.HAT' below.
""")

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(0.5)
uh_width, uh_height = unicorn.get_shape()

heights = []


def setup():

    global heights
    heights = []
    for b in range(0, (uh_width - 2)):
        heights.append(0)
    unicorn.off()
    for b in range(0, uh_height):
        unicorn.set_pixel(0, b, 255, 255, 255)
    for b in range(0, uh_height):
        unicorn.set_pixel((uh_width - 1), b, 255, 255, 255)
Exemple #4
0
import csv
import json
import pprint
import binascii
import mbe
import unicornhat as uh
import math
import time

version = "0.1"
rev_limit = 4000

uh.set_layout(uh.AUTO)
uh.rotation(0) # tested on pHAT/HAT with rotation 0, 90, 180 & 270
uh.brightness(0.5)
u_width,u_height=uh.get_shape()

if (mbe.test_mode):
	variables_to_follow = [
	    'RT_ENGINESPEED',
		'RT_AIRTEMP1(LIM)',
		'RT_COOLANTTEMP1(LIM)',
		'RT_BATTERYVOLTAGE(LIM)',
		'RT_SOFTCUTTIME',
		'RT_HARDCUTTIME'
	]
else:
	variables_to_follow = [
#    	'RT_THROTTLESITE1',
#    	'RT_BATTERYVOLTAGECOMP',
#    	'RT_IGNITIONADVANCEBANK1',
 def __init__(self, unicorn: unicorn):
     self.phat = unicorn
     self.width, self.height = unicorn.get_shape()
     print('pHAT dimensions %d x %d' % (self.width, self.height))
Exemple #6
0
uh.set_layout(uh.AUTO)
uh.brightness(0.6)

sprites = [{
    "index": 0,
    "name": "one",
    "dx": 0.05,
    "colour": "#abcdef"
}, {
    "index": 1,
    "name": "two",
    "dx": 0.07,
    "colour": "#fedcba"
}]

w, h = uh.get_shape()

# We want to be efficient about calculating distances
# But at a high enough resolution to look smooth
# So precalculate and save as a binary file for future
try:
    # Load up those distances if they exist
    d = pickle.load(open("d.pickle", "rb"))
except (OSError, IOError) as e:
    # Or calculate them
    # 1 d.p. resolution
    wd = [i / 10 for i in range((w + 1) * 10)]
    hd = [j / 10 for j in range((h + 1) * 10)]
    d = {}
    # Could probs use symmetries to speed this up but I only do it once :)
    for X in wd:
Exemple #7
0
        0b00000000,
        0b00111100,
        0b00100010,
        0b00111100,
        0b00101000,
        0b00100100,
        0b00100010,
    ],
]

GEAR_NAMES = [
    'N', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13',
    '14', 'R'
]

WIDTH, HEIGHT = Led_matrix.get_shape()


def draw_gear(gear_index):
    """ Draw the gear on the 8x8 display. """
    bitmap = GEAR_BITS[gear_index]
    for h in range(HEIGHT):
        for w in range(WIDTH):
            if bitmap[h] & (1 << (7 - w)):
                Led_matrix.set_pixel(w, h, 255, 0, 255)
            else:
                Led_matrix.set_pixel(w, h, 0, 0, 0)


# The rev (RPM %) indicator starts at the bottom left corner (row=7,col=0)and
# ends at the top right corner (row=0,col=7). The color shifts from green
Exemple #8
0
import unicornhat as hat
xRange, yRange = hat.get_shape()
#xRange, yRange = 8,8
hat.set_layout(
    hat.AUTO)  # rotate based on the orientation of the hat 90-180-270
hat.brightness(0.25)

import sys, termios, tty
import random
import time

red = [255, 0, 0]  # define some colours
green = [0, 255, 0]
blue = [0, 0, 255]
off = [0, 0, 0]


class Snake:
    def __init__(self, x, y):
        self.pos = [[x, y]]

    def show(self):
        for y in reversed(range(yRange)):
            for x in range(xRange):
                coord = [x + 1, y + 1]
                if coord in self.pos:
                    if coord == self.pos[0]:
                        hat.set_pixel(x, y, *blue)
                    else:
                        hat.set_pixel(x, y, *red)
                elif coord == food:
def init():
    global width, height
    unicorn.set_layout(unicorn.AUTO)
    unicorn.rotation(0)
    unicorn.brightness(0.5)
    width, height=unicorn.get_shape()
Exemple #10
0
 def __init__(self):
     uh.set_layout(uh.AUTO)
     uh.brightness(0.5)
     self.width, self.height = uh.get_shape()
def show_colour_on_unicorn(RGB):
    width,height = unicorn.get_shape()
    for y in range(height):
        for x in range(width):
            unicorn.set_pixel(x,y,RGB[0], RGB[1], RGB[2])
    unicorn.show()
Exemple #12
0
def handle_command(cmd):
    global width, height

    if cmd is not None:
        cmd = cmd.strip()

        if cmd.startswith("all") and ":" in cmd:
            cmd, data = cmd.split(":")
            r, g, b = [int(x) for x in data.split(",")]

            for x in range(width):
                for y in range(height):
                    unicornhat.set_pixel(x, y, r, g, b)

            unicornhat.show()
            return

        # Expects a message formatted like: pixel.x.y:r,g,b
        if cmd.startswith("pixel.") and ":" in cmd:
            cmd, data = cmd.split(":")
            cmd = cmd.split(".")

            if len(cmd) == 3:
                cmd, x, y = cmd
            else:
                cmd, x = cmd
                y = 0

            pixels = [int(p) for p in data.split(",")]
            x, y = int(x), int(y)

            while len(pixels) > 0:
                unicornhat.set_pixel(x, y, pixels.pop(0), pixels.pop(0),
                                     pixels.pop(0))
                x += 1
                if x > 7:
                    x = 0
                    y += 1
                if y > 7:
                    y = 0

            unicornhat.show()
            return

        if cmd == "hat":
            unicornhat.set_layout(unicornhat.HAT)
            width, height = unicornhat.get_shape()
            return

        if cmd == "phat":
            unicornhat.set_layout(unicornhat.PHAT)
            width, height = unicornhat.get_shape()
            return

        if cmd == "clear":
            unicornhat.clear()
            unicornhat.show()

        if cmd == "stop":
            stdin.stop()
            running = False
Exemple #13
0
import time
import json
import requests
import time
import unicornhat as unicorn
import urllib3
import socket
import commands

urllib3.disable_warnings()

unicorn.set_layout(unicorn.PHAT)
unicorn.rotation(180)
unicorn.brightness(0.4)
height,width=unicorn.get_shape()


#APIC Log In

apic = '172.26.18.9'
username = '******'
password = '******'


#Test to show if display is working.
def show_icon(timedel):	
	unicorn.set_pixel(0,0,0,0,255)
	unicorn.set_pixel(0,3,0,0,255)
	unicorn.set_pixel(7,0,0,0,255)
	unicorn.set_pixel(7,3,0,0,255)
Exemple #14
0
def run(params):
    width, height = unicorn.get_shape()

    size = width * height

    class GameOfLife:
        def __init__(self):
            self.board = [int(7 * random.getrandbits(1)) for _ in xrange(size)]
            self.color = [[154, 154, 174], [0, 0, 255], [0, 0, 200],
                          [0, 0, 160], [0, 0, 140], [0, 0, 90], [0, 0, 60],
                          [
                              0,
                              0,
                              0,
                          ]]

        def value(self, x, y):
            index = ((x % width) * height) + (y % height)
            return self.board[index]

        def neighbors(self, x, y):
            sum = 0
            for i in xrange(3):
                for j in xrange(3):
                    if i == 1 and j == 1:
                        continue
                    if self.value(x + i - 1, y + j - 1) == 0:
                        sum = sum + 1
            return sum

        def next_generation(self):
            new_board = [False] * size
            for i in xrange(width):
                for j in xrange(height):
                    neigh = self.neighbors(i, j)
                    lvl = self.value(i, j)
                    if lvl == 0:
                        if neigh < 2:
                            new_board[i * height + j] = min(7, lvl + 1)
                        elif 2 <= neigh <= 3:
                            new_board[i * height + j] = 0
                        else:
                            new_board[i * height + j] = min(7, lvl + 1)
                    else:
                        if neigh == 3:
                            new_board[i * height + j] = 0
                        else:
                            new_board[i * height + j] = min(7, lvl + 1)
            self.board = new_board

        def all_dead(self):
            for i in xrange(size):
                if self.board[i] != 7:
                    return False
            return True

        def show_board(self):
            for i in xrange(width):
                for j in xrange(height):
                    rgb = self.color[self.value(i, j)]
                    unicorn.set_pixel(i, j, rgb[0], rgb[1], rgb[2])
            unicorn.show()

    life = GameOfLife()
    while not life.all_dead():
        life.next_generation()
        life.show_board()
        time.sleep(0.05)
Exemple #15
0
def run(params):
    width,height=unicorn.get_shape()


    rows = []
    global row_pointer
    row_pointer = 0


    def init():

        # create a buffer of <height> blank rows
        for i in range(height):
            rows.append(get_blank_row())


    def get_blank_row():

        # generate a blank row
        return [0] * width


    def get_new_row():

        # get a new blank row and add a random brightness snowflake to a random column
        row = get_blank_row()
        row[randint(0, width - 1)] = 50 + randint(0, 155)
        return row


    def update_display():

        # keep track of the row we are updating
        c = row_pointer
        for h in range(height):
            for w in range(width):
                # val is between 50 and 255
                val = rows[c][w]

                # invert coordinates
                unicorn.set_pixel((width - 1) - w, (height - 1) - h, val, val, val)
            c += 1
            if c > height - 1:
                c = 0
        unicorn.show()


    def step():
        global row_pointer

        # add new row at current row pointer
        # leave other rows the same, display will start from this one which overwrites the
        # oldest existing row from the last time we updated the display
        rows[row_pointer] = get_new_row()
        update_display()

        # determine next row pointer, wrapping around if we went past zero
        row_pointer -= 1
        if row_pointer < 0:
            row_pointer = height - 1


    init()
    while True:
        step()
        sleep(0.3)
Exemple #16
0
 def init(self):
     unicorn.set_layout(unicorn.AUTO)
     self.set_rotate()
     self.set_brightness()
     self.cols, self.rows = unicorn.get_shape()
Exemple #17
0
# -*- coding: utf-8 -*-

# https://github.com/vaab/colour
from colour import Color

# for unicorn hat
import unicornhat as unicorn

import time

COLOFFSET = 0

unicorn.set_layout(unicorn.PHAT)
unicorn.rotation(0)
unicorn.brightness(0.3)
hatWidth, hatHeight = unicorn.get_shape()

cc1 = Color(rgb=(1, 0, 0))
cc2 = Color(rgb=(0.95, 0, 0.05))
locPal1 = list(cc1.range_to(cc2, hatWidth * hatHeight))

print locPal1

ii = 0

for yy in range(hatHeight):
    for xx in range(hatWidth):
        locCols = locPal1[ii].rgb
        ii = ii + 1
        r = locCols[0] * 255
        g = locCols[1] * 255
Exemple #18
0
import time

import unicornhat as unicorn

print("""Demo

This pixel shading demo transitions between 4 classic graphics demo effects.

If you're using a Unicorn HAT and only half the screen lights up, 
edit this example and  change 'unicorn.AUTO' to 'unicorn.HAT' below.
""")

unicorn.set_layout(unicorn.HAT)
unicorn.rotation(0)  # tested on pHAT/HAT with rotation 0, 90, 180 & 270
unicorn.brightness(0.5)
u_width, u_height = unicorn.get_shape()


# twisty swirly goodness
def swirl(x, y, step):
    x -= (u_width / 2)
    y -= (u_height / 2)

    dist = math.sqrt(pow(x, 2) + pow(y, 2)) / 2.0
    angle = (step / 10.0) + (dist * 1.5)
    s = math.sin(angle)
    c = math.cos(angle)

    xs = x * c - y * s
    ys = x * s + y * c
import sys
import time
import numpy
import signal
import datetime
import pixelarized
import unicornhat as unicorn

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(180)
unicorn.brightness(0.2)
numpy.random.rand(*unicorn.get_shape())
unicorn.show()


def draw_pixels(element, offset_x, offset_y, color):
    y = 0
    for element_slice in element:
        for x in [x for x, char in enumerate(element_slice) if char == "█"]:
            unicorn.set_pixel(x + offset_x, y + offset_y, *color)
        y += 1


def killing_me_softly(signal, frame):
    unicorn.off()
    sys.exit(0)


signal.signal(signal.SIGTERM, killing_me_softly)
while True:
    hour = datetime.datetime.now().hour
Exemple #20
0
def paint(r, g, b, t):
    unicorn.set_layout(unicorn.HAT)
    unicorn.rotation(0)
    unicorn.brightness(0.5)
    width, height = unicorn.get_shape()
    frameTime = .1  # the number of seconds for each frame
    iterations = int(round(t / frameTime, 0))
    unicorn.off()  # turn off all of the led's

    # vars used for the 2nd inner most circle
    c2LeadingX = 2
    c2LeadingY = 4
    c2TrailingX = 5
    c2TrailingY = 4

    # vars used for the 3rd inner most circle
    c3LeadingX = 2
    c3LeadingY = 1
    c3TrailingX = 2
    c3TrailingY = 6

    # vars used for the 4th inner most circle
    c4LeadingX = 6
    c4LeadingY = 7
    c4TrailingX = 6
    c4TrailingY = 0

    for i in range(iterations):

        # handle the inner circle (1 of 4), counter-clockwise
        if i % 4 == 0:
            unicorn.set_pixel(4, 4, r, g, b)  # turn one led on
            unicorn.set_pixel(3, 4, 0, 0, 0)  # turn one led off
        elif i % 4 == 1:
            unicorn.set_pixel(4, 3, r, g, b)  # turn one led on
            unicorn.set_pixel(4, 4, 0, 0, 0)  # turn one led off
        elif i % 4 == 2:
            unicorn.set_pixel(3, 3, r, g, b)  # turn one led on
            unicorn.set_pixel(4, 3, 0, 0, 0)  # turn one led off
        else:
            unicorn.set_pixel(3, 4, r, g, b)  # turn one led on
            unicorn.set_pixel(3, 3, 0, 0, 0)  # turn one led off

        # handle the 2nd inner most circle (2 of 4), counter-clockwise
        if i == 0:
            unicorn.set_pixel(5, 4, r, g, b)  # turn eight led's on to start
            unicorn.set_pixel(5, 3, r, g, b)
            unicorn.set_pixel(5, 2, r, g, b)
            unicorn.set_pixel(4, 2, r, g, b)
            unicorn.set_pixel(3, 2, r, g, b)
            unicorn.set_pixel(2, 2, r, g, b)
            unicorn.set_pixel(2, 3, r, g, b)
            unicorn.set_pixel(2, 4, r, g, b)
        else:
            unicorn.set_pixel(c2TrailingX, c2TrailingY, 0, 0,
                              0)  # turn off the trailing led
            if 5 <= (c2LeadingX + c2LeadingY
                     ) <= 10 and c2LeadingX >= c2LeadingY:  # going down
                if c2LeadingY == 2:
                    c2LeadingX -= 1  # iterate the leading X value
                else:
                    c2LeadingY -= 1  # iterate the leading Y value
            else:  # going up
                if c2LeadingY == 5:
                    c2LeadingX += 1  # iterate the leading X value
                else:
                    c2LeadingY += 1  # iterate the leading Y value

            unicorn.set_pixel(c2LeadingX, c2LeadingY, r, g,
                              b)  # turn on the new leading led
            if 5 <= (c2TrailingX + c2TrailingY
                     ) <= 10 and c2TrailingX >= c2TrailingY:  # going down
                if c2TrailingY == 2:
                    c2TrailingX -= 1  # iterate the trailing X value
                else:
                    c2TrailingY -= 1  # iterate the trailing Y value
            else:  # going up
                if c2TrailingY == 5:
                    c2TrailingX += 1  # iterate the trailing X value
                else:
                    c2TrailingY += 1  # iterate the trailing Y value

        # handle the 3rd inner most circle (3 of 4), counter-clockwise
        if i == 0:
            unicorn.set_pixel(2, 1, r, g, b)  # turn fourteen led's on to start
            unicorn.set_pixel(3, 1, r, g, b)
            unicorn.set_pixel(4, 1, r, g, b)
            unicorn.set_pixel(5, 1, r, g, b)
            unicorn.set_pixel(6, 1, r, g, b)
            unicorn.set_pixel(6, 2, r, g, b)
            unicorn.set_pixel(6, 3, r, g, b)
            unicorn.set_pixel(6, 4, r, g, b)
            unicorn.set_pixel(6, 5, r, g, b)
            unicorn.set_pixel(6, 6, r, g, b)
            unicorn.set_pixel(5, 6, r, g, b)
            unicorn.set_pixel(4, 6, r, g, b)
            unicorn.set_pixel(3, 6, r, g, b)
            unicorn.set_pixel(2, 6, r, g, b)
        else:
            unicorn.set_pixel(c3TrailingX, c3TrailingY, 0, 0,
                              0)  # turn off the trailing led
            if 3 <= (c3LeadingX + c3LeadingY
                     ) <= 12 and c3LeadingX >= c3LeadingY:  # going down
                if c3LeadingY == 1:
                    c3LeadingX -= 1  # iterate the leading X value
                else:
                    c3LeadingY -= 1  # iterate the leading Y value
            else:  # going up
                if c3LeadingY == 6:
                    c3LeadingX += 1  # iterate the leading X value
                else:
                    c3LeadingY += 1  # iterate the leading Y value

            unicorn.set_pixel(c3LeadingX, c3LeadingY, r, g,
                              b)  # turn on the new leading led
            if 3 <= (c3TrailingX + c3TrailingY
                     ) <= 13 and c3TrailingX >= c3TrailingY:  # going down
                if c3TrailingY == 1:
                    c3TrailingX -= 1  # iterate the trailing X value
                else:
                    c3TrailingY -= 1  # iterate the trailing Y value
            else:  # going up
                if c3TrailingY == 6:
                    c3TrailingX += 1  # iterate the trailing X value
                else:
                    c3TrailingY += 1  # iterate the trailing Y value

        # handle the 4th inner most circle (4 of 4), counter-clockwise
        if i == 0:
            unicorn.set_pixel(6, 0, r, g, b)  # turn twenty led's on to start
            unicorn.set_pixel(5, 0, r, g, b)
            unicorn.set_pixel(4, 0, r, g, b)
            unicorn.set_pixel(3, 0, r, g, b)
            unicorn.set_pixel(2, 0, r, g, b)
            unicorn.set_pixel(1, 0, r, g, b)
            unicorn.set_pixel(0, 0, r, g, b)
            unicorn.set_pixel(0, 1, r, g, b)
            unicorn.set_pixel(0, 2, r, g, b)
            unicorn.set_pixel(0, 3, r, g, b)
            unicorn.set_pixel(0, 4, r, g, b)
            unicorn.set_pixel(0, 5, r, g, b)
            unicorn.set_pixel(0, 6, r, g, b)
            unicorn.set_pixel(0, 7, r, g, b)
            unicorn.set_pixel(1, 7, r, g, b)
            unicorn.set_pixel(2, 7, r, g, b)
            unicorn.set_pixel(3, 7, r, g, b)
            unicorn.set_pixel(4, 7, r, g, b)
            unicorn.set_pixel(5, 7, r, g, b)
            unicorn.set_pixel(6, 7, r, g, b)
        else:
            unicorn.set_pixel(c4TrailingX, c4TrailingY, 0, 0,
                              0)  # turn off the trailing led
            if 1 <= (c4LeadingX + c4LeadingY
                     ) <= 14 and c4LeadingX >= c4LeadingY:  # going down
                if c4LeadingY == 0:
                    c4LeadingX -= 1  # iterate the leading X value
                else:
                    c4LeadingY -= 1  # iterate the leading Y value
            else:  # going up
                if c4LeadingY == 7:
                    c4LeadingX += 1  # iterate the leading X value
                else:
                    c4LeadingY += 1  # iterate the leading Y value

            unicorn.set_pixel(c4LeadingX, c4LeadingY, r, g,
                              b)  # turn on the new leading led
            if 1 <= (c4TrailingX + c4TrailingY
                     ) <= 14 and c4TrailingX >= c4TrailingY:  # going down
                if c4TrailingY == 0:
                    c4TrailingX -= 1  # iterate the trailing X value
                else:
                    c4TrailingY -= 1  # iterate the trailing Y value
            else:  # going up
                if c4TrailingY == 7:
                    c4TrailingX += 1  # iterate the trailing X value
                else:
                    c4TrailingY += 1  # iterate the trailing Y value

        unicorn.show()
        time.sleep(frameTime)
Exemple #21
0
""" Displays a knight rider animation on the hat"""

# -- setup

import time
import unicornhat as unicorn

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(180)
unicorn.brightness(0.5)
WIDTH, HEIGHT = unicorn.get_shape()

# -- globals

# define a list of reds to use to light up the pixels
# this can be tinkered with to produce something that looks better
color_range = [(110, 0, 0), (150, 0, 0), (220, 0, 0), (255, 0, 0), (255, 0, 0),
               (220, 0, 0), (150, 0, 0), (110, 0, 0)]

# -- functions


def light_up_row(palette, start_value):
    """ lights up all the rows on the hat:
    
        loops over the columns on the hat, if this is in the
        allowed range (0 -> 7) light up those pixels using the palette
        provided.
        
        The inner loop just applies the coloring to all rows of the hat
    """
Exemple #22
0
    # End for hue

    print('')

    return  # End def d2xy_walk


#*****************************************************************************80
#
## The MAIN program starts HERE

# If being run as the primary program (not a module)
if (__name__ == '__main__'):

    # Set up Unicorn HAT

    uh.set_layout(uh.HAT)  # Set layout to 8x8 Unicorn HAT - Default anyway?
    u_width, u_height = uh.get_shape()  # Get the shape

    uh.rotation(0)  # Set rotation
    uh.brightness(0.5)  # Set the brightness
    uh.off  # Clear the board

    timestamp()  # Timestamp the start

    # Run the Walk?
    d2xy_walk()

    timestamp()  # Timestamp the end

## End of the File
import time
import unicornhat

API_PATH_PREFIX = 'api'
NUM_HASH_FUNCTIONS = 3
COLOR_BIT_SET = (255, 0, 0)
COLOR_BIT_WRITING = (0, 255, 0)
COLOR_BIT_QUERYING = (0, 0, 255)
NUM_TRANSITIONS = 4

app = Flask(__name__)

unicornhat.set_layout(unicornhat.AUTO)
unicornhat.rotation(180)
unicornhat.brightness(0.19)
unicorn_width, unicorn_height = unicornhat.get_shape()
unicornhat.off()

NUM_LEDS = unicorn_width * unicorn_height


def get_led_position(led):
    unicorn_width, unicorn_height = unicornhat.get_shape()
    return (led % unicorn_height, led // unicorn_width)


def toggle_leds(leds, transition_color, new_color):
    orig_colors = []

    for led in leds:
        orig_colors.append(unicornhat.get_pixel(led[0], led[1]))
Exemple #24
0
#!/usr/bin/env python

import unicornhat as unicorn
import time
from random import randint

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(0.4)
uh_width,uh_height=unicorn.get_shape()

heights = []

def setup():

    global heights
    heights = []
    for b in range(0, (uh_width-2)):
        heights.append(0)
    unicorn.off()
    for b in range(0, uh_height):
        unicorn.set_pixel(0, b, 255, 255, 255)
    for b in range(0, uh_height):
        unicorn.set_pixel((uh_width-1), b, 255, 255, 255)
    for b in range(1, (uh_width-1)):
        unicorn.set_pixel(b, 0, 255, 255, 255)
    unicorn.show()


def drop_ball():
def get_led_position(led):
    unicorn_width, unicorn_height = unicornhat.get_shape()
    return (led % unicorn_height, led // unicorn_width)
Exemple #26
0
def run(params):
    u_width,u_height=unicorn.get_shape()


    # twisty swirly goodness
    def swirl(x, y, step):
        x -= (u_width/2)
        y -= (u_height/2)

        dist = math.sqrt(pow(x, 2)+pow(y,2)) / 2.0
        angle = (step / 10.0) + (dist * 1.5)
        s = math.sin(angle);
        c = math.cos(angle);

        xs = x * c - y * s;
        ys = x * s + y * c;

        r = abs(xs + ys)
        r = r * 64.0
        r -= 20

        return (r, r + (s * 130), r + (c * 130))

    # roto-zooming checker board
    def checker(x, y, step):
        x -= (u_width/2)
        y -= (u_height/2)

        angle = (step / 10.0)
        s = math.sin(angle);
        c = math.cos(angle);

        xs = x * c - y * s;
        ys = x * s + y * c;

        xs -= math.sin(step / 200.0) * 40.0
        ys -= math.cos(step / 200.0) * 40.0

        scale = step % 20
        scale /= 20
        scale = (math.sin(step / 50.0) / 8.0) + 0.25;

        xs *= scale
        ys *= scale

        xo = abs(xs) - int(abs(xs))
        yo = abs(ys) - int(abs(ys))
        l = 0 if (math.floor(xs) + math.floor(ys)) % 2 else 1 if xo > .1 and yo > .1 else .5

        r, g, b = colorsys.hsv_to_rgb((step % 255) / 255.0, 1, l)

        return (r * 255, g * 255, b * 255)

    # weeee waaaah
    def blues_and_twos(x, y, step):
        x -= (u_width/2)
        y -= (u_height/2)

        xs = (math.sin((x + step) / 10.0) / 2.0) + 1.0
        ys = (math.cos((y + step) / 10.0) / 2.0) + 1.0

        scale = math.sin(step / 6.0) / 1.5
        r = math.sin((x * scale) / 1.0) + math.cos((y * scale) / 1.0)
        b = math.sin(x * scale / 2.0) + math.cos(y * scale / 2.0)
        g = r - .8
        g = 0 if g < 0 else g

        b -= r
        b /= 1.4

        return (r * 255, (b + g) * 255, g * 255)

    # rainbow search spotlights
    def rainbow_search(x, y, step):
        xs = math.sin((step) / 100.0) * 20.0
        ys = math.cos((step) / 100.0) * 20.0

        scale = ((math.sin(step / 60.0) + 1.0) / 5.0) + 0.2
        r = math.sin((x + xs) * scale) + math.cos((y + xs) * scale)
        g = math.sin((x + xs) * scale) + math.cos((y + ys) * scale)
        b = math.sin((x + ys) * scale) + math.cos((y + ys) * scale)

        return (r * 255, g * 255, b * 255)

    # zoom tunnel
    def tunnel(x, y, step):

        speed = step / 100.0
        x -= (u_width/2)
        y -= (u_height/2)

        xo = math.sin(step / 27.0) * 2
        yo = math.cos(step / 18.0) * 2

        x += xo
        y += yo

        if y == 0:
            if x < 0:
                angle = -(math.pi / 2)
            else:
                angle = (math.pi / 2)
        else:
            angle = math.atan(x / y)

        if y > 0:
            angle += math.pi

        angle /= 2 * math.pi # convert angle to 0...1 range

        shade = math.sqrt(math.pow(x, 2) + math.pow(y, 2)) / 2.1
        shade = 1 if shade > 1 else shade


        angle += speed
        depth = speed + (math.sqrt(math.pow(x, 2) + math.pow(y, 2)) / 10)

        col1 = colorsys.hsv_to_rgb((step % 255) / 255.0, 1, .8)
        col2 = colorsys.hsv_to_rgb((step % 255) / 255.0, 1, .3)


        col = col1 if int(abs(angle * 6.0)) % 2 == 0 else col2

        td = .3 if int(abs(depth * 3.0)) % 2 == 0 else 0

        col = (col[0] + td, col[1] + td, col[2] + td)

        col = (col[0] * shade, col[1] * shade, col[2] * shade)

        return (col[0] * 255, col[1] * 255, col[2] * 255)

    effects = [tunnel, rainbow_search, checker, swirl]

    step = 0
    while True:
        for i in range(500):
            for y in range(u_height):
                for x in range(u_width):
                    r, g, b = effects[0](x, y, step)
                    if i > 400:
                        r2, g2, b2 = effects[-1](x, y, step)

                        ratio = (500.00 - i) / 100.0
                        r = r * ratio + r2 * (1.0 - ratio)
                        g = g * ratio + g2 * (1.0 - ratio)
                        b = b * ratio + b2 * (1.0 - ratio)
                    r = int(max(0, min(255, r)))
                    g = int(max(0, min(255, g)))
                    b = int(max(0, min(255, b)))
                    unicorn.set_pixel(x, y, r, g, b)

            step += 1

            unicorn.show()

            time.sleep(0.01)

        effect = effects.pop()
        effects.insert(0, effect)
Exemple #27
0
import unicornhat as hat
from time import sleep

hat.rotation(90)
hat.brightness(0.4)        
width,height = hat.get_shape() # Universal for hat and hatHD 8x8 or 16x16 grids


red     = (255, 0, 0)

def display(duration):
        hat.show()
        sleep(duration)

pixel = 1
xLower = 0
xUpper = width - 1
yLower = 0
yUpper = height - 1


while xUpper > xLower:
    for y in range(yLower,yUpper + 1):
        hat.set_pixel_hsv(xLower, y, (1 / (width * height) * pixel), 1, 1)
        display(0.2)
        pixel += 1
    xLower += 1
    for x in range(xLower,xUpper + 1):
        hat.set_pixel_hsv(x, yUpper,  (1 / (width * height) * pixel), 1, 1)
        display(0.2)
        pixel += 1
Exemple #28
0
import logging
import unicornhat as uh
import time

logger = logging.getLogger()
logger.setLevel(logging.INFO)
uh.set_layout(uh.PHAT)
width, height = uh.get_shape()


def critical(blink, bright):
    logger.info('Red alert for ' + str(blink) + ' s with brightness ' +
                str(bright))
    light(blink, bright, 255, 0, 0)
    return True


def warning(blink, bright):
    logger.info('Orange alert for ' + str(blink) + ' s with brightness ' +
                str(bright))
    light(blink, bright, 255, 129, 0)
    return True


def info(blink, bright):
    logger.info('Green alert for ' + str(blink) + ' s with brightness ' +
                str(bright))
    light(blink, bright, 55, 100, 0)
    return True

Exemple #29
0
#!/usr/bin/env python

import time

import unicornhat as unicorn

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(0.3)
width, height = unicorn.get_shape()

print(unicorn.get_shape(), unicorn.AUTO)

for y in range(height):
    for x in range(width):
        unicorn.set_pixel(x, y, 255, 0, 255)
        unicorn.show()
        print(x, y)
        time.sleep(0.1)
#unicorn.set_pixel(6,0,255,0,255)
#unicorn.show()
#time.sleep(0.5)
unicorn.off()
time.sleep(1)
#!/usr/bin/python
import time

import unicornhat as unicorn
unicorn.set_layout(unicorn.HAT)
unicorn.rotation(0)
unicorn.brightness(0.5)
width,height=unicorn.get_shape()


r = 255
g = 0
b = 0

msleep = lambda x: time.sleep(x / 1000.0)


def next_colour():
    global r
    global g
    global b

    if (r == 255 and g < 255 and b == 0):
        g += 1

    if (g == 255 and r > 0 and b == 0):
        r -= 1

    if (g == 255 and b < 255 and r == 0):
        b += 1
Exemple #31
0
#!/usr/bin/env python

import sys, termios, tty    # for getch - single character input
import re                   # regex parser
import unicornhat as hat

hat.set_layout(hat.AUTO)
hat.rotation(90)            # rotate based on the orientation of the hat 90-180-270
hat.brightness(0.25)        # setting this too high can result in discomfort
col,row = hat.get_shape()   # set the grid size

button_delay = 0.2          # allows hat the time the LEDs need to light

red     = [255,0,0]         # define some colours
green   = [0,255,0]
blue    = [0,0,255]
off     = [0,0,0]

f = open('8x8ascii', 'r+')  # opens 8x8ascii and breaks it into a numbered list
ascii = [line for line in f.readlines()]
f.close()

def getch():    # ref: http://code.activestate.com/recipes/134892/
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch
    exit("This script requires the numpy module\nInstall with: sudo pip install numpy")

import unicornhat as unicorn

print("""Random Blinky

Blinks random yellow-orange-red LEDs.

If you're using a Unicorn HAT and only half the screen lights up, 
edit this example and  change 'unicorn.AUTO' to 'unicorn.HAT' below.
""")

unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(0.4)
width,height=unicorn.get_shape()


while True:
    rand_mat = np.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()
# Background colour
back='black'

# Directory containing message scripts
messages_dir = 'messages'

# Directory containing images
images_dir = 'images'

# Detect HAT or PHAT layout and adapt accordingly
# If yours isn't detected properly, then you may need
# to change the following line to say HAT or PHAT
# instead of AUTO:
unicorn.set_layout(unicorn.AUTO)
geom = unicorn.get_shape()
if (geom[0] != geom[1]):
  unicorn.set_layout([
    [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ],
    [8 , 9 , 10, 11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20, 21, 22, 23],
    [24, 25, 26, 27, 28, 29, 30, 31]
  ])
  step *= 2


def set_brightness(pc):
    b = brightness_min + ((brightness_max - brightness_min) * pc / 100.0)
    if (b > brightness_max): b = brightness_max
    if (b < brightness_min): b = brightness_min
    unicorn.brightness(b)