def iss(r,g,b): UH.off() UH.rotation(180) UH.brightness(0.5) global working working = True UH.set_pixel(4,0,r,g,b) UH.set_pixel(4,1,r,g,b) UH.set_pixel(3,2,r,g,b) UH.set_pixel(4,2,r,g,b) UH.set_pixel(5,2,r,g,b) UH.set_pixel(3,3,r,g,b) UH.set_pixel(5,3,r,g,b) UH.set_pixel(3,4,r,g,b) UH.set_pixel(4,4,r,g,b) UH.set_pixel(5,4,r,g,b) UH.set_pixel(2,5,r,g,b) UH.set_pixel(3,5,r,g,b) UH.set_pixel(4,5,r,g,b) UH.set_pixel(5,5,r,g,b) UH.set_pixel(6,5,r,g,b) UH.set_pixel(2,6,r,g,b) UH.set_pixel(3,6,210,210,35) UH.set_pixel(4,6,240,130,10) UH.set_pixel(5,6,210,210,35) UH.set_pixel(6,6,r,g,b) UH.set_pixel(3,7,210,210,35) UH.set_pixel(4,7,210,210,35) UH.set_pixel(5,7,210,210,35) UH.show() time.sleep(2) UH.off() working = False
def tick(self): """ tick tock - update every minute """ uh.rotation(270) while True: self.show_time() time.sleep(60) uh.off()
def bleep(): z = 1 while z < 24: if (GPIO.input(12) == 0): print("Phone Button Pressed") time.sleep(1) restart() elif (GPIO.input(16) == 0): print("Handset Lifted") time.sleep(1) restart() else: GPIO.output(14, True) GPIO.output(26, True) unicorn.set_all(255, 0, 0) unicorn.show() time.sleep(0.2) GPIO.output(14, False) GPIO.output(26, False) unicorn.off() time.sleep(1) z += 1 print(z) restart()
def unicornSetup(): unicorn.off() unicorn.clear() unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(0.5) width, height = unicorn.get_shape()
def draw_animation(image): # this is the original pimoroni function for drawing sprites try: for o_x in range(int(image.size[0] / width)): for o_y in range(int(image.size[1] / height)): valid = False for x in range(width): for y in range(height): pixel = image.getpixel( ((o_x * width) + y, (o_y * height) + x)) r, g, b = int(pixel[0]), int(pixel[1]), int(pixel[2]) if r or g or b: valid = True unicorn.set_pixel(x, y, r, g, b) if valid: unicorn.show() time.sleep(cycle_time) except KeyboardInterrupt: unicorn.off()
def show_colors(color_scheme, seconds): """ show a chess board consisting of two colors """ # iterate over the rows of the matrix for row in range(0, 8): for col in range(0, 8): print(f"--- row is {row} and col is {col} -- ") # check if we have even | uneven rows cols mod_row = row % 2 mod_col = col % 2 print(mod_row) print(mod_col) # if we have even rows apply color1 on entry 0, 2, 4, 6 if mod_row == 0: if mod_col == 0: unicorn.set_pixel(row, col, tuple(color_scheme[0])) else: unicorn.set_pixel(row, col, tuple(color_scheme[1])) # if we have uneven rows, apply color1 on entry 1,3,5,7 if mod_row == 1: if mod_col == 1: unicorn.set_pixel(row, col, tuple(color_scheme[0])) else: unicorn.set_pixel(row, col, tuple(color_scheme[1])) unicorn.show() time.sleep(seconds) unicorn.off()
def pause(r,g,b): UH.off() UH.rotation(180) UH.brightness(0.5) global working working = True UH.set_pixel(1,1,r,g,b) UH.set_pixel(1,2,r,g,b) UH.set_pixel(1,3,r,g,b) UH.set_pixel(1,4,r,g,b) UH.set_pixel(1,5,r,g,b) UH.set_pixel(1,6,r,g,b) UH.set_pixel(2,1,r,g,b) UH.set_pixel(2,2,r,g,b) UH.set_pixel(2,3,r,g,b) UH.set_pixel(2,4,r,g,b) UH.set_pixel(2,5,r,g,b) UH.set_pixel(2,6,r,g,b) UH.set_pixel(5,1,r,g,b) UH.set_pixel(5,2,r,g,b) UH.set_pixel(5,3,r,g,b) UH.set_pixel(5,4,r,g,b) UH.set_pixel(5,5,r,g,b) UH.set_pixel(5,6,r,g,b) UH.set_pixel(6,1,r,g,b) UH.set_pixel(6,2,r,g,b) UH.set_pixel(6,3,r,g,b) UH.set_pixel(6,4,r,g,b) UH.set_pixel(6,5,r,g,b) UH.set_pixel(6,6,r,g,b) UH.show() time.sleep(2) UH.off() working = False
def uh_pulse(): UH.off() global working working = True global contWorking contWorking = True while contWorking == True: x0, y0 = 3.5, 3.5 for z in range(1, 5)[::-1] + range(1, 10): fwhm = 5/z gauss = make_gaussian(fwhm, x0, y0) for y in range(8): for x in range(8): h = 0.8 s = 0.8 v = gauss[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) UH.set_pixel(x, y, r, g, b) UH.show() time.sleep(0.025) UH.off() working = False
def uh_f1start(b = 0.5): UH.off() UH.brightness(b) UH.rotation(180) for x in range(2): for y in range(8): UH.set_pixel(x,y,255,0,0) UH.show() sleep(1) for x in range(2,4): for y in range(8): UH.set_pixel(x,y,255,0,0) UH.show() sleep(1) for x in range(4,6): for y in range(8): UH.set_pixel(x,y,255,0,0) UH.show() sleep(1) for x in range(6,8): for y in range(8): UH.set_pixel(x,y,255,0,0) UH.show() delayswitch = random.random() delay = random.random() if delayswitch > 0.66: sleep(6 + delay) elif delayswitch > 0.33: sleep(5 + delay) else: sleep(4 + delay) UH.off()
def play(r,g,b): UH.off() UH.rotation(180) UH.brightness(0.5) global working working = True UH.set_pixel(2,0,r,g,b) UH.set_pixel(2,1,r,g,b) UH.set_pixel(2,2,r,g,b) UH.set_pixel(2,3,r,g,b) UH.set_pixel(2,4,r,g,b) UH.set_pixel(2,5,r,g,b) UH.set_pixel(2,6,r,g,b) UH.set_pixel(2,7,r,g,b) UH.set_pixel(3,1,r,g,b) UH.set_pixel(3,2,r,g,b) UH.set_pixel(3,3,r,g,b) UH.set_pixel(3,4,r,g,b) UH.set_pixel(3,5,r,g,b) UH.set_pixel(3,6,r,g,b) UH.set_pixel(4,2,r,g,b) UH.set_pixel(4,3,r,g,b) UH.set_pixel(4,4,r,g,b) UH.set_pixel(4,5,r,g,b) UH.set_pixel(5,3,r,g,b) UH.set_pixel(5,4,r,g,b) UH.show() time.sleep(2) UH.off() working = False
def off(self): self.on = False settings.IDLE = False settings.STUNNED = False settings.RELEASING = False time.sleep(0.5) unicorn.off()
def uh_setFullColour(r, g, b, brightness = 0.5): UH.off() UH.brightness(brightness) for y in range(8): for x in range(8): UH.set_pixel(x,y,r,g,b) UH.show()
def pihat(): global state while state: for color in range(8): for i in range(color + 1): for h in range(4): if abs(i - 7) == 7: unicorn.set_pixel(abs(i - 7), h, 255, 0, 0) elif abs(i - 7) == 6: unicorn.set_pixel(abs(i - 7), h, 255, 99, 71) elif abs(i - 7) == 5: unicorn.set_pixel(abs(i - 7), h, 255, 140, 0) elif abs(i - 7) == 4: unicorn.set_pixel(abs(i - 7), h, 255, 215, 0) elif abs(i - 7) == 3: unicorn.set_pixel(abs(i - 7), h, 255, 255, 0) elif abs(i - 7) == 2: unicorn.set_pixel(abs(i - 7), h, 154, 205, 0) elif abs(i - 7) == 1: unicorn.set_pixel(abs(i - 7), h, 124, 252, 0) elif abs(i - 7) == 0: unicorn.set_pixel(abs(i - 7), h, 0, 255, 0) unicorn.show() if not state: break time.sleep(0.5) unicorn.off()
def light(r,g,b): UH.off() UH.rotation(180) UH.brightness(0.5) global working working = True UH.set_pixel(2,1,r,g,b) UH.set_pixel(3,1,r,g,b) UH.set_pixel(4,1,r,g,b) UH.set_pixel(5,1,r,g,b) UH.set_pixel(1,2,r,g,b) UH.set_pixel(6,2,r,g,b) UH.set_pixel(1,3,r,g,b) UH.set_pixel(3,3,r,g,b) UH.set_pixel(4,3,r,g,b) UH.set_pixel(6,3,r,g,b) UH.set_pixel(2,4,r,g,b) UH.set_pixel(5,4,r,g,b) UH.set_pixel(2,5,r,g,b) UH.set_pixel(5,5,r,g,b) UH.set_pixel(3,6,r,g,b) UH.set_pixel(4,6,r,g,b) UH.show() time.sleep(2) UH.off() working = False
def battery(r,g,b): UH.off() UH.rotation(180) UH.brightness(0.5) global working working = True UH.set_pixel(1,2,r,g,b) UH.set_pixel(2,2,r,g,b) UH.set_pixel(3,2,r,g,b) UH.set_pixel(4,2,r,g,b) UH.set_pixel(5,2,r,g,b) UH.set_pixel(1,3,r,g,b) UH.set_pixel(2,3,r,g,b) UH.set_pixel(3,3,r,g,b) UH.set_pixel(6,3,r,g,b) UH.set_pixel(1,4,r,g,b) UH.set_pixel(2,4,r,g,b) UH.set_pixel(3,4,r,g,b) UH.set_pixel(6,4,r,g,b) UH.set_pixel(1,5,r,g,b) UH.set_pixel(2,5,r,g,b) UH.set_pixel(3,5,r,g,b) UH.set_pixel(4,5,r,g,b) UH.set_pixel(5,5,r,g,b) UH.show() time.sleep(2) UH.off() working = False
def main(): ####### Start loop from here try: devSnapThread = Thread(target=checkDevSnap, args=()) devSnapThread.start() ## Offset start citest with 5 seconds to avoid build animations at the same time time.sleep(5) citestThread = Thread(target=checkCITest, args=()) citestThread.start() except (KeyboardInterrupt, SystemExit): print("Error!! Error!! Error!!") uh.off() sys.exit() #Print queue in leds try: while True: ## Check if building: color : red/blue_anime = building, red = locked and not building ## Kolla "claimed" : true, på bygge-api #printBuildQueue() global devSnapBuildStatus if 'blue' in devSnapBuildStatus: uh.set_pixel(0, 0, 0, 0, 255) if 'red' in devSnapBuildStatus: uh.set_pixel(0, 0, 255, 0, 0) uh.show() if 'anime' in devSnapBuildStatus: time.sleep(0.3) uh.set_pixel(0, 0, 0, 0, 0) uh.show() time.sleep(0.3) except (KeyboardInterrupt, SystemExit): uh.off() sys.exit()
def turn_on_pixels(seconds): """ show the data """ counter = 0 # loop from the bottom rows of the hat and go columnswise ---> ^ ---> etc for row in reversed(range(0, 8)): for col in range(0, 8): print(f"row is: {row}, col is: {col}") # extract temperature and convert to float tempus = df_show.loc[counter, "mean_temp"] tempus = float(tempus) print(f"temperature is {tempus}") # get the color value that should be displayed color = temp_to_rgb(tempus, rocket) print(f"color is {color}") # turn on pixels unicorn.set_pixel(col, row, color) time.sleep(0.05) unicorn.show() # add to counter print(f"counter is {counter}") counter = counter + 1 # let it shine time.sleep(seconds) unicorn.off()
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 """ # set starting position of the palette pal_pos = 0 print(f"range to light up is from {start_value} to {start_value + 7}") # loop from start value to end value (8 positions) for col in range(start_value, start_value + 8, 1): # if the value is not outside the hat range, light up this pixel if col >= 0 and col <= 7: print(f"lighting up column {col} with {palette[pal_pos]}") # color all pixels for row in range(2, 6): unicorn.set_pixel(col, row, palette[pal_pos]) # for each loop iteration, update display unicorn.show() pal_pos = pal_pos + 1 time.sleep(0.02) unicorn.off() print("-----------------------------------------------")
def loop(): print('Looping through all images in folder {}\n' 'CRL+C to skip image'.format(folder_path)) try: for img_file in os.listdir(folder_path): if img_file.endswith(icon_extension): print('Drawing image: {}'.format(folder_path + img_file)) img = Image.open(folder_path + img_file) draw_animation(img) else: print('Not using this file, might be not an image: {}'.format( img_file)) except KeyboardInterrupt: unicorn.off() unicorn.off()
def updateLEDs(): i = 0 unicorn.off() for pod_key in pods.keys(): c = COLOURS[pods[pod_key]] unicorn.set_pixel(math.floor(i / width), i % width, c[0], c[1], c[2]) i = i + 1 unicorn.show()
def piLoad(): # Loads image onto matrix uh.off() for led in leds: if led.lit: uh.set_pixel(led.pos[0], led.pos[1], led.color[0], led.color[1], led.color[2]) #print str(led.pos[0])+ ' ' +str(led.pos[1]) + ' ' + str(led.color[1]) uh.show()
def cross(self): unicorn.off() for i in range(0, 8): unicorn.set_pixel(i, i, 255, 0, 0) unicorn.set_pixel(i, 7 - i, 255, 0, 0) unicorn.show() time.sleep(1) unicorn.off()
def run(self): self.running = True try: while self.running: self.loopIteration() except self.ServerTerminationError: unicorn.off() print("Stopped")
def startup(self): for y in range(self.height): for x in range(self.width): unicorn.set_pixel(x, (self.height - 1) - y, 0, 255, 0) unicorn.show() time.sleep(0.05) time.sleep(0.5) unicorn.off()
def habit_light(adic, most): """ Activate NEOpixels (individual pixels of UnicornHAT) according to habit activity. """ # First, turn odff all pixels in case of previous program using the UnicornHAT. unicorn.off() # Half the brightness of the UnicornHAT (1.0 is blindingly bright)! unicorn.brightness(0.5) # What follows is a constant that converts UTC time to PST. Keep in mind that utctopstsec = 28800 # Classic print statements for troubleshooting! # print(len(adic)) # print(adic[0]["time"]) # Create an empty list to convert activity from SQL database. habitgrid = [] # This loop appends a sublist to habitgrid consisting of the week number (first week of the year = 1), # day number (Sunday = 1), and light intensity proportional to the day's actual activity divided by the daily goal. for i in adic: print(i) habitgrid.append([ int( datetime.utcfromtimestamp(i["time"] - utctopstsec).strftime('%U')), int( datetime.utcfromtimestamp(i["time"] - utctopstsec).strftime('%w')), int(165 * i["howmuch"] / most + 90) ]) # print(habitgrid) # The following loop uses values from the habitgrid and implements them with the unicorn's set_pixel function in a way... # that will look good with the RPi's orientation. Unfortunately, pixel 7,7 is at the top-left in this design. for j in habitgrid: print(j) if j[2] > 255: j[2] = 255 # Note the lsat three numbers in the tuple. The penultimate entry sets the color to green, but this option... # could easily be changed. # A reminder: set_pixel(x, y, R, G, B). unicorn.set_pixel(abs(j[0] - 7), abs(j[1] - 7), 0, j[2], 0) # This loop allows for a light in the bottom-most row to turn on when a week's worth of activity has occurred! # By the default the streak variable is set to one. If any day in a column lacks activity, streak = 0. # The bottom-most light of a week column lights up white when streak remains 1. for k in range(0, 8): streak = 1 for l in range(1, 8): print(unicorn.get_pixel(k, l) == (0, 0, 0)) if unicorn.get_pixel(k, l) == (0, 0, 0): streak = 0 if streak == 1: unicorn.set_pixel(k, 0, 255, 255, 255) # Turn it up! unicorn.show()
def set_error(self): for i in range(0, 10): UH.off() pixels = self._set_whole_grid(self.PURPLE) UH.set_pixels(pixels) UH.show() time.sleep(0.4) self._current_value = self.PURPLE self._logger.error("Light set to error state")
def switchOff() : global blinkThread, globalBlue, globalGreen, globalRed globalRed = 0 globalGreen = 0 globalBlue = 0 if blinkThread != None : blinkThread.do_run = False unicorn.clear() unicorn.off()
def set_error(self): for i in range(0,10): UH.off() pixels = self._set_whole_grid(self.PURPLE) UH.set_pixels(pixels) UH.show() time.sleep(0.4) self._current_value = self.PURPLE self._logger.error("Light set to error state")
def show_colour_confirmation(r=160, g=64, b=128): width, height = unicorn.get_shape() for i in range(0, width * height): x = i % width y = i // height unicorn.set_pixel(x, y, r, g, b) unicorn.show() sleep(0.01) unicorn.off()
def rainbow(self): steps = 200 for s in range(steps): for i in range(8): col = self.hsv2rgb((i * 45 + s * 5) % 360, 1, 1) self.drawRect(0, i, 8, 1, col[0], col[1], col[2]) unicorn.show() time.sleep(0.02) unicorn.off()
def set_error(self): for i in range(0,10): UH.off() pixels=self.__set_whole_grid(self.PURPLE) UH.set_pixels(pixels) UH.show() time.sleep(0.4) self.__current_value=self.PURPLE self.__log("ERROR")
def display(c, digit): logging.info('Display : ' + c) uh.brightness(0.5) uh.off() uh.set_pixels(digit) uh.show() logging.info('Waiting... ') time.sleep(5) logging.info('... done.')
def piLoad(): # Loads image onto AstroPi matrix #grid, grid_png = buildGrid() #ap.set_pixels(grid) uh.off() for led in leds: if led.lit: uh.set_pixel(led.pos[0], led.pos[1], led.color[0], led.color[1], led.color[2]) #print str(led.pos[0])+ ' ' +str(led.pos[1]) + ' ' + str(led.color[1]) uh.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): for y in range(8): for x in range(8): lights.set_pixel(x, y, color[0], color[1], color[2]) lights.show() time.sleep(delay) lights.off() time.sleep(delay)
def setup(): global heights heights = [] for b in range(0, 6): heights.append(0) uh.off() for b in range(0, 8): uh.set_pixel(0, b, 255, 255, 255) for b in range(0, 8): uh.set_pixel(7, b, 255, 255, 255) for b in range(1, 7): uh.set_pixel(b, 0, 255, 255, 255) uh.show()
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 do_swirl(duration): # modified from: https://github.com/pimoroni/unicorn-hat/blob/master/python/examples/demo.py step = 0 for i in range(duration): for y in range(8): for x in range(8): r, g, b = swirl(x, y, step) r = int(max(0, min(255, r))) g = int(max(0, min(255, g))) b = int(max(0, min(255, b))) lights.set_pixel(x, y, r, g, b) step += 1 lights.show() time.sleep(0.01) # turn off all lights when you're done lights.off()
def flash_random(flash_count, delay): # Copied from https://github.com/pimoroni/unicorn-hat/blob/master/python/examples/random_blinky.py for index in range(flash_count): rand_mat = np.random.rand(8, 8) for y in range(8): for x in range(8): 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) lights.set_pixel(x, y, r, g, b) lights.show() time.sleep(delay) lights.off() time.sleep(delay)
def set_activity_light(color, increment): # used to turn on one LED at a time across the bottom row of lights. The app uses this as an unobtrusive # indicator when it connects to Google to check the calendar. Its intended as a subtle reminder that things # are still working. # On 06/27/2016 changed the code so it leaves the light on (in a different color) so you can tell that the # Pi is still running the code. So, it shows GREEN when connecting to Google, then switches to BLUE when # its done. global current_activity_light # turn off (clear) any lights that are on lights.off() if increment: # OK. Which light will we be illuminating? if current_activity_light < 1: # start over at the beginning when you're at the end of the row current_activity_light = 8 # increment the current light (to the next one) current_activity_light -= 1 # set the pixel color lights.set_pixel(current_activity_light, 0, color[0], color[1], color[2]) # show the pixel lights.show()
def main( offset=30, brightness=1, brightness_step=0.001, brightness_shutoff=0.4, tick_max=120000, cycle_period=0.03, hue_step=0.005): offset = offset + 128 for tick in itertools.count(): cycle_end = time.time() + cycle_period if brightness <= brightness_shutoff: unicorn.off() time.sleep(tick_max / cycle_period / 1000) os.system('shutdown -h now') if tick > tick_max: brightness -= brightness_step unicorn.brightness(brightness) set_hat_color(tick * hue_step, level_boost=offset) time.sleep(max(time.time() - cycle_end, 0))
import random import unicornhat as unicorn from monitor import lightshow if __name__ == "__main__": while True: try: x = random.randint(0,7) y = random.randint(0,7) lightshow.sleep(x,y) except KeyboardInterrupt: break unicorn.off()
def clear(self): UH.off()
import sys try: import unicornhat as UH except ImportError: print("run: pip install --user --upgrade unicornhat\n before trying again.") sys.exit(0) try: raw_input # Python 2 except NameError: raw_input = input # Python 3 brightness = float(sys.argv[1])/100 UH.off() UH.brightness(brightness) while True: try: data = raw_input() if len(data) < 10: if data[0] == "B": UH.brightness(float(data[1:])/100) if data[0] == "R": UH.rotation(float(data[1:])) else: if data[0] == "P": data = data[1:].strip() s = data.split(',') for p in range(0,len(s),5):
def reset(): hat.off()