local caching implemented and smooth image transition

This commit is contained in:
Neythen 2021-05-05 15:44:43 +01:00
parent bcc8e551ae
commit f13e02c4c2
5 changed files with 77 additions and 72 deletions

View File

@ -1,11 +1,12 @@
MSFT MSFT,248.07,249.06
NFLX NFLX,502.91,504.99
GOOG GOOG,2365.73,2368.42
TSLA TSLA,671,681.06
AAPL AAPL,129.37,129.2
INTC INTC,56.87,56.96
TXN TXN,180.66,181.01
HPQ HPQ,34.04,34.24
HOG HOG,48.42,48.33
LUV LUV,61.4,60.77
WMT WMT,140.88,140.71
BJ,45.06,45.17

1 MSFT 248.07 249.06
2 NFLX 502.91 504.99
3 GOOG 2365.73 2368.42
4 TSLA 671 681.06
5 AAPL 129.37 129.2
6 INTC 56.87 56.96
7 TXN 180.66 181.01
8 HPQ 34.04 34.24
9 HOG 48.42 48.33
10 LUV 61.4 60.77
11 WMT 140.88 140.71
12 BJ 45.06 45.17

BIN
final.ppm

Binary file not shown.

View File

@ -16,6 +16,8 @@ import pexpect
from multiprocessing import Process from multiprocessing import Process
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
#stock_ticker = StockTicker() #stock_ticker = StockTicker()
api_caller = pexpect.spawn("sudo -E python3 api_caller.py")
command = 300 command = 300
tickerList = 0 tickerList = 0
@ -162,6 +164,7 @@ def matrix():
LastCommand = 'Run display' LastCommand = 'Run display'
elif "Stop Display (at next refresh)" in request.form: elif "Stop Display (at next refresh)" in request.form:
pass pass
print('run display')
#ticker.kill() #ticker.kill()
try: try:
#stock_ticker.stopStockTicker() #stock_ticker.stopStockTicker()

View File

@ -13,9 +13,9 @@ from PIL import Image, ImageDraw, ImageFont
import time import time
import csv import csv
import requests import requests
import pexpect
from rgbmatrix import RGBMatrix, RGBMatrixOptions from rgbmatrix import RGBMatrix, RGBMatrixOptions
import finnhub
def getInput(Block=False): def getInput(Block=False):
if Block or select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []): if Block or select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
@ -31,8 +31,7 @@ class StockTicker():
def __init__(self): def __init__(self):
#Define global resources #Define global resources
self.symbols = [] self.symbols = []
self.ApiKey = "c24qddqad3ickpckgg80"
self.SandboxApiKey = "sandbox_c24qddqad3ickpckgg8g"
self.greenORred = (255, 255, 255) self.greenORred = (255, 255, 255)
#self.blank = Image.open('logos/blank.png') #self.blank = Image.open('logos/blank.png')
self.blank = Image.new('RGB', (0, 32)) self.blank = Image.new('RGB', (0, 32))
@ -49,7 +48,7 @@ class StockTicker():
options.hardware_mapping = 'adafruit-hat' # If you have an Adafruit HAT: 'adafruit-hat' options.hardware_mapping = 'adafruit-hat' # If you have an Adafruit HAT: 'adafruit-hat'
options.gpio_slowdown = 3 options.gpio_slowdown = 3
self.matrix = RGBMatrix(options = options) self.matrix = RGBMatrix(options = options)
self.finnhubClient = finnhub.Client(api_key=self.ApiKey)
def openImage(self, image_file): def openImage(self, image_file):
image = Image.open(image_file) image = Image.open(image_file)
@ -103,22 +102,30 @@ class StockTicker():
# use two image files and switch between them with a seemless transition # use two image files and switch between them with a seemless transition
current_img = 1 current_img = 1
kill = False
while True: while True:
if current_img == 1: if current_img == 1:
image = self.openImage(image_files[0]) image1 = self.openImage(image_files[0])
image2 = self.openImage(image_files[1])
elif current_img == 2: elif current_img == 2:
image = self.openImage(image_files[1]) image1 = self.openImage(image_files[1])
image2 = self.openImage(image_files[0])
img_width, img_height = image.size img_width, img_height = image1.size
print('wh:', self.matrix.width, self.matrix.height)
while offset_x > -img_width: while offset_x > -img_width:
offset_x -= 1 offset_x -= 1
self.setImage(image, offset_x = offset_x, offset_y = offset_y) self.setImage(image1, offset_x = offset_x, offset_y = offset_y)
if offset_x + img_width < self.matrix.width: # if the image is ending
self.setImage(image2, offset_x = offset_x + img_width, offset_y = offset_y)
time.sleep(self.delay) time.sleep(self.delay)
@ -126,6 +133,7 @@ class StockTicker():
msg = getInput() msg = getInput()
if msg == 'K': if msg == 'K':
self.resetMatrix() self.resetMatrix()
kill = True
break break
elif msg == 's': elif msg == 's':
@ -143,6 +151,9 @@ class StockTicker():
pass pass
if kill: break
if current_img == 1: if current_img == 1:
current_img = 2 current_img = 2
elif current_img == 2: elif current_img == 2:
@ -223,26 +234,6 @@ class StockTicker():
print('width:', im.size[0]) print('width:', im.size[0])
return new_im return new_im
#Get current prices and day prices for the ticker then format the response into an array
def getStockPrices(self, symbols):
apiCalledError = False
stock_info = []
try:
quotes = [self.finnhubClient.quote(symbol) for symbol in symbols]
current_prices = [quote['c'] for quote in quotes]
opening_prices = [quote['o'] for quote in quotes]
for i, symbol in enumerate(symbols):
stock_info.append([symbol, current_prices[i], opening_prices[i]])
except Exception as e:
print("Could not fetch data - API CALLS REACHED? - Will display old image")
print(e)
apiCalledError = True
return stock_info, apiCalledError
def resetMatrix(self): def resetMatrix(self):
for x in range(self.matrix.width): for x in range(self.matrix.width):
for y in range(self.matrix.height): for y in range(self.matrix.height):
@ -256,15 +247,17 @@ class StockTicker():
image_list = [] image_list = []
start = time.time() start = time.time()
stock_info, apiCalledError = self.getStockPrices(self.symbols) self.readCSV()
if (apiCalledError == False):
for i in range(len(stock_info)):
change = float(stock_info[i][1])-float(stock_info[i][2]) #TEXT
ticker = stock_info[i][0] #TEXT
current = '%.2f' % float(stock_info[i][1]) #TEXT
logo = self.getLogo(stock_info[i][0]) for i, symbol in enumerate(self.symbols):
info = self.stock_info[symbol]
change = float(info[0])-float(info[1]) #TEXT
ticker = symbol #TEXT
current = '%.2f' % float(info[0]) #TEXT
logo = self.getLogo(symbol)
arrow, change = self.getArrow(change) arrow, change = self.getArrow(change)
change = '%.2f' % change change = '%.2f' % change
midFrame = self.textToImage(ticker, current, change, arrow) #IMAGE THE TEXT midFrame = self.textToImage(ticker, current, change, arrow) #IMAGE THE TEXT
@ -273,28 +266,34 @@ class StockTicker():
image_list.append(self.blank) image_list.append(self.blank)
image_list.append(stitchedStock) image_list.append(stitchedStock)
print('elapsed time loop ended: ' + str((time.time()-start)))
if (apiCalledError == False):
finalDisplayImage = self.stitchImage(image_list) finalDisplayImage = self.stitchImage(image_list)
finalDisplayImage.save('final.ppm') finalDisplayImage.save('final.ppm')
#Send the final stitched image to the display for set amount of time #Send the final stitched image to the display for set amount of time
def displayMatrix(self): def displayMatrix(self):
#os.system("sudo ./demo -D1 final.ppm -t " + str(displayTime) +" -m "+ str(speedDisplay) +" --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=256") #os.system("sudo ./demo -D1 final.ppm -t " + str(displayTime) +" -m "+ str(speedDisplay) +" --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=256")
#os.system("sudo ./demo -D1 final.ppm -t " + str(self.displayTime) +" -m "+ str(self.speedDisplay) +" --led-gpio-mapping=adafruit-hat --led-rows=64 --led-cols=64 --led-slowdown-gpio=4 ") #os.system("sudo ./demo -D1 final.ppm -t " + str(self.displayTime) +" -m "+ str(self.speedDisplay) +" --led-gpio-mapping=adafruit-hat --led-rows=64 --led-cols=64 --led-slowdown-gpio=4 ")
self.scrollImageTransition(['final.ppm', 'final.ppm'], offset_x = 0, offset_y = 0) self.scrollImageTransition(['final.ppm', 'final.ppm'], offset_x = 0, offset_y = 0)
#Retrieve symbols from the CSV file #Retrieve symbols and stock info from the csv file
def getSymbols(self): def readCSV(self):
self.symbols = [] self.symbols = []
CSV = csv.reader(open('csv/tickers.csv', 'r')) self.stock_info = {}
f = open('csv/tickers.csv', 'r')
CSV = csv.reader(f)
for row in CSV: for row in CSV:
print(row) print(row)
self.symbols.append(row[0]) try:
symbol, current_price, opening_price = row
self.symbols.append(symbol)
self.stock_info[symbol] = [current_price, opening_price]
except:
symbol = row[0]
self.symbols.append(symbol)
self.stock_info[symbol] = []
f.close()
print(self.symbols) print(self.symbols)
@ -337,9 +336,11 @@ if __name__ == '__main__':
#print(sys.stdin.readlines()) #print(sys.stdin.readlines())
stock_ticker = StockTicker() stock_ticker = StockTicker()
stock_ticker.getSymbols() #t = time.time()
#stock_ticker.getFullStockImage() #api_caller = pexpect.spawn("sudo -E python3 api_caller.py")
#stock_ticker.displayMatrix() #print('time to call api', time.time()-t)
stock_ticker.getFullStockImage()
stock_ticker.displayMatrix()
while True: while True: