def __init__(self): unicornhathd.clear() unicornhathd.set_all(10, 0, 10) unicornhathd.show() unicornhathd.rotation(270) # Rotation unicornhathd.brightness(.75) unicornhathd.show()
def flash_all(flash_count, delay, color): # light all of the LEDs in a RGB single color. Repeat 'flash_count' times # keep illuminated for 'delay' value for index in range(flash_count): # fill the light buffer with the specified color unicornhathd.set_all(*color) # show the color unicornhathd.show() # wait a bit time.sleep(delay) # turn everything off unicornhathd.off() # wait a bit more time.sleep(delay)
def tidal_handler(*args): try: unicornhathd.brightness(1) red = 0 blue = 0 green = 0 crush = False sustain = 1 ldecay = 0.05 for num, arg in enumerate(args, start=1): ## print("{0}: {1}".format(num, arg)) if arg == "red": red = int(args[num]) elif arg == "green": green = int(args[num]) elif arg == "blue": blue = int(args[num]) elif arg == "ldecay": ldecay = float(args[num]) elif arg == "sustain": sustain = float(args[num]) elif arg == "crush": crush = True print("r: {0}, g: {1}, b: {2}, crush: {3}, sustain: {4}, ldecay: {5}". format(red, green, blue, crush, sustain, ldecay)) if crush == True: red = 255 blue = 255 green = 255 ldecay = 1.0 unicornhathd.set_all(red, green, blue) unicornhathd.show() ## print("about to sleep for {0}".format(ldecay * sustain)) time.sleep(ldecay * sustain) ## print(".....awake!") unicornhathd.off() except KeyboardInterrupt: unicornhathd.off()
def init(): global current_activity_light, indicator_row, u_height, u_width # Clear the display (just in case) unicornhathd.clear() # Initialize all LEDs to black unicornhathd.set_all(0, 0, 0) # set the display orientation to zero degrees unicornhathd.rotation(90) # set u_width and u_height with the appropriate parameters for the HAT u_width, u_height = unicornhathd.get_shape() # calculate where we want to put the indicator light indicator_row = u_height - 1 # The app flashes a GREEN light in the first row every time it connects to Google to check the calendar. # The LED increments every time until it gets to the other side then starts over at the beginning again. # The current_activity_light variable keeps track of which light lit last. At start it's at -1 and goes from there. current_activity_light = u_width # Set a specific brightness level for the Pimoroni Unicorn HAT, otherwise it's pretty bright. # Comment out the line below to see what the default looks like. unicornhathd.brightness(0.5)
def burst(): uhat.clear() uhat.set_rotation(90) t = 16 randomcolors = [random_color() for i in range(t)] xyz = [(randint(0, 8) + 4, randint(0, 8) + 4, randint(0, 4) + 4, random_color()) for i in range(t)] for _ in range(25): uhat.set_brightness(0.5) random.shuffle(randomcolors) random.shuffle(xyz) for u in range(t): ut = max(0, math.cos(math.radians(1 - (u * 11)))) uhat.set_all(0, 0, 0) for n in range(5): draw_circle(xyz[n][0], xyz[n][1], min(xyz[n][2], u), randomcolors[n], ut) time.sleep(0.05) uhat.show() time.sleep(0.5)
def set_all(color): unicornhathd.set_all(*color) unicornhathd.show()
#!/usr/bin/env python import unicornhathd import time import random """ same as unicornhathd.off() """ unicornhathd.clear() unicornhathd.set_all(0, 0, 0) unicornhathd.show() unicornhathd.rotation(0) """ switch decices black or white pixel """ switch = 0 """ rotation changes each x for """ width = 15 rot = 90 counter = 0 unicornhathd.rotation(180) while True: if switch == 0: r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) else: r = 0 g = 0 b = 0 for x in range(0, 16):
async def set_all(r, g, b): return unicornhathd.set_all(r, g, b)
def set_all(r, g, b) -> None: uni.set_all(r, g, b)
This basic example shows use of the Python Pillow library: The tiny 16x16 bosses in lofi.png are from Oddball: http://forums.tigsource.com/index.php?topic=8834.0 Licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Press Ctrl+C to exit! """) unicornhathd.rotation(90) unicornhathd.brightness(0.5) unicornhathd.set_all(10,10,10) width, height = unicornhathd.get_shape() img = Image.open('pandaheart16.png') negpixmap = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1], [1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1], [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
3) 100W Tungsten bulb 4) Halogen bulb 5) Carbon Arc bulb Press CTRL+C to exit and turn off. """) # main loop try: while True: # present choice options to user choice = input() choice = int(choice) if choice == 1: rgblist = candle elif choice == 2: rgblist = fortyw elif choice == 3: rgblist = hundredw elif choice == 4: rgblist = halogen elif choice == 5: rgblist = carbon unicornhathd.clear() unicornhathd.set_all(rgblist[0], rgblist[1], rgblist[2]) unicornhathd.show() time.sleep(1.0 / 60) except KeyboardInterrupt: unicornhathd.off()
def show_failure(): print(':(') unicornhathd.set_all(255, 0, 0) unicornhathd.show()
#!/usr/bin/env python3 import unicornhathd unicornhathd.brightness(1) unicornhathd.set_all(255, 255, 255) unicornhathd.show()
from time import sleep from envirophat import light, motion import unicornhathd unicornhathd.brightness(1.0) unicornhathd.clear() def map_values(x, a, b, c, d): """Maps value between input and output ranges. Takes input value x in range a to b, returning corresponding value in range c to d. """ y = (x-a)/(b-a) * (d-c)+c return y while True: # We need separate RGB values for the UnicornHAT r, g, b = light.rgb() heading = motion.heading() # Heading is 0-359 degrees; map to 0-1.0 for UnicornHAT brightness brightness = map_values(heading, 0, 359, 0.0, 1.0) print(r, g, b, heading, brightness) unicornhathd.set_all(r, g, b) unicornhathd.brightness(brightness) unicornhathd.show() sleep(0.1)
#!/usr/bin/python import unicornhathd import time unicornhathd.rotation(270) unicornhathd.set_all(128, 0, 128) unicornhathd.show() time.sleep(1.0 / 2) unicornhathd.set_pixel(1, 2, 255, 255, 255) unicornhathd.set_pixel(1, 3, 255, 255, 255) unicornhathd.set_pixel(1, 4, 255, 255, 255) unicornhathd.set_pixel(1, 5, 255, 255, 255) unicornhathd.set_pixel(2, 2, 255, 255, 255) unicornhathd.set_pixel(3, 2, 255, 255, 255) unicornhathd.set_pixel(4, 2, 255, 255, 255) unicornhathd.set_pixel(5, 2, 255, 255, 255) unicornhathd.set_pixel(6, 2, 255, 255, 255) unicornhathd.set_pixel(6, 3, 255, 255, 255) unicornhathd.set_pixel(6, 4, 255, 255, 255) unicornhathd.set_pixel(6, 5, 255, 255, 255) unicornhathd.set_pixel(2, 6, 255, 255, 255) unicornhathd.set_pixel(3, 6, 255, 255, 255) unicornhathd.set_pixel(4, 6, 255, 255, 255) unicornhathd.set_pixel(5, 6, 255, 255, 255) time.sleep(1.0 / 2) unicornhathd.show() unicornhathd.set_pixel(1, 10, 255, 255, 255) unicornhathd.set_pixel(1, 11, 255, 255, 255) unicornhathd.set_pixel(1, 12, 255, 255, 255) unicornhathd.set_pixel(2, 9, 255, 255, 255) unicornhathd.set_pixel(2, 13, 255, 255, 255)
def show_general_failure(message): print(message) scroll_text(message, RED) unicornhathd.set_all(255, 0, 0) unicornhathd.show()
def main(): # used to setup our status indicator at the bottom of the led array global current_activity_light, indicator_row, service, u_height, u_width # tell the user what we're doing... print('\n') print(HASHES) print(HASH, 'Pi Remind (HD) ', HASH) print(HASH, 'https://github.com/johnwargo/pi-remind-hd', HASH) print(HASH, 'By John M. Wargo (www.johnwargo.com) ', HASH) print(HASHES) # Clear the display (just in case) unicornhathd.clear() # Initialize all LEDs to black unicornhathd.set_all(0, 0, 0) # set the display orientation to zero degrees unicornhathd.rotation(90) # set u_width and u_height with the appropriate parameters for the HAT u_width, u_height = unicornhathd.get_shape() # calculate where we want to put the indicator light indicator_row = u_height - 1 # The app flashes a GREEN light in the first row every time it connects to Google to check the calendar. # The LED increments every time until it gets to the other side then starts over at the beginning again. # The current_activity_light variable keeps track of which light lit last. At start it's at -1 and goes from there. current_activity_light = u_width # Set a specific brightness level for the Pimoroni Unicorn HAT, otherwise it's pretty bright. # Comment out the line below to see what the default looks like. unicornhathd.brightness(0.5) # output whether reboot mode is enabled if REBOOT_COUNTER_ENABLED: print('Reboot enabled ({} retries)'.format(REBOOT_NUM_RETRIES)) try: # Initialize the Google Calendar API stuff print('Initializing the Google Calendar API') # Google says: If modifying these scopes, delete your previously saved credentials at ~/.credentials/client_secret.json # On the pi, it's in /root/.credentials/ SCOPES = 'https://www.googleapis.com/auth/calendar.readonly' store = file.Storage('google_api_token.json') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets('client_secret.json', SCOPES) creds = tools.run_flow(flow, store) service = build('calendar', 'v3', http=creds.authorize(Http())) # Set the timeout for the rest of the Google API calls. # need this at its default (infinity, i think) during the registration process. socket.setdefaulttimeout(10) # 10 seconds except Exception as e: print('\nException type:', type(e)) # not much else we can do here except to skip this attempt and try again later print('Error:', sys.exc_info()[0]) print('Unable to initialize Google Calendar API') # make all the LEDs red set_all(FAILURE_COLOR) # Wait 5 seconds (so the user can see the error color on the display) time.sleep(5) # turn off all of the LEDs unicornhathd.off() # then exit, nothing else we can do here, right? sys.exit(0) print('Application initialized\n') # flash some random LEDs just for fun... flash_random(5, 0.5) # blink all the LEDs GREEN to let the user know the hardware is working flash_all(3, 0.10, GREEN) calendar_loop()
import unicornhathd from time import sleep unicornhathd.brightness(0.5) unicornhathd.clear() unicornhathd.set_all(255, 0, 0) unicornhathd.show() sleep(3) unicornhathd.off()