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
NFLX
GOOG
TSLA
AAPL
INTC
TXN
HPQ
HOG
LUV
WMT
MSFT,248.07,249.06
NFLX,502.91,504.99
GOOG,2365.73,2368.42
TSLA,671,681.06
AAPL,129.37,129.2
INTC,56.87,56.96
TXN,180.66,181.01
HPQ,34.04,34.24
HOG,48.42,48.33
LUV,61.4,60.77
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 subprocess import Popen, PIPE
#stock_ticker = StockTicker()
api_caller = pexpect.spawn("sudo -E python3 api_caller.py")
command = 300
tickerList = 0
@ -162,6 +164,7 @@ def matrix():
LastCommand = 'Run display'
elif "Stop Display (at next refresh)" in request.form:
pass
print('run display')
#ticker.kill()
try:
#stock_ticker.stopStockTicker()

View File

@ -13,9 +13,9 @@ from PIL import Image, ImageDraw, ImageFont
import time
import csv
import requests
import pexpect
from rgbmatrix import RGBMatrix, RGBMatrixOptions
import finnhub
def getInput(Block=False):
if Block or select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
@ -31,8 +31,7 @@ class StockTicker():
def __init__(self):
#Define global resources
self.symbols = []
self.ApiKey = "c24qddqad3ickpckgg80"
self.SandboxApiKey = "sandbox_c24qddqad3ickpckgg8g"
self.greenORred = (255, 255, 255)
#self.blank = Image.open('logos/blank.png')
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.gpio_slowdown = 3
self.matrix = RGBMatrix(options = options)
self.finnhubClient = finnhub.Client(api_key=self.ApiKey)
def openImage(self, 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
current_img = 1
kill = False
while True:
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:
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:
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)
@ -126,6 +133,7 @@ class StockTicker():
msg = getInput()
if msg == 'K':
self.resetMatrix()
kill = True
break
elif msg == 's':
@ -143,6 +151,9 @@ class StockTicker():
pass
if kill: break
if current_img == 1:
current_img = 2
elif current_img == 2:
@ -223,26 +234,6 @@ class StockTicker():
print('width:', im.size[0])
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):
for x in range(self.matrix.width):
for y in range(self.matrix.height):
@ -256,45 +247,53 @@ class StockTicker():
image_list = []
start = time.time()
stock_info, apiCalledError = self.getStockPrices(self.symbols)
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])
arrow, change = self.getArrow(change)
change = '%.2f' % change
midFrame = self.textToImage(ticker, current, change, arrow) #IMAGE THE TEXT
stitchedStock = self.stitchImage([logo,midFrame])
image_list.append(self.blank)
image_list.append(stitchedStock)
self.readCSV()
print('elapsed time loop ended: ' + str((time.time()-start)))
if (apiCalledError == False):
finalDisplayImage = self.stitchImage(image_list)
finalDisplayImage.save('final.ppm')
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)
change = '%.2f' % change
midFrame = self.textToImage(ticker, current, change, arrow) #IMAGE THE TEXT
stitchedStock = self.stitchImage([logo,midFrame])
image_list.append(self.blank)
image_list.append(stitchedStock)
finalDisplayImage = self.stitchImage(image_list)
finalDisplayImage.save('final.ppm')
#Send the final stitched image to the display for set amount of time
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(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)
#Retrieve symbols from the CSV file
def getSymbols(self):
#Retrieve symbols and stock info from the csv file
def readCSV(self):
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:
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)
@ -337,9 +336,11 @@ if __name__ == '__main__':
#print(sys.stdin.readlines())
stock_ticker = StockTicker()
stock_ticker.getSymbols()
#stock_ticker.getFullStockImage()
#stock_ticker.displayMatrix()
#t = time.time()
#api_caller = pexpect.spawn("sudo -E python3 api_caller.py")
#print('time to call api', time.time()-t)
stock_ticker.getFullStockImage()
stock_ticker.displayMatrix()
while True: