Skip to content

Instantly share code, notes, and snippets.

@dustyfresh
Created November 18, 2020 00:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustyfresh/b4a309369331d925503d2d65672989cc to your computer and use it in GitHub Desktop.
Save dustyfresh/b4a309369331d925503d2d65672989cc to your computer and use it in GitHub Desktop.
Crypto price ticker with the inkyphat eink display
#!/usr/bin/env python3
import cryptocompare
from time import sleep
from inky import InkyPHAT
from random import shuffle
from datetime import datetime
from PIL import Image, ImageDraw, ImageColor, ImageFont
def log(msg):
#print(msg)
open('ticker.log', 'a+').write(f'{msg}\n')
def main():
inkyphat = InkyPHAT('black')
# Load font file
font = ImageFont.truetype('/home/pi/fonts/Roboto-Bold.ttf', 22)
# Currencies that we want to track
cryptos = [
'BTC',
'LTC',
'XMR',
'ZEC',
'ETH',
'BCH',
'TRX',
'DOGE'
]
shuffle(cryptos)
counter = 0
cache = {}
while True:
# Reset counter and cache
if counter >= 20:
cache = {}
counter = 0
log(f'{datetime.today()} -- Resetting cache...')
for crypto in cryptos:
err = False
# Create and image object to fit the display
img = Image.new('P', (inkyphat.WIDTH, inkyphat.HEIGHT))
draw = ImageDraw.Draw(img)
# Get crypto price or use the cached price until we need to update the price again
if crypto not in cache:
try:
price = '${:,}'.format(cryptocompare.get_price(crypto, curr='USD')[crypto]['USD'])
cache[crypto] = price
log(f'{datetime.today()} -- Updating cache for {crypto} => {price} / counter => {counter}')
except Exception as e:
log(f'{datetime.today()} -- Error... -- {e}')
err = True
else:
price = cache[crypto]
log(f'{datetime.today()} -- Cached price for {crypto} => ({price}) / counter => {counter}')
# Display error message if we cannot grab the price from cryptocompare
if err is not True:
message = f"1 {crypto} = {price}"
else:
message = "Error!"
# draw image on the image object
w, h = font.getsize(message)
x = (inkyphat.WIDTH / 2) - (w / 2)
y = (inkyphat.HEIGHT / 2) - (h / 2)
draw.text((x, y), message, inkyphat.BLACK, font)
# Set the image that we have drawn
inkyphat.set_image(img)
# Rotate the image
inkyphat.h_flip = True
inkyphat.v_flip = True
# Display the final image
inkyphat.show()
# Increment counter per display refresh
counter += 1
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment