def unicornSetup(): unicorn.off() unicorn.clear() unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(0.5) width, height = unicorn.get_shape()
def blue(): import unicornhat as unicorn import time unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(0.5) width,height=unicorn.get_shape()
def weather(): uh.set_layout(uh.PHAT) uh.brightness(0.7) owm = pyowm.OWM(owmapikey.strApiKey) w = owm.weather_at_id(5134693) wd = w.get_weather() tempF = wd.get_temperature('fahrenheit')['temp'] print tempF if tempF < 60.0: a = 0 b = 0 c = 255 elif tempF < 70.0: a = 0 b = 255 c = 0 #elif tempF < 80.0: else: a = 255 b = 0 c = 0 for x in range(8): for y in range(4): uh.set_pixel(x, y, a, b, c) uh.show() time.sleep(5)
def initunicornhat(): """ This function initialises the unicornhat hat. """ unicornhat.rotation(0) unicornhat.brightness(0.5) unicornhat.set_layout(unicornhat.AUTO) width, height = unicornhat.get_shape() return [width, height]
def rainbow(endClockTime): unicorn.set_layout(unicorn.PHAT) unicorn.rotation(0) unicorn.brightness(.5) i = 0.0 offset = 30 while True: dt = datetime.datetime.now() clockTime = dt.strftime('%H%M') clockTime = int(clockTime) i = i + 0.3 for y in range(4): for x in range(8): r = 0#x * 32 g = 0#y * 32 xy = x + y / 4 r = (math.cos((x+i)/2.0) + math.cos((y+i)/2.0)) * 64.0 + 128.0 g = (math.sin((x+i)/1.5) + math.sin((y+i)/2.0)) * 64.0 + 128.0 b = (math.sin((x+i)/2.0) + math.cos((y+i)/1.5)) * 64.0 + 128.0 r = max(0, min(255, r + offset)) g = max(0, min(255, g + offset)) b = max(0, min(255, b + offset)) unicorn.set_pixel(x,y,int(r),int(g),int(b)) unicorn.show() if clockTime == endClockTime: break time.sleep(0.01)
def runPulse(): intensity = 1.0 if args['intensity']: intensity = float(args['intensity']) colour = [255, 0, 255] if args['colour']: colour = list(map(int, args['colour'].split(","))) unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(intensity) program = [[3, 5], [2, 6], [1, 7], [0, 8], [1, 7], [2, 6]] controller = 0 while True: unicorn.clear() topLeft, extent = program[controller] for y in range(topLeft, extent): for x in range(topLeft, extent): pulse(x, y, colour) unicorn.show() time.sleep(0.5) if controller <= 4: controller += 1 else: controller = 0
def runBanner(): intensity = 1.0 if args['intensity']: intensity = float(args['intensity']) background_colour = [255, 0, 255] if args['bcolour']: background_colour = list(map(int, args['bcolour'].split(","))) wave_colour = [220, 20, 60] if args['wcolour']: wave_colour = list(map(int, args['wcolour'].split(","))) unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(intensity) width, height = unicorn.get_shape() g = -1 while True: g += 1 for y in range(height): for x in range(width): if x == g or x == g - 1 or x == g - 2: wave(x, y, wave_colour) else: background(x, y, background_colour) if g > 9: g = -1 unicorn.show() time.sleep(0.8)
def unicorn_init(orientation, lowlight): unicorn.set_layout(unicorn.AUTO) unicorn.rotation(orientation) if lowlight: unicorn.brightness(0.3) else: unicorn.brightness(0.5)
def __init__(self): self.light_on = False self.display_cleared = False self.stop_thread = False unicorn.set_layout(unicorn.PHAT) unicorn.rotation(0) unicorn.brightness(0.5)
def main(): try: unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(0.3) width, height = unicorn.get_shape() letters = Letter.V + Letter.I + Letter.T yellow = [247, 178, 28] white = [255, 255, 255] colors = [yellow, yellow, yellow, white, yellow, yellow, yellow, white] for x in range(width): rgb = colors[x] for y in range(height): if len(letters) > x: row = letters[x] if len(row) > y: if row[y] == 1: unicorn.set_pixel(x, y, rgb[0], rgb[1], rgb[2]) unicorn.show() time.sleep(0.05) except: print('traceback.format_exc():\n%s', traceback.format_exc()) exit()
def init(): uh.set_layout(uh.PHAT) for c in get_ip(): if c == '0': display(c, ZERO) elif c == '1': display(c, ONE) elif c == '2': display(c, TWO) elif c == '3': display(c, THREE) elif c == '4': display(c, FOUR) elif c == '5': display(c, FIVE) elif c == '6': display(c, SIX) elif c == '7': display(c, SEVEN) elif c == '8': display(c, EIGHT) elif c == '9': display(c, NINE) else: display(c, DOT)
def __init__(self, default_r=255, default_g=255, default_b=255): self.brightness = 0 unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(self.brightness) self.width, self.height = unicorn.get_shape() self.default_r, self.default_g, self.default_b = default_r, default_g, default_b self.r, self.g, self.b = self.default_r, self.default_g, self.default_b
def __init__(self, correction=[1., 1., 1.]): BaseLamp.__init__(self, correction=correction) unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) # Get LED matrix dimensions self.width, self.height = unicorn.get_shape()
def __init__(self, oversample): self.oversample = oversample self.size = 8 * self.oversample self.initGrid() # fire up the unicorn hat unicorn.set_layout(unicorn.HAT) unicorn.rotation(90) unicorn.brightness(1) self.width, self.height = unicorn.get_shape()
def __init__(self, rotation=180, brightness=0.3): self.rotation = rotation self.brightness = brightness unicorn.set_layout(unicorn.AUTO) unicorn.rotation(rotation) unicorn.brightness(brightness) self.width, self.height = unicorn.get_shape()
def __init__(self, brightness=0.2, rotation=0): self.brightness = brightness self.rotation = rotation unicorn.set_layout(unicorn.PHAT) self.columns, self.rows = unicorn.get_shape() unicorn.clear() unicorn.rotation(self.rotation) unicorn.brightness(self.brightness) unicorn.show()
def __init__(self): Layer.__init__(self, None) uh.set_layout(uh.PHAT) self.mWidth = 4 self.mHeight = 8 # mValues is a mWidth x mHeight array of tuples formatted # as (r, g, b) values for that pixel. self.mValues = [[(0, 0, 0) for x in range(self.mWidth)] for y in range(self.mHeight)]
def cleanUp(): unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(0.0) width, height = unicorn.get_shape() for y in range(height): for x in range(width): unicorn.set_pixel(x, y, int(0), int(0), int(0)) unicorn.show()
def __init__(self): unicornhat.set_layout(unicornhat.AUTO) unicornhat.rotation(270) unicornhat.clear() unicornhat.brightness(.5) width,height=unicornhat.get_shape() self.ROW_LENGTH = height self._leds = np.zeros((width,height), np.bool) self.prev_sent = 0 self.prev_recv = 0
def paint(r, g, b, t): unicorn.set_layout(unicorn.HAT) unicorn.rotation(0) unicorn.brightness(0.5) width, height = unicorn.get_shape() for x in range(width): for y in range(width): unicorn.set_pixel(x, y, r, g, b) unicorn.show() time.sleep(t)
def clear(): import unicornhat as unicorn import time unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(0.0) width,height=unicorn.get_shape() for y in range(height): for x in range(width): unicorn.set_pixel(x,y,0,0,0) unicorn.show() time.sleep(0.05)
def __init__(self): uh.set_layout(uh.PHAT) uh.brightness(1.0) self.tasked = False self.loop = False self.min_val = 43 # The hat seems to ignore values <= this self.min_bright = 0.25 self.red = self.min_val self.green = self.min_val self.blue = self.min_val self.brightness = 1.0
def main(): print("Start") # set params for unicorn hat unicorn.set_layout(unicorn.PHAT) unicorn.rotation(0) unicorn.brightness(0.3) hatWidth,hatHeight=unicorn.get_shape() for xx in range(10): print("Showing number", xx) unicorn.clear() setUniPxNum(xx,(255,0,0),4) time.sleep(3)
def unicorn_hat_output(R, G, B): ''' A function which takes RGB values and outputs it to the Pi's hat :param R: :param G: :param B: ''' uh.set_layout(uh.HAT) uh.brightness(0.5) for x in range(9): for y in range(9): uh.set_pixel(x, y, R, G, B) uh.show()
def initOnce(self): if self.isInitOnce: return self.isInitOnce = True unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(0.2) width, height = unicorn.get_shape() self.width = width self.height = height signal.signal(signal.SIGINT, self.exit_gracefully) signal.signal( signal.SIGTERM, self.exit_gracefully) #sigterm is sent by docker stop command
def Unic_init(rotation=0, brightness=0.5): global _UnicWidth global _UnicHeight global _UnicPlaneList unicorn.set_layout(unicorn.AUTO) unicorn.rotation(rotation) unicorn.brightness(brightness) _UnicWidth, _UnicHeight = unicorn.get_shape() for plane in range(UNIC_PLANE_MAX): for x in range(_UnicWidth): for y in range(_UnicHeight): _UnicPlaneList.append([0, 0, 0])
def randomLights(): uh.set_layout(uh.PHAT) uh.brightness(0.7) for x in range(8): for y in range(4): a = random.randint(0, 255) b = random.randint(0, 255) c = random.randint(0, 255) uh.set_pixel(x, y, a, b, c) uh.show() time.sleep(5) uh.clear() uh.show()
def __init__(self): self.on = True self.restartStun = False self.restartRelease = False if not settings.NOGPIO: # initialize the unicorn hat settings unicorn.brightness(1) unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) self.u_width, self.u_height = unicorn.get_shape() # starts an idle thread idle_thread = threading.Thread(target=self.idleLED) idle_thread.start()
def color(): hex_color = request.args.get('color') red, green, blue = map(ord, hex_color.decode('hex')) unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(1) width, height = unicorn.get_shape() for y in range(height): for x in range(width): unicorn.set_pixel(x, y, red, green, blue) unicorn.show() time.sleep(0.03) return "Success, set to " + hex_color
def setup(): global settings global logArray #load the settings file settings = Settings('/home/pi/Artificial_Life/') #setup the log array logArray = [] #unicorn hat setup unicorn.set_layout(unicorn.AUTO) unicorn.brightness(0.5) unicorn.rotation(0) unicorn.brightness(0.5) webiopi.info('setup complete')
#!/usr/bin/env python import unicornhat as unicorn import numpy as np import time, math, colorsys, random, serial from random import randint unicorn.set_layout(unicorn.PHAT) def kick_rainbow(): random_r = randint(0,255) random_g = randint(0,255) random_b = randint(0,255) random_math_r = randint(32,640) random_math_g = randint(32,640) random_math_b = randint(32,640) bright = 1 i = 0.0 offset = 30 for q in range(20): #splash a kick for 20 revs i = i + 0.3 for y in range(4): for x in range(8): r = 0#x * 32 g = 0#y * 32 xy = x + y / 4 r = (math.cos((x+i)/2.0) + math.cos((y+i)/2.0)) * 64 + 128.0 g = (math.sin((x+i)/1.5) + math.sin((y+i)/2.0)) * 64 + 128.0 b = (math.sin((x+i)/2.0) + math.cos((y+i)/1.5)) * 64 + 128.0 r = max(0, min(random_r, r + offset))
step = 0.05 # 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
import unicornhat as uh import time import serial import random from random import randint uh.set_layout(uh.PHAT) uh.brightness(0.5) ser = serial.Serial('/dev/ttyAMA0', baudrate=38400) #original #ser = serial.Serial('/dev/ttyAMA0') # bens first one, doesnt seem to work #ser = serial.Serial('/dev/ttyAMA0', baudrate=31250) #sees program change? #ser = serial.Serial('/dev/ttyAMA0', baudrate=38400) # my try on jani comment # THIS IS KIND OF WORKING - i DON'T KNOW WHY 23 IS THE BEAT RATE, BUT IT SEEMS RIGHT. message = [0, 0, 0] clock_count = 0 while True: i = 0 while i < 3: data = ord(ser.read(1)) # read a byte # if data != 240: # i think 240 is the bpm? # print data # me if data == 14 or data == 142: for y in range(4): for x in range(8): uh.set_pixel(x,y,randint(0,255),randint(0,255),randint(0,255)) uh.show() if data == 240: # changed this to 240, trying to get the clock clock_count += 1
[1, 1, 1, 1, 1, 1, 1, 1], [1, 6, 6, 1, 8, 8, 8, 1], [1, 6, 6, 1, 8, 8, 8, 1], [1, 6, 6, 1, 8, 8, 8, 1]] tog(1, 2, 3, h1, 3) tog(1, 4, 7, h2, 5) tog(5, 1, 3, m1, 7) tog(5, 4, 7, m2, 9) for x in range(8): for y in range(8): r, g, b = colors[ disp[y][x] ] unicornhat.set_pixel(x, y, r, g, b) unicornhat.show() unicornhat.set_layout(unicornhat.AUTO) unicornhat.rotation(0) unicornhat.brightness(0.5) unicornhat.clear() unicornhat.show() try: while True: mainprog() time.sleep(inter) except KeyboardInterrupt: unicornhat.clear() unicornhat.show() time.sleep(0.1)
try: import numpy as np except ImportError: 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)
import unicornhat as uh import time, serial, random from random import randint uh.set_layout(uh.PHAT) # PHAT is 8x4 uh.brightness(0.5) # 1 is bright, 0 is dark. <0.4 is normally black... ser = serial.Serial('/dev/ttyAMA0', baudrate=38400) # MIDI signal in while True: data = ord(ser.read(1)) # read a byte if data != 240: # ignore 240, i think 240 is the bpm/clock? print data # me if data == (14 or 142): # when you hear a kick for y in range(4): for x in range(8): uh.set_pixel(x,y,255,0,0) uh.show() if data == (206 or 78): # when you hear a snare for y in range(4): for x in range(8): uh.set_pixel(x,y,0,255,0) uh.show() if data == (17): # when you hear a closed hihat for y in range(4): for x in range(8): uh.set_pixel(x,y,0,0,255) uh.show()