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

code now grabs stocks and make matrix

This commit is contained in:
Anson 2019-07-18 17:17:55 -07:00
parent faf029b9d9
commit 621474ca43

View File

@ -10,7 +10,7 @@ from keys import IEX_TOKEN
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
font_file = "matrix\\fonts\\9x18.bdf"
font_file = "matrix\\fonts\\9x15.bdf"
font = Font(font_file)
matrix_shape = (16, 32) # rows, cols
stocks = ["tsla", "psec", "aapl"]
@ -26,6 +26,17 @@ options.rows, options.cols = matrix_shape
red = (255, 51, 51)
green = (51, 255, 51)
white = (255, 255, 255)
blank = np.zeros((matrix_shape[0], matrix_shape[1], 3))
def stockMatrix(symbols: list, matrix=blank):
array = blank
for symbol in symbols:
message, color = symbolData(symbol)
mess_arr = matrix_message(message, color, matrix_shape[0])
array = np.concatenate((array, mess_arr), axis=1)
return array
def symbolData(symbol: str):
@ -39,7 +50,7 @@ def symbolData(symbol: str):
response = requests.get(IEXurl)
if response.status_code is 200:
IEXData = response.json()
message = "The current stock price of {name} is $**{price}**".format(
message = "The current stock price of {name} is {price}".format(
name=IEXData["companyName"], price=IEXData["latestPrice"]
)
@ -76,6 +87,7 @@ def run(pos):
try:
print("Press CTRL-C to stop.")
pos = 0
matrix = stockMatrix(symbols)
while True:
run(pos)
pos += 1
@ -85,8 +97,8 @@ except KeyboardInterrupt:
sys.exit(0)
def color_message(message, color):
message_arr = font.word(message)
def matrix_message(message, color, rows):
message_arr = fit_array(font.word(message), rows)
r = np.multiply(np.full(message_arr.shape, color[0]), message_arr)
g = np.multiply(np.full(message_arr.shape, color[1]), message_arr)
@ -100,9 +112,9 @@ def color_message(message, color):
return array
def fit_array(array, shape, operation="centered", fill_value=0):
rows = np.subtract(shape, array.shape)[0]
cols = shape[1]
def fit_array(array, rows, operation="centered", fill_value=0):
rows = rows - array.shape[0]
cols = array.shape[1]
offset = (rows, cols)
if operation is "centered":
@ -111,7 +123,6 @@ def fit_array(array, shape, operation="centered", fill_value=0):
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":
@ -124,3 +135,4 @@ def fit_array(array, shape, operation="centered", fill_value=0):
else:
raise Exception("Invalid Operation. Must be either centered, top, or bottom.")