diff --git a/examples/fonts/DejaVuSansMono.ttf b/examples/fonts/DejaVuSansMono.ttf new file mode 100644 index 0000000..8b7bb2a Binary files /dev/null and b/examples/fonts/DejaVuSansMono.ttf differ diff --git a/examples/sys_info.py b/examples/sys_info.py index e42f7cd..ce619ee 100755 --- a/examples/sys_info.py +++ b/examples/sys_info.py @@ -14,6 +14,7 @@ """ import os +import signal import sys import time from pathlib import Path @@ -37,6 +38,15 @@ # TODO: Load histogram +def shutdown(signum, frame): + device.clear() + sys.exit(0) + + +signal.signal(signal.SIGTERM, shutdown) +signal.signal(signal.SIGINT, shutdown) + + def bytes2human(n): """ >>> bytes2human(10000) @@ -56,23 +66,32 @@ def bytes2human(n): def cpu_usage(): - # load average, uptime + # cpu usage, uptime uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time()) - av1, av2, av3 = os.getloadavg() - return "Ld:%.1f %.1f %.1f Up: %s" \ - % (av1, av2, av3, str(uptime).split('.')[0]) + days = uptime.days + hours, remainder = divmod(uptime.seconds, 3600) + minutes, _ = divmod(remainder, 60) + + cpu_percent = psutil.cpu_percent(interval=1) + return "CPU: %d%% Up: %dd%dh%dm" % (cpu_percent, days, hours, minutes) def mem_usage(): usage = psutil.virtual_memory() - return "Mem: %s %.0f%%" \ - % (bytes2human(usage.used), 100 - usage.percent) + return "RAM: %s/%s (%.0f%%)" % ( + bytes2human(usage.used), + bytes2human(usage.total), + usage.percent + ) def disk_usage(dir): usage = psutil.disk_usage(dir) - return "SD: %s %.0f%%" \ - % (bytes2human(usage.used), usage.percent) + return "SD: %s/%s (%.0f%%)" % ( + bytes2human(usage.used), + bytes2human(usage.total), + usage.percent + ) def network(iface): @@ -83,18 +102,22 @@ def network(iface): def stats(device): # use custom font - font_path = str(Path(__file__).resolve().parent.joinpath('fonts', 'C&C Red Alert [INET].ttf')) - font2 = ImageFont.truetype(font_path, 12) + font_path = str(Path(__file__).resolve().parent.joinpath('fonts', 'DejaVuSansMono.ttf')) + font2 = ImageFont.truetype(font_path, 10) + ascent, descent = font2.getmetrics() + line_height = ascent + descent with canvas(device) as draw: - draw.text((0, 0), cpu_usage(), font=font2, fill="white") - if device.height >= 32: - draw.text((0, 14), mem_usage(), font=font2, fill="white") + draw.rectangle(device.bounding_box, outline="white", fill=None) + draw.text((2, line_height * 0), cpu_usage(), font=font2, fill="white") + if device.height >= (line_height * 2): + draw.text((2, line_height * 1), mem_usage(), font=font2, fill="white") - if device.height >= 64: - draw.text((0, 26), disk_usage('/'), font=font2, fill="white") + if device.height >= (line_height * 3): + draw.text((2, line_height * 2), disk_usage('/'), font=font2, fill="white") try: - draw.text((0, 38), network('wlan0'), font=font2, fill="white") + if device.height >= (line_height * 4): + draw.text((2, line_height * 3), network('wlan0'), font=font2, fill="white") except KeyError: # no wifi enabled/available pass @@ -112,3 +135,5 @@ def main(): main() except KeyboardInterrupt: pass + finally: + device.clear()