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 run_ticker(bg_color, text): FONT = ("DejaVuSans-Bold.ttf", 10) unicornhathd.rotation(90) unicornhathd.brightness(0.6) 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 w, h = font.getsize(text) 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)), color) draw = ImageDraw.Draw(image) offset_left = 0 draw.text((text_x + offset_left, text_y), text, fill="white", font=font) offset_left += font.getsize(text)[0] + width while True: 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)
def __init__(self, rotation): unicornhathd.brightness(0.6) unicornhathd.clear() unicornhathd.rotation(rotation) #(x|colonne, y|lignes) (0,0): en bas à gauche self.c_gris_fonce = [20,20,10] self.c_rouge = [255,0,0] self.c_orange = [240,150,28] self.c_vert = [0,255,0] self.c_bleu = [23,7,238] self.c_jaune = [100,100,0] self.hue_min = 0.33 # color HSV: vert pour un niveau 0% self.hue_max = 1.0 # color HSV: rouge pour un niveau 100% self.hue_delta = self.hue_max - self.hue_min # calculé une fois pour toutes self.msg = Msg() #initialisation de l'affichage self.animation_start() self.draw_titles(self.c_orange) #dessine les titres persistants 4 premières lignes self.draw_level(0,1,6) #fond niveau CPU0 self.draw_level(0,2,6) #fond niveau CPU1 self.draw_level(0,3,6) #fond niveau CPU2 self.draw_level(0,4,6) #fond niveau CPU3 self.draw_level(0,7,6) #fond niveau RAM self.draw_fullbox(10, 6, 14, 10 ,self.c_gris_fonce) #fond niveau DISK self.draw_level(0,1,0) #fond niveau T° unicornhathd.show()
def __init__(self): ''' @see ByComponentNotifier.__init__() ''' super(UnicornHatNotifier, self).__init__() # Unicorn settings unicornhathd.rotation(0) (w, h) = unicornhathd.get_shape() self._width = w self._height = h # The time, since epoch, when each component type stopped being active self._input_time = 0 self._service_time = 0 self._output_time = 0 # The direction of the swirl self._input_dir = 0 self._service_dir = 0 self._output_dir = 0 # The currently non-idle components self._inputs = set() self._services = set() self._outputs = set()
def printMessage(): unicornhathd.rotation(90) 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.02)
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_remote_image(url: str, brightness: float = 0.3) -> None: import unicornhathd response = requests.get(url) source = Image.open(BytesIO(response.content)) unicornhathd.rotation(0) unicornhathd.brightness(brightness) width, height = unicornhathd.get_shape() sat_booster = ImageEnhance.Color(source) img = sat_booster.enhance(1.25) # increase contrast of image contr_booster = ImageEnhance.Contrast(img) img = contr_booster.enhance(1.2) # reduce the number of colors used in picture img = img.convert("P", palette=Image.ADAPTIVE, colors=10) img = source.resize((width, height), resample=Image.BICUBIC) img = ImageOps.mirror(img) for x in range(width): for y in range(height): pixel = img.getpixel((x, y)) r, g, b = int(pixel[0]), int(pixel[1]), int(pixel[2]) unicornhathd.set_pixel(x, y, r, g, b) unicornhathd.show()
def show(): text = open('./isaax-project.env', 'r').readlines()[0] matchObj = re.match(r'.*?="(.*)"$', text) lines = matchObj.group(1).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 UnicornImage(): print("Showing image " + conf.IMAGE_NAME) unicorn.rotation(0) lenght = conf.IMAGE_LENGHT current_pos = 1 width, height = unicorn.get_shape() img = Image.open(conf.IMAGE_NAME) while current_pos <= lenght: 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 unicorn.set_pixel(x, y, r, g, b) if valid: unicorn.show() time.sleep(0.5) current_pos = current_pos + 1 unicorn.off() return 0
def mainloop(config): unicornhathd.rotation(config["unicornhathd"]["rotation"]) unicornhathd.brightness(config["unicornhathd"]["brightness"]) rss_feeds = [] rss_feeds.append("https://feeds.bbci.co.uk/news/rss.xml") rss_feeds.append("https://feeds.bbci.co.uk/news/world/rss.xml") rss_feeds.append("https://feeds.bbci.co.uk/news/uk/rss.xml") rss_feeds.append("https://feeds.bbci.co.uk/news/business/rss.xml") rss_feeds.append("https://feeds.bbci.co.uk/news/politics/rss.xml") rss_feeds.append("https://feeds.bbci.co.uk/news/health/rss.xml") rss_feeds.append("https://feeds.bbci.co.uk/news/education/rss.xml") rss_feeds.append( "https://feeds.bbci.co.uk/news/science_and_environment/rss.xml") rss_feeds.append("https://feeds.bbci.co.uk/news/technology/rss.xml") rss_feeds.append( "https://feeds.bbci.co.uk/news/entertainment_and_arts/rss.xml") # rss_feeds.append("http://www.eurogamer.net/?format=rss&platform=PS4") rss = [] while True: for rss_feed in rss_feeds: xml_data = get_xml_request(rss_feed) if xml_data: rss.extend(get_data_list(xml_data)) for text in rss: output_text = u"{}: {} ==> {}".format( time.strftime("%d/%m/%Y %H:%M:%S"), text[0], text[1]) scroll_text(output_text) time.sleep(0.25)
def __init__(self): unicornhathd.clear() unicornhathd.set_all(10, 0, 10) unicornhathd.show() unicornhathd.rotation(270) # Rotation unicornhathd.brightness(.75) unicornhathd.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 init(): print('initializing..') unicornhathd.clear() unicornhathd.rotation(270) unicornhathd.brightness(1.0) scroll_text("Hola! Hola! Hola! Hola!", NEUTRAL) unicornhathd.brightness(0.5)
def UnicornStarfield(): print("Running starfield") unicorn.rotation(270) lenght = conf.STARFIELD_LENGHT current_pos = 1 star_count = 25 star_speed = 0.05 stars = [] for i in range(0, star_count): stars.append((random.uniform(4, 11), random.uniform(4, 11), 0)) while current_pos <= lenght: unicorn.clear() for i in range(0, star_count): stars[i] = (stars[i][0] + ((stars[i][0] - 8.1) * star_speed), stars[i][1] + ((stars[i][1] - 8.1) * star_speed), stars[i][2] + star_speed * 50) if stars[i][0] < 0 or stars[i][1] < 0 or stars[i][0] > 16 or stars[ i][1] > 16: stars[i] = (random.uniform(4, 11), random.uniform(4, 11), 0) v = stars[i][2] unicorn.set_pixel(stars[i][0], stars[i][1], v, v, v) unicorn.show() current_pos = current_pos + 1 #print(current_pos) unicorn.clear() unicorn.off() return 0
def draw(): # Clear the display unicornhathd.off() # Clear the buffer unicornhathd.clear() # Set the rotation of the display unicornhathd.rotation(270) time.sleep(0.5) drawPurpleRow(2) drawPurpleRow(3) drawPurpleRow(4) drawPurpleRow(5) drawBlueRow(6) drawBlueRow(7) drawPurpleRow(8) drawPurpleRow(9) drawPurpleRow(10) drawPurpleRow(11) drawBow() unicornhathd.show() time.sleep(5)
def loop(event): unicornhathd.rotation(90) logger.debug('weather loop stert.') while not event.is_set(): main(event) logger.debug('weather loop end.')
def __init__(self, oversample): self.oversample = oversample self.size = 16 * self.oversample self.initGrid() # fire up the unicorn hat unicorn.rotation(90) unicorn.brightness(1) self.width, self.height = unicorn.get_shape()
def loop(event, city): unicornhathd.rotation(90) logger.debug('{} loop stert.'.format(city)) while not event.is_set(): main(event, city) logger.debug('{} loop end.'.format(city))
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 __init__(self): self._lock_ui = _thread.allocate_lock() unicorn.rotation(270) unicorn.brightness(0.5) self._unicorn_width, self._unicorn_height = unicorn.get_shape() self._default_font_file = 'fonts/Hack-Regular.ttf' self._default_font_size = 12 self._default_font = ImageFont.truetype(self._default_font_file, self._default_font_size)
def __init__(self): unicorn.brightness(1) unicorn.rotation(180) self.xDim = 16 self.yDim = 16 self.xMax = self.xDim -1 self.xMin = 0 self.yMax = self.yDim -1 self.yMin = 0 self.debugMe = False
def scroll_txt(image, text_width): unicornhathd.rotation(0) 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.2) 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 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 show_pixel_image(pixels, colours): for row in range(0, len(pixels)): for col in range(0, len(pixels[row])): pixel = pixels[row][col] if pixel > 0: rgb = colours[pixel] unicornhathd.set_pixel(col, row, rgb[0], rgb[1], rgb[2]) else: unicornhathd.set_pixel(col, row, 0, 0, 0) unicornhathd.rotation(ROT) unicornhathd.show() return
def UnicornTextScroll(): TEXT_FULL = conf.SCROLL_TEXT_RND TEXT_LEN = RandomNum(len(TEXT_FULL)) TEXT = TEXT_FULL[TEXT_LEN] print("Scrolling text: " + TEXT) #TEXT = conf.SCROLL_TEXT FONT = ('/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 12) width, height = unicorn.get_shape() unicorn.rotation(270) text_x = 1 text_y = 2 font_file, font_size = FONT font = ImageFont.truetype(font_file, font_size) text_width, text_height = font.getsize(TEXT) text_width += width + text_x image = Image.new('RGB', (text_width, max(height, text_height)), (0, 0, 0)) draw = ImageDraw.Draw(image) draw.text((text_x, text_y), TEXT, fill=(255, 255, 255), font=font) for scroll in range(text_width - width): for x in range(width): hue = (x + scroll) / float(text_width) br, bg, bb = [ int(n * 255) for n in colorsys.hsv_to_rgb(hue, 1.0, 1.0) ] for y in range(height): pixel = image.getpixel((x + scroll, y)) r, g, b = [float(n / 255.0) for n in pixel] r = int(br * r) g = int(bg * g) b = int(bb * b) unicorn.set_pixel(width - 1 - x, y, r, g, b) unicorn.show() # And sleep for a little bit, so it doesn't scroll too quickly! time.sleep(0.02) unicorn.off() return 0
def display_text(data): text = "{} from {} backers = {}%!".format(data['pledged'], data['backers'], data['percent']) colours = [ tuple([int(n * 255) for n in colorsys.hsv_to_rgb(x / 1.0, 1.0, 1.0)]) for x in range(1) ] FONT = ("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", 12) 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 w, h = font.getsize(text) 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 draw.text((text_x, text_y), text, colours[index], font=font) 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
def display_clock( hours, minutes, brightness, cycle, color_width ): #temperature has tobe string with at least two chars (add whitspace before)| add color coutner which is counted in main FONT = ("/home/pi/.fonts/fixed.ttf", 10) #also time font in file unicornhathd.rotation(0) unicornhathd.brightness(brightness) width, height = unicornhathd.get_shape() text_x = width text_y = 0 font_file, font_size = FONT font = ImageFont.truetype(font_file, font_size) w, text_height = font.getsize(hours) image = Image.new("RGB", (16, max(16, text_height)), (0, 0, 0)) draw = ImageDraw.Draw(image) draw.fontmode = "1" #turn antialiasing off for pixel fonts #draw.line(((11,0),(11,15)),(0,0,255),1) #draw.text((-1, 1), hours, (255,255,0), font=font) #todo change rand int colours draw.text((-1, 0), hours, (0, 255, 0), font=font) draw.text((-1, 9), minutes, (0, 255, 0), font=font) for x in range(12): for y in range(16): pixel = image.getpixel((x, y)) r, g, b = [int(n) for n in pixel] if r > 0 or g > 0 or b > 0: r, g, b = [ int(n * 255) for n in colorsys.hsv_to_rgb((y + cycle) / float(color_width), 1.0, 1.0) ] unicornhathd.set_pixel(-x - 1, y, r, g, b) #unicornhathd.show() return
def loop(event): """ ループ関数 引数にEventオブジェクトをとり、終了イベントを受け取れるように改変を加えている。 """ logger.debug('clock loop start.') unicornhathd.rotation(0) # ループ条件をeventオブジェクトがイベントを受け取っていないことにしている # eventがセットされるとループを終了する while not event.is_set(): # 描写用キャンバスの新規作成 image = Image.new("RGB", (width, height), (0, 0, 0)) draw = ImageDraw.Draw(image) # 現在時刻の取得 now = datetime.datetime.now() # キャンバスに時、分の描写 draw.text((0, -1), '{0:02}'.format(now.hour), fill=COLOR, font=font) draw.text((0, 8), '{0:02}'.format(now.minute), fill=COLOR, font=font) # ここからunicornhatへキャンバス描写作業 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) # コロンの代わりに2x2のドットを描写する if now.second % 2: unicornhathd.set_pixel(0, height - 1, *COLOR) unicornhathd.set_pixel(1, height - 1, *COLOR) unicornhathd.set_pixel(0, height - 2, *COLOR) unicornhathd.set_pixel(1, height - 2, *COLOR) # 画面のリフレッシュ命令 unicornhathd.show() time.sleep(0.1) logger.debug('clock loop end.')
def start(): threading.Timer(3.0, start).start() FONT = ('/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 12) 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 lines = str(datetime.datetime.now())[10:-10] 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))] unicornhathd.rotation(90) if (int(lines[:3])>=21 or int(lines[:3])<9): unicornhathd.brightness(0.01) else: unicornhathd.brightness(1) 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] + 1 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()
def __init__(self, text=None, rainbow=True, rotation=0): if text==None: self.text = 'Splash screen! Informing you that the program has started!' else: self.text = text self.font_size = 12 linuxFont = "/usr/share/fonts/truetype/freefont/FreeSansBold.ttf" macFont = "/Library/Fonts/GillSans.ttc" if os.path.isfile(linuxFont): self.font = linuxFont else: self.font = macFont self.rainbow = rainbow unicornhathd.rotation(rotation) self.prepareImage() self.show()
from sys import exit import digits try: import unicornhathd as unicorn print("unicorn hat hd detected") except ImportError: from unicorn_hat_sim import unicornhathd as unicorn try: from PIL import Image except ImportError: exit("This script requires the pillow module\nInstall with: sudo pip install pillow") unicorn.rotation(0) width, height = unicorn.get_shape() def setPixel(x, y, r, g, b): unicorn.set_pixel((width-1)-x, y, r, g, b) def loadImage(imageFile): return loadImages([imageFile]) def applyFade(pixels, mult): newpixels = copy.deepcopy(pixels) for x in range(width): for y in range(height): pixel = newpixels[x][y] newpixel = (pixel[0] * mult, pixel[1] * mult, pixel[2] * mult) newpixels[x][y] = newpixel