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 langtonant(): #for i in range(32): # uh.set_pixel(random.randint(0,7), random.randint(0,7), 255, 255, 255) antpos = [random.randint(0, 7), random.randint(0, 7)] antdir = random.randint(0, 3) #0 up, 1 r, 2 down, 3 l while 1: atpos = uh.get_pixel(antpos[0], antpos[1]) if atpos == (255, 255, 255): antdir = (antdir + 1) % 4 uh.set_pixel(antpos[0], antpos[1], 0, 0, 0) print "white", antdir, antpos[0], antpos[1] elif atpos == (0, 0, 0): antdir = (antdir - 1) % 4 uh.set_pixel(antpos[0], antpos[1], 255, 255, 255) print "black", antdir, antpos[0], antpos[1] if antdir == 0: antpos = [antpos[0] % 8, (antpos[1] + 1) % 8] if antdir == 1: antpos = [(antpos[0] + 1) % 8, (antpos[1]) % 8] if antdir == 2: antpos = [antpos[0] % 8, (antpos[1] - 1) % 8] if antdir == 3: antpos = [(antpos[0] - 1) % 8, antpos[1] % 8] uh.show() time.sleep(.005)
def random_pixel(color_function): """ Generate a randomly positioned pixel with the color returned by color_function. Args: color_function (func): Should return a (R,G,B) color value. """ color = color_function() def random_position(): """ Get the position of a random pixel bound by function_pos. """ x = randint(0, function_pos[load_sparkles]) y = randint(0, (height-1)) return (x,y) selected_pixel = random_position() ''' Aesthetic: If the randomly generated pixel is currently lit, turn it off and try with a new pixel. Also works as sort of a population control on how many pixels will be lit. ''' while sum(unicorn.get_pixel(*selected_pixel)) > 0: unicorn.set_pixel(*(selected_pixel + (0, 0, 0))) selected_pixel = random_position() unicorn.set_pixel(*(selected_pixel + color)) return
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()
def query_led_status(led): pos = get_led_position(led) r, g, b = unicornhat.get_pixel(pos[0], pos[1]) toggle_leds([pos], COLOR_BIT_QUERYING, (r, g, b)) return not (r == 0 and g == 0 and b == 0)
def uniformr(): while 1: pix = int(abs(random.gauss(4, 2))) oc = uh.get_pixel(0, pix) for i in range(8): uh.set_pixel(i, pix, oc[0] + 1, oc[1] + 1, oc[2] + 1) uh.show()
def get_led_color(): color = uh.get_pixel(0, 0) return { "red": color[0], "green": color[1], "blue": color[2], }, http.HTTPStatus.OK
def status_light(self, blink_color, col=0, row=3, blinks=3, duration=0.4, pause=0.6): org_color = unicorn.get_pixel(col, row) for b in range(blinks): self.set_pixel(col, row, blink_color) self.show() time.sleep(duration) self.set_pixel(col, row, org_color) self.show() time.sleep(pause)
def walk(width, height, rgb, xcoord, ycoord): """ Take the pixel for a walk stepwise """ choices_advance = [-1, 0, 1] choice_x = random.choice(choices_advance) choice_y = random.choice(choices_advance) newx = xcoord + choice_x newy = ycoord + choice_y if newx < 0: newx = width -1 if newx > width - 1: newx = 0 if newy < 0: newy = height -1 if newy > height - 1: newy = 0 target_rgb = unicornhat.get_pixel(newx, newy) if target_rgb == rgb: return newx, newy else: return xcoord, ycoord
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 pixel_blink(self, col, row, blink_color, time_delta, steps, loops): start_color = unicorn.get_pixel(col, row) color_list = [] for i, c in enumerate(start_color): clist = np.linspace(start_color[i], blink_color[i], steps) clist = clist.tolist() color_list.append([int(r) for r in list]) for l in range(loops): for step in range(steps): self.set_pixel(col, row, (color_list[0][step], color_list[1][step], color_list[2][step])) unicorn.show() time.sleep(time_delta) for step in range(steps, 0, -1): self.set_pixel( col, row, (color_list[0][step - 1], color_list[1][step - 1], color_list[2][step - 1])) unicorn.show() time.sleep(time_delta)
def white_dot_loop(): global height global width global fetch_every_seconds sleep_time = fetch_every_seconds / float(height * width) print("fetch_every_seconds is " + str(fetch_every_seconds)) print("height is " + str(height)) print("width is " + str(width)) print("height * width is " + str(height * width)) print("sleep_time is " + str(sleep_time)) while True: if unicorn_detected: yrange = range(height) if unicornhd_detected: yrange = range(height - 1, -1, -1) for y in yrange: 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 # this is a weak fix to my multi threaded bug with the white dot. lets turn this off and fix the issue another way #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(sleep_time) unicorn.set_pixel(x, y, saved_pixel[0], saved_pixel[1], saved_pixel[2]) unicorn.show()
def generate_chart(clean_data, color, ripple, orientation, lowlight): info_chart = [] domain_min = clean_data[0][0] domain_max = clean_data[0][0] ad_min = clean_data[0][1] ad_max = clean_data[0][1] #calculate minimum, maximum, and interval values to scale graph appropriately for i in clean_data: if i[0] > domain_max: domain_max = i[0] elif i[0] < domain_min: domain_min = i[0] if i[1] > ad_max: ad_max = i[1] elif i[1] < ad_min: ad_min = i[1] domain_interval = (domain_max - domain_min) / 8 ad_interval = (ad_max - ad_min) / 8 #append scaled values to new list for i in clean_data: info_chart.append([int((i[0] - domain_min) / domain_interval) if domain_interval > 0 \ else 0, int((i[1] - ad_min) / ad_interval) if ad_interval > 0 else 0]) info_chart = list(reversed(info_chart[:8])) #set pixel values on rgb display for col in range(0, 8): if info_chart[col][0] > 0: for row in range(0, info_chart[col][0]): #if color not set, default to red for all values if color == 'traffic': if ripple: unicorn.set_pixel(row, 7 - col, 0, 0, 0) unicorn.show() time.sleep(RIPPLE_SPEED) unicorn.set_pixel(row, 7 - col, color_dict_pix(info_chart[col][0], 0), color_dict_pix(info_chart[col][0], 1), color_dict_pix(info_chart[col][0], 2)) elif color == 'ads': if ripple: unicorn.set_pixel(row, 7 - col, 0, 0, 0) unicorn.show() time.sleep(RIPPLE_SPEED) unicorn.set_pixel(row, 7 - col, color_dict_pix(info_chart[col][1], 0), color_dict_pix(info_chart[col][1], 1), color_dict_pix(info_chart[col][1], 2)) elif color == 'basic': if ripple: unicorn.set_pixel(row, 7 - col, 0, 0, 0) unicorn.show() time.sleep(RIPPLE_SPEED) unicorn.set_pixel(row, 7 - col, 255, 0, 0) for row in range(info_chart[col][0], 8): unicorn.set_pixel(row, 7 - col, 0, 0, 0) if ripple: time.sleep(RIPPLE_SPEED) unicorn.show() unicorn.show() else: for row in range(0, 8): if not all(v is 0 for v in unicorn.get_pixel(row, 7 - col)): if ripple: unicorn.set_pixel(row, 7 - col, 0, 0, 0) unicorn.show() time.sleep(RIPPLE_SPEED) unicorn.set_pixel(row, 7 - col, 0, 0, 0) unicorn.show()
def old_color(self): return (uh.get_pixel(self.x_pos, self.y_pos))
def getColor(self): return uh.get_pixel(0, 0)
def btnPress(x, y): global lastSelectedx global lastSelectedy sldRed.value, sldGreen.value, sldBlue.value = uni.get_pixel(x, y) lastSelectedx, lastSelectedy = x, y
def drop(self): u.set_pixel(self.x, self.y, self.last[0], self.last[1], self.last[2]) self.x += 1 self.last = u.get_pixel(self.x, self.y) u.set_pixel(self.x, self.y, 255, 255, 255) u.show()
def __init__(self): self.x = 0 self.y = randint(0, 3) self.last = u.get_pixel(self.x, self.y)
print x, y, r, g, b, rem_r, rem_g, rem_b uh.set_pixel(x, y, r, g, b) uh.set_pixel(x + 1, y + 1, rem_r, rem_g, rem_b) def rotate(x, y, angle): angle = math.pi / 2.0 xs = x * math.cos(angle) + y * math.sin(angle) ys = -x * math.sin(angle) + y * math.cos(angle) return xs, ys if __name__ == "__main__": step = 0 setpix = [[4, 3], [4, 4], [5, 3], [5, 4]] for s in setpix: uh.set_pixel(s[0], s[1], 0, 0, 255) uh.show() time.sleep(2) angle = 0 for x in range(8): for y in range(8): if uh.get_pixel(x, y) != (0, 0, 0): uh.set_pixel(x, y, 0, 0, 0) xs, ys = rotate(x, y, angle) set_half_pixel(xs, ys, 0, 0, 255) uh.show() time.sleep(5)
def get_pixel(self, coord): r, g, b = unicornhat.get_pixel(coord[0], coord[1]) return (r, g, b)
def isPixelAlreadySet(self, x, y, r, g, b): if not self.isInsideScreen(x, y): return True oldR, oldG, oldB = unicorn.get_pixel(x, y) return oldR == r and oldG == g and oldB == b
def getPixel(self, x, y): if not self.isInsideScreen(x, y): return return unicorn.get_pixel(x, y)
def get_pixel(self, x, y): return hat.get_pixel(x, y)
def color(self, red, green, blue, write=True, length=1): distance = 255 resolution = 60 max_movement = round(distance / resolution) if red < self.min_val: red = self.min_val if green < self.min_val: green = self.min_val if blue < self.min_val: blue = self.min_val if write: self.red = red self.green = green self.blue = blue grid = uh.get_pixels() max_distance = 1 for y in grid: for x in y: r_dist = abs(x[0] - red) g_dist = abs(x[1] - green) b_dist = abs(x[2] - blue) if r_dist > max_distance: max_distance = r_dist if g_dist > max_distance: max_distance = g_dist if b_dist > max_distance: max_distance = b_dist steps = [[[math.ceil((abs(x[0] - red) / max_distance) * max_movement), math.ceil((abs(x[1] - green) / max_distance) * max_movement), math.ceil((abs(x[2] - blue) / max_distance) * max_movement)] for x in y] for y in grid] self.tasked = True color_loop = True while color_loop: work = False for x in range(8): for y in range(4): r, g, b = uh.get_pixel(x, y) red_step = steps[y][x][0] red_dist = abs(red - r) green_step = steps[y][x][1] green_dist = abs(green - g) blue_step = steps[y][x][2] blue_dist = abs(blue - b) if red_dist < red_step: r = red elif r < red: r = r + red_step work = True elif r > red: r = r - red_step work = True if green_dist < green_step: g = green elif g < green: g = g + green_step work = True elif g > green: g = g - green_step work = True if blue_dist < blue_step: b = blue elif b < blue: b = b + blue_step work = True elif b > blue: b = b - blue_step work = True uh.set_pixel(x, y, r, g, b) if not work: color_loop = False uh.show() time.sleep(length / resolution) self.tasked = False