Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions examples/sys_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""

import os
import signal
import sys
import time
from pathlib import Path
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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
Comment on lines +112 to 123

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for drawing the text lines can be simplified and made more robust.

  1. The first line is drawn unconditionally, which might cause clipping on small displays. It should have a height check like the other lines.
  2. The if condition for network info is nested inside the one for disk info. This can be flattened to make the logic for each line independent and clearer.

Here is a suggested refactoring that addresses these points:

Suggested change
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
if device.height >= line_height:
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 >= (line_height * 3):
draw.text((2, line_height * 2), disk_usage('/'), font=font2, fill="white")
if device.height >= (line_height * 4):
try:
draw.text((2, line_height * 3), network('wlan0'), font=font2, fill="white")
except KeyError:
# no wifi enabled/available
pass

Expand All @@ -112,3 +135,5 @@ def main():
main()
except KeyboardInterrupt:
pass
finally:
device.clear()
Loading