def make_banner(icons_small_bmp, icons_small_pal, x=0, y=0): """Make a single future forecast info banner group.""" day_of_week = display_util.make_label("DAY", (0, 10)) day_temp = display_util.make_label("+100F", (50, 10)) icon = displayio.TileGrid( icons_small_bmp, pixel_shader=icons_small_pal, x=25, y=0, width=1, height=1, tile_width=20, tile_height=20) return display_util.make_group([day_of_week, icon, day_temp], x, y)
def _render_asset_status(stock_categories, data): asset_status = displayio.Group(max_size=2 * len(stock_categories)) for i in range(0, len(stock_categories)): status = data['performances'][stock_categories[i]] scaled_status = int(status * ALLOCATION_SCALE) scaled_status = 1 if scaled_status == 0 else scaled_status offset_x = 230 if scaled_status > 0: asset_status.append( rect.Rect(offset_x, int(15 + i * 14), scaled_status, 12, fill=0xCCCCCC, outline=0x000000, stroke=1)) offset_x = offset_x + 7 # Make '-' align with '+' items else: asset_status.append( rect.Rect(offset_x + scaled_status, int(15 + i * 14), -scaled_status, 12, fill=0xCCCCCC, outline=0x000000, stroke=1)) asset_status.append( display_util.make_label('{:.1f}%'.format(status), (offset_x - 20, 20 + i * 14))) return asset_status
def render_and_light_sleep(magtag): # Clear all UI while len(magtag.splash) > default_magtag_splash_length: magtag.splash.pop() magtag.graphics.set_background(0xFFFFFF) time.sleep(magtag.display.time_to_refresh) # Render page status.set(2, status.RED) if state.display_weather: weather.run(magtag) else: financials.run(magtag) status.set(2, status.GREEN) # Render common UI. battery_label = display_util.make_label("?.??", (271, 6)) battery_label.text = "{:.2f}".format(magtag.peripherals.battery) magtag.splash.append(battery_label) print("Refreshing...") time.sleep(magtag.display.time_to_refresh + 1) magtag.display.refresh() time.sleep(magtag.display.time_to_refresh + 1) status.set(2, status.BLUE) print("Lightly sleeping for one minute before entering deep sleep...") if state.light_sleep(): render_and_light_sleep(magtag)
def _render_other_allocations(data): allocation = data['allocations']['Other'] scaled_allocation = int(allocation * ALLOCATION_SCALE) scaled_allocation = 1 if scaled_allocation == 0 else scaled_allocation other = displayio.Group(max_size=3) other.append( display_util.make_label('{:.1f}%'.format(allocation), (110, 118))) other.append( rect.Rect(145, 113, scaled_allocation, 12, fill=0x888888, outline=0x000000, stroke=1)) other.append( display_util.make_label(secrets['otherCategoryName'], (150, 118))) return other
def _render_asset_allocations(stock_categories, data): asset_allocations = displayio.Group(max_size=2 * len(stock_categories)) for i in range(0, len(stock_categories)): allocation = data['allocations'][stock_categories[i]] scaled_allocation = int(allocation * ALLOCATION_SCALE) scaled_allocation = 1 if scaled_allocation == 0 else scaled_allocation asset_allocations.append( display_util.make_label('{:.1f}%'.format(allocation), (110, 20 + i * 14))) asset_allocations.append( rect.Rect(145, int(15 + i * 14), scaled_allocation, 12, fill=0x888888, outline=0x000000, stroke=1)) return asset_allocations
def _render_stock_categories(stock_categories): stock_category_labels = displayio.Group(max_size=len(stock_categories)) for i in range(0, len(stock_categories)): stock_category_labels.append( display_util.make_label(stock_categories[i], (10, 20 + i * 14))) return stock_category_labels
def _render_sum_total(data): # At this point the typo is baked in until I need to update the server again. return display_util.make_label('{:.1f}%'.format(data['overalPerformance']), (228, 117))
def run(magtag): magtag.graphics.set_background(BACKGROUND_BMP) time.sleep(magtag.display.time_to_refresh) icons_large_bmp, icons_large_pal = adafruit_imageload.load(ICONS_LARGE_FILE) icons_small_bmp, icons_small_pal = adafruit_imageload.load(ICONS_SMALL_FILE) # == UI == city_name = display_util.make_label(secrets["openweather_location"], (15, 19)) today_date = display_util.make_label("?" * 30, (15, 32)) today_icon = displayio.TileGrid( icons_large_bmp, pixel_shader=icons_small_pal, x=10, y=40, width=1, height=1, tile_width=70, tile_height=70) # Weather today_morn_temp = display_util.make_label("+100F", (110, 64)) today_day_temp = display_util.make_label("+100F", (139, 64)) today_night_temp = display_util.make_label("+100F", (170, 64)) today_humidity = display_util.make_label("100%", (120, 85)) today_wind = display_util.make_label("99m/s", (157, 85)) today_sunrise = display_util.make_label("12:00 PM", (130, 104)) today_sunset = display_util.make_label("12:00 PM", (130, 117)) today_banner = displayio.Group(max_size=10) today_banner.append(today_date) today_banner.append(city_name) today_banner.append(today_icon) today_banner.append(today_morn_temp) today_banner.append(today_day_temp) today_banner.append(today_night_temp) today_banner.append(today_humidity) today_banner.append(today_wind) today_banner.append(today_sunrise) today_banner.append(today_sunset) future_banners = [ make_banner(icons_small_bmp, icons_small_pal, x=210, y=18), make_banner(icons_small_bmp, icons_small_pal, x=210, y=39), make_banner(icons_small_bmp, icons_small_pal, x=210, y=60), make_banner(icons_small_bmp, icons_small_pal, x=210, y=81), make_banner(icons_small_bmp, icons_small_pal, x=210, y=102), ] magtag.splash.append(today_banner) for future_banner in future_banners: magtag.splash.append(future_banner) print("Getting Lat/Lon...") latlon = get_latlon(magtag) print('Location: {}'.format(latlon)) print("Fetching forecast...") forecast_data, utc_time, local_tz_offset = get_forecast(magtag, latlon) print("Updating...") date = time.localtime(forecast_data[0]["dt"]) sunrise = time.localtime(forecast_data[0]["sunrise"] + local_tz_offset) sunset = time.localtime(forecast_data[0]["sunset"] + local_tz_offset) today_date.text = "{} {}, {} ({})".format( const.MONTHS[date.tm_mon - 1], date.tm_mday, date.tm_year, const.DAYS[date.tm_wday]) today_icon[0] = ICON_MAP.index(forecast_data[0]["weather"][0]["icon"][:2]) today_morn_temp.text = temperature_text(forecast_data[0]["temp"]["morn"]) today_day_temp.text = temperature_text(forecast_data[0]["temp"]["day"]) today_night_temp.text = temperature_text(forecast_data[0]["temp"]["night"]) today_humidity.text = "{:3d}%".format(forecast_data[0]["humidity"]) today_wind.text = wind_text(forecast_data[0]["wind_speed"]) today_sunrise.text = "{:2d}:{:02d} AM".format(sunrise.tm_hour, sunrise.tm_min) today_sunset.text = "{:2d}:{:02d} PM".format(sunset.tm_hour - 12, sunset.tm_min) for day, forecast in enumerate(forecast_data[1:6]): update_banner(future_banners[day], forecast)