|
| 1 | +# Run on Metro M4 Airlift w RGB Matrix shield and 64x32 matrix display |
| 2 | +# show current value of Bitcoin in USD |
| 3 | + |
| 4 | +import time |
| 5 | +import board |
| 6 | +import terminalio |
| 7 | +from adafruit_matrixportal.matrixportal import MatrixPortal |
| 8 | + |
| 9 | +# You can display in 'GBP', 'EUR' or 'USD' |
| 10 | +CURRENCY = "USD" |
| 11 | +# Set up where we'll be fetching data from |
| 12 | +DATA_SOURCE = "https://api.coindesk.com/v1/bpi/currentprice.json" |
| 13 | +DATA_LOCATION = ["bpi", CURRENCY, "rate_float"] |
| 14 | + |
| 15 | + |
| 16 | +def text_transform(val): |
| 17 | + if CURRENCY == "USD": |
| 18 | + return "$%d" % val |
| 19 | + if CURRENCY == "EUR": |
| 20 | + return "€%d" % val |
| 21 | + if CURRENCY == "GBP": |
| 22 | + return "£%d" % val |
| 23 | + return "%d" % val |
| 24 | + |
| 25 | + |
| 26 | +# the current working directory (where this file is) |
| 27 | +cwd = ("/" + __file__).rsplit("/", 1)[0] |
| 28 | + |
| 29 | +matrixportal = MatrixPortal( |
| 30 | + url=DATA_SOURCE, |
| 31 | + json_path=DATA_LOCATION, |
| 32 | + status_neopixel=board.NEOPIXEL, |
| 33 | + default_bg=cwd + "/bitcoin_background.bmp", |
| 34 | + debug=True, |
| 35 | +) |
| 36 | + |
| 37 | +matrixportal.add_text( |
| 38 | + text_font=terminalio.FONT, |
| 39 | + text_position=(27, 16), |
| 40 | + text_color=0x3d1f5c, |
| 41 | + text_transform=text_transform, |
| 42 | +) |
| 43 | +matrixportal.preload_font(b"$012345789") # preload numbers |
| 44 | +matrixportal.preload_font((0x00A3, 0x20AC)) # preload gbp/euro symbol |
| 45 | + |
| 46 | +while True: |
| 47 | + try: |
| 48 | + value = matrixportal.fetch() |
| 49 | + print("Response is", value) |
| 50 | + except (ValueError, RuntimeError) as e: |
| 51 | + print("Some error occured, retrying! -", e) |
| 52 | + |
| 53 | + time.sleep(3 * 60) # wait 3 minutes |
0 commit comments