1
0
mirror of https://gitlab.com/triple-hops-brewed/raspberry-pi-stock-ticker.git synced 2025-06-16 23:26:40 +00:00

added a ton of code to work with bdfparse

This commit is contained in:
Anson 2019-07-18 13:36:15 -07:00
parent bb25ec8081
commit faf029b9d9

108
stock.py
View File

@ -1,22 +1,37 @@
# Display a runtext with double-buffering.
from rgbmatrix import graphics, RGBMatrix, RGBMatrixOptions
import time
import sys import sys
import time
from math import ceil
import numpy as np
import requests import requests
from bdfparse import Font
from keys import IEX_TOKEN
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
font_file = "matrix\\fonts\\9x18.bdf"
font = Font(font_file)
matrix_shape = (16, 32) # rows, cols
stocks = ["tsla", "psec", "aapl"]
options = RGBMatrixOptions() options = RGBMatrixOptions()
options.rows = 16
options.cols = 32
options.chain_length = 1 options.chain_length = 1
options.parallel = 1 options.parallel = 1
options.hardware_mapping = "regular" options.hardware_mapping = "regular"
options.rows, options.cols = matrix_shape
red = (255, 51, 51)
green = (51, 255, 51)
white = (255, 255, 255)
def symbolData(symbol: str): def symbolData(symbol: str):
""" """
Takes a list of symbol and returns a dictionary of strings with information about the symbol. Takes a list of symbol and returns a dictionary of strings with information about the symbol.
""" """
IEX_TOKEN = "TOKEN HERE"
IEXurl = "https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={IEX}".format( IEXurl = "https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={IEX}".format(
symbol=symbol, IEX=IEX_TOKEN symbol=symbol, IEX=IEX_TOKEN
) )
@ -31,44 +46,81 @@ def symbolData(symbol: str):
# Determine wording of change text # Determine wording of change text
change = round(IEXData["changePercent"] * 100, 2) change = round(IEXData["changePercent"] * 100, 2)
if change > 0: if change > 0:
message += ", the stock is currently **up {change}%**".format(change=change) message += ", the stock is currently up {change}%".format(change=change)
color = green
elif change < 0: elif change < 0:
message += ", the stock is currently **down {change}%**".format( message += ", the stock is currently down {change}%".format(change=change)
change=change color = red
)
else: else:
message += ", the stock hasn't shown any movement today." message += ", the stock hasn't shown any movement today."
color = white
else: else:
message = "The symbol: {symbol} was not found.".format(symbol=symbol) message = "The symbol: {symbol} was not found.".format(symbol=symbol)
color = white
return message return (message, color)
def run(): def run(pos):
matrix = RGBMatrix(options=options) matrix = RGBMatrix(options=options)
offscreen_canvas = matrix.CreateFrameCanvas() canvas = matrix.CreateFrameCanvas()
font = graphics.Font()
font.LoadFont("../../../fonts/7x13.bdf")
textColor = graphics.Color(255, 255, 0)
pos = offscreen_canvas.width
my_text = "loading"
while True: for row in range(options.rows):
offscreen_canvas.Clear() for col in range(options.cols):
len = graphics.DrawText(offscreen_canvas, font, pos, 10, textColor, my_text) canvas.SetPixel(x, y, 255, 1, 1)
pos -= 1
if pos + len < 0:
my_text = symbolData("tsla")
pos = offscreen_canvas.width
time.sleep(0.05) canvas = matrix.SwapOnVSync(canvas)
offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas) time.sleep(1)
try: try:
print("Press CTRL-C to stop.") print("Press CTRL-C to stop.")
pos = 0
while True: while True:
run() run(pos)
pos += 1
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit(0) sys.exit(0)
def color_message(message, color):
message_arr = font.word(message)
r = np.multiply(np.full(message_arr.shape, color[0]), message_arr)
g = np.multiply(np.full(message_arr.shape, color[1]), message_arr)
b = np.multiply(np.full(message_arr.shape, color[2]), message_arr)
array = np.zeros((message_arr.shape[0], message_arr.shape[1], 3), dtype=int)
array[:, :, 0] = np.add(array[:, :, 0], r)
array[:, :, 1] = np.add(array[:, :, 1], g)
array[:, :, 2] = np.add(array[:, :, 2], b)
return array
def fit_array(array, shape, operation="centered", fill_value=0):
rows = np.subtract(shape, array.shape)[0]
cols = shape[1]
offset = (rows, cols)
if operation is "centered":
top = ceil(rows / 2)
top_fill = np.full((top, cols), fill_value)
bottom = rows // 2
bottom_fill = np.full((bottom, cols), fill_value)
return np.concatenate((top_fill, array, bottom_fill), axis=0)
elif operation is "top":
fill = np.full(offset, fill_value)
return np.concatenate((fill, array), axis=0)
elif operation is "bottom":
fill = np.full(offset, fill_value)
return np.concatenate((array, fill), axis=0)
else:
raise Exception("Invalid Operation. Must be either centered, top, or bottom.")