def option_2():###Select a website to create the light show your_webiste_choice = raw_input("Please enter the web address") final_address = "http://%s" %(your_webiste_choice) print "finding", final_address website = urllib2.urlopen(final_address ) ##print website.read() sentence = website.read() print sentence ###Checks the letters in the website, then works out the co- ordinates### for letter in sentence: index = ord(letter)-65 #print index ### remove when complete### if index > 0: x = int(index/8.0) #print x ### remove when complete### #x = x - 4 y = int(index%8) #print (x, y) UH.clear UH.brightness(0.2) UH.set_pixel(y, x, 0, 255, 0) UH.set_pixel(x, y, 0, 255, 0) UH.show() time.sleep(0.02) UH.clear() elif index <= 0: random_sparkle()
def go(): unicorn.brightness(1) unicorn.rotation(90) wrd_rgb = [[154, 173, 154], [0, 255, 0], [0, 200, 0], [0, 162, 0], [0, 145, 0], [0, 96, 0], [0, 74, 0], [0, 0, 0,]] clock = 0 blue_pilled_population = [[randint(0,7), 7]] t_end = time.time() + 10 while time.time() < t_end: for person in blue_pilled_population: y = person[1] for rgb in wrd_rgb: if (y <= 7) and (y >= 0): unicorn.set_pixel(person[0], y, rgb[0], rgb[1], rgb[2]) y += 1 person[1] -= 1 unicorn.show() time.sleep(0.1) clock += 1 if clock % 5 == 0: blue_pilled_population.append([randint(0,7), 7]) if clock % 7 == 0: blue_pilled_population.append([randint(0,7), 7]) while len(blue_pilled_population) > 100: blue_pilled_population.pop(0)
def option_1():###User enters a sentance to create the light show### phrase = raw_input("Please enter your phrase ").lower() ###Checks the letters in the phrase, then works out the co- ordinates### for letter in phrase: index = ord(letter)-65 #print index ### remove when complete### if index > 0: x = int(index/8.0) #print x ### remove when complete### x = x - 4 y = int(index%8) #print (x, y) UH.clear UH.brightness(0.20) UH.set_pixel(y, x, 255, 255, 255) #UH.set_pixel(x, y, 0, 255, 0) UH.show() time.sleep(0.5) UH.clear() elif index <= 0: random_sparkle()
def drawTile(y, x, value): rgb=colour[(index[value])] unicorn.set_pixel((x*2),(y*2),rgb[0],rgb[1],rgb[2]) unicorn.set_pixel((x*2)+1,(y*2),rgb[0],rgb[1],rgb[2]) unicorn.set_pixel((x*2),(y*2)+1,rgb[0],rgb[1],rgb[2]) unicorn.set_pixel((x*2)+1,(y*2)+1,rgb[0],rgb[1],rgb[2]) unicorn.show()
def uh_show_matrix(mat, pause = .075): for y in range(len(mat)): for x in range(len(mat[0])): uh.set_pixel(x, y, mat[x][y][0], mat[x][y][1], mat[x][y][2]) uh.show() time.sleep(pause)
def clearsystem(): led.clear() led.rotation(0) os.system("arp -d *") os.system("ip neigh flush all") led.show() return;
def mum2(): unicorn.set_pixel(4,0,0,132,0) unicorn.set_pixel(4,1,0,132,0) unicorn.set_pixel(3,1,0,132,0) unicorn.set_pixel(5,1,0,132,0) unicorn.set_pixel(5,2,0,132,0) unicorn.set_pixel(4,2,0,132,0) unicorn.set_pixel(3,2,0,132,0) unicorn.set_pixel(2,2,0,132,0) unicorn.set_pixel(6,2,0,132,0) unicorn.set_pixel(5,3,0,132,0) unicorn.set_pixel(4,3,0,132,0) unicorn.set_pixel(3,3,0,132,0) unicorn.set_pixel(2,4,0,132,0) unicorn.set_pixel(3,4,0,132,0) unicorn.set_pixel(5,4,0,132,0) unicorn.set_pixel(6,4,0,132,0) unicorn.set_pixel(3,5,0,132,0) unicorn.set_pixel(4,4,0,132,0) unicorn.set_pixel(4,5,0,132,0) unicorn.set_pixel(5,5,0,132,0) unicorn.set_pixel(4,6,132,101,0) unicorn.set_pixel(4,7,132,101,0) unicorn.set_pixel(3,6,255,0,0) unicorn.set_pixel(5,6,255,0,0) unicorn.show() time.sleep(2) unicorn.clear()
def main(): while True: led.clear() led.show() timestamp = datetime.datetime.now().time() if (os.system("ping -c " + p + " 192.168.111.25")) == 0: if starttime <= timestamp <= endtime: clearsystem() if (os.system("ping -c " + p + " " + hosts[1])) == 0: led_reset() led_piping() else: led_reset() led_router() else: if (os.system("ping -c " + p + " " + hosts[8])) == 0: led.clear() sshconnect(hosts[8], "base64-password", "poweroff") led_poweroff(hosts[8]) led_reset() if (os.system("ping -c " + p + " " + hosts[10])) == 0: led.clear() sshconnect(hosts[10], "base64-password", "poweroff") led_poweroff(hosts[10]) led_reset() continue else: led_arrow() continue
def go(): effects = [tunnel, rainbow_search, checker, swirl] unicorn.brightness(1) step = 0 t_end = time.time() + 10 while time.time() < t_end: for i in range(500): for y in range(8): for x in range(8): 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)
def fill(r=0,g=255,b=0): for x in range(8): for y in range(8): unicorn.set_pixel(8-x-1, 8-y-1, r, g, b) unicorn.show() time.sleep(0.05) time.sleep(2)
def draw_heart(hue, brightness): for x, y in pixels_to_light: color = colorsys.hsv_to_rgb(hue, 1, brightness) color = [int(c * 255) for c in color] r, g, b = color unicorn.set_pixel(x, y, r, g, b) unicorn.show()
def uni_show(prev_x, prev_y): a = np.random.rand(1) b = np.random.rand(1) c = np.random.rand(1) rgb_on = colorsys.hsv_to_rgb(a, b, c) rgb_off = colorsys.hsv_to_rgb(0.5, 0.5, 0.1) r_on = int(rgb_on[0]*255.0) g_on = int(rgb_on[1]*255.0) b_on = int(rgb_on[2]*255.0) r_off = int(rgb_off[0]*255.0) g_off = int(rgb_off[1]*255.0) b_off = int(rgb_off[2]*255.0) a = np.random.rand(1) x = int(a[0] * 8)-1 b = np.random.rand(1) y = int(b[0] * 8)-1 #print x, y if x < 0: x = 0 if y < 0: y = 0 unicorn.set_pixel(x, y, r_on, g_on, b_on) unicorn.set_pixel(prev_x, prev_y, r_off, g_off, b_off) unicorn.show() return x, y
def drawpet(pet): for y in range(8): for x in range(8): #set pixel with color r, g, b = pet[y][x] unicorn.set_pixel(x,y,r,g,b) unicorn.show()
def display_frame(self, screen): #screen used to display information to the player, also required to detect pygame events #Unicorn hat display is also handled here if self.game_over or self.start: screen.fill(WHITE) font = pygame.font.SysFont('serif', 25) title = font.render('Unicorn Snake.', True, BLACK) text = font.render('Press any key to play.', True, BLACK) instructions = font.render('Use the arrow keys to control your snake on the unicorn hat', True, BLACK) title_center_x = (SCREEN_WIDTH // 2) - (title.get_width() // 2) text_center_x = (SCREEN_WIDTH // 2) - (text.get_width() // 2) instructions_center_x = (SCREEN_WIDTH // 2) - (instructions.get_width() // 2) top_y = title.get_height() * 2 center_y = (SCREEN_HEIGHT // 2) - (text.get_height() // 2) bottom_y = SCREEN_HEIGHT - (instructions.get_height() * 2) screen.blit( title, [title_center_x, top_y] ) screen.blit( text, [text_center_x, center_y] ) screen.blit( instructions, [instructions_center_x, bottom_y] ) unicorn.clear() # removes last snake image from unicorn hat else: screen.fill(BLACK) font = pygame.font.SysFont('serif', 25) title = font.render('Unicorn Snake.', True, WHITE) text = font.render('Game on', True, WHITE) title_center_x = (SCREEN_WIDTH // 2) - (title.get_width() // 2) text_center_x = (SCREEN_WIDTH // 2) - (text.get_width() // 2) top_y = title.get_height() * 2 center_y = (SCREEN_HEIGHT // 2) - (text.get_height() // 2) screen.blit( title, [title_center_x, top_y] ) screen.blit( text, [text_center_x, center_y] ) unicorn.show() # display the snake and food on the unicorn hat pygame.display.flip()
def display_matrix(max_x, max_y, text=False, r=255, g=255, b=255): """ Display the matrix, either on the unicorn or on the stdout :param max_x: :param max_y: :param text: If True, display on stdout instead of unicornhat. For debugging """ if text: for x in range(max_x): for y in range(max_y): coordinate_tuple = (x, y) if LifeCell.matrix[coordinate_tuple].current_state == 'alive': print '*', else: print '.', print print else: for x in range(max_x): for y in range(max_y): coordinate_tuple = (x, y) if LifeCell.matrix[coordinate_tuple].current_state == 'alive': unicorn.set_pixel(x, y, r, g, b) else: unicorn.set_pixel(x, y, 0, 0, 0) unicorn.show()
def message(message): unicorn.rotation(270) for _ in range(2): unicorn_scroll(message,'white',255,0.1) unicorn.clear() unicorn.show() return "ok"
def draw_dot( x1, y1, r1, g1, b1, str1): "This draws the dot and outputs some text" UH.set_pixel(x1,y1,r1,g1,b1) UH.show() # print (str1 + str(x1) + ' ' + str(y1)) time.sleep(0.1) return
def icon(icon): unicorn.rotation(270) for _ in range(5): playIcon(icon) unicorn.clear() unicorn.show() return "ok"
def show_letter(letter,colour,brightness): #displays a single letter on th UH UH.rotation(_scrollrotation) for i in range(8): for j in range(8): if letter[j][i]: if colour == 'red': UH.set_pixel(j,flip[i],brightness,0,0) elif colour == 'green': UH.set_pixel(j,flip[i],0,brightness,0) elif colour == 'blue': UH.set_pixel(j,flip[i],0,0,brightness) elif colour == 'white': UH.set_pixel(j,flip[i],brightness,brightness,brightness) elif colour == 'pink': UH.set_pixel(j,flip[i],brightness,52,179) elif colour == 'cyan': UH.set_pixel(j,flip[i],0,brightness,brightness) elif colour == 'yellow': UH.set_pixel(j,flip[i],brightness,brightness,0) elif colour == 'orange': UH.set_pixel(j,flip[i],brightness,128,0) else: UH.set_pixel(j,flip[i],0,0,0) UH.show()
def update(self): while True: self.t = datetime.datetime.now() self.set_hour() self.set_min() self.set_sec() UH.show()
def paint(): global last_members index = 0 members = r.find_members() if(len(last_members) != len(members)): UH.clear() UH.show() last_members = members for member in members: temp = r.get_key(member + ".temp") baseline = r.get_key(member + ".temp.baseline") motion = r.get_key(member + ".motion") if safeFloat(motion) > 0: if(quiet == False): print("moved") on(index, 0, 0, 255) elif is_hot(temp, baseline): on(index, 255, 0, 0) if(quiet == False): print("HOT") else: on(index, 0, 255, 0) index = index +1
def set_build_success(self): self.logger.debug("Last build worked !") for y in range(8): for x in range(8): UH.set_pixel(x, y, 0, 255, 0) UH.show() time.sleep(0.05)
def writePixel(pixel): #try: #GPIO.setup(int(pin), GPIO.IN) #if GPIO.input(int(pin)) == True: # response = "Pin number " + pin + " is high!" #else: # response = "Pin number " + pin + " is low!" #except: #response = "There was an error reading pin " + pin + "." color = int(pixel) for i in range(0,8): UH.set_pixel(0,i,color,color,color) UH.show() response="All correct" now = datetime.datetime.now() timeString = now.strftime("%Y-%m-%d %H:%M") templateData = { 'title' : 'Status of Pixel' + str(pixel), 'time': timeString, 'response' : response } return render_template('main.html', **templateData)
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)) g = max(0, min(random_g, g + offset)) b = max(0, min(random_b, b + offset)) unicorn.set_pixel(x,y,int(r),int(g),int(b)) unicorn.brightness(bright) unicorn.show() time.sleep(0.001) if bright > 0.3: bright = bright - 0.01 #fade on each pass until black unicorn.clear()
def effect(): # trigger effect for i in range(steps_per * 8): for y in range(8): for x in range(8): r, g, b = background(x, y, i) 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) unicorn.show() time.sleep(0.01) for i in range(200): v = (math.sin(i / 6.0) + 1.0) / 2.0 for y in range(8): for x in range(8): r = 0 b = 0 g = 100 g *= tick_mask[y][x] g *= v 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) unicorn.show() time.sleep(0.02)
def load_rainbow(update_rate=5): """ A lightly modified version of Pimeroni's "rainbow" example. Displays a moving rainbow of colors that increases with load. Args: update_rate (float): How often to update the load value (seconds). """ i = 0.0 offset = 30 function_values[load_fetcher] = 1 threading.Thread(target=load_fetcher).start() while True: load_function = function_values[load_fetcher]/10 if function_values[load_fetcher] <= 10 else 10 for w in range(int(update_rate/0.01)): i = i + load_function for y in range(height): for x in range(function_pos[load_rainbow] + 1): 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() time.sleep(0.01)
def __init__(self): self._logger = logging.getLogger(self.__class__.__name__) self._current_value = self.BLUE UH.brightness(1.0) pixels = self._set_whole_grid(self._current_value) UH.set_pixels(pixels) UH.show()
def mum3(): unicorn.set_pixel(4,0,255,255,0) unicorn.set_pixel(3,1,255,255,0) unicorn.set_pixel(2,2,255,255,0) unicorn.set_pixel(2,3,255,255,0) unicorn.set_pixel(3,4,255,255,0) unicorn.set_pixel(5,4,255,255,0) unicorn.set_pixel(4,5,255,255,0) unicorn.set_pixel(4,6,255,255,255) unicorn.set_pixel(4,7,255,255,255) unicorn.set_pixel(3,6,255,255,255) unicorn.set_pixel(3,7,255,255,255) unicorn.set_pixel(3,5,255,255,0) unicorn.set_pixel(2,4,255,255,0) unicorn.set_pixel(3,0,255,255,0) unicorn.set_pixel(5,2,255,255,0) unicorn.set_pixel(4,1,255,255,0) unicorn.set_pixel(5,3,255,255,0) unicorn.set_pixel(4,4,255,255,0) unicorn.set_pixel(4,2,255,118,0) unicorn.set_pixel(3,2,255,118,0) unicorn.set_pixel(3,3,255,118,0) unicorn.set_pixel(4,3,255,118,0) unicorn.show() time.sleep(2) unicorn.clear()
def drawChar(offset_y, offset_x): for x in range(7,-1,-1): for y in range(8): pixel = img.getpixel(((offset_x*8)+x,(offset_y*8)+y)) r, g, b = int(pixel[0]),int(pixel[1]),int(pixel[2]) unicorn.set_pixel(x, y, r, g, b) unicorn.show() time.sleep(0.5)
def clear(self): r = 0 g = 0 b = 0 for y in range(8): for x in range(8): unicornhat.set_pixel(x, y, r, g, b) unicornhat.show()
def draw_game(snake, food, game_over): #if the player has lost, don't draw the game if game_over: for y in range(0, 8): for x in range(0, 8): UH.set_pixel(x, y, 50, 0, 0) else: #clear canvas for y in range(0, 8): for x in range(0, 8): UH.set_pixel(x, y, 0, 0, 0) #draw food (below snake) UH.set_pixel(food[0], food[1], 255, 0, 0) #draw snake for i in range(0, len(snake)): UH.set_pixel(snake[i][0], snake[i][1], 100, 100, 100) #update Unicorn HAT UH.show()
def sprinkles(): """ Lights up and turns off random LEDs """ start_time = time.time() time.clock() seconds_elapsed = 0 while seconds_elapsed < 15: seconds_elapsed = time.time() - start_time # Turn on a random LED red, green, blue = get_random_color() light_up_random_led(red, green, blue) # Turn OFF a random LED unicornhat.set_pixel(random_x_coordinate(), random_y_coordinate(), 0, 0, 0) unicornhat.show() time.sleep(0.01)
def handle(self): sock = self.request f = sock.makefile('rb') while True: hdr = f.read(MSG_HEADER.size) if not hdr: break channel, command, length = MSG_HEADER.unpack(hdr) data = f.read(length) if command == 0: for off in range(0, len(data), 3): y, x = divmod(off // 3, 64) if y > height or x > width: continue color = tuple(ord(c) for c in data[off:off + 3]) unicorn.set_pixel(y, 8 - x, color) unicorn.show() elif command == 2: print("Unsupported command %d" % command)
def pulse(): for b in range(0, 7): blockPrint() unicorn.brightness(b/10) enablePrint() for y in range(height): for x in range(width): unicorn.set_pixel(x, y, 102, 255, 255) unicorn.show() sleep(0.05) sleep(1) for b in range(6, 0, -1): blockPrint() unicorn.brightness(b/10) enablePrint() for y in range(height): for x in range(width): unicorn.set_pixel(x, y, 102, 255, 255) unicorn.show() sleep(0.05)
def hihat_splash(): unicorn.brightness(1) y_loc = randint(0, 2) x_loc = randint(0, 6) red = randint(192, 255) gre = randint(192, 255) blu = randint(192, 255) unicorn.set_pixel(x_loc, y_loc, red, gre, blu) unicorn.set_pixel((x_loc + 1), y_loc, red, gre, blu) unicorn.set_pixel((x_loc + 1), (y_loc + 1), red, gre, blu) unicorn.set_pixel(x_loc, (y_loc + 1), red, gre, blu) for p in range(3): red2 = randint(0, 255) gre2 = randint(0, 255) blu2 = randint(0, 255) unicorn.set_pixel(randint(0, 8), randint(0, 4), red2, gre2, blu2) unicorn.show() time.sleep(0.06) unicorn.clear() unicorn.show()
def mainprog(): "Display current time with pixels on a unicorn HAT." global disp t = time.strftime(f, time.localtime()) h1, h2, m1, m2 = [int(x) for x in t] disp = [[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 2, 1, 4, 4, 4, 1], [1, 1, 2, 1, 4, 4, 4, 1], [1, 1, 2, 1, 4, 4, 4, 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()
def flicker(hue, stop): """ Make the light flicker around the passed hue until told to stop. """ while True: rand_mat = numpy.random.rand(width, height) for y in range(height): for x in range(width): h = hue + (0.1 * rand_mat[x, y]) s = 1 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() time.sleep(0.05) if stop(): break
def randomizr(): for i in range(8): for j in range(8): color = [ random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) ] uh.set_pixel(i, j, color[0], color[1], color[2]) uh.show() while 1: color = [ random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) ] uh.set_pixel(random.randint(0, 7), random.randint(0, 7), color[0], color[1], color[2]) uh.show() time.sleep(.05)
def white_dot_knight_rider(): while True: y = 7 # last row for y in range(height): for x in range(width): #we can run into timing issue when we save the pixel it can change. so lets flush the history out periodically to fix if x == 7: draw_history_to_unicorn() #print("white dot loop") saved_pixel = unicorn.get_pixel(x, y) #print(pixel) unicorn.set_pixel(x, y, 200, 200, 200) unicorn.show() sleep(4.6875) unicorn.set_pixel(x, y, saved_pixel[0], saved_pixel[1], saved_pixel[2]) unicorn.show()
def ubars_display(): """update unicornhat with data from file""" displayfile_name = "/home/pi/unicorn.json" with open(displayfile_name, 'r') as openfile: position = json.load(openfile) for y in range(0, 8): for x in range(0, 8): if position[x][y] == 0: r, g, b = blank elif position[x][y] == 1: r, g, b = green elif position[x][y] == -1: r, g, b = red elif position[x][y] == 8: r, g, b = gray unicornhat.set_pixel(x, y, r, g, b) unicornhat.show() print(position)
def drawISS(lng, lat): lng = lng - origin[0] lat = lat - origin[1] x = abs(int((lng + 90) / 22.5)) if x > 7: x = 15 - x y = abs(int(((lat * -1) + 90) / 22.5)) print 'x-value: %i, y-value: %i' % (x, y) UH.clear() if (lng < -90) or (lng > 90): #'behind earth UH.set_pixel(x, y, 0, 0, 0xFF) UH.brightness(0.2) else: UH.set_pixel(x, y, 0xFF, 0xFF, 0) UH.brightness(0.4) UH.show()
def internet_color(update_rate=5): """ Color bar - tests internet connectivity. Displays white if connected; orange if not. Args: update_rate (float): seconds to wait before checking connectivity again """ # Ping a Google DNS server to check for internet connectivity. while True: ping_response = subprocess.Popen( ["/bin/ping", "-c1", "-w100", "8.8.8.8"], stdout=subprocess.PIPE).stdout.read() if "1 received" in str(ping_response): moving_pixel(function_pos[internet_color], (0, 255, 255), (255, 255, 255)) else: moving_pixel(function_pos[internet_color], (255, 255, 255), (255, 127, 80)) unicorn.show() time.sleep(update_rate)
def showPixels(): global pixels index = 0 if mode != 0: #green tree for coords in pattern: UH.set_pixel(coords[0],coords[1], 0, 0xFF, 0) for pixel in pixels: if mode == 0: coords = pattern[index] else: coords = lights[index] UH.set_pixel(coords[0],coords[1], pixel[0], pixel[1], pixel[2]) index += 1 if index >= maxPixels: #print index, maxPixels pixels = pixels[:maxPixels-1] #trim the list as we've maxed out break UH.show()
def showcapacity(bat, startanim): # Plot the battery level on the Unicorn Hat b = 0 for y in range(0, 6): batthreshold = int(16.666 * y) if y == 0: # Red r = 255 g = 0 if y == 1: r = 255 g = 128 if y == 2: r = 255 g = 192 if y == 3: r = 192 g = 255 if y == 4: r = 128 g = 255 if y == 5: # through to Green r = 0 g = 255 if bat < batthreshold: r = 0 g = 0 elif startanim == 1: unicornhat.set_pixel(4, 6 - y, 255, 255, 255) unicornhat.set_pixel(5, 6 - y, 255, 255, 255) unicornhat.set_pixel(6, 6 - y, 255, 255, 255) unicornhat.show() time.sleep(0.1) unicornhat.set_pixel(4, 6 - y, r, g, b) unicornhat.set_pixel(5, 6 - y, r, g, b) unicornhat.set_pixel(6, 6 - y, r, g, b) unicornhat.show() time.sleep(0.1) unicornhat.set_pixel(4, 6 - y, r, g, b) unicornhat.set_pixel(5, 6 - y, r, g, b) unicornhat.set_pixel(6, 6 - y, r, g, b)
def run(): unicorn.set_layout(unicorn.AUTO) unicorn.rotation(0) unicorn.brightness(1) 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) else: spectrum(x, y) if g > 9: g = -1 unicorn.show() time.sleep(0.8)
def drop_ball(): ball_colour = [randint(100, 255), randint(100, 255), randint(100, 255)] ball_column = randint(0, 5) while heights[ball_column] == 7: ball_column = randint(0, 5) height = heights[ball_column] ball_y = 7 uh.set_pixel(ball_column + 1, ball_y, ball_colour[0], ball_colour[1], ball_colour[2]) uh.show() dropcount = 6 - height for y in range(0, dropcount): uh.set_pixel(ball_column + 1, ball_y, 0, 0, 0) ball_y -= 1 uh.set_pixel(ball_column + 1, ball_y, ball_colour[0], ball_colour[1], ball_colour[2]) uh.show() time.sleep(0.02) heights[ball_column] += 1
def display_flag(): """ The function lights up the LEDs to create a picture of the American flag. """ flag = [ [B, B, B, B, R, R, R, R], [B, B, B, B, W, W, W, W], [R, R, R, R, R, R, R, R], [W, W, W, W, W, W, W, W], # Don't use the 4 rows below for UnicornPHAT [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF], [OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF] ] unicornhat.set_pixels(flag) unicornhat.show() pause()
def draw(): global ball, player1, player2, BG_COLOUR UH.off() #fill_hex(BG_COLOUR) # draw ball set_pixel(ball.x, ball.y, ball.colour) # draw player1 set_pixel_hue(0, player1.y, player1.hue) set_pixel_hue(0, player1.y + 1, player1.hue) set_pixel_hue(0, player1.y + 2, player1.hue) # draw player2 set_pixel_hue(7, player2.y, player2.hue) set_pixel_hue(7, player2.y + 1, player2.hue) set_pixel_hue(7, player2.y + 2, player2.hue) UH.show()
def paint(r, g, b, t): unicorn.set_layout(unicorn.HAT) unicorn.rotation(0) unicorn.brightness(0.5) width, height = unicorn.get_shape() blinkTime = .25 # .25 is the amount of seconds of the blink (open and closed) iterations = int(round(t / blinkTime, 0)) showColor = True # unicorn.off() # turn off all of the led's for i in range(iterations): for x in range(width): for y in range(width): if showColor: unicorn.set_pixel(x, y, r, g, b) # display the color else: unicorn.off() # turn off the color unicorn.show() time.sleep(blinkTime) showColor = not showColor # toggle the boolean value
def drop_ball(): ball_colour = [randint(100, 255), randint(100, 255), randint(100, 255)] ball_column = randint(0, (uh_width - 3)) while heights[ball_column] == (uh_height - 1): ball_column = randint(0, (uh_width - 3)) height = heights[ball_column] ball_y = (uh_height - 1) unicorn.set_pixel(ball_column + 1, ball_y, ball_colour[0], ball_colour[1], ball_colour[2]) unicorn.show() dropcount = (uh_height - 2) - height for y in range(0, dropcount): unicorn.set_pixel(ball_column + 1, ball_y, 0, 0, 0) ball_y -= 1 unicorn.set_pixel(ball_column + 1, ball_y, ball_colour[0], ball_colour[1], ball_colour[2]) unicorn.show() time.sleep(0.02) heights[ball_column] += 1
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 show_ip(timedel): for address in filter(None, commands.getoutput("hostname -I").split(" ")): show_icon(1) try: print "processing: ", address for octetidx, octet in enumerate(address.split(".")): bin_octet = "{0:08b}".format(int(octet)) for idx, c in enumerate(bin_octet[::-1]): if c == "0": unicorn.set_pixel(idx, octetidx, 0, 255, 0) elif c == "1": unicorn.set_pixel(idx, octetidx, 255, 0, 0) print "display: ", address unicorn.show() time.sleep(timedel) except: print "skip ipv6" pass unicorn.clear() unicorn.show() return ()
def set_color(value): # color RGB calues may be separated by comma, blank, semicolon, dash, colon in config colors = filter(None, re.split("[, ;\-:]", value)) if len(colors) == 3: # RGB values for k, v in enumerate(colors): try: colors[k] = int(v) except ValueError: colors[k] = 0 if colors[k] < 0: colors[k] = 0 if colors[k] > 255: colors[k] = 255 r, g, b = colors elif len(colors) == 1: # value in degrees Kelvin, only good between 1000..40000 try: colors[0] = int(colors[0]) except ValueError: colors[0] = 6504 # D65 if colors[0] < 1000: colors[0] = 1000 elif colors[0] > 40000: colors[0] = 40000 r, g, b = ct.color_temperature_to_rgb(colors[0]) else: # return early, no valid colors logger.info("Ignoring invalid color: " + value) return uh.set_all(r, g, b) uh.show() config.set(my_section, 'color', ','.join([str(r), str(g), str(b)])) status_topic = config.get(mqtt_section, 'base_topic') + config.get( my_section, 'client_topic') + 'status/color' mqttclient.publish(status_topic, config.get(my_section, 'color'), retain=True) logger.info("Color set to " + config.get(my_section, 'color'))
def pulse_color( color=(255, 255, 255), brightness=0.8, total_time=1, step_time=0.01): """ Set all LCDs to green. :param color: tuple representing color as (R,G,B) :param brightness: max brightness value of pulse (0-1) :param total_time: time for full color pulse to complete, in seconds :param step_time: step time between brightness updates, in seconds """ unicornhat.brightness(0) # initial pulse value is 0 unicornhat.set_all(color) unicornhat.show() # Calculate pulse data steps_half = int(total_time / step_time / 2) print('Half steps: {}'.format(steps_half)) delta_brightness = brightness / steps_half # Start pulse up for _ in range(steps_half): brightness = unicornhat.get_brightness() + delta_brightness if brightness > 1: brightness = 1 # ensure max limit (in case of steps_half rounding) unicornhat.brightness(brightness) unicornhat.show() time.sleep(step_time) # Start pulse down for _ in range(steps_half): brightness = unicornhat.get_brightness() - delta_brightness if brightness < 0: brightness = 0 # ensure min limit (in case of steps_half rounding) unicornhat.brightness(brightness) unicornhat.show() time.sleep(step_time)
def kscope(width, height, rgb): """ Freakin' Kaleidoscope F.X. """ choices = [0, 1] choice = random.choice(choices) shift = lsc.getshift() mytime = lsd.getduration() xcoord, ycoord = lsd.getcoords(width, height) for _ in range(mytime): if width == height: for thisrot in range(0, 360, 90): unicornhat.rotation(thisrot) unicornhat.set_pixel(xcoord, ycoord, rgb[0], rgb[1], rgb[2]) unicornhat.show() else: for thisrot in range(0, 270, 180): unicornhat.rotation(thisrot) unicornhat.set_pixel(xcoord, ycoord, rgb[0], rgb[1], rgb[2]) unicornhat.show() if choice == 0: rgb = lsc.warpcolour(rgb) else: rgb = lsc.shiftcolour(rgb, shift) time.sleep(1) xcoord, ycoord = lsd.getcoords(width, height) unicornhat.set_all(0, 0, 0) unicornhat.show()
def setColor(r, g, b, brightness, speed) : global crntColors, globalBlue, globalGreen, globalRed globalRed = r globalGreen = g globalBlue = b if brightness != '' : unicorn.brightness(brightness) for y in range(height): for x in range(width): unicorn.set_pixel(x, y, r, g, b) unicorn.show() if speed != '' : sleep(speed) unicorn.clear() crntT = threading.currentThread() while getattr(crntT, "do_run", True) : for y in range(height): for x in range(width): unicorn.set_pixel(x, y, r, g, b) unicorn.show() sleep(speed) unicorn.clear() unicorn.show() sleep(speed)
def rainbow(self): i = 0.0 offset = 30 while not self.stop_thread: if self.light_on: 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() self.display_cleared = False else: if not self.display_cleared: unicorn.clear() unicorn.show() self.display_cleared = True time.sleep(0.02) unicorn.clear() unicorn.show()
def draw_history_to_unicorn(): global history counter = 0 if unicorn_detected: yrange = range(height) if unicornhd_detected: yrange = range(height - 1, -1, -1) for y in yrange: for x in range(width): # if we have an item in history for this position if counter < len(history): # pull the data out of the history pm25_float_from_history = history[counter] rgb_dict = pm25_to_rgb(pm25_float_from_history) # set the pixel color to the purpleair color unicorn.set_pixel(x, y, rgb_dict["r"], rgb_dict["g"], rgb_dict["b"]) counter = counter + 1 # change unicorn brightness based on time of day hour = dt.datetime.now().hour #print("hour is " + str(hour)) if (hour >= 22 or hour < 8): if unicornhd_detected: unicorn.brightness(0.1) else: unicorn.brightness(0.3) else: unicorn.brightness(0.5) # flush the changes out to the unicorn hat unicorn.show()
def pibow_zero_horizontal_striper_shuffle_alternate(x_coordinate_list, y_coordinate_list): start_time = time.time() time.clock() seconds_elapsed = 0 while seconds_elapsed < 15: seconds_elapsed = time.time() - start_time unicornhat.clear() random.shuffle(x_coordinate_list) for x in x_coordinate_list: #Get the RGB color for the x coordinate if x == 0: r, g, b, = get_x_color(x0_color_tuple) if x == 1: r, g, b, = get_x_color(x1_color_tuple) if x == 2: r, g, b, = get_x_color(x2_color_tuple) if x == 3: r, g, b, = get_x_color(x3_color_tuple) if x == 4: r, g, b, = get_x_color(x4_color_tuple) if x == 5: r, g, b, = get_x_color(x5_color_tuple) if x == 6: r, g, b, = get_x_color(x6_color_tuple) if x == 7: r, g, b, = get_x_color(x7_color_tuple) # Light up selected x column, alternately if x_coordinate_list[x] == 0 or x_coordinate_list[x] == 2 or \ x_coordinate_list[x] == 4 or x_coordinate_list[x] == 6: y_coordinate_list = y_coordinates else: y_coordinate_list = y_coordinates[::-1] for y in y_coordinate_list: unicornhat.set_pixel(x, y, r, g, b) unicornhat.show() time.sleep(0.05) time.sleep(1.0)
def toggle_leds(leds, transition_color, new_color): orig_colors = [] for led in leds: orig_colors.append(unicornhat.get_pixel(led[0], led[1])) for n in range(NUM_TRANSITIONS): for l in range(len(leds)): this_led = leds[l] unicornhat.set_pixel(this_led[0], this_led[1], transition_color[0], transition_color[1], transition_color[2]) unicornhat.show() time.sleep(0.3) for l in range(len(leds)): this_led = leds[l] this_orig_color = orig_colors[l] unicornhat.set_pixel(this_led[0], this_led[1], this_orig_color[0], this_orig_color[1], this_orig_color[2]) unicornhat.show() time.sleep(0.3) if n == (NUM_TRANSITIONS - 1): for l in range(len(leds)): this_led = leds[l] unicornhat.set_pixel(this_led[0], this_led[1], new_color[0], new_color[1], new_color[2]) unicornhat.show()