import urequests
import time
import board
import neopixel
import adafruit_bitmap_font as font
import adafruit_framebuf as framebuf
# Set up NeoPixel matrix
np = neopixel.NeoPixel(board.GP0, 512, brightness=0.5)
# Set up bitmap font
font = font.BitmapFont("/fonts/arial_12.bdf")
buf = bytearray(16 * 32 // 8)
fb = framebuf.FrameBuffer(buf, 16, 32, framebuf.MONO_HLSB)
# API endpoint to get current Bitcoin price in USD
bitcoin_api_url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
def get_bitcoin_price():
response = urequests.get(bitcoin_api_url)
data = response.json()
price = data['bpi']['USD']['rate_float']
return price
def display_price_on_matrix(price):
# Convert price to a string
price_str = '${:,.2f}'.format(price)
# Pad the price string with leading zeros if necessary
num_zeros = 8 - len(price_str)
price_str = '0' * num_zeros + price_str
# Set the starting position for displaying the price
x = 0
# Loop through each character in the price string
for char in price_str:
# Get the LED matrix for the character from the bitmap font
matrix, width, height, dx, dy = font.get_ch(char)
# Clear the framebuffer and draw the character in it
fb.fill(0)
fb.blit(matrix, x - dx, 0)
# Loop through each LED in the matrix and turn on the corresponding LED on the NeoPixel matrix
for i in range(16):
for j in range(32):
if fb.pixel(i, j):
np[j * 16 + i] = (255, 255, 255)
else:
np[j * 16 + i] = (0, 0, 0)
# Move the display position to the right by the width of the character plus one
x += width + 1
# Update the NeoPixel matrix
np.show()
while True:
price = get_bitcoin_price()
display_price_on_matrix(price)
time.sleep(60) # Update once per minute