Skip to content

Instantly share code, notes, and snippets.

@fireismyflag
Created January 7, 2014 00:48
Show Gist options
  • Save fireismyflag/8292770 to your computer and use it in GitHub Desktop.
Save fireismyflag/8292770 to your computer and use it in GitHub Desktop.
Raspberry Pi dashboard using PIL and rm-hull's pcd8544 library, for display on a Nokia 5110 lcd
#!/usr/bin/env python
import sys
import os
import pcd8544.lcd as lcd
import wiringPy
import psutil
import socket
import fcntl
import struct
import threading
from PIL import Image, ImageDraw, ImageFont
def bytes2human(n):
"""
>>> bytes2human(10000)
'9K'
>>> bytes2human(100001221)
'95M'
"""
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i+1)*10
for s in reversed(symbols):
if n >= prefix[s]:
value = int(float(n) / prefix[s])
return '%s%s' % (value, s)
return "%sB" % n
def network(iface):
stat = psutil.network_io_counters(pernic=True)[iface]
return {"sent":stat.bytes_sent, "received":stat.bytes_recv }
def temp():
t = os.popen("vcgencmd measure_temp").readline()
return t[5:10]
def ip_addr(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def mem_usage():
free = os.popen("more /proc/meminfo | grep MemFree").readline()[10:25]
total = os.popen("more /proc/meminfo | grep MemTotal").readline()[10:25]
return 100 - (int(free) * 100 / int(total))
def disk_usage(dir):
usage = psutil.disk_usage(dir)
return usage.percent
def place_text(x, y, text):
global font
global draw
text_x, text_y = font.getsize(text)
y = y + 5 - text_y + 2
draw.text((x, y), text, font=font)
def build(count):
global font
global im
global draw
font = ImageFont.truetype("/home/pi/packages/piwatch/wendy.ttf", 10)
im = Image.new('1',(84,48))
draw = ImageDraw.Draw(im)
bitmap = Image.open("/home/pi/packages/piwatch/basedash.png")
im.paste(bitmap, (0,0) + bitmap.size)
place_text(12, 0, temp())
ip = ""
try:
ip = ip_addr("eth0")
except:
ip = ip_addr("wlan0")
place_text(37,0, ip)
net = network("eth0")
place_text(43,6, bytes2human(net["sent"]))
#place_text(43,6, "888M")
net = network("eth0")
place_text(67,6, bytes2human(net["received"]))
#place_text(67,6, "850M")
place_text(12,12, "cpu")
place_text(12,18, "ram")
cpu = psutil.cpu_percent()
cpu_end = 27 + cpu * 0.5
draw.line((27,14, int(cpu_end),14), fill=0, width=4)
if cpu_end <= 69: #cpu usage <= 84%, so that text fits
place_text(cpu_end + 3, 12, str(int(cpu)) + "%")
else:
place_text(cpu_end + 2, 12, "!")
ram = mem_usage()
ram_end = 27 + ram * 0.5
draw.line((27,20, int(ram_end),20), fill=0, width=4)
if ram_end <= 69:
place_text(ram_end + 3, 18, str(ram) + "%")
else:
place_text(ram_end + 3, 18, "!")
place_text(12,24, "msd")
place_text(12,30, "usb")
msd_end = 27 + disk_usage("/") * 0.5
draw.line((27,26, int(msd_end),26), fill=0, width=4)
if msd_end <= 69: #cpu usage <= 84%, so that text fits
place_text(msd_end + 3, 24, str(int(disk_usage("/"))) + "%")
else:
place_text(msd_end + 2, 24, "M")
torrent = os.popen("python /home/pi/packages/piwatch/torrent.py")
data = torrent.read()
torrent.close()
data = data.split("\n===\n");
if count == len(data) - 1:
count = 0
tor_data = data[count].split("\n")
place_text(12, 37, tor_data[0])
place_text(12, 43, tor_data[1])
im.show()
lcd.cls()
lcd.image(im, reverse=True)
del bitmap
del im
threading.Timer(30, build, [count + 1]).start()
if __name__ == "__main__":
lcd.init()
lcd.backlight(2)
build(0) #the parameter is used to iterate over the active torrents since only 1 can be displayed on the
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment