def Erase(self,h1,v1): #This function draws a black sprite, erasing the sprite. This may be useful for #a future "floating over the screen" type of sprite motion #It is pretty fast now, seems just as fast as blanking whole screen using off() or clear() x = 0 y = 0 #print ("Erase:",self.width, self.height, self.r, self.g, self.b,v1,h1) for count in range (0,(self.width * self.height)): y,x = divmod(count,self.width) #print("Count:",count,"xy",x,y) if self.grid[count] == 1: if (CheckBoundary(x+h1,y+v1) == 0): #setpixel(x+h1,y+v1,0,0,0) setpixel(x+h1,y+v1,0,0,0) try: unicorn.show() except Exception as ErrorMessage: TheTrace = traceback.format_exc() print("") print("") print("--------------------------------------------------------------") print("ERROR - Unicorn.show") print(ErrorMessage) print("") #print("EXCEPTION") #print(sys.exc_info()) print("") print ("TRACE") print (TheTrace) print("--------------------------------------------------------------") print("") print("") unicorn.clear() ShowScrollingBanner("Display Error!",100,0,0,0.05)
def write_pixels(self, data): import unicornhathd as unicorn for y, row in enumerate((data * 255).astype(np.uint8)): for x, color in enumerate(row): unicorn.set_pixel(x, y, *color) unicorn.show()
def DisplayIncludeBlack(self,h1,v1): x = 0, y = 0 for count in range (0,(self.width * self.height)): y,x = divmod(count,self.width) if self.grid[count] == 1: if (CheckBoundary(x+h1,y+v1) == 0): setpixel(x+h1,y+v1,self.r,self.g,self.b) elif self.grid[count] == 0: if (CheckBoundary(x+h1,y+v1) == 0): setpixel(x+h1,y+v1,0,0,0) try: unicorn.show() except Exception as ErrorMessage: TheTrace = traceback.format_exc() print("") print("") print("--------------------------------------------------------------") print("ERROR - Unicorn.show") print(ErrorMessage) print("") #print("EXCEPTION") #print(sys.exc_info()) print("") print ("TRACE") print (TheTrace) print("--------------------------------------------------------------") print("") print("") unicorn.clear() ShowScrollingBanner("Display Error!",100,0,0,0.05)
def go(style=0): effects = [gradient, tunnel, rainbow_search, checker, swirl] effect = effects[style] step = 0 a_list = [] threading.Thread(target=input_thread, args=(a_list, )).start() i = 0 while ((not a_list) and (i < 100)): if i == 99: i = 0 else: i = i + 1 for y in range(u_height): for x in range(u_width): r, g, b = effect(x, y, step) r = int(max(0, min(255, r))) g = int(max(0, min(255, g))) b = int(max(0, min(255, b))) unicornhathd.set_pixel(x, y, r, g, b) step += 2 unicornhathd.show() unicornhathd.off()
def draw(): for x in range(0, view.w): for y in range(0, view.h): color = frame_buffer[x * view.w + y] unicornhathd.set_pixel(x, y, color.x * 255, color.y * 255, color.z * 255) unicornhathd.show()
def showSpans(spans): for (startTime, image, brightness) in spans: unicorn.brightness(brightness) showTime(startTime) curImage = showImage(image) unicorn.show() time.sleep(3)
def refreshDisplay(tempture, pressure): image = Image.new("RGB", (width, height), (0, 0, 0)) draw = ImageDraw.Draw(image) draw.text((0, -1), '{0:d}℃'.format(tempture), fill=COLOR, font=font) draw.text((0, 4), '{0:04d}'.format(pressure), fill=COLOR, font=font) draw.text((4, 10), 'hPa', fill=COLOR, font=font) unicornhathd.clear() # x, yを指定して1ドットずつ描写する for x in range(width): for y in range(height): r, g, b = image.getpixel((x, y)) # ここの部分でx軸を反転させている unicornhathd.set_pixel(width - x - 1, y, r, g, b) # 現在時刻の秒を基準にドットの点滅をさせる if datetime.datetime.now().second % 2: unicornhathd.set_pixel(15, 15, *COLOR) unicornhathd.set_pixel(15, 14, *COLOR) unicornhathd.set_pixel(14, 15, *COLOR) unicornhathd.set_pixel(14, 14, *COLOR) # 画面のリフレッシュ命令 unicornhathd.show()
def load(saveName): with open(os.path.join(SAVES_DIR, saveName), "rb") as f: pixels = pickle.load(f) for x, row in enumerate(pixels): for y, pixel in enumerate(row): unicorn.set_pixel(x, y, *pixel) unicorn.show()
def calibrate_preview(): '''Opens a preview for user focusing, then takes series of pics for averaging and background subtraction''' with picamera.PiCamera() as camera: camera.resolution = (1664, 1232) camera.awb_mode = 'off' camera.awb_gains = (1.2, 1.2) camera.framerate = 30 camera.start_preview() pihat.brightness(1.0) pihat.clear() for y in range(16): for x in range(16): v = display[x, y] # brightness depends on range if v == 0: red = int(255) # makes 0-1 range > 0-255 range green = int(255) blue = int(255) elif v == 1: red = int(0) green = int(0) blue = int(0) elif v == 2: red = int(0) green = int(0) blue = int(0) pihat.set_pixel(x, y, red, green, blue) # sets pixels on the hat pihat.rotation(180) pihat.show() # show the pixels GPIO.output(VALVE, True) time.sleep(20) GPIO.output(VALVE, False) pihat.clear() pihat.off() camera.stop_preview()
def plot(grid: np.ndarray): if not u: return u.clear() for x, y in zip(*np.where(grid[:, :, 2] > 0.)): u.set_pixel_hsv(x, y, *grid[x, y]) u.show()
def run(imgFile='lofi.png'): unicornhathd.rotation(0) unicornhathd.brightness(0.6) width, height = unicornhathd.get_shape() img = Image.open(imgFile) a_list = [] threading.Thread(target=input_thread, args=(a_list, )).start() while not a_list: for o_x in range(int(img.size[0] / width)): for o_y in range(int(img.size[1] / height)): valid = False for x in range(width): for y in range(height): pixel = img.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 unicornhathd.set_pixel(x, y, r, g, b) if valid: unicornhathd.show() time.sleep(0.5) unicornhathd.off()
def display_animation(image, brightness): #displays a animation as multi png unicornhathd.rotation(90) unicornhathd.brightness(brightness) width, height = unicornhathd.get_shape() img = Image.open(image) try: while True: for o_x in range(int(img.size[0] / width)): for o_y in range(int(img.size[1] / height)): valid = False for x in range(width): for y in range(height): pixel = img.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 unicornhathd.set_pixel(x, y, r, g, b) if valid: unicornhathd.show() time.sleep(1) except KeyboardInterrupt: unicornhathd.off() return
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 doCPU(): cpu = psutil.cpu_times_percent() u = cpu.user + cpu.nice s = cpu.system + cpu.steal + cpu.guest_nice + cpu.guest w = cpu.irq + cpu.iowait + cpu.softirq i = cpu.idle # Cumulative ac = s bc = ac + w cc = bc + u tot = cc + i aIdx = 16 * (ac / 100) bIdx = 16 * (bc / 100) cIdx = 16 * (cc / 100) uhd.clear() def set_row(y, r, g, b): for x in range(5, 10): uhd.set_pixel(x, y, r, g, b) for y in range(16): if y <= aIdx: set_row(y, 70, 0, 0) elif y <= bIdx: set_row(y, 0, 0, 150) elif y <= cIdx: set_row(y, 0, 100, 0) else: set_row(y, 0, 0, 0) uhd.show()
def __init__(self): unicornhathd.clear() unicornhathd.set_all(10, 0, 10) unicornhathd.show() unicornhathd.rotation(270) # Rotation unicornhathd.brightness(.75) unicornhathd.show()
def _animate_icon(self, image, repeat=3, cycle_time=0.10): if image == None or type(image) is str == False: print("Not a string:", image) return self._lock_ui.acquire() for i in range(0, repeat): # this is the original pimoroni function for drawing sprites for o_x in range(int(image.size[0] / self._unicorn_width)): for o_y in range(int(image.size[1] / self._unicorn_height)): valid = False for x in range(self._unicorn_width): for y in range(self._unicorn_height): pixel = image.getpixel(((o_x * self._unicorn_width) + y, (o_y * self._unicorn_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((self._unicorn_height - y - 1), x, r, g, b) if valid: unicorn.show() time.sleep(cycle_time) unicorn.off() self._lock_ui.release()
def doClock(clock, spans, stepTime=0.04, showClockTime=datetime.timedelta(seconds=60)): tick = 0 prev = datetime.datetime.fromtimestamp(0) while True: if tick == 0: today = clock.today().replace(hour=0, minute=0, second=0, microsecond=0).date() now = clock.now() # Every minute, show the time if now - prev >= showClockTime: showTime(now) prev = now curSpan = None for (startTime, image, brightness) in spans: st = clock.combine(today, startTime) if st < now: curSpan = (startTime, image, brightness) if curSpan is None: curSpan = spans[-1] (_, image, brightness) = curSpan unicorn.brightness(brightness) curImage = showImage(image) # drawComet(tick, (255, 0, 0), 20, curImage) blinkEye(tick, (255, 0, 0), (20, 0, 0), 10, 3) tick += 1 if (tick >= (width*4)-3): # That is, has gone all the way around. tick = 0 time.sleep(stepTime) unicorn.show()
def run(self): while self.is_alive(): unicornhathd.clear() for y in range(0, 16): for x in range(0, 16): if y == self.scanline: unicornhathd.set_pixel(x, y, 0, 0, 32) if (y + 1) % 16 == self.scanline: unicornhathd.set_pixel(x, y, 0, 0, 16) if (y + 2) % 16 == self.scanline: unicornhathd.set_pixel(x, y, 0, 0, 8) if (y + 3) % 16 == self.scanline: unicornhathd.set_pixel(x, y, 0, 0, 4) if self.blips[x][y] > 0: self.set_green(x, y, self.blips[x][y]) self.blips[x][y] -= 1 for ac in airport_coords: c = self.gps_to_matrix_coords(ac) self.set_red(c[1], c[0], 128) ref = self.gps_to_matrix_coords(center_coord) self.set_red(ref[1], ref[0], 16) unicornhathd.show() self.scanline = (self.scanline + 1) % 16 time.sleep(1.0)
def show(): message = os.environ.get('MESSAGE') lines = message.split(',') colours = [ tuple([ int(n * 255) for n in colorsys.hsv_to_rgb(x / float(len(lines)), 1.0, 1.0) ]) for x in range(len(lines)) ] FONT = ("/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf", 10) unicornhathd.rotation(0) unicornhathd.brightness(1.0) width, height = unicornhathd.get_shape() text_x = width text_y = 2 font_file, font_size = FONT font = ImageFont.truetype(font_file, font_size) text_width, text_height = width, 0 for line in lines: w, h = font.getsize(line) text_width += w + width text_height = max(text_height, h) text_width += width + text_x + 1 image = Image.new("RGB", (text_width, max(16, text_height)), (0, 0, 0)) draw = ImageDraw.Draw(image) offset_left = 0 for index, line in enumerate(lines): draw.text((text_x + offset_left, text_y), line, colours[index], font=font) offset_left += font.getsize(line)[0] + width for scroll in range(text_width - width): for x in range(width): for y in range(height): pixel = image.getpixel((x + scroll, y)) r, g, b = [int(n) for n in pixel] unicornhathd.set_pixel(width - 1 - x, y, r, g, b) unicornhathd.show() time.sleep(0.01) unicornhathd.off() return True
def displayFour(x, y): clearNumberPixels(x, y) bothSides(x, y) bothSides(x, y - 1) fullLine(x, y - 2) rightSide(x, y - 3) rightSide(x, y - 4) unicorn.show()
def displayDateDots(x, y): unicorn.set_pixel(x + 1, y, 255, 0, 0) unicorn.set_pixel(x + 1, y - 1, 255, 0, 0) unicorn.set_pixel(x + 1, y - 2, 255, 0, 0) unicorn.set_pixel(x, y - 2, 255, 0, 0) unicorn.set_pixel(x, y - 3, 255, 0, 0) unicorn.set_pixel(x, y - 4, 255, 0, 0) unicorn.show()
def displayZero(x, y): clearNumberPixels(x, y) fullLine(x, y) bothSides(x, y - 1) bothSides(x, y - 2) bothSides(x, y - 3) fullLine(x, y - 4) unicorn.show()
def firework_tail(i, j, r, g, b): # Make the firework fiery tail. for x in range(15, j, -1): unicorn.set_pixel(i, x, r, g, b) sleep(SPEED) # Tidy the tail once the tail has gone all the way. tidy_tail(i, j) unicorn.show()
def displaySix(x, y): clearNumberPixels(x, y) fullLine(x, y) leftSide(x, y - 1) fullLine(x, y - 2) bothSides(x, y - 3) fullLine(x, y - 4) unicorn.show()
def emoji_show(emoji): R = np.load('rgb/' + emoji + '_R.npy') G = np.load('rgb/' + emoji + '_G.npy') B = np.load('rgb/' + emoji + '_B.npy') for x in range(0, 16): for y in range(0, 16): unicornhathd.set_pixel(x, y, R[x][y], G[x][y], B[x][y]) unicornhathd.show()
def blit(image, offset): """Show image pixels on the Unicorn hat""" for x in range(hat_width): for y in range(hat_height): pixel = image.getpixel((x + offset, y)) r, g, b = [int(n) for n in pixel] unicorn.set_pixel(hat_width - 1 - x, y, r, g, b) unicorn.show()
def DrawSun(r, g, b): pixels = GetPixels(r, g, b) for x in range(unicornhathd.WIDTH): for y in range(unicornhathd.HEIGHT): red, green, blue = pixels[x][y] unicornhathd.set_pixel(x, y, red, green, blue) unicornhathd.show() time.sleep(2.0)
def displayThree(x, y): clearNumberPixels(x, y) fullLine(x, y) rightSide(x, y - 1) fullLine(x, y - 2) rightSide(x, y - 3) fullLine(x, y - 4) unicorn.show()
def displayOne(x, y): clearNumberPixels(x, y) rightSide(x, y) rightSide(x, y - 1) rightSide(x, y - 2) rightSide(x, y - 3) rightSide(x, y - 4) unicorn.show()
def display_text(lines, colors): # Use `fc-list` to show a list of installed fonts on your system, # or `ls /usr/share/fonts/` and explore. FONT = ("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 12) # sudo apt install fonts-droid #FONT = ("/usr/share/fonts/truetype/droid/DroidSans.ttf", 12) # sudo apt install fonts-roboto #FONT = ("/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf", 10) unicornhathd.rotation(90) unicornhathd.brightness(1.0) width, height = unicornhathd.get_shape() text_x = width text_y = 2 font_file, font_size = FONT font = ImageFont.truetype(font_file, font_size) text_width, text_height = width, 0 for line in lines: w, h = font.getsize(line) text_width += w + width text_height = max(text_height, h) text_width += width + text_x + 1 image = Image.new("RGB", (text_width, max(16, text_height)), (0, 0, 0)) draw = ImageDraw.Draw(image) offset_left = 0 for index, line in enumerate(lines): draw.text((text_x + offset_left, text_y), line, colors[index], font=font) offset_left += font.getsize(line)[0] + width for scroll in range(text_width - width): for x in range(width): for y in range(height): pixel = image.getpixel((x + scroll, y)) r, g, b = [int(n) for n in pixel] unicornhathd.set_pixel(width - 1 - x, y, r, g, b) unicornhathd.show() time.sleep(0.02) unicornhathd.off()
def unicorn_init(): unicorn.brightness(BRIGHTNESS) if UNICORN_VERSION == "HD": unicorn.rotation(0) elif UNICORN_VERSION == "SD": unicorn.rotation(90) else: log_string('No valid Unicorn Version found in config file - use "SD" or "HD"') unicorn.clear() unicorn.show()
def showImage(imageFile): pixels = Image.open(imageFile) image = [[(0, 0, 0) for x in range(width)] for y in range(height)] for x in range(width): for y in range(height): pixel = pixels.getpixel((x, y)) r, g, b = int(pixel[0]),int(pixel[1]),int(pixel[2]) image[x][y] = (r, g, b) setPixel(x, y, r, g, b) unicorn.show() pixels.close() return image
def main(): # spans = [ # (datetime.time(7, 00, 00), 'bb82.png', 0.2), # (datetime.time(19, 30, 00), 'stormtrooper3.png', 0.05), # ] spans = [ (datetime.time(7, 00, 00), 'bb9e-day.png', 0.02), (datetime.time(19, 00, 00), 'bb9e-night.png', 0.02), ] showSpans(spans) doClock(datetime.datetime, spans) while True: unicorn.show() time.sleep(1)
def slideShow(imageList, delay): max_x = width * len(imageList) pixels = loadImages(imageList) x_offset = 0 while True: if (x_offset % width) == 1: unicorn.show() time.sleep(5 * delay) for x in range(width): for y in range(height): px = (x + x_offset) % max_x py = y (r, g, b, a) = pixels[px][py] setPixel(x, y, r, g, b) unicorn.show() time.sleep(delay) x_offset += 1
def scroll(bitmap, color, stepTime, yoffset): h = len(bitmap) w = len(bitmap[0]) r, g, b = color for xoffset in range(w): for x in range(width): for sy in range(h): if x+xoffset < w: pixel = bitmap[sy][x+xoffset] else: pixel = False if pixel: setPixel(x, sy+yoffset, r, g, b) else: setPixel(x, sy+yoffset, 0, 0, 0) time.sleep(stepTime) unicorn.show()
x_or_y += dx_or_dy return x_or_y, dx_or_dy print("Press <CTRL+C> to exit...") unicornhathd.off() # Bounce backwards and forwards along each edge: p1 = Point(0, 0, 0, 1) p2 = Point(0, 15, 1, 0) p3 = Point(15, 0, -1, 0) p4 = Point(15, 15, 0, -1) p1.turn_on() p2.turn_on() p3.turn_on() p4.turn_on() unicornhathd.show() try: while True: p1.move() p2.move() p3.move() p4.move() unicornhathd.show() sleep(0.1) except KeyboardInterrupt: pass unicornhathd.off()
# if angle > 2 * math.pi: # angle = 0.0 if angle in blips: x, y = blips[angle] print 'Deleting blip: %d %d %f' % (x, y, angle) unicorn.set_pixel(x, y, 0, 0, 0) del blips[angle] drawLine(angle-0.3, 0, 50, 0) drawLine(angle-0.2, 0, 100, 0) drawLine(angle-0.1, 0, 150, 0) drawLine(angle, 0, 255, 0) makeBlip(angle) for blipangle, (x, y) in blips.items(): dim = int((angle - blipangle) * 30) if dim < 0: dim = 0 if dim > 255: dim = 255 unicorn.set_pixel(x, y, 255 - dim, 0, 0) unicorn.show() time.sleep(0.02) drawLine(angle-0.3, 0, 0, 0) drawLine(angle-0.2, 0, 0, 0) drawLine(angle-0.1, 0, 0, 0) drawLine(angle, 0, 0, 0)