Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4"""
5This example will access the coindesk API, grab a number like bitcoin value in
6USD and display it on a screen
7If you can find something that spits out JSON data, we can display it!
8
9You can find any resources in the associated Learn Guide at:
10https://learn.adafruit.com/pyportal-bitcoin-value-display
11
12Note: This library is designed to run on CPython and not CircuitPython.
13"""
14import os
15import time
16from adafruit_pyportal import PyPortal
17
18# You can display in 'GBP', 'EUR' or 'USD'
19CURRENCY = "USD"
20# Set up where we'll be fetching data from
21DATA_SOURCE = "https://api.coindesk.com/v1/bpi/currentprice.json"
22DATA_LOCATION = ["bpi", CURRENCY, "rate_float"]
23
24
25def text_transform(val):
26 if CURRENCY == "USD":
27 return "$%d" % val
28 if CURRENCY == "EUR":
29 return "€%d" % val
30 if CURRENCY == "GBP":
31 return "£%d" % val
32 return "%d" % val
33
34
35# the current working directory (where this file is)
36try:
37 cwd = os.path.dirname(os.path.realpath(__file__))
38except AttributeError:
39 cwd = ("/" + __file__).rsplit("/", 1)[0]
40
41pyportal = PyPortal(
42 url=DATA_SOURCE,
43 json_path=DATA_LOCATION,
44 default_bg=cwd + "/bitcoin_background.bmp",
45 text_font=cwd + "/fonts/Arial-Bold-24-Complete.bdf",
46 text_position=(195, 130),
47 text_color=0x0,
48 text_transform=text_transform,
49)
50pyportal.preload_font(b"$012345789") # preload numbers
51pyportal.preload_font((0x00A3, 0x20AC)) # preload gbp/euro symbol
52
53while True:
54 try:
55 value = pyportal.fetch()
56 print("Response is", value)
57 except (ValueError, RuntimeError) as e:
58 print("Some error occured, retrying! -", e)
59
60 time.sleep(3 * 60) # wait 3 minutes