Created
June 29, 2017 19:35
-
-
Save spynappels/b8491809c06857d1ca96f6dcc166d4bf to your computer and use it in GitHub Desktop.
Python script to display balance and value of a Bitcoin wallet on a 128x32 SSD1306 OLED
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import time | |
import json | |
import requests | |
import Adafruit_GPIO.SPI as SPI | |
import Adafruit_SSD1306 | |
from PIL import Image | |
from PIL import ImageFont | |
from PIL import ImageDraw | |
# Raspberry Pi pin configuration: | |
RST = None | |
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) | |
# Set variables | |
# replace with your own wallet ID (public key NOT private key) | |
wallet = "1PXLgcjt2SHtRTCLt1yyoqo69Gv6AocoJA" | |
balance_url = "https://blockchain.info/q/addressbalance/" + wallet | |
# Initialize library. | |
disp.begin() | |
# Get display width and height. | |
width = disp.width | |
height = disp.height | |
# Clear display. | |
disp.clear() | |
disp.display() | |
# Create image buffer. | |
# Make sure to create image with mode '1' for 1-bit color. | |
image = Image.new('1', (width, height)) | |
# Load default font. | |
#font = ImageFont.load_default() | |
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as this python script! | |
# Some nice fonts to try: http://www.dafont.com/bitmap.php | |
font = ImageFont.truetype('BEBAS.ttf', 15) | |
line1 = 0 | |
line2 = 16 | |
# Create drawing object. | |
draw = ImageDraw.Draw(image) | |
# Animate text moving in sine wave. | |
#print('Press Ctrl-C to quit.') | |
while True: | |
r = requests.get(balance_url) | |
ticker = requests.get("https://blockchain.info/ticker").json() | |
btc = int(r.text)/100000000 | |
price = ticker['GBP']['15m'] | |
wal_val = btc*price | |
text1 = "Bal: " + str(btc) + " BTC." | |
#print(text1) | |
text2 = "Worth £" + str('{:7.2f}'.format(wal_val)) | |
#print(text2) | |
draw.rectangle((0,0,width,height), outline=0, fill=0) | |
# Enumerate characters and draw them offset vertically based on a sine wave. | |
x = 0 | |
# Line 1 on display | |
for i, c in enumerate(text1): | |
# Stop drawing if off the right side of screen. | |
if x > width: | |
break | |
# Calculate width but skip drawing if off the left side of screen. | |
if x < -10: | |
char_width, char_height = draw.textsize(c, font=font) | |
x += char_width | |
continue | |
# Calculate offset from sine wave. | |
y = line1 | |
# Draw text. +math.floor(amplitude*math.sin(x/float(width)*2.0*math.pi)) | |
draw.text((x, y), c, font=font, fill=255) | |
# Increment x position based on chacacter width. | |
char_width, char_height = draw.textsize(c, font=font) | |
x += char_width | |
# Line 2 on Display | |
x = 0 | |
for i, c in enumerate(text2): | |
# Stop drawing if off the right side of screen. | |
if x > width: | |
break | |
# Calculate width but skip drawing if off the left side of screen. | |
if x < -10: | |
char_width, char_height = draw.textsize(c, font=font) | |
x += char_width | |
continue | |
# Calculate offset from sine wave. | |
y = line2 | |
# Draw text. +math.floor(amplitude*math.sin(x/float(width)*2.0*math.pi)) | |
draw.text((x, y), c, font=font, fill=255) | |
# Increment x position based on chacacter width. | |
char_width, char_height = draw.textsize(c, font=font) | |
x += char_width | |
# Draw the image buffer. | |
disp.image(image) | |
disp.display() | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the BEBAS.ttf font I used, which you can get from here, or you can edit the code to select the default font, I've left it in but commented out.
Thanks to the Adafruit team, whose SSD1306 library I used in this script.