def set_to_timer(self): self.dim_down() self.feed_start_time = time.localtime() if self.feed_start_time.tm_mday != self.prev_feed_start_time.tm_mday: self.prev_feed_count = 0 self.clear_display() self.set_background('back_timer.bmp') # hour since, min since, sec_since positions = [(100-50+10,120),(220-50+10,120)] font = bitmap_font.load_font('/fonts/Nunito-Light-75.bdf') #font.load_glyphs(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') font.load_glyphs(b'0123456789: ') for x, y in positions: text = Label(font, text='', max_glyphs=2) text.x = x text.y = y text.color = 0xffffff # white self.splash.append(text) self.texts.append(text) # add : between numbers text = Label(font, text=':') text.x = 160-7 text.y = 120-5 text.color = 0xffffff # white self.splash.append(text) self.dim_up()
def set_to_info(self, max_glyphs=2): self.dim_down() self.clear_display() self.set_background('back_info.bmp') # hour since, min since, count positions = [(210,45),(250,45),(230,110)] font = bitmap_font.load_font('/fonts/Nunito-Black-17.bdf') #font.load_glyphs(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') font.load_glyphs(b'0123456789:/-_,. ') for x, y in positions: text = Label(font, text='', max_glyphs=max_glyphs) text.x = x text.y = y text.color = 0x000000 self.splash.append(text) self.texts.append(text) # : text = Label(font, text=':') text.x = 238 text.y = 43 text.color =0x000000 self.splash.append(text) self.dim_up()
def day_graphics(medium_font, large_font): """Format the day, date, and time info from time.localtime() for display. Inputs are medium and large fonts. """ text_group = displayio.Group(max_size=5) ur_label = Label(medium_font, max_glyphs=8) # time text ur_label.x = 220 ur_label.y = 25 ur_label.color = 0xFFFFFF text_group.append(ur_label) sub_label = Label(medium_font, max_glyphs=18) # month, date, year text sub_label.x = 60 sub_label.y = 140 sub_label.color = 0xFFFFFF text_group.append(sub_label) main_label = Label(large_font, max_glyphs=9) # day of week main_label.x = 80 main_label.y = 100 main_label.color = 0xFFFFFF text_group.append(main_label) """Fetch the time.localtime(), parse it out and update the display text""" day_names = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ] month_names = [ "Unknown", "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] now = time.localtime() day_str = day_names[now[6]] date_str = str(now[2]) month_str = month_names[now[1]] year_str = str(now[0]) day_date_str = month_str + " " + date_str + ", " + year_str sub_label.text = day_date_str main_label.text = day_str hour = now[3] minute = now[4] format_str = "%d:%02d" if hour >= 12: hour -= 12 format_str = format_str + " PM" else: format_str = format_str + " AM" if hour == 0: hour = 12 time_str = format_str % (hour, minute) ur_label.text = time_str return (text_group)
def message(text, axisX, axisY, color): text_group = displayio.Group(max_size=1, scale=2) label = Label(terminalio.FONT, text=text) if color == "white": color = 0xFFFFFF if color == "red": color = 0xFF0000 if color == "blue": color = 0x0000FF if color == "green": color = 0x00CC00 label.color = color #if bgcolor == "white": # bgcolor = 0xFFFFFF #if bgcolor == "red": # bgcolor = 0xFF0000 #if bgcolor == "blue": # bgcolor = 0x0000FF #if bgcolor == "green": # bgcolor = 0x00FF00 #label.background_color = bgcolor (x, y, w, h) = label.bounding_box #label.x = (80 - w // 2) label.x = axisX #label.y = (64 - h // 2) label.y = axisY text_group.append(label) #group.append(label) group.append(text_group)
def setupText(position, text_color): textarea = Label(big_font, text=' ') textarea.x = position[0] textarea.y = position[1] textarea.color = text_color pyportal.splash.append(textarea) text_areas.append(textarea)
def display_win(): text_group = displayio.Group(max_size=2) win_text = Label(m_font, max_glyphs=15) win_text.x = 20 win_text.y = 50 win_text.color = 0xFF0000 text_group.append(win_text) win_text.text = "YOU WON!" return text_group
def display_directions(): text_group = displayio.Group(max_size=2) title_text = Label(m_font, max_glyphs=15) title_text.x = 0 title_text.y = 10 title_text.color = 0x0F0FFF text_group.append(title_text) title_text.text = "Slider Puzzle" sub_text = Label(s_font, max_glyphs=128) sub_text.x = 0 sub_text.y = 70 sub_text.color = 0xFFFFFF text_group.append(sub_text) sub_text.text = """Press A for 3x3 Press B for 4x4 After win, press START to play again """ return text_group
def create_text_areas(configs): """Given a list of area specifications, create and return test areas.""" text_areas = [] for cfg in configs: textarea = Label(cfg['font'], text=' '*cfg['size']) textarea.x = cfg['x'] textarea.y = cfg['y'] textarea.color = cfg['color'] text_areas.append(textarea) return text_areas
def market_graphics(medium_font, large_font, market): """Take input JSON pulled from the Alpha Vantage API, extract the relevent data, and format for display. Inputs are medium and large fonts and the JSON input from the API """ text_group = displayio.Group(max_size=2) market = json.loads(market) main_text = Label(large_font, max_glyphs=20) main_text.x = 40 main_text.y = 80 main_text.color = 0xFFFFFF text_group.append(main_text) sub_text = Label(medium_font, max_glyphs=20) sub_text.x = 30 sub_text.y = 120 text_group.append(sub_text) price = market['Global Quote']['05. price'] price = price[:-2] # only want 2 decimal points change = market['Global Quote']['09. change'] change = change[:-2] # only want two decimal points percent = market['Global Quote']['10. change percent'] l = len(percent) - 3 percent = percent[:l] + percent[-1] # only want two decimal points print(price, change, percent) main_text.text = price sub_text.text = change + ' (' + percent + ')' # set the background and color of change if float(change) >= 0: background_file = "/icons/green_up.bmp" sub_text.color = 0x00FF00 else: background_file = "/icons/red_down.bmp" sub_text.color = 0xFF0000 return (text_group, background_file)
def market_graphics(medium_font, large_font, market): """Take input JSON pulled from the Alpha Vantage API, extract the relevent data, and format for display. Inputs are medium and large fonts and the JSON input from the API """ text_group = displayio.Group(max_size=2) market = json.loads(market) main_text = Label(large_font, max_glyphs=20) main_text.x = 40 main_text.y = 80 main_text.color = 0xFFFFFF text_group.append(main_text) sub_text = Label(medium_font, max_glyphs=20) sub_text.x = 30 sub_text.y = 120 text_group.append(sub_text) price = market['c'] * 10 + 7 # Convert SPY ETF to S&P (about 10x) prev_close = market['pc'] * 10 + 7 # Convert SPY ETF to S&P (about 10x) change = round(price - prev_close, 2) percent = round((100 * change / prev_close), 2) print(price, change, percent) main_text.text = str(price) sub_text.text = str(change) + ' (' + str(percent) + '%)' # set the background and color of change if change >= 0: background_file = "/icons/green_up.bmp" sub_text.color = 0x00FF00 else: background_file = "/icons/red_down.bmp" sub_text.color = 0xFF0000 return (text_group, background_file)
def st_graphics(medium_font, large_font, small_font, st): """Take input JSON pulled from the Shower Thoughts subreddit API, extract the title field, and format for display. Inputs are medium, large, and small fonts and the JSON input from the API """ text_group = displayio.Group(max_size=6) st = json.loads(st) thought = st['data']['children'][0]['data']['title'] print(thought) # Split into multiple lines (based on pyportal's wrap_nicely method) # Use smaller font if needed to fit print("length: ", len(thought)) if len(thought) > 140: max_chars = 38 font = small_font max_glyphs = 38 print("using small font") else: max_chars = 28 font = medium_font max_glyphs = 28 print("using medium font") words = thought.split(' ') the_lines = [] the_line = "" for w in words: if len(the_line + ' ' + w) <= max_chars: the_line += ' ' + w else: the_lines.append(the_line) the_line = '' + w if the_line: # last line remaining the_lines.append(the_line) # remove first space from first line: the_lines[0] = the_lines[0][1:] for idx, line in enumerate(the_lines): thought_text = Label(font, line_spacing=1.2, max_glyphs=max_glyphs) thought_text.x = 15 thought_text.color = 0xFFFFFF thought_text.y = 110 + (idx * 24) thought_text.text = line text_group.append(thought_text) return (text_group)
def writemessage(text, axisX, axisY, color, font, group): if font != "default": font = bitmap_font.load_font(font) else: font = terminalio.FONT # Definition for adding a text label text_group = displayio.Group(max_size=1, scale=1) #label = Label(terminalio.FONT, text=text) label = Label(font, text=text) # Section for label font colors if color == "white": color = 0xFFFFFF if color == "red": color = 0xFF0000 if color == "blue": color = 0x0000FF if color == "green": color = 0x00CC00 if color == "black": color = 0x000000 label.color = color # Section for label background colors #if bgcolor == "white": # bgcolor = 0xFFFFFF #if bgcolor == "red": # bgcolor = 0xFF0000 #if bgcolor == "blue": # bgcolor = 0x0000FF #if bgcolor == "green": # bgcolor = 0x00FF00 #label.background_color = bgcolor (x, y, w, h) = label.bounding_box #label.x = (80 - w // 2) label.x = axisX #label.y = (64 - h // 2) label.y = axisY text_group.append(label) #group.append(label) group.append(text_group)
def _create_label_group(self, text, font, scale, height_adjustment, color=0xFFFFFF, width_adjustment=2, line_spacing=0.75): """Create a label group with the given text, font, and spacing""" # If the given font is a string, treat it as a file path and try to load it if isinstance(font, str): font = load_font(font, text) group = displayio.Group(scale=scale) label = Label(font, text=text, line_spacing=line_spacing) _, _, width, _ = label.bounding_box label.x = ((self.display.width // (width_adjustment * scale)) - width // 2) label.y = int(self.display.height * (height_adjustment / scale)) label.color = color group.append(label) return group
maxi = t maxx = w maxy = h image_bitmap[w, (23-h)] = int(map_range(t, min_t, max_t, 0, last_color )) # min and max temperature indicator min_label.text="%0.2f" % (min_t) max_string="%0.2f" % (max_t) max_label.x=120-(5*len(max_string)) # Tricky calculation to left align max_label.text=max_string # Compute average_center temperature of the middle of sensor and convert to palette color center_average = (frame[11*32 + 15] + frame[12*32 + 15] + frame[11*32 + 16] + frame[12*32 + 16]) / 4 center_color = int(map_range(center_average, min_t, max_t, 0, last_color )) center_label.text = "%0.2f" % (center_average) center_label.color = palette[center_color] # Set the location of X for lowest temperature x_label.x = maxx * 4 x_label.y = (23-maxy) * 4 # Set the location of O for highest temperature o_label.x = minx * 4 o_label.y = (23-miny) * 4 min_t = mini # Automatically change the color scale max_t = maxi # print((mini, maxi)) # Use this line to display min and max graph in Mu # print("Total time for aquisition and display %0.2f s" % (time.monotonic()-stamp))
weight_label = Label(weight_font) weight_label.x = 75 weight_label.y = 120 root_group.append(weight_label) weight_label.text = "---" title_label = Label(weight_font) title_label.x = 65 title_label.y = 20 root_group.append(title_label) title_label.text = "IO SCALE" text_label = Label(text_font) text_label.x = 100 text_label.y = 200 text_label.color = 0xFFFFFF root_group.append(text_label) board.DISPLAY.show(root_group) # PyPortal ESP32 Setup esp32_cs = digitalio.DigitalInOut(board.ESP_CS) esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY) esp32_reset = digitalio.DigitalInOut(board.ESP_RESET) spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager( esp, secrets, status_light) # Set your Adafruit IO Username and Key in secrets.py
big_font = bitmap_font.load_font(cwd + "/fonts/Helvetica-Bold-24.bdf") big_font.load_glyphs(b'0123456789') # pre-load glyphs for fast printing years_position = (126, 15) days_position = (13, 41) hours_position = (118, 41) minutes_position = (25, 68) text_color = 0xFF0000 text_areas = [] for pos in (years_position, days_position, hours_position, minutes_position): textarea = Label(big_font, text=' ') textarea.x = pos[0] textarea.y = pos[1] textarea.color = text_color pyportal.splash.append(textarea) text_areas.append(textarea) refresh_time = None while True: # only query the online time once per hour (and on first run) if (not refresh_time) or (time.monotonic() - refresh_time) > 3600: try: print("Getting time from internet!") pyportal.get_local_time() refresh_time = time.monotonic() except RuntimeError as e: print("Some error occured, retrying! -", e) continue
event_background = cwd + "/happy_halloween.bmp" # Initialize the pyportal object and let us know what data to fetch and where # to display it pyportal = PyPortal(status_neopixel=board.NEOPIXEL, default_bg=cwd + backgrounds[0], caption_text='DAYS REMAINING', caption_font=cwd + '/fonts/Terror-31.bdf', caption_position=(24, 218), caption_color=0x000000) countdown_text = Label(big_font, max_glyphs=3) countdown_text.x = 25 countdown_text.y = 120 countdown_text.color = 0x7942a0 pyportal.splash.append(countdown_text) refresh_time = None while True: # only query the online time once per hour (and on first run) if (not refresh_time) or (time.monotonic() - refresh_time) > 3600: try: print("Getting time from internet!") pyportal.get_local_time(location=secrets['timezone']) refresh_time = time.monotonic() except RuntimeError as e: print("Some error occured, retrying! -", e) continue
#inPreMainLoop = 1 bypassKeyboard = False print("Beginning Superloop") while True: if bypassKeyboard == False: timesPressed = 0 timesPressedMax = 5 #change to "2" for matchid inPreMainLoop = 1 #change to "2" for matchid board.DISPLAY.show(preMainGroup) k_update_display() gc.collect() print("Starting preMain Loop") print("RAM (post-spots loop): ", gc.mem_free()) print("Allocated RAM: ", gc.mem_alloc()) teamIdLabelText.color = HEX_BLUE timesPressedMax = 5 while inPreMainLoop > 0: touch = ts.touch_point if touch: for button in kbuttons: if button.contains(touch): print("Touched", button.name) lastPressed = button.name if timesPressed < timesPressedMax: #in coda, this looks wrong, but its fine if button.name in [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]: if inPreMainLoop == 2: matchid = int(str(matchid) + str(button.name))
names_font = bitmap_font.load_font(cwd + "/fonts/Helvetica-Bold-16.bdf") # pre-load glyphs for fast printing names_font.load_glyphs( b'abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- ()') names_position = (100, 175) names_color = 0xFF00FF while True: try: value = pyportal.fetch() print("Response is", value) except RuntimeError as e: print("Some error occured, retrying! -", e) stamp = time.monotonic() while (time.monotonic() - stamp) < 5 * 60: # wait 5 minutes before getting again if pyportal.touchscreen.touch_point: names = "" for astro in value[1]: names += "%s (%s)\n" % (astro['name'], astro['craft']) names = names[:-1] # remove final '\n' names_textarea = Label(names_font, text=names) names_textarea.x = names_position[0] names_textarea.y = names_position[1] names_textarea.color = names_color pyportal.splash.append(names_textarea) time.sleep(30) # wait 30 seconds to read it pyportal.splash.pop()
group.append(tile_grid) # Add the TileGrid to the Group display.show(group) if not DEBUG: # font = bitmap_font.load_font("fonts/IBMPlexMono-Medium-24_jep.bdf") font = bitmap_font.load_font("fonts/Arial-14.bdf") font2 = bitmap_font.load_font("fonts/Arial-12.bdf") small_font = bitmap_font.load_font("fonts/helvR10.bdf") else: font = terminalio.FONT font2 = terminalio.FONT small_font = terminalio.FONT clock_label = Label(font, max_glyphs=8) clock_label.color_idx = 1 clock_label.color = color[clock_label.color_idx] clock_label.text = 'Matrix' clock_label.x = 0 clock_label.y = display.height // 4 clock_label.normal = True event_label = Label(font2, max_glyphs=32) event_label.color_idx = 2 event_label.color = color[event_label.color_idx] event_label.text = 'Clock' event_label.x = 0 event_label.y = display.height // 4 * 3 DATA_LOCATION = [] class Weather:
def showGameResultScreen(self, pla, sco, rounds_tot=None): # pylint: disable=unused-argument,too-many-locals,too-many-statements """Display a high score table with a visual bubble sort slow enough to see the algorithm at work.""" self.fadeUpDown("down") self.emptyGroup(self.disp_group) # Score list group + background + question mark for sorting gs_group = Group(max_size=4) # Pale grey large GAME SCORES background bg_scale = 6 sbg_dob1 = Label(self.font, text="GAME", scale=bg_scale, color=GS_COL) sbg_dob1.x = (self.width - 4 * bg_scale * self.font_width) // 2 sbg_dob1.y = self.height // 4 sbg_dob2 = Label(self.font, text="SCORES", scale=bg_scale, color=GS_COL) sbg_dob2.x = (self.width - 6 * bg_scale * self.font_width) // 2 sbg_dob2.y = self.height // 4 * 3 gs_group.append(sbg_dob1) gs_group.append(sbg_dob2) self.showGroup(gs_group) self.fadeUpDown("up") # Calculate maximum length player name # and see if scores happen to already be in order max_len = 0 prev_score = sco[0] descending = True for idx, (name, _) in enumerate(pla): max_len = max(max_len, len(name)) if sco[idx] > prev_score: descending = False prev_score = sco[idx] fmt = "{:" + str(max_len) + "s} {:2d}" x_pos = (self.width - (max_len + 3) * 2 * self.font_width) // 2 scale = 2 spacing = 4 if len(pla) <= 6 else 0 top_y_pos = round((self.height - len(pla) * scale * self.font_height - (len(pla) - 1) * spacing) / 2 + scale * self.font_height / 2) scores_group = Group(max_size=len(pla)) gs_group.append(scores_group) for idx, (name, _) in enumerate(pla): op_dob = Label( self.font, text=fmt.format(name, sco[idx]), scale=2, color=(PLAYER_NAME_COL_FG if idx == 0 else OPP_NAME_COL_FG)) op_dob.x = x_pos op_dob.y = top_y_pos + idx * (scale * self.font_height + spacing) scores_group.append(op_dob) time.sleep(0.2) # Sort the entries if needed sort_scores = list(sco) # Make an independent local copy if not descending: empty_group = Group() # minor hack to aid swaps in scores_group step = 3 qm_dob = Label(self.font, text="?", scale=2, color=QM_SORT_FG) qm_dob.x = round(x_pos - 1.5 * scale * self.font_width) gs_group.append(qm_dob) while True: swaps = 0 for idx in range(0, len(sort_scores) - 1): above_score = sort_scores[idx] above_y = scores_group[idx].y below_y = scores_group[idx + 1].y qm_dob.y = (above_y + below_y) // 2 if above_score < sort_scores[idx + 1]: qm_dob.text = "<" qm_dob.color = QM_SORTING_FG swaps += 1 # make list of steps range_y = below_y - above_y offsets = list(range(step, range_y + 1, step)) # Ensure this goes to the exact final position if offsets[-1] != range_y: offsets.append(range_y) for offset in offsets: scores_group[idx].y = above_y + offset scores_group[idx + 1].y = below_y - offset time.sleep(0.050) # swap the scores around sort_scores[idx] = sort_scores[idx + 1] sort_scores[idx + 1] = above_score # swap the graphical objects around using empty_group # to avoid ValueError: Layer already in a group old_above_dob = scores_group[idx] old_below_dob = scores_group[idx + 1] scores_group[idx + 1] = empty_group scores_group[idx] = old_below_dob scores_group[idx + 1] = old_above_dob qm_dob.text = "?" qm_dob.color = QM_SORT_FG time.sleep(0.2) else: time.sleep(0.6) if swaps == 0: break # Sort complete if no values were swapped gs_group.remove(qm_dob)
def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF, background_text_color=0xFFFFFF, foreground_text_color=0x000000, hello_font=terminalio.FONT, hello_scale=1, hello_string="HELLO", my_name_is_font=terminalio.FONT, my_name_is_scale=1, my_name_is_string="MY NAME IS", name_font=terminalio.FONT, name_scale=1, name_string="Blinka"): """Create a "Hello My Name is"-style badge. :param background_color: The color of the background. Defaults to 0xFF0000. :param foreground_color: The color of the foreground rectangle. Defaults to 0xFFFFFF. :param background_text_color: The color of the "HELLO MY NAME IS" text. Defaults to 0xFFFFFF. :param foreground_text_color: The color of the name text. Defaults to 0x000000. :param hello_font: The font for the "HELLO" string. Defaults to ``terminalio.FONT``. :param hello_scale: The size scale of the "HELLO" string. Defaults to 1. :param hello_string: The first string of the badge. Defaults to "HELLO". :param my_name_is_font: The font for the "MY NAME IS" string. Defaults to ``terminalio.FONT``. :param my_name_is_scale: The size scale of the "MY NAME IS" string. Defaults to 1. :param my_name_is_string: The second string of the badge. Defaults to "MY NAME IS". :param name_font: The font for the name string. Defaults to ``terminalio.FONT``. :param name_scale: The size scale of the name string. Defaults to 1. :param name_string: The third string of the badge - change to be your name. Defaults to "Blinka". """ splash = displayio.Group(max_size=20) color_bitmap = displayio.Bitmap(self.display.width, self.display.height, 1) color_palette = displayio.Palette(1) color_palette[0] = background_color bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) rect = Rect(0, (int(self.display.height * 0.4)), self.display.width, (int(self.display.height * 0.5)), fill=foreground_color) splash.append(rect) if isinstance(hello_font, str): hello_font = load_font(hello_font, hello_string) hello_group = displayio.Group(scale=hello_scale) hello_label = Label(font=hello_font, text=hello_string, line_spacing=0.75) (_, _, width, _) = hello_label.bounding_box hello_label.x = ((self.display.width // (2 * hello_scale)) - width // 2) hello_label.y = int(self.display.height * (0.117 / hello_scale)) hello_label.color = background_text_color hello_group.append(hello_label) if isinstance(my_name_is_font, str): my_name_is_font = load_font(my_name_is_font, my_name_is_string) my_name_is_group = displayio.Group(scale=my_name_is_scale) my_name_is_label = Label(font=my_name_is_font, text=my_name_is_string, line_spacing=0.75) (_, _, width, _) = my_name_is_label.bounding_box my_name_is_label.x = ((self.display.width // (2 * my_name_is_scale)) - width // 2) my_name_is_label.y = int(self.display.height * (0.28 / my_name_is_scale)) my_name_is_label.color = background_text_color my_name_is_group.append(my_name_is_label) if isinstance(name_font, str): name_font = load_font(name_font, name_string) name_group = displayio.Group(scale=name_scale) name_label = Label(font=name_font, text=name_string, line_spacing=0.75) (_, _, width, _) = name_label.bounding_box name_label.x = ((self.display.width // (2 * name_scale)) - width // 2) name_label.y = int(self.display.height * (0.65 / name_scale)) name_label.color = foreground_text_color name_group.append(name_label) group = displayio.Group() group.append(splash) group.append(hello_group) group.append(my_name_is_group) group.append(name_group) self.display.show(group)
def show_business_card(self, *, image_name=None, name_string=None, name_scale=1, name_font=terminalio.FONT, email_string_one=None, email_scale_one=1, email_font_one=terminalio.FONT, email_string_two=None, email_scale_two=1, email_font_two=terminalio.FONT): """Display a bitmap image and a text string, such as a personal image and email address. :param str image_name: REQUIRED. The name of the bitmap image including .bmp, e.g. ``"Blinka.bmp"``. :param str name_string: A name string to display along the bottom of the display, e.g. ``"Blinka"``. :param int name_scale: The scale of ``name_string``. Defaults to 1. :param name_font: The font for the name string. Defaults to ``terminalio.FONT``. :param str email_string_one: A string to display along the bottom of the display, e.g. ``"*****@*****.**"``. :param int email_scale_one: The scale of ``email_string_one``. Defaults to 1. :param email_font_one: The font for the first email string. Defaults to ``terminalio.FONT``. :param str email_string_two: A second string to display along the bottom of the display. Use if your email address is longer than one line or to add more space between the name and email address, e.g. (blinka@) ``"adafruit.com"``. :param int email_scale_two: The scale of ``email_string_two``. Defaults to 1. :param email_font_two: The font for the second email string. Defaults to ``terminalio.FONT``. """ business_card_splash = displayio.Group(max_size=30) self.display.show(business_card_splash) with open(image_name, "rb") as file_name: on_disk_bitmap = displayio.OnDiskBitmap(file_name) face_image = displayio.TileGrid( on_disk_bitmap, pixel_shader=displayio.ColorConverter()) business_card_splash.append(face_image) try: self.display.refresh(target_frames_per_second=60) except AttributeError: self.display.wait_for_frame() if name_string: if isinstance(name_font, str): name_font = load_font(name_font, name_string) name_group = displayio.Group(scale=name_scale) name_label = Label(name_font, text=name_string, line_spacing=0.75) (_, _, width, _) = name_label.bounding_box name_label.x = ((self.display.width // (2 * name_scale)) - width // 2) name_label.y = int(self.display.height * (0.73 / name_scale)) name_label.color = 0xFFFFFF name_group.append(name_label) business_card_splash.append(name_group) if email_string_one: if isinstance(email_font_one, str): email_font_one = load_font(email_font_one, email_string_one) email_group_one = displayio.Group(scale=email_scale_one) email_label_one = Label(email_font_one, text=email_string_one, line_spacing=0.75) (_, _, width, _) = email_label_one.bounding_box email_label_one.width = self.display.width email_label_one.x = ((self.display.width // (2 * email_scale_one)) - width // 2) email_label_one.y = int(self.display.height * (0.84 / email_scale_one)) email_label_one.color = 0xFFFFFF email_group_one.append(email_label_one) business_card_splash.append(email_group_one) if email_string_two: if isinstance(email_font_two, str): email_font_two = load_font(email_font_two, email_string_two) email_group_two = displayio.Group(scale=email_scale_two) email_label_two = Label(email_font_two, text=email_string_two, line_spacing=0.75) (_, _, width, _) = email_label_two.bounding_box email_label_two.width = self.display.width email_label_two.x = ((self.display.width // (2 * email_scale_two)) - width // 2) email_label_two.y = int(self.display.height * (0.91 / email_scale_two)) email_label_two.color = 0xFFFFFF email_group_two.append(email_label_two) business_card_splash.append(email_group_two)
pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) # Load the Name font pronoun_font_name = FONTNAME pronoun_font = bitmap_font.load_font(pronoun_font_name) pronoun_font.load_glyphs(PRONOUNS[0].encode('utf-8')) # Setup and Center the Pronoun Label name_label = Label(pronoun_font, text=PRONOUNS[0], line_spacing=0.75) (x, y, w, h) = name_label.bounding_box name_label.x = (80 - w // 2) name_label.y = 65 name_label.color = FOREGROUND_TEXT_COLOR splash.append(name_label) # Remap the calculated rotation to 0 - 255 def remap(vector): return int(((255 * vector + 85) * 0.75) + 0.5) # Calculate the Hue rotation starting with Red as 0 degrees def rotate(degrees): cosA = cos(radians(degrees)) sinA = sin(radians(degrees)) red = cosA + (1.0 - cosA) / 3.0 green = 1. / 3. * (1.0 - cosA) + sqrt(1. / 3.) * sinA blue = 1. / 3. * (1.0 - cosA) - sqrt(1. / 3.) * sinA
def aws_announcements(self, *, background_color=0xFF9900, foreground_color=0xFFFFFF, background_text_color=0x000000, foreground_text_color=0x000000, hello_font=terminalio.FONT, hello_scale=1, my_name_is_font=terminalio.FONT, my_name_is_scale=1, my_name_is_string="MY NAME IS", name_font=terminalio.FONT, name_scale=1, name_string="Blinka"): splash = displayio.Group(max_size=20) color_bitmap = displayio.Bitmap(self.display.width, self.display.height, 1) color_palette = displayio.Palette(1) color_palette[0] = background_color bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) rect = Rect(0, (int(self.display.height * 0.5)), self.display.width, (int(self.display.height * 0.5)), fill=foreground_color) splash.append(rect) # Header header_string = "re:Invent" hello_font_name = "/fonts/Verdana-Bold-18.bdf" hello_font = bitmap_font.load_font(hello_font_name) hello_font.load_glyphs(header_string.encode('utf-8')) hello_scale = hello_scale hello_group = displayio.Group(scale=hello_scale) hello_label = Label(font=hello_font, text=header_string) (_, _, width, height) = hello_label.bounding_box hello_label.x = ((self.display.width // (2 * hello_scale)) - width // 2) hello_label.y = int(height // (1.2 * hello_scale)) hello_label.color = background_text_color hello_group.append(hello_label) # Subheader my_name_is_string = "Announcements" my_name_is_string2 = "this week" my_name_is_font_name = "/fonts/Arial-12.bdf" my_name_is_font = bitmap_font.load_font(my_name_is_font_name) my_name_is_font.load_glyphs(my_name_is_string.encode('utf-8')) my_name_is_font.load_glyphs(my_name_is_string2.encode('utf-8')) my_name_is_scale = my_name_is_scale my_name_is_group = displayio.Group(scale=my_name_is_scale) my_name_is_label = Label(font=my_name_is_font, text=my_name_is_string) (_, _, width, height) = my_name_is_label.bounding_box my_name_is_label.x = ((self.display.width // (2 * my_name_is_scale)) - width // 2) my_name_is_label.y = int(height // (0.32 * my_name_is_scale)) my_name_is_label.color = background_text_color my_name_is_group.append(my_name_is_label) my_name_is_group2 = displayio.Group(scale=my_name_is_scale) my_name_is_label2 = Label(font=my_name_is_font, text=my_name_is_string2) (_, _, width, height) = my_name_is_label2.bounding_box my_name_is_label2.x = ((self.display.width // (2 * my_name_is_scale)) - width // 2) my_name_is_label2.y = int(height // (0.32 * my_name_is_scale)) + 16 my_name_is_label2.color = background_text_color my_name_is_group2.append(my_name_is_label2) # Main Body name_font_name = "/fonts/IdealSans-Semibold-48.bdf" name_font = bitmap_font.load_font(name_font_name) name_font.load_glyphs(name_string.encode('utf-8')) name_scale = name_scale name_group = displayio.Group(scale=name_scale) name_label = Label(font=name_font, text=name_string, line_spacing=0.75) (_, _, width, height) = name_label.bounding_box name_label.x = ((self.display.width // (2 * name_scale)) - width // 2) # name_label.y = int(height // (0.5 * name_scale)) name_label.y = int((self.display.height // 2) + 25) name_label.color = foreground_text_color name_group.append(name_label) group = displayio.Group(max_size=5) group.append(splash) group.append(hello_group) group.append(my_name_is_group) group.append(my_name_is_group2) group.append(name_group) self.display.show(group)
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) # Load the font font = bitmap_font.load_font(THE_FONT) font.load_glyphs(DISPLAY_STRING.encode('utf-8')) print(DISPLAY_STRING) text = Label(font, text=DISPLAY_STRING) text.x = 20 text.y = 100 text.color = 0x0 # Make a background color fill dims = text.bounding_box print(dims) textbg_bitmap = displayio.Bitmap(dims[2], dims[3], 1) textbg_palette = displayio.Palette(1) textbg_palette[0] = 0xFF0000 textbg_sprite = displayio.TileGrid(textbg_bitmap, pixel_shader=textbg_palette, x=text.x + dims[0], y=text.y + dims[1]) splash.append(textbg_sprite) splash.append(text) board.DISPLAY.refresh_soon() board.DISPLAY.wait_for_frame()
def showPlayerVPlayerScreen(self, me_name, op_name, my_ch_idx, op_ch_idx, result, summary, win, draw, void): # pylint: disable=too-many-locals,too-many-branches,too-many-statements """Display a win, draw, lose or error message.""" self.fadeUpDown("down") self.emptyGroup(self.disp_group) if void: error_tot = 3 error_group = Group(max_size=error_tot + 1) # Opponent's name helps pinpoint the error op_dob = Label(self.font, text=op_name, scale=2, color=OPP_NAME_COL_FG) op_dob.x = 40 op_dob.y = self.font_height error_group.append(op_dob) self.showGroup(error_group) self.fadeUpDown("up", duration=0.4) if result is not None: self.sample.play(result) font_scale = 2 # Attempting to put the three pieces of "Error!" text on screen # synchronised with the sound sample repeating the word for idx in range(error_tot): error_dob = Label(self.font, text="Error!", scale=font_scale, color=ERROR_COL_FG) error_dob.x = 40 error_dob.y = 60 + idx * 60 error_group.append(error_dob) time.sleep(0.5) # Small attempt to synchronise audio with text font_scale += 1 else: # Would be slightly better to create this Group once and re-use it pvp_group = Group(max_size=3) # Add player's name and sprite just off left side of screen # and opponent's just off right player_detail = [[ me_name, self.sprites[my_ch_idx], -16 - 3 * self.sprite_size, PLAYER_NAME_COL_FG, PLAYER_NAME_COL_BG ], [ op_name, self.opp_sprites[op_ch_idx], 16 + self.width, OPP_NAME_COL_FG, OPP_NAME_COL_BG ]] idx_lr = [0, 1] # index for left and right sprite if win: player_detail.reverse() # this player is winner so put last idx_lr.reverse() # Add some whitespace around winner's name player_detail[1][0] = " " + player_detail[1][0] + " " for (name, sprite, start_x, fg, bg) in player_detail: s_group = Group(scale=2, max_size=2) # Audio is choppy at scale=3 s_group.x = start_x s_group.y = (self.height - 2 * (self.sprite_size + self.font_height)) // 2 s_group.append(sprite) p_name_dob = Label( self.font, text=name, scale=1, # This is scaled by the group color=fg, background_color=bg) # Centre text below sprite - values are * Group scale p_name_dob.x = (self.sprite_size - len(name) * self.font_width) // 2 p_name_dob.y = self.sprite_size + 4 s_group.append(p_name_dob) pvp_group.append(s_group) if draw: sum_text = "Draw" elif win: sum_text = "You win" else: sum_text = "You lose" # Text starts invisible (BLACK) and color is later changed summary_dob = Label(self.font, text=sum_text, scale=3, color=BLACK) summary_dob.x = round( (self.width - 3 * self.font_width * len(sum_text)) / 2) summary_dob.y = round(self.height - (3 * self.font_height / 2)) pvp_group.append(summary_dob) self.showGroup(pvp_group) self.fadeUpDown("up", duration=0.4) # Start audio half way through animations if draw: # Move sprites onto the screen leaving them at either side for idx in range(16): pvp_group[idx_lr[0]].x += 6 pvp_group[idx_lr[1]].x -= 6 if idx == 8 and result is not None: self.sample.play(result) time.sleep(0.2) else: # Move sprites together, winning sprite overlaps and covers loser for idx in range(16): pvp_group[idx_lr[0]].x += 10 pvp_group[idx_lr[1]].x -= 10 if idx == 8 and result is not None: self.sample.play(result) time.sleep(0.2) self.sample.wait() # Wait for first sample to finish if summary is not None: self.sample.play(summary) # Flash colours for win, fad up to blue for rest if not draw and win: colours = [YELLOW_COL, ORANGE_COL, RED_COL] * 5 else: colours = [DRAWLOSE_COL * sc // 15 for sc in range(1, 15 + 1)] for col in colours: summary_dob.color = col time.sleep(0.120) self.sample.wait() # Ensure second sample has completed
def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF, background_text_color=0xFFFFFF, foreground_text_color=0x000000, hello_font=terminalio.FONT, hello_scale=1, hello_string="HELLO", my_name_is_font=terminalio.FONT, my_name_is_scale=1, my_name_is_string="MY NAME IS", name_font=terminalio.FONT, name_scale=1, name_string="Blinka"): """Create a "Hello My Name is"-style badge. :param background_color: The color of the background. Defaults to 0xFF0000. :param foreground_color: The color of the foreground rectangle. Defaults to 0xFFFFFF. :param background_text_color: The color of the "HELLO MY NAME IS" text. Defaults to 0xFFFFFF. :param foreground_text_color: The color of the name text. Defaults to 0x000000. :param hello_font: The font for the "HELLO" string. Defaults to ``terminalio.FONT``. :param hello_scale: The size scale of the "HELLO" string. Defaults to 1. :param hello_string: The first string of the badge. Defaults to "HELLO". :param my_name_is_font: The font for the "MY NAME IS" string. Defaults to ``terminalio.FONT``. :param my_name_is_scale: The size scale of the "MY NAME IS" string. Defaults to 1. :param my_name_is_string: The second string of the badge. Defaults to "MY NAME IS". :param name_font: The font for the name string. Defaults to ``terminalio.FONT``. :param name_scale: The size scale of the name string. Defaults to 1. :param name_string: The third string of the badge - change to be your name. Defaults to "Blinka". """ # Make the Display Background splash = displayio.Group(max_size=20) color_bitmap = displayio.Bitmap(self.display.width, self.display.height, 1) color_palette = displayio.Palette(1) color_palette[0] = background_color bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) # Draw a Foreground Rectangle where the name goes # x, y, width, height rect = Rect(0, (int(self.display.height * 0.4)), self.display.width, (int(self.display.height * 0.5)), fill=foreground_color) splash.append(rect) hello_scale = hello_scale hello_group = displayio.Group(scale=hello_scale) # Setup and Center the Hello Label hello_label = Label(font=hello_font, text=hello_string) (_, _, width, height) = hello_label.bounding_box hello_label.x = ((self.display.width // (2 * hello_scale)) - width // 2) hello_label.y = int(height // (1.2 * hello_scale)) hello_label.color = background_text_color hello_group.append(hello_label) my_name_is_scale = my_name_is_scale my_name_is_group = displayio.Group(scale=my_name_is_scale) # Setup and Center the "My Name Is" Label my_name_is_label = Label(font=my_name_is_font, text=my_name_is_string) (_, _, width, height) = my_name_is_label.bounding_box my_name_is_label.x = ((self.display.width // (2 * my_name_is_scale)) - width // 2) my_name_is_label.y = int(height // (0.42 * my_name_is_scale)) my_name_is_label.color = background_text_color my_name_is_group.append(my_name_is_label) name_scale = name_scale name_group = displayio.Group(scale=name_scale) # Setup and Center the Name Label name_label = Label(font=name_font, text=name_string) (_, _, width, height) = name_label.bounding_box name_label.x = ((self.display.width // (2 * name_scale)) - width // 2) name_label.y = int(height // (0.17 * name_scale)) name_label.color = foreground_text_color name_group.append(name_label) group = displayio.Group() group.append(splash) group.append(hello_group) group.append(my_name_is_group) group.append(name_group) self.display.show(group)
# change this to match the location's pressure (hPa) at sea level bme280.sea_level_pressure = 1021.10 # set up strings, 2 decimal places TEMP_STRING = "Temp: " + str(round(bme280.temperature, 2)) + "C" HUMID_STRING = "Humidity: " + str(round(bme280.humidity, 2)) + "%" PRESS_STRING = "Pressure: " + str(round((bme280.pressure / 10), 2)) + "kPa" ALT_STRING = "Altitude: " + str(round(bme280.altitude, 2)) + "M" BACKGROUND_TEXT_COLOR = 0xFFFFFF HEADING_BACKGROUND = 0xFF0000 font = terminalio.FONT heading_label = Label(large_font, text=HEAD_STRING, color=0x00FF00) (x, y, w, h) = heading_label.bounding_box heading_label.x = 5 heading_label.y = 10 heading_label.color = HEADING_BACKGROUND splash.append(heading_label) temp_label = Label(font, text=TEMP_STRING, color=0x00FF00) (x, y, w, h) = temp_label.bounding_box temp_label.x = 5 temp_label.y = 30 temp_label.color = BACKGROUND_TEXT_COLOR splash.append(temp_label) humid_label = Label(font, text=HUMID_STRING, color=0x00FF00) (x, y, w, h) = humid_label.bounding_box humid_label.x = 5 humid_label.y = 45 humid_label.color = BACKGROUND_TEXT_COLOR splash.append(humid_label) pressure_label = Label(font, text=PRESS_STRING, color=0x00FF00) (x, y, w, h) = pressure_label.bounding_box
# Load the "My Name Is" font small_font_name = "/fonts/Arial-12.bdf" small_font = bitmap_font.load_font(small_font_name) small_font.load_glyphs(MY_NAME_STRING.encode('utf-8')) # Load the Name font name_font_name = NAME_FONTNAME name_font = bitmap_font.load_font(name_font_name) name_font.load_glyphs(NAME_STRING.encode('utf-8')) # Setup and Center the Hello Label hello_label = Label(large_font, text=HELLO_STRING) (x, y, w, h) = hello_label.bounding_box hello_label.x = (80 - w // 2) hello_label.y = 15 hello_label.color = BACKGROUND_TEXT_COLOR splash.append(hello_label) # Setup and Center the "My Name Is" Label mni_label = Label(small_font, text=MY_NAME_STRING) (x, y, w, h) = mni_label.bounding_box mni_label.x = (80 - w // 2) mni_label.y = 35 mni_label.color = BACKGROUND_TEXT_COLOR splash.append(mni_label) # Setup and Center the Name Label name_label = Label(name_font, text=NAME_STRING, line_spacing=0.75) (x, y, w, h) = name_label.bounding_box name_label.x = (80 - w // 2) name_label.y = 85