Hi Guys,
First post. N00b here. Picked up this project and have been battling for weeks/months. Don’t really have any python or json experience, just been fighting my way through it - trying to learn/work it out.
The basic gestalt:
LED Matrix/Crypto ticker powered by a Raspbberry Pi 3 Model B. Uses an “adafruit” matrix bonnet, 8 x chained 32 x 64 panels for a 2m width (6ft) or a display length of 32 (H) x 512 (W) total.
Using python /bindings/python/samples/runtext.py:
At the moment, I am using a modified scrolling text demo (runtext.py) - which reads from a coingecko json request and updates the price - displaying it on the matrix. This is working fine, I’d like to improve it but have no idea how.
Modified runtext.py:
#!/usr/bin/env python
# Display a runtext with double-buffering.
from samplebase import SampleBase
from rgbmatrix import graphics
import time
import json
class RunText(SampleBase):
def __init__(self, *args, **kwargs):
super(RunText, self).__init__(*args, **kwargs)
self.parser.add_argument(
"-t", "--text", help="The text to scroll on the RGB LED panel", default="Scrolling Demo")
def run(self):
offscreen_canvas = self.matrix.CreateFrameCanvas()
font = graphics.Font()
font.LoadFont("/home/pi/rpi-rgb-led-matrix/fonts/7x13.bdf")
textColor = graphics.Color(255, 255, 0)
pos = offscreen_canvas.width
my_text = self.args.text
my_text1 = "Reading Cryptocurrencies"
cdsn = 0
while True:
offscreen_canvas.Clear()
len = graphics.DrawText(
offscreen_canvas, font, pos, 10, textColor, my_text1)
pos -= 1
if (pos + len < 0):
my_text1 = ''
pos = offscreen_canvas.width
with open('asp.json') as json_file:
json_object = json.load(json_file)
# print(len(json_object))
# print(json_object)
for cdsn in range(int(json_object['total_stocks'])):
# print(cdsn)
my_text1 += str(json_object['stocks'][cdsn]['stockname']) + ' $'+str(json_object['stocks'][cdsn]['stockprice'])+ ' '
# print(my_text1)
print(my_text1)
time.sleep(0.03)
offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
# Main function
if __name__ == "__main__":
run_text = RunText()
if (not run_text.process()):
run_text.print_help()
Coingecko API JSON request:
from pycoingecko import CoinGeckoAPI
import json
import time
import datetime
currentData = {}
stockInfo = {}
current_dt = 0
current_unixdt = ""
cg = CoinGeckoAPI()
def getDateTime():
print()
current_dt = datetime.datetime.now()
current_dt = str(current_dt)
print("current_dt From Function :", current_dt)
current_unixdt = int(time.time())
# current_unixdt = int(round(time.mktime(time.localtime(time.time())), 0))
print("current_unixdt From Function :", current_unixdt)
return current_dt, current_unixdt
def getallstocks():
with open('/home/pi/coingecko/stocks.json') as json_file:
stockInfo = json.load(json_file)
print("1_1_Total Quantity: ", stockInfo['total_cryptos'])
# print("Stocks Information")
# print(stockInfo)
return stockInfo
def updateCurrentData(stockInfo):
currentData = {}
currentData['stocks'] = []
requested_json = cg.get_price(ids=stockInfo["cryptos"], vs_currencies='usd', include_last_updated_at=True)
for estockInfo in stockInfo["cryptos"]:
print("estockInfo :", estockInfo)
if(estockInfo in requested_json):
stockName = estockInfo
stockPrice = round(requested_json[estockInfo]['usd'], 2)
currentData['stocks'].append({'stockname': stockName, 'stockprice': stockPrice})
else:
print(estockInfo, "not in requested json")
currentData['total_stocks'] = len(currentData['stocks'])
changed_dt, changed_unixdt = getDateTime()
currentData['changed_unixdt'] = changed_unixdt
currentData['changed_dt'] = changed_dt
with open('/home/pi/rpi-rgb-led-matrix/bindings/python/samples/asp.json', 'w+') as json_file:
json.dump(currentData, json_file, indent=4)
with open('/home/pi/coingecko/asp.json', 'w+') as json_file:
json.dump(currentData, json_file, indent=4)
return currentData
print("Started New Iterartion .........................................")
current_dt, current_unixdt = getDateTime()
print("0_DateTime")
print("current_dt :", current_dt)
print("current_unixdt :", current_unixdt)
stockInfo = getallstocks()
print("1_Stocks Information")
print(stockInfo)
currentData = updateCurrentData(stockInfo)
print("3_1_Curent Data")
print(currentData)
The changes I’d like, but can’t figure out:
Change 1. Infinite Scroll - At the moment, the code scrolls to the “end” where it starts again. There is a large gap and nothing is displayed on the matrix during this time. There is a large blank space of no data. I’d like it to “infinite scroll” or “loop” so it repeats/begins where it ends and there is no longer a gap of nothing/blank on the display. (See video example)
Change 2. Cryptocurrency Images - Instead of displaying “Bitcoin” or “Ethereum” text, display the relevant logos. I looked into the goingecko API and I believe I can “pull” the images URL via a json request - but I don’t know how to implement this/display the images.
Please see:
https://www.coingecko.com/en/api/documentation:
Request:
Part of the response with images, in this example Bitcoin:
"image": {
"thumb": "https://assets.coingecko.com/coins/images/1/thumb/bitcoin.png?1547033579",
"small": "https://assets.coingecko.com/coins/images/1/small/bitcoin.png?1547033579",
"large": "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579"
If this won’t work or is too hard, I am happy to code the images manually in the script of my local machine - rather than displaying them via a json request.
Change 3. If the 24 hour volume begins with “-” than display red down arrow image / if not display green arrow for 24 Hour price change.
What I have tried:
- Combining the scroll-image example script, with the scrolling text demo.
- Combining the scrolling text example script with the scrolling image demo.
- Hybrid code from both.
- Many bits and pieces of code.
- Importing PIL library/image samples.
- Banging my head against the wall.
- Pulling my hair out.
What I have at the moment:
[Cryptocurrency-Name-Text] [Price]
The dream/what id like:
[Cryptocurrency-Logo] [Price] [24 Hour change %] [Green up arrow or red down arrow depending on 24 hour % change]
Please see both video’s below.