LibrariesWe'll need to make sure we have these libraries installed. (Check out this link on installing libraries if needed.)
|
Connect to the Internet
Once you have CircuitPython setup and libraries installed we can get your board connected to the Internet. The process for connecting can be found here.
Text Editor
Adafruit recommends using the Mu editor for editing your CircuitPython code. You can get more info in this guide.
Alternatively, you can use any text editor that saves simple text files.
Code
Copy the code from the code-block below and paste it into the Mu editor and save it to your Metro M4 Airlift as code.py (or copy code.py from the zip file and place on the CIRCUITPY drive).
# Run on Metro M4 Airlift w RGB Matrix shield and 64x32 matrix display # show current value of Bitcoin in USD 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"] def text_transform(val): if CURRENCY == "USD": return "$%d" % val if CURRENCY == "EUR": return "€%d" % val if CURRENCY == "GBP": return "£%d" % val return "%d" % 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, default_bg=cwd + "/bitcoin_background.bmp", debug=False, ) matrixportal.add_text( text_font=terminalio.FONT, text_position=(27, 16), text_color=0x3d1f5c, text_transform=text_transform, ) matrixportal.preload_font(b"$012345789") # preload numbers matrixportal.preload_font((0x00A3, 0x20AC)) # preload gbp/euro symbol while True: try: value = matrixportal.fetch() print("Response is", value) except (ValueError, RuntimeError) as e: print("Some error occured, retrying! -", e) time.sleep(3 * 60) # wait 3 minutes
Background BitmapFrom the project zip file, drag the bitcoin_background.bmp file onto the Feather's CIRCUITPY drive. This is a 64x32 pixel .bmp file, designed for this use on this display -- you can edit it or swap in your own graphics if you like. |
How it Works
First we'll import some libraries to help with the heavy lifting. The time
library will allow us to pause (or sleep) for three minutes between check of the Bitcoin value index.
The board
library gives us access to the Metro M4 Airlift's pin definitions.
The terminalio
library will be used for the price font.
And, last but not least, adafruit_matrixportal
library will handle online connectivity and json file parsing when checking the Coindesk data.
Currency and Data
The CURRENCY
variable can be set to either GBP
, EUR
, or USD
. These are the three key values supported by the Coindesk API.
Next well set the data source of the Coindesk json file as DATA_SOURCE = "https://api.coindesk.com/v1/bpi/currentprice.json"
If you go to codebeautify.org/jsonviewer and enter the above URL you'll see the json file with output like this:
The DATA_LOCATION
variable provides the data key:value hierarchy we need to traverse. DATA_LOCATION = ["bpi", CURRENCY, "rate_float"]
when parsed from the above json file will return the value of 11762.7728 for USD
.
Text Transform
The text_transform()
function will be used to turn the rate float value into a string we can display, including the currency symbol, such as $11762
Matrixportal Setup
Next, the MatrixPortal
object is created with the url
, json_path
, status_neopixel
, background bitmap file, and debug
arguments set.
Text is added to the matrixportal object so it can display the Bitcoin value string using terminalio.FONT
, at an x/y position of 27, 16, in a nice blue color.
The glyphs (characters) that will be used are preloaded for speed.
Main Loop
In the main loop of the program we simply run value - matrixportal.fetch()
. This will grab the json file, parse it, and updated the display with the current value.
Then, we use time.sleep(3 * 60)
to wait three minutes until the next check!