Ich will den aktuellen Börsenkurs einer Cryptowährung über ein 8x32 LED Matrix Display anzeigen lassen.
Ich hab dazu schon 3 threads erstellt - (CMC)Data json.load, (REFRESH)image refresh & (MATRIX)scrolling text.
Die 3 threads laufen schon parallel.
Aber wie kann ich jetzt die Daten von (CMC) immer aktualisiert am Display anzeigen lassen?
Ich bekomme die Variable text nicht von einem thread zum anderen.
Vielen lieben Dank
mfg
Christian
#!/usr/bin/env python3
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import threading
import json
import time
import sys
import board
import neopixel
from colorsys import hsv_to_rgb
from PIL import Image, ImageDraw, ImageFont
text = "### FTM $ 2.624 | 1h -0.705 % | 24h -8.476 % | 7d -6.670 % ### TOTAL T$ 2.665E+12 | -2.431 % ###"
pixel_pin = board.D18
num_pixels = 256
display_width = 32
display_height = 8
matrixbrightness = 0.1
scrollSpeed = 0.2 #adjust the scrolling speed here-> smaller number=faster scroll
TextColor = (55,55,255) #set the color of your text here in RGB, default is white
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=matrixbrightness, auto_write=False, pixel_order=ORDER)
rotation = 0
#load your font
#font = ImageFont.truetype("LiberationMono-Regular.ttf",

#5x7.ttf font is easier to read and available for download for personal use from the Internet
font = ImageFont.truetype("5x7.ttf",

def CMC():
global text
while True :
####TOTAL
url1 = 'https://pro-api.coinmarketcap.com/v1/gl ... tes/latest'
parameters1 = {'convert':'USD',}
####FTM
url2 = 'https://pro-api.coinmarketcap.com/v1/cr ... tes/latest'
parameters2 = {'convert':'USD', 'id': '3513',}
####CODE
# headers = {'Accepts': 'application/json', 'X-CMC_PRO_API_KEY': '', }
session = Session()
session.headers.update(headers)
try:
x = session.get(url1, params=parameters1)
data1 = json.loads(x.text)
y = session.get(url2, params=parameters2)
data2 = json.loads(y.text)
####FTM
print('###',data2["data"]["3513"]["symbol"],end=' $ ')
print("%5.3f" % (data2["data"]["3513"]["quote"]["USD"]["price"]),end=' ')
print('| 1h ' "%5.3f" % (data2["data"]["3513"]["quote"]["USD"]["percent_change_1h"]),end=' % ')
print('| 24h ' "%5.3f" % (data2["data"]["3513"]["quote"]["USD"]["percent_change_24h"]),end=' % ')
print('| 7d ' "%5.3f" % (data2["data"]["3513"]["quote"]["USD"]["percent_change_7d"]),'% ###', end=' ')
####TOTAL
print('TOTAL T$',"%1.3E"% data1["data"]["quote"]["USD"]["total_market_cap"],end=' | ')
print("%5.3f" % data1["data"]["quote"]["USD"]["total_market_cap_yesterday_percentage_change"],'% ###')
# a = ('TOTAL T$',"%1.3E"% data1["data"]["quote"]["USD"]["total_market_cap"])
# text = a
# return text
time.sleep(200)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
#for the Adafruit NeoMatrix grid
def getIndex(x, y):
x = display_width-x-1
return (x*8)+y
#use for the flex grid
def getIndex2(x, y):
x = display_width-x-1
if x % 2 != 0:
return (x*8)+y
else:
return (x*8)+(7-y)
if len(sys.argv) > 1:
try:
rotation = int(sys.argv[1])
except ValueError:
print("Usage: {} <rotation>".format(sys.argv[0]))
sys.exit(1)
def REFRESH():
global image
# Measure the size of our text
text_width, text_height = font.getsize(text)
# Create a new PIL image big enough to fit the text
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
# Draw the text into the image
draw.text((display_width, -1), text, font=font, fill=255)
image.save("img.png", "PNG")
offset_x = 0
#andere schleife parallel gleich aufgebaut
def MATRIX(offset_x = 0):
global image
image =Image.open(r"img.png")
while True :
for x in range(display_width):
for y in range(display_height):
if image.getpixel((x + offset_x, y)) == 255:
pixels[getIndex2(x,y)] = TextColor
else:
pixels[getIndex2(x,y)] = (0, 0, 0)
offset_x += 1
if offset_x + display_width > image.size[0]:
offset_x = 0
pixels.show()
time.sleep(scrollSpeed) #scrolling text speed
thread1 = threading.Thread(target=CMC)
thread1.start()
thread2 = threading.Thread(target=REFRESH)
thread2.start()
thread3 = threading.Thread(target=MATRIX)
thread3.start()