diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a017b6b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "rpi-rgb-led-matrix"] + path = rpi-rgb-led-matrix + url = https://github.com/hzeller/rpi-rgb-led-matrix/ diff --git a/__pycache__/stockTicker.cpython-37.pyc b/__pycache__/stockTicker.cpython-37.pyc index 0b5c6b4..f086f3c 100644 Binary files a/__pycache__/stockTicker.cpython-37.pyc and b/__pycache__/stockTicker.cpython-37.pyc differ diff --git a/rpi-rgb-led-matrix b/rpi-rgb-led-matrix new file mode 160000 index 0000000..dfc27c1 --- /dev/null +++ b/rpi-rgb-led-matrix @@ -0,0 +1 @@ +Subproject commit dfc27c15c224a92496034a39512a274744879e86 diff --git a/stockTicker.py b/stockTicker.py index 34f2277..a43c06a 100644 --- a/stockTicker.py +++ b/stockTicker.py @@ -13,12 +13,15 @@ from PIL import Image, ImageDraw, ImageFont import time import csv +from rgbmatrix import RGBMatrix, RGBMatrixOptions + + class StockTicker(): def __init__(self): #Define global resources self.symbols = [] - self.ApiKeys = [["b1be6696d54342bdb5c8f59a052854fd","237d183faf984e7eba708a647c55153a","3012b3cb19784ed4be2b02e9907e53cb"],["f835e85df2a74df1904fe6dc53bcc17a","41ec268496744c34a96e3c408081300b","2d566fc71ade46d688713b13730c5219"]] - #self.ApiKeys = [["6172b84d1f464ad88b95ed52e4391bce","979f9b8c9ee748fbbb57cb490ccaaf42","593fa818a44144699b75433aafd92d15"] + #self.ApiKeys = [["b1be6696d54342bdb5c8f59a052854fd","237d183faf984e7eba708a647c55153a","3012b3cb19784ed4be2b02e9907e53cb"],["f835e85df2a74df1904fe6dc53bcc17a","41ec268496744c34a96e3c408081300b","2d566fc71ade46d688713b13730c5219"]] + self.ApiKeys = [["6172b84d1f464ad88b95ed52e4391bce","979f9b8c9ee748fbbb57cb490ccaaf42","593fa818a44144699b75433aafd92d15"]] self.retrievedList = [] self.greenORred = (255, 255, 255) self.ListStocks = [] @@ -31,7 +34,64 @@ class StockTicker(): self.Delay = 20 self.parseCSV = 0 self.symbolLength = 0 + self.brightness = 1.0 + + # Configuration for the matrix + options = RGBMatrixOptions() + options.rows = 64 + options.cols = 64 + options.chain_length = 1 + options.parallel = 1 + options.hardware_mapping = 'adafruit-hat' # If you have an Adafruit HAT: 'adafruit-hat' + options.gpio_slowdown = 4 + self.matrix = RGBMatrix(options = options) + def openImage(self, image_file): + image = Image.open(image_file) + # Make image fit our screen. + #image.thumbnail((self.matrix.width, self.matrix.height), Image.ANTIALIAS) + + image = image.convert('RGB') + return image + + + def SetImage(self, image, pos_x = 0, pos_y = 0, offset_x = 0, offset_y = 0, unsafe=True): + + + if (image.mode != "RGB"): + raise Exception("Currently, only RGB mode is supported for SetImage(). Please create images with mode 'RGB' or convert first with image = image.convert('RGB'). Pull requests to support more modes natively are also welcome :)") + + if unsafe: + #In unsafe mode we directly access the underlying PIL image array + #in cython, which is considered unsafe pointer accecss, + #however it's super fast and seems to work fine + #https://groups.google.com/forum/#!topic/cython-users/Dc1ft5W6KM4 + img_width, img_height = image.size + self.matrix.SetPixelsPillow(offset_x, offset_y, img_width, img_height, image) + else: + # First implementation of a SetImage(). OPTIMIZE_ME: A more native + # implementation that directly reads the buffer and calls the underlying + # C functions can certainly be faster. + img_width, img_height = image.size + pixels = image.load() + for x in range(max(0, -offset_x), min(img_width, self.matrix.width - offset_x)): + for y in range(max(0, -offset_y), min(img_height, self.matrix.height - offset_y)): + (r, g, b) = pixels[x, y] + + self.matrix.SetPixel(x + offset_x, y + offset_y, r*brightness, g*brightness, b*brightness) + + + def ScrollImage(self, image_file, pos_x = 0, pos_y = 0, delay = 0.05): + image = self.openImage(image_file) + + i = 0 + while True: + self.SetImage(image, offset_x = -i, offset_y = 0) + time.sleep(delay) + i += 1 + + + #Get the logo from file that is the same as ticker name def getLogo(self, Ticker): try: @@ -72,7 +132,7 @@ class StockTicker(): #Draw Ticker, current and change onto one image def textToImage(self, TICKER, CURRENT, CHANGE, ARROW): - font = ImageFont.truetype("EncodeSans.ttf", 18) + font = ImageFont.load("7x14.pil") img = Image.new('RGB', (150, 32)) d = ImageDraw.Draw(img) @@ -110,7 +170,7 @@ class StockTicker(): #Get current prices and day prices for the ticker then format the response into an array def getStockPrices(self, APIKEY, SYMBOLS): - + APIKEY = "237d183faf984e7eba708a647c55153a" try: symbols_list = SYMBOLS.split(",") td = TDClient(apikey=APIKEY) @@ -141,7 +201,7 @@ class StockTicker(): j=0 while (j<3): print('API KEY: ' + str(self.ApiKeys[self.keySwapper][j])) - + # NT: this weird API key thing doesnt work, when I hard code the API key in the above function it does self.getStockPrices(self.ApiKeys[self.keySwapper][j], self.symbols[(j+self.parseCSV)][0]) if (self.ApiCalledError == False): @@ -203,7 +263,7 @@ class StockTicker(): self.keySwapper += 1 self.running = True self.parseCSV += 3 - + while (True): if (self.running == True): if (self.keySwapper<=1): diff --git a/stockTicker.pyc b/stockTicker.pyc index 2007ff8..ab2d03d 100644 Binary files a/stockTicker.pyc and b/stockTicker.pyc differ diff --git a/test.py b/test.py new file mode 100644 index 0000000..748c75f --- /dev/null +++ b/test.py @@ -0,0 +1,18 @@ +from twelvedata import TDClient +import sys +import os +import threading +from PIL import Image, ImageDraw, ImageFont +import time +import csv + +from rgbmatrix import RGBMatrix, RGBMatrixOptions +from stockTicker import StockTicker + + +if __name__ == '__main__': + stock_ticker = StockTicker() + + while(True): + stock_ticker.ScrollImage('final.ppm') + time.sleep(100)