def display_weather(weather_data): """ Displays the weather data on inky display """ global icons, masks global inkyphat if weather_data['condition_code'] in icon_mapping: icon_current = icon_mapping[weather_data['condition_code']] tolog("...icon %s" % (icon_current)) else: icon_current = None tolog("...no icon found") if not has_inky: print("Current weather:") print("Temperature = %s" % (weather_data['temp'])) print("Time weather = %s" % (weather_data['time'])) print("Condition = %s" % (weather_data['condition_name'])) return True if icon_current is not None: inkyphat.paste(icons[icon_current], (8, 44), masks[icon_current]) else: inkyphat.text((16, 54), "?", inkyphat.RED, font=font20) inkyphat.text((14, 84), u"{:.0f}°".format(weather_data['temp']), inkyphat.RED, font=font18) inkyphat.text((3, 32), weather_data['time'], inkyphat.BLACK, font=font18) return True
def printBusTimes(times): prefix = ["82:", "Rosa:", bustimes.Either("158:", "82B:")] m = "min" op = [] toL5 = lambda s: " " * (max(5 - len(s), 0)) + s for i, ts in enumerate(times): if type(ts) is bustimes.Either: pf = prefix[i].left if ts.left else prefix[i].right next_times = ts.left if ts.left else ts.right else: pf = prefix[i] next_times = ts s = [pf, "-", "-"] for j, t in enumerate(next_times): s[j + 1] = "Núna!" if t == 0 else str(t) + m op.append(" ".join(map(toL5, s))) bt = "\n".join(op) inkyphat.clear() td = datetime.now().strftime("%Y-%m-%d %H:%M") inkyphat.text(((inkyphat.WIDTH - 16 * fontwidth) // 2, 18), td, inkyphat.RED, font) inkyphat.text((inkyphat.WIDTH // 2 - 14 * fontwidth // 2, 2), "Strætóferðir", inkyphat.RED, bigfont) printlines(bt) vtc = ((inkyphat.WIDTH - 10 * 14 - vt.size[0] - 10) // 2, inkyphat.HEIGHT - 2 - 14) inkyphat.text((vtc[0] + vt.size[0] + 2, vtc[1] - 4), "Västtrafik", inkyphat.RED, bigfont) inkyphat.paste(vt, ((vtc[0], vtc[1] - vt.size[1] + 14 - 2))) inkyphat.show()
def draw_error(rect, exc, font=None): left, top, right, bottom = rect inkyphat.rectangle(rect, fill=inkyphat.WHITE) icon = Image.open(os.path.join(resource_dir, 'error.png')) inkyphat.paste(icon, (left, top), inkyphat.create_mask(icon)) inkyphatwidget.textutil.print_width_limited( (left + 16, top), "%s (%s)" % (exc, type(exc).__name__), inkyphat.RED, font, right - left - 16) inkyphatwidget.request()
def update(self, temperature, weather): if temperature and weather: self.temp = temperature self.weather = weather self.active = True else: self.active = False self.update_colors() try: inkyphat.set_colour(self.color) except ValueError: print('Invalid colour "{}" for V{}\n'.format( self.color, inkyphat.get_version())) if inkyphat.get_version() == 2: sys.exit(1) print('Defaulting to "red"') inkyphat.set_border(inkyphat.BLACK) # Load our backdrop image inkyphat.set_image("resources/backdrop.png") # Let's draw some lines! inkyphat.line((69, 36, 69, 81)) # Vertical line inkyphat.line((31, 35, 184, 35)) # Horizontal top line inkyphat.line((69, 58, 174, 58)) # Horizontal middle line inkyphat.line((169, 58, 169, 58), 2) # Red seaweed pixel :D # And now some text dt = (datetime.datetime.now() + datetime.timedelta(seconds=self.tz)).strftime("%m/%d %H:%M") inkyphat.text((36, 12), dt, inkyphat.WHITE, font=font) inkyphat.text((72, 34), "T", inkyphat.WHITE, font=font) inkyphat.text( (92, 34), u"{:.2f}°".format(self.temp), inkyphat.WHITE if self.temp < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((72, 58), "Active" if self.active else "Disconn", inkyphat.WHITE, font=font) # Draw the current weather icon over the backdrop if self.weather is not None: inkyphat.paste(icons[icon_mapping[self.weather]], (28, 36), masks[icon_mapping[self.weather]]) else: inkyphat.text((28, 36), "?", inkyphat.RED, font=font) self.display()
def draw(rect, state, title, font): left, top, right, bottom = rect inkyphat.rectangle(rect, fill=inkyphat.WHITE) text_left = left icon = status_icon.get(state, None) if icon: inkyphat.paste(icon, (left, top), inkyphat.create_mask(icon)) text_left += 16 inkyphatwidget.textutil.print_width_limited( (text_left, top), title, inkyphat.BLACK, font, right - text_left) inkyphatwidget.request()
def draw_text(position, text, font=None, colour=inkyphat.BLACK): x, y = position x = x + 5 if font is None: font = inkyphat.ImageFont.truetype( inkyphat.fonts.AmaticSC, 20) # The font size here must match test_font w, h = font.getsize(text) mask = inkyphat.Image.new('1', (w, h)) draw = inkyphat.ImageDraw.Draw(mask) draw.text((0, 0), text, 1, font) position = x, y inkyphat.paste(colour, position, mask)
def print_digit(position, digit, colour): o_x, o_y = position num_margin = 2 num_width = 6 num_height = 7 s_y = 11 s_x = num_margin + (digit * (num_width + num_margin)) sprite = text_mask.crop((s_x, s_y, s_x + num_width, s_y + num_height)) inkyphat.paste(colour, (o_x, o_y), sprite)
def draw_text(position, text, font=1, colour=inkyphat.BLACK, rotation=0, size=16): x, y = position if font == 1: font = ImageFont.truetype("/home/pi/tubestatus/BetterPixels.ttf", size) else: font = ImageFont.truetype("/home/pi/tubestatus/Extrude.ttf", size) # font = inkyphat.ImageFont.truetype(inkyphat.fonts.FredokaOne,size) w, h = font.getsize(text) mask = inkyphat.Image.new('1', (w, h)) draw = inkyphat.ImageDraw.Draw(mask) draw.text((0, 0), text, 1, font) mask = mask.rotate(rotation, expand=True) inkyphat.paste(colour, position, mask)
def display_forecast(forecast_data): """ Displays the forecast data on inky display """ global icons, masks global inkyphat if not has_inky: print("Forecast weather:") for day in range(nb_forecast): daily_forecast = forecast_data[day] print("For %s at Noon: Weather is %s, temperature is %s" % (daily_forecast['nameday'], daily_forecast['condition_name'], daily_forecast['temp'])) return True for day in range(nb_forecast): daily_forecast = forecast_data[day] if daily_forecast['condition_code'] in icon_mapping: icon_current = icon_mapping[daily_forecast['condition_code']] tolog("...icon %s" % (icon_current)) else: icon_current = None tolog("...no icon found") # Draw the current weather icon over the backdrop if icon_current is not None: inkyphat.paste(icons[icon_current], (52 + day * 38, 44), masks[icon_current]) else: inkyphat.text((56 + day * 38, 54), "?", inkyphat.RED, font=font20) inkyphat.text((64 + day * 38, 84), u"{:.0f}°".format(daily_forecast['temp']), inkyphat.RED, font=font18) inkyphat.text((64 + day * 38, 32), daily_forecast['nameday'], inkyphat.BLACK, font=font18) return True
def print_digit(position, digit, colour): """Print a single digit using the sprite sheet. Each number is grabbed from the masked sprite sheet, and then used as a mask to paste the desired colour onto Inky pHATs image buffer. """ o_x, o_y = position num_margin = 2 num_width = 6 num_height = 7 s_y = 11 s_x = num_margin + (digit * (num_width + num_margin)) sprite = text_mask.crop((s_x, s_y, s_x + num_width, s_y + num_height)) inkyphat.paste(colour, (o_x, o_y), sprite)
def draw_weather(): weather = get_weather() if weather == None: print 'No weather!' return if ROTATE_180 == True: inkyphat.set_rotation(180) # TODO configurable color inkyphat.set_colour('red') inkyphat.set_border(inkyphat.RED) # background inkyphat.rectangle((0, 0, inkyphat.WIDTH, inkyphat.HEIGHT), inkyphat.RED) # draw columns # (assuming get_weather has returned using NUM_COLS) for i in range(0, NUM_COLS): # draw time label time = weather['hours'][i]['time'] w, h = timeFont.getsize(time) draw_shadowed_text(get_x(w, i), 4, time, timeFont) # draw icon try: # This could be optimized to not load the same file more than once img = Image.open('assets/' + weather['hours'][i]['icon'] + '.png') inkyphat.paste(img, (get_x(30, i), 22)) # drawing icons without transparency as it didn't work with whatever gimp was producing except: print 'Error with icon:' + weather['hours'][i]['icon'] # draw temperature label temp = weather['hours'][i]['temperature'] w, h = temperatureFont.getsize(temp) draw_shadowed_text(get_x(w, i), 56, temp, temperatureFont) # TODO multiple lines if too long draw_shadowed_text(5, 84, weather['summary'], summaryFont) inkyphat.show()
inkyphat.set_image("resources/backdrop.png") # Let's draw some lines! #inkyphat.line((69, 36, 69, 81)) # Vertical line #inkyphat.line((31, 35, 184, 35)) # Horizontal top line #inkyphat.line((69, 58, 174, 58)) # Horizontal middle line #inkyphat.line((169, 58, 169, 58), 2) # Red seaweed pixel :D # And now some text datetime = time.strftime("%d %b %Y") inkyphat.text((50, 3), datetime, inkyphat.WHITE, font=font2) inkyphat.text((160, 24), u"{:.1f}°".format(local_temperature), inkyphat.WHITE if local_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((165, 48), u"{:.1f}°".format(mean_temperature), inkyphat.WHITE if mean_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((1, 72), u"{:.1f}°".format(outside_temperature), inkyphat.WHITE, font=font) inkyphat.text((1, 1), "?", inkyphat.RED, font=font) if boiler_state == "True": bob_name = "resources/bob_2colour_tiny.png" bob_image = Image.open(bob_name) bob_mask = inkyphat.create_mask(bob_image) inkyphat.paste(bob_image, (50, 55), bob_mask) # And show it! inkyphat.show()
#map description from the darksky API icon_map = { "snow": ["snow", "sleet"], "rain": ["rain"], "cloud": ["cloudy", "partly-cloudy-day", "cloudy", "partly-cloudy-night"], "sun": ["clear-day", "clear-night"], "storm": ["thunderstorm", "tornado", "hail"], "wind": ["wind", "fog"] } for icon in icon_map: if iconFromDS in icon_map[icon]: weather_icon = icon break for icon in glob.glob("resources/icon-*.png"): icon_name = icon.split("icon-")[1].replace(".png", "") icon_image = Image.open(icon) icons[icon_name] = icon_image masks[icon_name] = inkyphat.create_mask(icon_image) if weather_icon is not None: inkyphat.paste(icons[weather_icon], (10, 27), masks[weather_icon]) #show current temp inkyphat.text((21, 76), currentTempFormatted, inkyphat.YELLOW, font=fontBig) inkyphat.text((11, 95), "currently. ", inkyphat.BLACK, font=fontSmall) #push to the screen! inkyphat.show()
(160, 24), u"{:.1f}°".format(local_temperature), inkyphat.WHITE if local_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text( (165, 48), u"{:.1f}°".format(mean_temperature), inkyphat.WHITE if mean_temperature < WARNING_TEMP else inkyphat.RED, font=font2) inkyphat.text((1, 72), u"{:.1f}°".format(outside_temperature), inkyphat.WHITE, font=font2) # Draw the current weather icon over the backdrop if weather_icon is not None: inkyphat.paste(icons[weather_icon], (1, 1), masks[weather_icon]) else: inkyphat.text((1, 1), "?", inkyphat.RED, font=font) if boiler_state == "True": bob_name = "resources/bob_2colour_tiny.png" bob_image = Image.open(bob_name) bob_mask = inkyphat.create_mask(bob_image) inkyphat.paste(bob_image, (50, 55), bob_mask) # And show it! inkyphat.show()
#inkyphat.line((69, 58, 174, 58)) # Horizontal middle line #inkyphat.line((169, 58, 169, 58), 2) # Red seaweed pixel :D # And now some text #datetime = time.strftime("%d/%b/%Y") datetime = time.strftime("%d %b %Y") inkyphat.text((50, 3), datetime, inkyphat.WHITE, font=font2) inkyphat.text((160, 24), u"{:.1f}°".format(local_temperature), inkyphat.WHITE if local_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((165, 48), u"{:.1f}°".format(mean_temperature), inkyphat.WHITE if mean_temperature < WARNING_TEMP else inkyphat.RED, font=font2) inkyphat.text((1, 72), u"{:.1f}°".format(outside_temperature), inkyphat.WHITE, font=font2) # Draw the current weather icon over the backdrop if weather_icon is not None: inkyphat.paste(icons[weather_icon], (1, 1), masks[weather_icon]) else: inkyphat.text((1 ,1), "?", inkyphat.RED, font=font) if boiler_state == "True": bob_name = "resources/bob_2colour_tiny.png" bob_image = Image.open(bob_name) bob_mask = inkyphat.create_mask(bob_image) inkyphat.paste(bob_image, (50, 55), bob_mask) # And show it! inkyphat.show()
# Let's draw some lines! inkyphat.line((69, 36, 69, 81)) # Vertical line inkyphat.line((31, 35, 184, 35)) # Horizontal top line inkyphat.line((69, 58, 174, 58)) # Horizontal middle line inkyphat.line((169, 58, 169, 58), 2) # Red seaweed pixel :D # And now some text datetime = time.strftime("%d/%m %H:%M") inkyphat.text((36, 12), datetime, inkyphat.WHITE, font=font) inkyphat.text((72, 34), "T", inkyphat.WHITE, font=font) inkyphat.text((92, 34), u"{:.2f}°".format(temperature), inkyphat.WHITE if temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((72, 58), "P", inkyphat.WHITE, font=font) inkyphat.text((92, 58), "{}".format(pressure), inkyphat.WHITE, font=font) # Draw the current weather icon over the backdrop if weather_icon is not None: inkyphat.paste(icons[weather_icon], (28, 36), masks[weather_icon]) else: inkyphat.text((28, 36), "?", inkyphat.RED, font=font) # And show it! inkyphat.show()
inkyphat.set_image("resources/backdrop.png") # Let's draw some lines! #inkyphat.line((69, 36, 69, 81)) # Vertical line #inkyphat.line((31, 35, 184, 35)) # Horizontal top line #inkyphat.line((69, 58, 174, 58)) # Horizontal middle line #inkyphat.line((169, 58, 169, 58), 2) # Red seaweed pixel :D # And now some text datetime = time.strftime("%d %b %Y") inkyphat.text((50, 3), datetime, inkyphat.WHITE, font=font2) inkyphat.text((160, 24), u"{:.1f}°".format(local_temperature), inkyphat.WHITE if local_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((165, 48), u"{:.1f}°".format(mean_temperature), inkyphat.WHITE if mean_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((1, 72), u"{:.1f}°".format(outside_temperature), inkyphat.WHITE, font=font) inkyphat.text((1, 1), "?", inkyphat.RED, font=font) if boiler_state == "True": bob_name = "resources/bob_2colour_tiny.png" bob_image = Image.open(bob_name) bob_mask = inkyphat.create_mask(bob_image) inkyphat.paste(bob_image, (50, 55), bob_mask) # And show it! inkyphat.show()
def draw_status(rect, status_id): inkyphat.rectangle(rect, fill=inkyphat.WHITE) left, top, right, bottom = rect icon = status_icon.get(status_id) if icon: inkyphat.paste(icon, (left, top), inkyphat.create_mask(icon))
inkyphat.line((31, 35, 184, 35)) # Horizontal top line inkyphat.line((69, 58, 174, 58)) # Horizontal middle line inkyphat.line((169, 58, 169, 58), 2) # Red seaweed pixel :D # And now some text datetime = time.strftime("%d/%b/%Y") inkyphat.text((36, 12), datetime, inkyphat.WHITE, font=font) inkyphat.text((72, 34), u"{:.1f}°".format(local_temperature), inkyphat.WHITE if local_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((122, 34), u"{:.1f}°".format(mean_temperature), inkyphat.WHITE if mean_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((72, 58), u"{:.1f}°".format(outside_temperature), inkyphat.WHITE, font=font) # Draw the current weather icon over the backdrop if weather_icon is not None: inkyphat.paste(icons[weather_icon], (28, 36), masks[weather_icon]) else: inkyphat.text((28, 36), "?", inkyphat.RED, font=font) if boiler_state == "True": bob_name = "resources/bob_2colour.png" bob_image = Image.open(bob_name) bob_mask = inkyphat.create_mask(bob_image) inkyphat.paste(bob_image, (180, 36), bob_mask) # And show it! inkyphat.show()
async def widget(loop, rect): left, top, right, bottom = rect icon = Image.open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'img', 'ahiruyaki.png')) inkyphat.paste(icon, (left, top), inkyphat.create_mask(icon)) inkyphatwidget.request()
month_col = (now.month - 1) % months_cols month_row = (now.month - 1) // months_cols # Convert that location to usable X/Y coordinates month_x = months_x + (month_col * month_w) month_y = months_y + (month_row * month_h) crop_region = (month_x, month_y, month_x + month_w, month_y + month_h) month = text.crop(crop_region) month_mask = text_mask.crop(crop_region) monthyear_x = 28 # Paste in the month name we grabbed from our sprite sheet inkyphat.paste(inkyphat.WHITE, (monthyear_x, cal_y + 4), month_mask) # Print the year right below the month print_number((monthyear_x, cal_y + 5 + col_h), now.year, inkyphat.WHITE) # Draw the vertical lines which separate the columns # and also draw the day names into the table header for x in range(cols): # Figure out the left edge of the column o_x = (col_w + 1) * x o_x += cal_x crop_x = 2 + (16 * x) # Crop the relevant day name from our text image crop_region = ((crop_x, 0, crop_x + 16, 9))
(72, 34), u"{:.1f}°".format(local_temperature), inkyphat.WHITE if local_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text( (122, 34), u"{:.1f}°".format(mean_temperature), inkyphat.WHITE if mean_temperature < WARNING_TEMP else inkyphat.RED, font=font) inkyphat.text((72, 58), u"{:.1f}°".format(outside_temperature), inkyphat.WHITE, font=font) # Draw the current weather icon over the backdrop if weather_icon is not None: inkyphat.paste(icons[weather_icon], (28, 36), masks[weather_icon]) else: inkyphat.text((28, 36), "?", inkyphat.RED, font=font) if boiler_state == "True": bob_name = "resources/bob_2colour.png" bob_image = Image.open(bob_name) bob_mask = inkyphat.create_mask(bob_image) inkyphat.paste(bob_image, (180, 36), bob_mask) # And show it! inkyphat.show()