Skip to content

Instantly share code, notes, and snippets.

@willwm
Last active August 23, 2022 16:20
Show Gist options
  • Save willwm/940c41d490e946ce6af0cdf4afe676b9 to your computer and use it in GitHub Desktop.
Save willwm/940c41d490e946ce6af0cdf4afe676b9 to your computer and use it in GitHub Desktop.
MatrixPortal: My Clock
# SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams, written for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
"""
This example checks the current Bitcoin price and displays it in the middle of the screen
"""
import time
import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal
# You can display in 'GBP', 'EUR' or 'USD'
CURRENCY = "USD"
# Set up where we'll be fetching data from
DATA_SOURCE = "https://api.coindesk.com/v1/bpi/currentprice.json"
DATA_LOCATION = ["bpi", CURRENCY, "rate_float"]
color_red = '#FF0000'
color_amber = '#CC4000'
color_greenish = '#85FF00'
# timezone = secrets['timezone']
# api_url = 'http://worldtimeapi.org/api/timezone/' + timezone
def update_time(*, hours=None, minutes=None, show_colon=False):
now = time.localtime() # Get the time values we need
if hours is None:
hours = now[3]
if hours > 12: # Handle times later than 12:59
ampm = "pm"
hours -= 12
elif not hours: # Handle times between 0:00 and 0:59
ampm = "am"
hours = 12
else:
ampm = "am"
if minutes is None:
minutes = now[4]
return "{hours}:{minutes:02d} {ampm}".format(
hours=hours, minutes=minutes, ampm=ampm
)
def update_btc(val):
if CURRENCY == "USD":
return "$%d/BTC" % val
if CURRENCY == "EUR":
return "‎€%d/BTC" % val
if CURRENCY == "GBP":
return "£%d/BTC" % val
return "%d/BTC" % val
def text_transform(val):
return " {time}\n{btc}".format(time=update_time(), btc=update_btc(val))
# the current working directory (where this file is)
cwd = ("/" + __file__).rsplit("/", 1)[0]
matrixportal = MatrixPortal(
url=DATA_SOURCE,
json_path=DATA_LOCATION,
status_neopixel=board.NEOPIXEL,
)
network = matrixportal.network
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(2, 16),
text_color=0x85FF00,
text_transform=text_transform,
)
matrixportal.preload_font(b"$012345789") # preload numbers
matrixportal.preload_font((0x00A3, 0x20AC)) # preload gbp/euro symbol
last_check = None
last_btc_value = None
while True:
if last_check is None or time.monotonic() > last_check + 60:
try:
network.get_local_time()
last_check = time.monotonic()
last_btc_value = matrixportal.fetch()
print("Response is", last_btc_value)
except (ValueError, RuntimeError) as e:
print("Some error occured, retrying! -", e)
#matrixportal.set_text(text_transform(last_btc_value))
time.sleep(1)
# Based on Usage Example from MatrixPortal docs:
# https://docs.circuitpython.org/projects/matrixportal/en/latest/
import time
import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal
from secrets import secrets
# --- Display setup ---
matrixportal = MatrixPortal(
status_neopixel=board.NEOPIXEL,
debug=True
)
# Create a new label with the color and text selected
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(0, (matrixportal.graphics.display.height // 2) - 1),
scrolling=True,
)
SCROLL_DELAY = 0.03
def update_time(*, hours=None, minutes=None, show_colon=False):
now = time.localtime() # Get the time values we need
if hours is None:
hours = now[3]
if hours > 12: # Handle times later than 12:59
ampm = "pm"
hours -= 12
elif not hours: # Handle times between 0:00 and 0:59
ampm = "am"
hours = 12
else:
ampm = "am"
if minutes is None:
minutes = now[4]
return "{hours}:{minutes:02d} {ampm}".format(
hours=hours, minutes=minutes, ampm=ampm
)
contents = [
{ 'text': update_time(), 'color': '#CC4000' },
{ 'text': secrets["timezone"], 'color': '#85FF00' },
]
while True:
for content in contents:
matrixportal.set_text(content['text'])
# Set the text color
matrixportal.set_text_color(content['color'])
# Scroll it
matrixportal.scroll_text(SCROLL_DELAY)
# Based on Usage Example from MatrixPortal docs:
# https://docs.circuitpython.org/projects/matrixportal/en/latest/
import time
import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal
from adafruit_matrixportal.network import Network
from secrets import secrets
SCROLL_DELAY = 0.03
color_red = '#FF0000'
color_amber = '#CC4000'
color_greenish = '#85FF00'
timezone = secrets['timezone']
api_url = 'http://worldtimeapi.org/api/timezone/' + timezone
# --- Display setup ---
matrixportal = MatrixPortal(
url=api_url,
json_path=['datetime'],
status_neopixel=board.NEOPIXEL,
debug=True
)
# Create a new label with the color and text selected
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(0, (matrixportal.graphics.display.height // 2) - 1),
scrolling=True,
)
contents = [
{ "text": timezone, "color": color_amber },
{ 'text': "Young, dumb, and full of ... you know ;)", 'color': color_red },
]
def fetch_time():
try:
value = matrixportal.fetch()
matrixportal.set_text(value)
matrixportal.set_text_color(color_greenish)
matrixportal.scroll_text(SCROLL_DELAY)
print("Response is", value)
except (ValueError, RuntimeError) as e:
print("Some error occured, retrying! -", e)
while True:
fetch_time()
for content in contents:
matrixportal.set_text(content['text'])
# Set the text color
matrixportal.set_text_color(content['color'])
# Scroll it
matrixportal.scroll_text(SCROLL_DELAY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment