Skip to content

Instantly share code, notes, and snippets.

@chorsnell
Last active December 23, 2021 20:40
Show Gist options
  • Save chorsnell/696b85562fb2a5186d2d7af46e39c428 to your computer and use it in GitHub Desktop.
Save chorsnell/696b85562fb2a5186d2d7af46e39c428 to your computer and use it in GitHub Desktop.
Raspberry Pi Zero W + Waveshare 2.13 V2 e-ink HAT. Get LRC + ETH prices from Coingecko every minute
#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
import operator
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):
sys.path.append(libdir)
import logging
from waveshare_epd import epd2in13_V2
import time
from PIL import Image,ImageDraw,ImageFont
import traceback
import requests
from pycoingecko import CoinGeckoAPI
cg = CoinGeckoAPI()
logging.basicConfig(level=logging.DEBUG)
try:
logging.info("epd2in13_V2 Demo")
epd = epd2in13_V2.EPD()
logging.info("init and Clear")
epd.init(epd.FULL_UPDATE)
epd.Clear(0xFF)
# Drawing on the image
font_tiny = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 10)
font15 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 20)
font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
# # partial update
logging.info("4.show time...")
time_image = Image.new('1', (epd.height, epd.width), 255)
#time_image = time_image.rotate(angle=180)
time_draw = ImageDraw.Draw(time_image)
epd.init(epd.FULL_UPDATE)
epd.displayPartBaseImage(epd.getbuffer(time_image))
##epd.set_rotate(epd2in13_V2.ROTATE_180)
epd.init(epd.PART_UPDATE)
#epd.displayPartial(epd.getbuffer(time_image))
#response = requests.get("https://61b4fff50e84b70017331a28.mockapi.io/folio/1")
#logging.info(response.json())
#prices = cg.get_price(ids='loopring,ethereum', vs_currencies='usd')
#logging.info(prices)
starttime = time.time()
num = 0
while (True):
prices = cg.get_price(ids='loopring,ethereum,bitcoin', vs_currencies='usd', include_24hr_change=True)
logging.info(prices)
coins = cg.get_coins_markets("usd")
#logging.info(coins)
coins.sort(key=operator.itemgetter('price_change_percentage_24h'), reverse=True)
coins = coins[:5]
logging.info(coins[:5])
logging.info(coins[0]['symbol'])
time_draw.rectangle((5, 5, 220, 105), fill = 255)
time_draw.text((5, 5), time.strftime('%d/%m/%Y %H:%M:%S'), font = font15, fill = 0)
time_draw.text((5, 30), "LRC - $"+ str(prices["loopring"]["usd"]) +" "+ str(round(prices["loopring"]["usd_24h_change"], 2)) + "%", font = font15, fill = 0)
time_draw.text((5, 55), "ETH - $"+ str(prices["ethereum"]["usd"]) +" "+ str(round(prices["ethereum"]["usd_24h_change"], 2)) + "%", font = font15, fill = 0)
time_draw.text((5, 80), "BTC - $"+ str(prices["bitcoin"]["usd"]) +" "+ str(round(prices["bitcoin"]["usd_24h_change"], 2)) + "%", font = font15, fill = 0)
logging.info(type(coins))
logging.info(type(coins[0]))
blah = ""
for value in coins:
logging.info(value)
blah += value['symbol'] + ":" + str(round(value['price_change_percentage_24h'], 0)) + " "
time_draw.text((5, 100), blah, font = font_tiny, fill = 0)
epd.displayPartial(epd.getbuffer(time_image.rotate(180)))
time.sleep(60.0 - ((time.time() - starttime) % 60.0))
#num = num + 1
#if(num == 10):
# break
# epd.Clear(0xFF)
logging.info("Clear...")
epd.init(epd.FULL_UPDATE)
epd.Clear(0xFF)
logging.info("Goto Sleep...")
epd.sleep()
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("ctrl + c:")
epd2in13_V2.epdconfig.module_exit()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment