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